ragav4456
Full Member level 4
- Joined
- Aug 1, 2012
- Messages
- 228
- Helped
- 19
- Reputation
- 38
- Reaction score
- 17
- Trophy points
- 1,308
- Activity points
- 2,568
PIC 16f1527 have a two UART ports. Am using UART1 for my application. See my UART program,any problem in UART1 configuration registers?
I need standard (tested) UART program or UART configure register format. FOSC=20MHZ.
Baudrate calculated by using datasheet SPBRG baudrate value(Table22-5) depends upon SYNC,BRGH and BRG16.
How to configure 8bit SPBRG and 16bit SPBRG?
I need standard (tested) UART program or UART configure register format. FOSC=20MHZ.
Baudrate calculated by using datasheet SPBRG baudrate value(Table22-5) depends upon SYNC,BRGH and BRG16.
How to configure 8bit SPBRG and 16bit SPBRG?
Code:
#include<pic.h>
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_ON & FCMEN_ON);
__CONFIG(WRT_OFF & VCAPEN_OFF & LPBOR_OFF & STVREN_OFF & BORV_HI & LVP_OFF);
unsigned int flag=0,rx_count=0,count;
bank1 unsigned char a[75]={0};
void send_message(unsigned char const *msg);
void main()
{
// UART checking
TRISA=0X3F;
TRISB=0XFF;
TRISC=0X80;
TRISD=0xFF;
TRISE=0X07;
PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
PORTE=0;
TX1STAbits.BRGH=1; /* high baud rate */
SPBRG=520; /* set the baud rate */
BAUD1CONbits.BRG16 = 1;
BAUD1CONbits.ABDOVF = 0;
BAUD1CONbits.RCIDL = 0;
BAUD1CONbits.SCKP = 0;
BAUD1CONbits.WUE = 0;
BAUD1CONbits.ABDEN = 0;
TX1STAbits.SYNC = 0; /* asynchronous */
TX1STAbits.SENDB = 1;
TX1STAbits.CSRC = 0;
TX1STAbits.TXEN = 1; /* enable the transmitter */
TX2STAbits.TXEN = 0; /* disable the transmitter2 */
RC1STAbits.SPEN = 1; /* enable serial port pins */
RC1STAbits.CREN = 1; /* enable reception */
RC1STAbits.SREN = 0; /* no effect *///
RC2STAbits.SPEN = 0; /* disable serial port2 pins */
RC2STAbits.CREN = 0; /* disable reception2 */
OSCCONbits.IRCF3=1; //16 MHz Internal osc freq
OSCCONbits.IRCF2=1;
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF0=1;
OSCCONbits.SCS1=1;
OSCCONbits.SCS0=0;
INTCONbits.GIE=PEIE=1;
PIE1bits.RC1IE=1;
send_message("0x35");
while(1)
{
//if(flag==1)
//{
LATC3=0xFF;
for(count=0;count<50000;count++);
for(count=0;count<50000;count++);
for(count=0;count<50000;count++);
LATC3 =0x00;
for(count=0;count<50000;count++);
for(count=0;count<50000;count++);
for(count=0;count<50000;count++);
rx_count=0;
flag=0;
send_message("\r\n");
send_message("UART Receive");
send_message("\r\n");
//}
}
}
void send_message(unsigned char const *msg)
{
while(*msg!='\0')
{
TX1REG=*msg;
while(!TX1STAbits.TRMT);
msg++;
}
}
void interrupt isr()
{
if(PIR1bits.RC1IF==1)
{
PIR1bits.RC1IF=0;
rx_count++;
if(rx_count>70)
rx_count=1;
a[rx_count]=RC1REG;
if(a[rx_count]==0x0d)//Press Enter button
{
flag=1;
}
}
}