本帖最后由 qgy1007 于 2024-3-5 09:27 编辑
/******************************************************************************
* Include files
******************************************************************************/
#include "ddl.h"
#include "uart.h"
#include "gpio.h"
#include "sysctrl.h"
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/******************************************************************************
* Local variable definitions ('static') *
******************************************************************************/
uint8_t u8TxData[50] = {
0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA
};
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
void App_PortInit(void);
void App_UartCfg(void);
/**
******************************************************************************
** \brief Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/
int32_t main(void)
{
uint8_t i;
App_PortInit(); //端口初始化
App_UartCfg(); //串口模块配置
while(1)
{
if((Uart_GetStatus(M0P_UART1, UartFE))||(Uart_GetStatus(M0P_UART1, UartPE))) //错误请求
{
Uart_ClrStatus(M0P_UART1, UartFE); //清除帧错误标记
Uart_ClrStatus(M0P_UART1, UartPE); //清除奇偶校验错误标记
}
if(Uart_GetStatus(M0P_UART1,UartRC)) //接收到数据
{
Uart_ClrStatus(M0P_UART1,UartRC);
u8TxData[0] = Uart_ReceiveData(M0P_UART1); //接收数据一字节数据后,回复50字节数据
for(i=0;i<50;i++)
{
Uart_SendDataPoll(M0P_UART1,u8TxData); //查询方式发送数据 }
}
}
}
//串口引脚配置
void App_PortInit(void)
{
stc_gpio_cfg_t stcGpioCfg;
DDL_ZERO_STRUCT(stcGpioCfg);
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //GPIO外设模块时钟使能
stcGpioCfg.enDir = GpioDirOut;
Gpio_Init(GpioPortD,GpioPin0,&stcGpioCfg);
Gpio_SetAfMode(GpioPortD,GpioPin0,GpioAf3); //配置PA09 为UART0 TX
stcGpioCfg.enDir = GpioDirIn;
Gpio_Init(GpioPortD,GpioPin1,&stcGpioCfg);
Gpio_SetAfMode(GpioPortD,GpioPin1,GpioAf3);//配置PA10 为UART0 RX
}
//串口模块配置
void App_UartCfg(void)
{
stc_uart_cfg_t stcCfg;
stc_uart_multimode_t stcMulti;
stc_uart_baud_t stcBaud;
DDL_ZERO_STRUCT(stcCfg);
DDL_ZERO_STRUCT(stcMulti);
DDL_ZERO_STRUCT(stcBaud);
Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);//UART0外设模块时钟使能
stcCfg.enRunMode = UartMskMode3; //模式3
stcCfg.enStopBit = UartMsk1bit; //1位停止位
stcCfg.enMmdorCk = UartMskEven; //偶校验
stcCfg.stcBaud.u32Baud = 2400; //波特率9600
stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div; //通道采样分频配置
stcCfg.stcBaud.u32Pclk = Sysctrl_GetPClkFreq(); //获得外设时钟(PCLK)频率值
Uart_Init(M0P_UART1, &stcCfg); //串口初始化
Uart_ClrStatus(M0P_UART1,UartRC); //清接收请求
Uart_ClrStatus(M0P_UART1,UartTC); //清发送请求
Uart_EnableIrq(M0P_UART1,UartRxIrq); //使能串口接收中断
Uart_EnableIrq(M0P_UART1,UartTxIrq); //使能串口发送中断
}
/******************************************************************************
* EOF (not truncated)
******************************************************************************/
|
得看看见证奇迹的代码