#pragma config POSCMOD=XT,FNOSC=PRIPLL//#pragma config POSCMOD=OFF,FNOSC=FRCPLL
#pragma config FPLLMUL=MUL_20,FPLLIDIV=DIV_2,FPLLODIV=DIV_4
#pragma config FPBDIV = DIV_2,FWDTEN=OFF,CP=OFF,BWP=OFF
#pragma config JTAGEN = OFF,FUSBIDIO = OFF
#define SYS_FREQ (20000000L)
#define pbclk 10000000
#define DESIRED_BAUDRATE (9600)
void __ISR(_UART_1_VECTOR, ipl2) IntUart1Handler(void)
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART1)))
{
PutCharacter(UARTGetDataByte(UART1));
INTClearFlag(INT_SOURCE_UART_RX(UART1));
}
// We don't care about TX interrupt
if (INTGetFlag(INT_SOURCE_UART_TX(UART1)))
{
INTClearFlag(INT_SOURCE_UART_TX(UART1));
}
}
void PutCharacter(const char character)
{
while(!UARTTransmitterIsReady(UART1)) ;
UARTSendDataByte(UART1, character);
while(!UARTTransmissionHasCompleted(UART1));
}
UARTinit()
{
PPSInput(3,U1RX,RPA2); // Assign RPA2 as input pin for U1RX
PPSOutput(1,RPB3,U1TX); // Set RB3 pin as output for U1TX
UARTConfigure(UART1, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART1, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART1, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART1,pbclk, DESIRED_BAUDRATE);
UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
INTEnable(INT_SOURCE_UART_RX(UART1), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART1), INT_PRIORITY_LEVEL_2);
INTSetVectorSubPriority(INT_VECTOR_UART(UART1), INT_SUB_PRIORITY_LEVEL_0);
}
int main (void)
{
SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
UARTinit();
INTEnableInterrupts();
while(1);
}
想实现接收数据回显,回显总是00,8M晶振,求大神指点