Это драйвер от моего контроллера освещения в неолите.Это для PIC18F14K22, но должно быть достаточно близко, чтобы дать вам старт.Эта плата также немного ускоряет протокол для World Semi LEDs, отсюда и комментарий о времени.
/* 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 ;
}