Microcontroller › PIC › uart communication on pic18F24k40
- This topic has 1 reply, 2 voices, and was last updated 6 years ago by Abhishek.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
December 1, 2017 at 10:28 am #4700ajitParticipant
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 20000000unsigned 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 1volatile 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 != '