Page 1 of 1

I am new, and need help

PostPosted: Sat Aug 04, 2018 2:10 pm
by elZero
I am new to microcontrollers and programming for embedded systems. Just got a gift of a PICKit3 and decided to test it out. The ode compiles, but the microcontroller doesn't do what I expected it to do(count from 1-31, in binary and output it to connected LEDs. There is an external 6MHz Crystal Oscillator, and the MCLR pin is pulled high through a 10K resistor.

Here's the code:
Code: Select all
 
#include <xc.h>
 
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config CP = OFF
 
#define _XTAL_FREQ 6000000
 
void main(void)
{
    TRISA = 0xE0;

    unsigned char i;

    i = 0;
    for(i = 0; i <= 31; i++)
    {
        PORTA = i;
        __delay_ms(100);
        if(i=31)i=0;
    }
}

Re: I am new, and need help

PostPosted: Sun Aug 05, 2018 5:03 am
by Jorgef
Hi

I just saw that you are already receving replies in the MCHP forum.
if I can add something, just ask.

Re: I am new, and need help

PostPosted: Sun Aug 05, 2018 10:23 am
by ric
Appears to be cross posted from: https://www.microchip.com/forums/m1063072.aspx
Over there it has been revealed that the PIC is a PIC16F628A

Also the bug in " if(i=31)i=0;" which should be "==" has been noted.

Re: I am new, and need help

PostPosted: Sun Aug 05, 2018 12:52 pm
by ric
Here it is with the config bits taken from the MPLABX config editor, and the main loop done in a more standard way
Code: Select all
// PIC16F628A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define _XTAL_FREQ 6000000
 
void main(void)
{
    unsigned char i;
    TRISA = 0b11100000;    // RA0-4 all output
    while (1)
    {
        for(i = 0; i <= 31; i++)
        {
            PORTA = i;
            __delay_ms(100);
        }
    }
}