void TxIntCallback(void)
{
}
void uart_send_byte(uint8_t by);
void RxIntCallback(void)
{
uint8_t d;
uint8_t rxBuffer[256] ={0};
//Uart_ClrStatus(UARTCH1,UartRC);
d=Uart_ReceiveData(UARTCH1);
uart_send_byte(d);
}
void ErrIntCallback(void)
{
}
void PErrIntCallBack(void)
{
}
void CtsIntCallBack(void)
{
}
void Uart_PortInit(void)
{
stc_gpio_config_t stcGpioCfg;
DDL_ZERO_STRUCT(stcGpioCfg);
stcGpioCfg.enDir = GpioDirOut;
Gpio_Init(GpioPortA,GpioPin2,&stcGpioCfg);
Gpio_SetAfMode(GpioPortA,GpioPin2,GpioAf1);//TX
stcGpioCfg.enDir = GpioDirIn;
Gpio_Init(GpioPortA,GpioPin3,&stcGpioCfg);
Gpio_SetAfMode(GpioPortA,GpioPin3,GpioAf1);//RX
}
void uart_init(void)
{
uint16_t u16Scnt = 0;
stc_gpio_config_t stcGpioCfg;
stc_uart_config_t stcConfig;
stc_uart_irq_cb_t stcUartIrqCb;
stc_uart_multimode_t stcMulti;
stc_uart_baud_t stcBaud;
en_uart_mmdorck_t enTb8;
DDL_ZERO_STRUCT(stcConfig);
DDL_ZERO_STRUCT(stcUartIrqCb);
DDL_ZERO_STRUCT(stcMulti);
DDL_ZERO_STRUCT(stcBaud);
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);//使能GPIO模块时钟
Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);//使能uart1模块时钟
Uart_PortInit();
stcUartIrqCb.pfnRxIrqCb = RxIntCallback;//中断入口地址
stcUartIrqCb.pfnTxIrqCb = TxIntCallback;
stcUartIrqCb.pfnRxFEIrqCb = ErrIntCallback;
stcUartIrqCb.pfnPEIrqCb = PErrIntCallBack;
stcUartIrqCb.pfnCtsIrqCb = CtsIntCallBack;
stcConfig.pstcIrqCb = &stcUartIrqCb;
stcConfig.bTouchNvic = TRUE;
if(TRUE == stcConfig.bTouchNvic)
{
EnableNvic(UART1_IRQn,IrqLevel3,TRUE);
}
stcConfig.enRunMode = UartMode3;//模式3
stcConfig.enStopBit = Uart1bit; //1bit停止位
stcMulti.enMulti_mode = UartNormal;//正常工作模式
Uart_SetMultiMode(UARTCH1,&stcMulti);//多主机单独配置
enTb8 = UartDataOrAddr;//偶校验
Uart_SetMMDOrCk(UARTCH1,enTb8);
Uart_Init(UARTCH1, &stcConfig);//串口初始化
Uart_SetClkDiv(UARTCH1,Uart8Or16Div);//采样分频
stcBaud.u32Pclk = Sysctrl_GetPClkFreq();
stcBaud.enRunMode = UartMode3;
stcBaud.u32Baud = 9600;
u16Scnt = Uart_CalScnt(UARTCH1,&stcBaud);//波特率计算
Uart_SetBaud(UARTCH1,u16Scnt);//波特率设置
Uart_ClrStatus(UARTCH1,UartRC);//清接收请求
Uart_EnableIrq(UARTCH1,UartRxIrq);//使能串口中断
Uart_EnableFunc(UARTCH1,UartRx);//使能收发
}
void uart_send_byte(uint8_t by)
{
Uart_SendData(UARTCH1, by);
}
|