PIC Development Board with PK3

hexreader

Joined Apr 16, 2011
581
Hmm - that sucks - really thought I had it :(

Oh well - that's life when not able to test code due to not owning same PIC...

Will try on closest similar PIC to find out where I went wrong. Will read datasheet harder to see if I missed something.

Ignore all of my posts till then...
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Quick observations:
You can not use RB7 or RB6 if you are going to use PK3 for debugging on the actual hardware. Move those inputs and outputs somewhere else and set TRIS accordingly.
The LEDs on the board do not have current limiting resistors so be sure to add one when connecting.
The use of LAT instead of PORT for outputs is correct. TS should review the IO sections to understand why this is so.
I'll leave the rest of the debugging up to TS.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Quick observations:
You can not use RB7 or RB6 if you are going to use PK3 for debugging on the actual hardware. Move those inputs and outputs somewhere else and set TRIS accordingly.
The LEDs on the board do not have current limiting resistors so be sure to add one when connecting.
The use of LAT instead of PORT for outputs is correct. TS should review the IO sections to understand why this is so.
I'll leave the rest of the debugging up to TS.
I have modified the code

C:
#define _XTAL_FREQ 8000000

#include <xc.h>

// PIC18F45K80 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

#define LED1 LATBbits.LATB5
#define LED1_TRIS TRISBbits.TRISB5

void main(void)
{

    TRISC0 = 1;
     
    ANCON0 = 0x00;
    ANCON1 = 0x00;

    LED1 = 0;  // LED OFF

    while (1)
    {
        if (RC0 == 1)  // check switch if pressed
         __delay_ms(40); // wait
        {
           if (RC0 == 1)   // check switch if pressed
           {
             LED1 = 1;   // LED ON
           }
        }
    }

}
I never get line 90
1601219748120.png
 

hexreader

Joined Apr 16, 2011
581
What is recommended way to initialize I/O ports for PIC18F45K80?
The recommended way is the way that the datasheet describes:

Example 11-1 for PORTA
Example 11-2 for PORTB
Example 11-3 for PORTC
Example 11-4 for PORTD
Example 11-5 for PORTE

Examples are in assembly language, but conversion to C is a trivial exercise. Let me know if you need help assembly to C

Do not blindly follow those examples - they must be tailored to fit your hardware.

PIC18F45K80 is on order, so I will be able to provide proven, tested code in about a week when parcel arrives

Start by writing code that lights an LED - forget button press until you acheive light LED. Start simple and work your way up
next light LED - delay - turn off LED - delay - repeat.
only when you fully understand flash LED - then is the time to add button - which on your board is normally '1' input, going to '0' when pressed.
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Thanks @hexreader I actually had to decompress after that last question.

What is recommended way to initialize I/O ports for PIC18F45K80?
In addition to what hexreader posted, my recommended way for ANY PIC includes these steps. I do not consider them optional.
  • Each databook section noted above covers one port. At the end of each section, there is a table of registers that are important to the operation of the port covering direction, pullups, output type, shared pins etc. It is vitally important that you inspect EACH BIT of EACH ONE of those registers and set them up for your specific application.
  • EXPLICITLY initialize ALL of the I/O of the chip, not just the pins you are using at the time. This is a CMOS device and uninitialized ports are left floating. CMOS does NOT like that. Set any unconnected pins to output a 0 value. It is unfortunate that so many texts and internet examples don't do that. Consider printing the tables and checking off each bit as you verify your IO setup.
  • I prefer not to take the defaults for various good reasons.
  • Unlike the 8051, you must explicitly set the direction for each output.
  • A common error is not disabling analog, comparators and other peripherals that share IO pins.
  • On 18Fxxxx and 16F1xxx, use PORTx for inputs. Use LATx for everything else.
  • Do not set/clear TRIS bits individually. Specify a byte-wide value for all 8 bits then write that byte to the entire TRIS register. There are good reasons for this.
  • When initializing, set the LATx values first, then set the TRIS direction values to avoid spurious outputs when starting up after reset.
  • Remember that at RESET, all IO pins are floating. The hardware design must provide any required circuitry to avoid faulty operation when the port pin is floating.
Consider putting all of this into one routine called initIO(); Call that on startup and whenever you need to restore the chip to its starting condition.

My remark about the LED not being an output is because you did not clear the TRIS bit for RB5. You still have logic errors..
 
Last edited:

hexreader

Joined Apr 16, 2011
581
Yes, that is good - you have learnt to follow the datasheet.

You only actually need one of these two lines - either one (or both) will do, but I prefer the second - see the note about "alternative":
Code:
PORTA = 0;
LATA = 0;
Sadly there is more to it, as JohnInTx suggests...

A6 and A7 will be unavailable if you have external (2-pin) crystal configured.

Comparators may need to be turned off in order for A1, A2 and A5 to work as IO.

But you are getting all of the right ideas and where things do not work as expected - we are here to help (but I may need to work it out for myself first)

All of this is difficult at first - but you will soon get the hang of it. Assume nothing, test everything in the simplest possible way, learn the basics well before progressing
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I have doubt on A/D converter and Comparator. Have I disabled them correctly ?
Did I miss anything I should have disabled ?

C:
void IO_PORTS(void)
{

    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
  
    TRISA = 0b0000001;// RA0 as input,  Switch connected
    TRISB = 0b0000000;// all are output, Unused
    TRISC = 0b0000000;// All are output, Unused
    TRISD = 0b0000000;//  All are output, RD1 LED connected
    
    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port
  
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
  
    ADCON0 = 0; // A/D conversion Disabled       
}
 

JohnInTX

Joined Jun 26, 2012
4,787
A good practice is to print a copy of the pinout just like you have in your post then color in each pin as you assign it. When all of those little boxes are filled, you at least have looked at all the IO, power supply etc. Missing pins are readily apparent.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
@JohnInTX so the code is working on the simulator but not working actual hardware

C:
#define _XTAL_FREQ 8000000

#include <xc.h>

// PIC18F45K80 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)


void main(void)
{

    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;

    TRISA = 0b0000001;// RA0 as input,  Switch connected
    TRISB = 0b0000000;// all are output, Unused
    TRISC = 0b0000000;// All are output, Unused
    TRISD = 0b0000000;//  All are output, RD1 LED connected
    TRISE = 0b0000000;// All are output, Unused
   
    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port

    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off

    ADCON0 = 0; // A/D conversion Disabled

    while (1)
    {

           if (RA0 == 0)   // check switch if pressed
           {
             __delay_ms(40); // wait
           
             if (RA0 == 0)   // check switch if pressed
             {
               RD7 = 1;   // LED ON
             }
           }
    }
}
1601253505702.png
 
Top