#include #include "usart.h" #include "util.h" void usart_init_core(void){ // pins trisc.7 = 1; trisc.6 = 0; // baud rate txsta.BRGH = BAUD_BRGH; baudcon.BRG16 = BAUD_BRG16; baudcon.WUE = 0; txsta.SYNC = 0; rcsta.SPEN = 1; // transmitter txsta.TXEN = 1; pie1.TXIE = 0; txsta.TX9 = 0; // reception pie1.RCIE = 0; rcsta.RX9 = 0; rcsta.CREN = 1; } void usart_init(void){ // baud rate spbrgh = (BAUD_RATE >> 8) & 0xff; spbrg = BAUD_RATE & 0xff; usart_init_core(); } /* The Auto-Baud Rate Detection must receive a byte with the value 55h (ASCII U) */ void usart_init_auto(void){ spbrg = 0; spbrgh = 0; //baudcon.WUE = 1; baudcon.ABDEN = 1; usart_init_core(); while(baudcon.ABDEN == 1){ usart_getc(); } } void usart_putc(unsigned char data){ while(!pir1.TXIF){ delay_ms(1); } txreg = data; #ifdef TX_LED TX_LED = 1; delay_us(1); TX_LED = 0; #endif } unsigned char usart_getc(void){ while(!pir1.RCIF){ delay_ms(1); } #ifdef RX_LED RX_LED = 1; delay_us(1); RX_LED = 0; #endif if (rcsta.OERR){ rcsta.CREN = 0; rcsta.CREN = 1; } return rcreg; } void usart_gets(unsigned char *destination){ while ((*destination++ = usart_getc()) != 0x0d); // wait until tx register is empty *--destination = 0; } void usart_puts(unsigned char *source){ while (*source != 0) // wait until tx register is empty usart_putc(*source++); } void usart_putsln(unsigned char *source){ usart_puts(source); usart_put_newline(); } void usart_put_newline(){ usart_putc(0x0d); usart_putc(0x0a); } void usart_waitfor(unsigned char c){ while(usart_getc() != c){} } void usart_put_baoud(){ usart_puts("Baud rate: 0x"); usart_putc(bin2Hex( (spbrgh>>0x4) & 0x0f )); usart_putc(bin2Hex( spbrgh & 0x0f )); usart_putc(bin2Hex( (spbrg>>0x4) & 0x0f )); usart_putc(bin2Hex( spbrg & 0x0f )); usart_put_newline(); }