程序 - 用于 pic18f65k40 的 UART 通信



我正在搜索 pic18f65k40 的 UART 代码,但我还没有找到。 任何人都可以提供一个示例代码来使用 pic18f65k40 uart 传输单个字符。

这是我的新光剧院照明控制器的驱动程序。它适用于PIC18F14K22,但应该足够接近以给您一个开始。该板还咬碎了世界半 LED 的极快协议,因此关于时序的评论。

/* Neolight board optional UART support. The UART is used as an
alternative to contact closures to trigger effects, and can be used
to download completely arbitrary patterns to the LEDS.
NOTE that during a write to the LEDs there are NO cycles left over
to operate the UART, so it does not work during updates. The
protocol reflects this.
*/
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "neolight.h"
/* Baud rate calculation */
#define brgh 1    /* Use high rate mode */
#if brgh
#define divisor 4
#else
#define divisor 64
#endif
/* Macros to compute baud rate setup at compile time */
#define spbrg ( ( (long) Fosc ) / ( (long) divisor * (long) ( baud_rate ) ) - 1 )
#define spbrgl ( spbrg & 0xFF )
#define spbrgh ( ( spbrg >> 8 ) & 0xFF )
/* Turn on the UART and set baud rate */
void uart_setup ()
{
RCSTAbits . SPEN = 1 ;
BAUDCONbits . BRG16 = 1 ;
TXSTAbits . TXEN = 1 ;
TXSTAbits . BRGH = brgh ;
RCSTAbits . CREN = 1 ;
SPBRG = spbrgl ;
SPBRGH = spbrgh ;
}
/* Send data to UART */
void uart_send ( unsigned char * buffer, int count )
{
int n ;
for ( n = 0; n < count; n++ )
{
while ( !TXSTAbits . TRMT ) ;
TXREG = buffer [ n ] ;
}
}
/* Receive from UART, return -1 if no char */
int uart_receive ()
{
if ( RCSTAbits . OERR )
{
RCSTAbits . CREN = 0 ;
RCSTAbits . CREN = 1 ;
}
if ( PIR1bits . RCIF )
return RCREG ;
else
return -1 ;
}
void Setup_UART(void)
{
TX1STA    = 0b00100100;
RC1STA    = 0b00010000;
BAUDCON1  = 0b00001000;
SP1BRGL   = xxx;           //Baudrate value, look at datasheet
SP1BRGH   = xxx;
RCSTA1bits.SPEN = 1;
}
void Send_Byte(uint8_t Data)
{
while (!PIR3bits.TXIF);
TX1REG = Data;
}

此示例适用于 PIC1827k40。请查看您的控制器的数据表。

最新更新