2016年7月26日 星期二

pic 18f4620 uart study

Ref :  https://www.youtube.com/watch?v=TbKW8vFe-Aw

Ref : http://www.dmi.unict.it/~santoro/teaching/lap1/slides/UART_MCU.pdf



























The following parameters must be set in the UART hardware:
transmission speed, in bps = Bit Per Second or baud

















  • 白色線為接收(RXD),接 Raspberry Pi 的傳送(TXD),8 號腳位。
  • 綠色線為傳送(TXD),接 Raspberry Pi 的接收(RXD),10 號腳位。


number of bits per character, usually 8
presence/absence of partity bit, usually absent
number of stop bits, usually 1
A setting 19200,8,N,1 means:
speed = 19200 bit-per-second;
bits = 8;
parity = None;
stop bits = 1.



UART in the PIC18F25K22
From the datasheet:
Two different UART
The TX line of the UART1 is shared with RC6
The RX line of the UART1 is shared with RC7
The TX line of the UART2 is shared with RB6
The RX line of the UART2 is shared with RB7
Basic registers per UARTx (“x” is the UART number):
TXSTAx, configuration of the transmitter
RCSTAx, configuration of the receiver
BAUDCONx, configuration of the baud rate (speed)
generator
SPBRGx, baud rate (speed) generator value



It is divisor driven from system clock frequency (FOSC).
It is controlled by the following bits/registers:
SPBRGHx:SPBRGx: the divisor value to be applied to
FOSC to obtain the desired baud rate
BAUDCONxbits.BRG16, a bit which indicates whether the
divisor value uses 8 or 16 bits
TXSTAxbits.BRGH, a bit which indicates if we are using a
high-speed baud date
A formula exists to compute the divisor value, but... Microchip
provides very usefull tables!



// setup UART
TRISCbits.TRISC6 = 0; // TX as output
TRISCbits.TRISC7 = 1; // RX as input
TXSTA1bits.SYNC = 0; // Async operation
TXSTA1bits.TX9 = 0; // No tx of 9th bit
TXSTA1bits.TXEN = 1 // Enable transmitter
RCSTA1bits.RX9 = 0; // No rx of 9th bit
RCSTA1bits.CREN = 1; // Enable receiver
RCSTA1bits.SPEN = 1; // Enable serial port
// Setting for 19200 BPS
BAUDCON1bits.BRG16 = 0; // Divisor at 8 bit
TXSTA1bits.BRGH = 0; // No high-speed baudrate
SPBRG1 = 51; // divisor value for 19200





Basic transmission
// wait the end of transmission
while (TXSTA1bits.TRMT == 0) {};
TXREG1 = new data; // start sending the new byte
... or, you can use directly the printf function, provided that you
implement a proper putch(char c) function, that is then called
by printf for each character to send.
Using printf
void putch(char c) {
// wait the end of transmission
while (TXSTA1bits.TRMT == 0) {};
TXREG1 = c; // send the new byte
}
...
printf("Hello world!!");



Reading a character
UART Char Reading
char read char(void)
{
while (PIR1bits.RC1IF == 0) { // wait for char
if (RCSTA1bits.OERR == 1) {
RCSTA1bits.OERR = 0; // clear overrun if it occurs
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
}
return RCREG1;




===================================================================
void oopenUART(void){


  /* Open the USART configured as 8N1, 2400 baud, in polled mode */

OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,
_BAUD_9600);

//disable RX, we will enable it when INT0 Occur
 RCSTAbits.CREN = 0;

}




沒有留言:

張貼留言