以下是和串口相关的,使用了串口超时中断接收数据帧。这个应该没有问题的。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
//////////////////////
////////////////外设时钟设置//////////////////////////
/// Enable peripheral clock
PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_USART1, Enable);
PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_USART2, Enable);
PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_USART3, Enable);
/* Timer0 peripheral enable */
PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM01, Enable);
PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM02, Enable);
void TimeInit(void) //定时器初始化
{
stc_clk_freq_t stcClkTmp;
stc_tim0_base_init_t stcTimerCfg;
stc_tim0_trigger_init_t StcTimer0TrigInit;
MEM_ZERO_STRUCT(stcClkTmp);
MEM_ZERO_STRUCT(stcTimerCfg);
MEM_ZERO_STRUCT(StcTimer0TrigInit);
/* Timer0 peripheral enable */
PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM01, Enable);
PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM02, Enable);
/* Clear CNTAR register for channel A */
TIMER0_WriteCntReg(M4_TMR01, Tim0_ChannelA, 0u);
TIMER0_WriteCntReg(M4_TMR01, Tim0_ChannelB, 0u);
TIMER0_WriteCntReg(M4_TMR02, Tim0_ChannelA, 0u);
/* Config register for channel A */
stcTimerCfg.Tim0_CounterMode = Tim0_Async;
stcTimerCfg.Tim0_AsyncClockSource = Tim0_LRC;
stcTimerCfg.Tim0_ClockDivision = Tim0_ClkDiv8;
stcTimerCfg.Tim0_CmpValue = 800u;
TIMER0_BaseInit(M4_TMR01, Tim0_ChannelA, &stcTimerCfg);
stcTimerCfg.Tim0_CounterMode = Tim0_Async;
stcTimerCfg.Tim0_AsyncClockSource = Tim0_LRC;
stcTimerCfg.Tim0_ClockDivision = Tim0_ClkDiv8;
stcTimerCfg.Tim0_CmpValue = 800u;
TIMER0_BaseInit(M4_TMR01, Tim0_ChannelB, &stcTimerCfg);
stcTimerCfg.Tim0_CounterMode = Tim0_Async;
stcTimerCfg.Tim0_AsyncClockSource = Tim0_LRC;
stcTimerCfg.Tim0_ClockDivision = Tim0_ClkDiv8;
stcTimerCfg.Tim0_CmpValue = 800u;
TIMER0_BaseInit(M4_TMR02, Tim0_ChannelA, &stcTimerCfg);
/* Clear compare flag */
TIMER0_ClearFlag(M4_TMR01, Tim0_ChannelA);
TIMER0_ClearFlag(M4_TMR01, Tim0_ChannelB);
TIMER0_ClearFlag(M4_TMR02, Tim0_ChannelA);
/* Config timer0 hardware trigger */
StcTimer0TrigInit.Tim0_InTrigEnable = false;
StcTimer0TrigInit.Tim0_InTrigClear = true;
StcTimer0TrigInit.Tim0_InTrigStart = true;
StcTimer0TrigInit.Tim0_InTrigStop = false;
TIMER0_HardTriggerInit(M4_TMR01, Tim0_ChannelA, &StcTimer0TrigInit);
StcTimer0TrigInit.Tim0_InTrigEnable = false;
StcTimer0TrigInit.Tim0_InTrigClear = true;
StcTimer0TrigInit.Tim0_InTrigStart = true;
StcTimer0TrigInit.Tim0_InTrigStop = false;
TIMER0_HardTriggerInit(M4_TMR01, Tim0_ChannelB, &StcTimer0TrigInit);
StcTimer0TrigInit.Tim0_InTrigEnable = false;
StcTimer0TrigInit.Tim0_InTrigClear = true;
StcTimer0TrigInit.Tim0_InTrigStart = true;
StcTimer0TrigInit.Tim0_InTrigStop = false;
TIMER0_HardTriggerInit(M4_TMR02, Tim0_ChannelA, &StcTimer0TrigInit);
}
/*******************************************************************************
* 函 数 名: UART_Init
* 函数功能: 串口初始化
* 输 入: 无
* 输 出: 无
*******************************************************************************/
void UART_Init(void)
{
//USART_DeInit(M4_USART1);
//USART_DeInit(M4_USART2);
// USART_DeInit(M4_USART3);
en_result_t enRet = Ok;
stc_irq_regi_conf_t stcIrqRegiCfg;
const stc_usart_uart_init_t stcInitCfg = {
UsartIntClkCkNoOutput,
UsartClkDiv_1,
UsartDataBits8,
UsartDataLsbFirst,
UsartOneStopBit,
UsartParityNone,
UsartSamleBit8,
UsartStartBitFallEdge,
UsartRtsEnable,
};
const stc_usart_uart_init_t stcInitCfg1 = {
UsartIntClkCkNoOutput,
UsartClkDiv_1,
UsartDataBits9,
UsartDataLsbFirst,
UsartOneStopBit,
UsartParityEven,
UsartSamleBit8,
UsartStartBitFallEdge,
UsartRtsEnable,
};
/////////配置串口参数/////////////////////////
enRet = USART_UART_Init(M4_USART1, &stcInitCfg1);
if (enRet != Ok)
{
while (1)
{
}
}
enRet = USART_UART_Init(M4_USART2, &stcInitCfg1);
if (enRet != Ok)
{
while (1)
{
}
}
enRet = USART_UART_Init(M4_USART3, &stcInitCfg);
if (enRet != Ok)
{
while (1)
{
}
}
///////////配置波特率///////////////////
enRet = USART_SetBaudrate(M4_USART3, 9600u);
if (enRet != Ok)
{
while (1)
{
}
}
enRet = USART_SetBaudrate(M4_USART1, 9600u);
if (enRet != Ok)
{
while (1)
{
}
}
enRet = USART_SetBaudrate(M4_USART2, 9600u);
if (enRet != Ok)
{
while (1)
{
}
}
///Set USART RX IRQ
stcIrqRegiCfg.enIRQn = Int000_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart1RxIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART1_RI;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
// Set USART RX error IRQ
stcIrqRegiCfg.enIRQn = Int001_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart1ErrIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART1_RI;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
///Set USART RX IRQ
stcIrqRegiCfg.enIRQn = Int002_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart2RxIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART2_RI;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
// Set USART RX error IRQ
stcIrqRegiCfg.enIRQn = Int003_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart2ErrIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART2_RI;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
///Set USART RX IRQ
stcIrqRegiCfg.enIRQn = Int004_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3RxIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART3_RI;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
// Set USART RX error IRQ
stcIrqRegiCfg.enIRQn = Int005_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3ErrIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART3_RI;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
///Set USART1 RX timeout error IRQ
stcIrqRegiCfg.enIRQn = Int006_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart1TimeoutIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART1_RTO;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/// Set USART1 RX timeout error IRQ
stcIrqRegiCfg.enIRQn = Int007_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart2TimeoutIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART2_RTO;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
/// Set USART1 RX timeout error IRQ
stcIrqRegiCfg.enIRQn = Int008_IRQn;
stcIrqRegiCfg.pfnCallback = &Usart3TimeoutIrqCallback;
stcIrqRegiCfg.enIntSrc = INT_USART3_RTO;
enIrqRegistration(&stcIrqRegiCfg);
NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);
///Enable RX && TX function
USART_FuncCmd(M4_USART1, UsartRx, Enable);
USART_FuncCmd(M4_USART1, UsartTx, Enable);
USART_FuncCmd(M4_USART2, UsartRx, Enable);
USART_FuncCmd(M4_USART2, UsartTx, Enable);
USART_FuncCmd(M4_USART3, UsartRx, Enable);
USART_FuncCmd(M4_USART3, UsartTx, Enable);
//Enable RX && RX interupt function*/
USART_FuncCmd(M4_USART1, UsartRxInt, Enable);
USART_FuncCmd(M4_USART2, UsartRxInt, Enable);
USART_FuncCmd(M4_USART3, UsartRxInt, Enable);
USART_FuncCmd(M4_USART1, UsartTimeOut, Enable);
USART_FuncCmd(M4_USART1, UsartTimeOutInt, Enable);
USART_FuncCmd(M4_USART2, UsartTimeOut, Enable);
USART_FuncCmd(M4_USART2, UsartTimeOutInt, Enable);
USART_FuncCmd(M4_USART3, UsartTimeOut, Enable);
USART_FuncCmd(M4_USART3, UsartTimeOutInt, Enable);
}
///////////////////////////接收中断//////////////////////////////////////
////////////////////////////////////////////////////////////////
void Usart1RxIrqCallback(void)
{
m_u16RxData=USART_RecData(M4_USART1);
USART_FuncCmd(M4_USART1, UsartTxAndTxEmptyInt, Enable);
}
void Usart2RxIrqCallback(void)
{
m_u16RxData=USART_RecData(M4_USART1);
USART_FuncCmd(M4_USART1, UsartTxAndTxEmptyInt, Enable);
}
void Usart3RxIrqCallback(void)
{
m_u16RxData=USART_RecData(M4_USART1);
USART_FuncCmd(M4_USART1, UsartTxAndTxEmptyInt, Enable);
}
////////////////////////////接收超时中断/////////////////////////////////////
////////////////////////////////////////////////////////////////
void Usart1TimeoutIrqCallback(void)
{
TIMER0_Cmd(M4_TMR01, Tim0_ChannelA,Disable);
USART_ClearStatus(M4_USART1, UsartRxTimeOut);
}
void Usart2TimeoutIrqCallback(void)
{
TIMER0_Cmd(M4_TMR01, Tim0_ChannelB,Disable);
USART_ClearStatus(M4_USART2, UsartRxTimeOut);
}
void Usart3TimeoutIrqCallback(void)
{
cmcc=1;
TIMER0_Cmd(M4_TMR02, Tim0_ChannelA,Disable);
USART_ClearStatus(M4_USART3, UsartRxTimeOut);
}
////////////////////错误中断////////////////////////////////
//////////////////////////////////////////////////////
void Usart1ErrIrqCallback(void)
{
if (Set == USART_GetStatus(M4_USART1, UsartFrameErr))
{
USART_ClearStatus(M4_USART1, UsartFrameErr);
}
else
{
}
if (Set == USART_GetStatus(M4_USART1, UsartParityErr))
{
USART_ClearStatus(M4_USART1, UsartParityErr);
}
else
{
}
if (Set == USART_GetStatus(M4_USART1, UsartOverrunErr))
{
USART_ClearStatus(M4_USART1, UsartOverrunErr);
}
else
{
}
}
void Usart2ErrIrqCallback(void)
{
if (Set == USART_GetStatus(M4_USART2, UsartFrameErr))
{
USART_ClearStatus(M4_USART2, UsartFrameErr);
}
else
{
}
if (Set == USART_GetStatus(M4_USART2, UsartParityErr))
{
USART_ClearStatus(M4_USART2, UsartParityErr);
}
else
{
}
if (Set == USART_GetStatus(M4_USART2, UsartOverrunErr))
{
USART_ClearStatus(M4_USART2, UsartOverrunErr);
}
else
{
}
}
void Usart3ErrIrqCallback(void)
{
if (Set == USART_GetStatus(M4_USART3, UsartFrameErr))
{
USART_ClearStatus(M4_USART3, UsartFrameErr);
}
else
{
}
if (Set == USART_GetStatus(M4_USART3, UsartParityErr))
{
USART_ClearStatus(M4_USART3, UsartParityErr);
}
else
{
}
if (Set == USART_GetStatus(M4_USART3, UsartOverrunErr))
{
USART_ClearStatus(M4_USART3, UsartOverrunErr);
}
else
{
}
}
|