1
votes

I'm trying to send and receive string to my PC via UART. My MCU is PIC18F65K40. The programmer is PicKit 3. The problem is that I receive some garbage values while programming the device, but after that noting happened. Almost the same code is working on PIC18F25K50 without a problem. In my opinion the problem is in the clock value or in the baudrate setting. I'm trying to set internal clock to 8MHz and I'm using 9600 baudrate.

configuration bits:

#define CONBITS_H

#include <xc.h>

#define _XTAL_FREQ 8000000UL

// PIC18F65K40 Configuration Bit Settings

// 'C' source line config statements


// CONFIG1L
#pragma config FEXTOSC = HS     // External Oscillator mode Selection bits (HS (crystal oscillator) above 8 MHz; PFM set to high power)
#pragma config RSTOSC = HFINTOSC_64MHZ// Power-up default value for COSC bits (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)

// CONFIG1H
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)

// CONFIG2L
#pragma config MCLRE = EXTMCLR  // Master Clear Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RG5 pin function is MCLR )
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (Power up timer disabled)
#pragma config LPBOREN = OFF    // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled , SBOREN bit is ignored)

// CONFIG2H
#pragma config BORV = VBOR_2P45 // Brown Out Reset Voltage selection bits (Brown-out Reset Voltage (VBOR) set to 2.45V)
#pragma config ZCD = OFF        // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PPS1WAY = ON     // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config DEBUG = OFF      // Debugger Enable bit (Background debugger disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)

// CONFIG3L
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = ON        // WDT operating mode (WDT enabled regardless of sleep)

// CONFIG3H
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4L
#pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (006000-007FFFh) not write-protected)

// CONFIG4H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config SCANE = ON       // Scanner Enable bit (Scanner module is available for use, SCANMD bit can control the module)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored)

// CONFIG5L
#pragma config CP = OFF         // UserNVM Program Memory Code Protection bit (UserNVM code protection disabled)
#pragma config CPD = OFF        // DataNVM Memory Code Protection bit (DataNVM code protection disabled)

// CONFIG5H

// CONFIG6L
#pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG6H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

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



#endif
#include "pic18f65k40.h"
#include "conbits.h"

void interrupt UART3_receive_int(void);
void interrupt low_priority UART3_transmit_int(void);

void UART3_init(void)
{
    //UART3 init - PC communication

    TRISEbits.TRISE0 = 0;
    TRISEbits.TRISE1 = 1; // input?    

    RC3STAbits.SPEN = 1; 
    RC3STAbits.CREN = 1;
    TX3STAbits.TXEN = 1;
    TX3STAbits.SYNC = 0;
    TX3STAbits.BRGH = 1; //????
    //framing error ??
    //overrun error ??

    BAUD3CONbits.SCKP = 0;
    BAUD3CONbits.BRG16 = 1;
    SP3BRGL = 0xCF; // baud rate 9600

    //PIE4bits.RC3IE = 1;
    PIE4bits.TX3IE = 1;

    IPR4bits.TX3IP = 0;
    //IPR4bits.RC3IP = 1;
}

void UART_Send(unsigned char data[])
{
    int i = 0;
    while(data[i] != '\0')
    {
        if(PIR4bits.TX3IF == 1)
        {
            TX3REG = data[i];
            i++;
        }
    }
    __delay_ms(5);
}

void K1_active(void)
{
    LATDbits.LATD1 = 1;
    //delay
    __delay_ms(2000);
    LATDbits.LATD1 = 0;
    __delay_ms(2000);
}

//relay 2,3,4,5,6 functions
//active led function / on and blink - choose functionality for LEDs
//error led function / on and blink

void main(void) 
{
    //Internal Clock set

    OSCCON1bits.NOSC = 0x06;
    OSCCON1bits.NDIV = 0;

    OSCFRQbits.HFFRQ = 0x03;

    OSCENbits.HFOEN = 1;
    while(OSCSTATbits.HFOR != 1);

    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;




    //Relays
    TRISDbits.TRISD1 = 0; //K1
    TRISDbits.TRISD2 = 0; //K2
    TRISHbits.TRISH2 = 0; //K3
    TRISHbits.TRISH3 = 0; //K4
    TRISEbits.TRISE7 = 0; //K5
    TRISDbits.TRISD0 = 0; //K6

    //LEDs
    TRISEbits.TRISE5 = 0; //Error LED
    TRISEbits.TRISE6 = 0; //Active LED

    TRISAbits.TRISA2 = 0;

    UART3_init();

    //temp
    LATDbits.LATD1 = 0; //K1
    LATDbits.LATD2 = 0; //K2
    LATHbits.LATH2 = 0; //K3
    LATHbits.LATH3 = 0; //K4
    LATEbits.LATE7 = 0; //K5
    LATDbits.LATD0 = 0; //K6

    //TX3REG =65;

    while(1)
    {
        //K1_active();
        UART_Send("aaaaaa\n");


        //__delay_ms(1000);        
    }
}```
1
Why are you doing RSTOSC = HFINTOSC_64MHZ? With that, OSCCON1bits.NOSC = 0x06 and OSCCON1bits.NDIV = 0 you must be actually running at 64 Mhz.Marcos G.
I tried with OSCCON1bits.NDIV = 0x03; //clock divider 8 but the result was the same. For initialization of internal clock I have two options - 64MHz and 1MHz.NBE
You're right, the divider for the internal oscillator is set with OSCFRQbits.HFFRQ = 0x03 so you should get 8 MHz indeed. And your baudrate should be close to 9600 with SP3BRGL = 0xCF. Is the chip brand new? I've seen instances of UART circuitry going bad, do you have another one to try it out? Do you get anything at all from the UART? What are you using to verify (computer serial port with Windows/Linux...)?Marcos G.
I simulated your code and I think you're not clearing the watchdog, can you try setting #pragma config WDTE = OFF?Marcos G.
I disabled the watch-dog timer, but there was no result. After that I changed the MCU, but the situation was the same. I receive few garbage symbols while the device programming and after that nothing came.NBE

1 Answers

1
votes

According to the errata of your device, silicon revision A3 has a bug that affects register NVMREG. This might have an effect on the UARTs and explains why your code was working on the K50 familiy.

To check if your PIC is revision A3 you have to connect your programmer and click on Refresh Debug Tool Status Icon:

enter image description here

The silicon revision will be printed in the output of the debugger console.

If you have revision A3 you can fix this issue going to Project Properties --> XC8 Linker --> Additional Options and writing +NVMREG in the errata box:

enter image description here

This bug has been extensively reported in several Microchip forums and also here.