Close or Esc Key

Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

uart communication on pic18F24k40

1 post / 0 new
ajit
ajit's picture
Offline
Last seen: 1 week 6 days ago
Joined: 26/07/2011 - 14:37
uart communication on pic18F24k40

Dear all.

I am using mplab x ide with xc8 compiler. My code with 16Mhz internal oscilator its working fine. I am testing with 20mhz external oscilator without MCC generated file.

Here i could not able to receive or send any data over 20Mhz external crystal

mode: asyn 9600 baudarate 16bit 

IC:PIC18F24K40

 
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#pragma config FEXTOSC =111
#pragma config RSTOSC= 111
//#pragma config RSTOSC=HFINTOSC_64MHZ
#pragma config CLKOUTEN=OFF
#pragma config CSWEN=ON
#pragma config FCMEN=ON
 
// CONFIG2L
#pragma config MCLRE = EXTMCLR // ->If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR 
#pragma config PWRTE = OFF // Power-up Timer Enable bit->Power up timer disabled
#pragma config LPBOREN = OFF // ->ULPBOR disabled
#pragma config BOREN = ON // 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 // ->Divider ratio 1:65536; software control of WDTPS
#pragma config WDTE = OFF // WDT operating mode->WDT Disabled
 
// 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
 
// 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 module is available for use, SCANMD bit can control the module
#pragma config LVP = OFF // 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
 
// 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
 
// CONFIG6H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit->Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
 
 
#define LED LATC2
 
#define _XTAL_FREQ 20000000
unsigned int i=0; 
unsigned int k=0;  
#define Pulse RA5
#define Seg1 0x01
#define Seg2 0x02
#define Seg3 0x04
#define Seg4 0x08
 
//#define LED RC2
#define LED_RX LATC7     // Pin assigned RX LED
#define LED_TX LATC6   // Pin assigned TX LED
//#define LED RC2     // Pin assigned for LED 
#define DE LATC5
#define RECEIVE     0
#define TRANSMIT 1
#define READ_REG     3
#define WRITE_REG   6
#define ILLEGAL_DATAVALUE 0x03
#define FALSE   0
#define TRUE 1
#define METER_ID        1
 
 
volatile unsigned short cnt;
volatile unsigned short Dgt;
unsigned int j=0;
unsigned char* str;
unsigned int count = 0;
char data = 0;
unsigned char rxbuf[50], ser_data[100], crc_data[100], Max_scroll = 0;
unsigned char buff[10];
volatile int index = 0, rec_flag = 0, Delay_count = 0, Id[10], Buffer_count = 0, Cal_count = 0, Disp_count = 0, inc = 0, One_sec_update = 0, Auto_scroll_count = 0;
char data1[10];
unsigned char buf[20];
 
 
 
 
 
void Timer1_Interrupt()
{
 TMR0L  =0XFA;
 TMR0H = 0x00;
 T0CON0bits.T016BIT=1;
 T0CON0bits.T0EN=1;
 T0CON1=0b01000100;  
 INTCON=0X00;
 PIR0bits.TMR0IF=0;
 PIE0bits.TMR0IE=1;
 IPR0bits.TMR0IP=1;
 
}
    
    
    
void OSCILLATOR_Initialize(void)
{
    // NOSC HFINTOSC; NDIV 8; 
    OSCCON1 = 0x67;
    // CSWHOLD may proceed; SOSCPWR Low power; 
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled; 
    OSCEN = 0x00;
    // HFFRQ 64_MHz; 
    OSCFRQ = 0x08;
    // TUN 0; 
    OSCTUNE = 0x00;
}
 
 
void Serial_1_Send_byte(int trbuf1) 
{
 TX1REG = trbuf1;
 
while (0 == PIR3bits.TXIF);
 
 //while (!TX1IF);
 
}
 
 
void Send_string_uart1(const unsigned char *string) {
 
 
    do {
        TX1REG = string[i++];
        while (0 == TX1STAbits.TRMT);
    } while (string[i] != '\0');
}
 
char Serial_Receive_byte()
{
   
   while(0==PIR3bits.RCIF);
    return RC1REG;
    
}
 
char usart_RxString()
{
 
   while(*str !='\0')
   {            // while not end of string
      while(0==PIR3bits.RCIF);
       RCREG = *str;            // read next character 
       str++;                    // increment pointer to next character
   }
       return RC1REG;    // return the contents of uart
}
 
 
 
void interrupt ISR(void)
{
if(PIR0bits.TMR0IF==1)
{
            TMR0H=0xFF; // Load the time value(0xBDC) for 100ms delay
TMR0L =0xDC;
PIR0bits.TMR0IF=0;
       Dgt++;
  if(Dgt>=5)
           {
               cnt++;
             LED=!LED;
        Dgt=0;
            
              
             
            
           }
           
       }   
 
 
 
}
 
void Serial_1_Init()
    PIE3bits.RCIE = 0;
    PIE3bits.TXIE = 0;
INTCON = 0b00000000;
    PIR3bits.RCIF=0;
   RC1STA = 0x90;
   TX1STA = 0x24;
   BAUD1CON = 0x09;
    RCSTA=0b10010000;
    SP1BRGL = 0x00;
SP1BRGH = 0x20;
    PIE3bits.RCIE = 1;
    
}
 
 
 
 
 
 
 
 
void Init_Controller()
{
    
 
cnt=100;
TRISC=0X80;
PORTC=0X80;
TRISB=0b00000000;
PORTB = 0b00000000;
TRISA=0b00000000;
PORTA=0X00;
ADCON0 = 0b00000000;
 
ANSELC = 0x00;
 ANSELB = 0x00;
  ANSELA = 0x00;
 
  PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS
 
    RC6PPS = 0x09;   //RC6->EUSART:TX;
    RXPPSbits.RXPPS = 0x17;   //RC7->EUSART:RX;
 
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS
  
  
  
  
LED=0;
 
 
 
}
 
void main(void)
  {
  OSCILLATOR_Initialize();
  Init_Controller();
  Timer1_Interrupt();
  Serial_1_Init();
  
  INTCONbits.GIE=1;
  INTCONbits.PEIE=1;  
   PIE0bits.TMR0IE=1;
      
        while(1)
        {
         // Send_string_uart1("Welcome");
              Serial_1_Send_byte(1);    
        }
            
            
         
  }