今天捣鼓了一下串口发送。
原理图:
可以看到发送腿是RC0。
代码:
#include "eusart1.h"
volatile eusart1_status_t eusart1RxLastError;
void (*EUSART1_FramingErrorHandler)(void);
void (*EUSART1_OverrunErrorHandler)(void);
void (*EUSART1_ErrorHandler)(void);
void EUSART1_DefaultFramingErrorHandler(void);
void EUSART1_DefaultOverrunErrorHandler(void);
void EUSART1_DefaultErrorHandler(void);
void EUSART1_Initialize(void)
{
BAUD1CON = 0x08; // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
RC1STA = 0x80; // SPEN enabled; RX9 8-bit; CREN disabled; ADDEN disabled; SREN disabled;
TX1STA = 0x24; // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
SP1BRGL = 0x19; // BRG = 0x19 = 9600 [url=home.php?mod=space&uid=72445]@[/url] 1 MHz FOSC
SP1BRGH = 0x00;
EUSART1_SetFramingErrorHandler(EUSART1_DefaultFramingErrorHandler);
EUSART1_SetOverrunErrorHandler(EUSART1_DefaultOverrunErrorHandler);
EUSART1_SetErrorHandler(EUSART1_DefaultErrorHandler);
eusart1RxLastError.status = 0;
}
void EUSART1_Write(uint8_t txData)
{
while(0 == PIR1bits.TX1IF)
{
}
TX1REG = txData; // Write the data byte to the USART.
}
void putch(char txData)
{
EUSART1_Write(txData);
}
void EUSART1_DefaultFramingErrorHandler(void){}
void EUSART1_DefaultOverrunErrorHandler(void)
{
RC1STAbits.CREN = 0;
RC1STAbits.CREN = 1;
}
void EUSART1_DefaultErrorHandler(void){}
void EUSART1_SetFramingErrorHandler(void (* interruptHandler)(void))
{
EUSART1_FramingErrorHandler = interruptHandler;
}
void EUSART1_SetOverrunErrorHandler(void (* interruptHandler)(void))
{
EUSART1_OverrunErrorHandler = interruptHandler;
}
void EUSART1_SetErrorHandler(void (* interruptHandler)(void))
{
EUSART1_ErrorHandler = interruptHandler;
}
/**
End of File
*/
#include "mcc_generated_files/mcc.h"
void main(void)
{
SYSTEM_Initialize(); // Initialize the device
while (1)
{
printf("Hello World! \r\n");
__delay_ms(2000);
}
}
效果图:
|