STM32串口空闲中断

串口初始化

#include "usart5.h"

vu16 UART5_RX_STA=0;
char UART5_RX_BUF[UART5_REC_LEN];
u8 UART5_TX_BUF[UART5_MAX_SEND_LEN]; extern unsigned char send_flag,Return;
extern unsigned char uart5_flag0;
extern unsigned int bufj;
void usart5_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); //ʹÄÜUART5£¬GPIOCʱÖÓ
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯
// GPIO_SetBits(GPIOD,GPIO_Pin_1); // //UART5_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯ //UART5_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯ //Uart5 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×ÓÓÅÏȼ¶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷ //UART5 ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//´®¿Ú²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½ USART_Init(UART5, &USART_InitStructure); //³õʼ»¯´®¿Ú5
USART_ITConfig(UART5,USART_IT_IDLE,ENABLE);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖжÏ
// TIM7_Int_Init(99,7199); //10msÖжÏ
// UART5_RX_STA=0;
USART_Cmd(UART5, ENABLE); //ʹÄÜ´®¿Ú4
delay_FOR(1000);
} char NUMFind[200]={0};
int NUNFind_cnt = 0;
void Uart5_Char(unsigned char  ch)
{
while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET);
USART_SendData(UART5,ch);
} void Uart5_SendFIFO(unsigned char cmd[],unsigned int Len)
{
unsigned char i;
while((UART5->SR & 0x40)==0);
for(i=0;i<Len;i++)
{
UART5->DR = cmd[i];
while((UART5->SR & 0x40)==0);
}
} void u5_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)UART5_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)UART5_TX_BUF);
for(j=0;j<i;j++)
{
while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET);
USART_SendData(UART5,UART5_TX_BUF[j]);
}
}

中断调用代码

extern uint8_t Usart5_ReceiveState;
extern u16 count;
__asm void SystemReset(void); void UART5_IRQHandler(void)
{
u8 clear= clear;
u8 res; if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(UART5, USART_IT_RXNE);
res = USART_ReceiveData(UART5);
UART5_RX_BUF[count] = res;
count++;
// Uart5_Char(res);
}
else if(USART_GetITStatus(UART5, USART_IT_IDLE) != RESET)
{
clear = UART5->SR;
clear = UART5->DR;
Usart5_ReceiveState = 1;
} }

主函数返回

uint8_t Usart5_ReceiveState;
void RCC_Configuration(void);
void Port_Init(void);
u16 count=0;
int main()
{
u16 i; RCC_ClearFlag();
RCC_Configuration();
Init485();
RF_PortInit();
uart1_Init(115200);
usart5_Init(115200);
GET_ID(); while(1)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_4);
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
for(i=0;i<test;i++)
{
// u5_printf("hello Dev,I am %s,\n\n",ID);
// u5_printf("who are you?\n\n",ID);
}
// u1_printf("hello Dev,I am %s\n\n",ID);
delay_FOR(400);
if(Usart5_ReceiveState)
{ for(i=0;i<count;i++)
{
Uart5_Char(UART5_RX_BUF[i]); }
// Uart5_Char(0x01);
count = 0;
Usart5_ReceiveState=0;
}
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //¶¨ÒåÍⲿ¾§ÕñΪÆô¶¯×´Ì¬Ã¶¾Ù±äÁ¿
SystemInit(); //ϵͳ³õʼ»¯
RCC_DeInit(); //¸´Î»RCC¼Ä´æÆ÷µ½Ä¬ÈÏÖµ
RCC_HSEConfig(RCC_HSE_ON); //´ò¿ªÍⲿ¸ßËÙ¾§Õñ
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //µÈ´ýÍⲿ¸ßËÙ¾§Õñ×¼±¸ºÃ
if(HSEStartUpStatus == SUCCESS) //Æô¶¯³É¹¦
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); ////flash¶ÁÈ¡»º´æÇøʹÄÜ
FLASH_SetLatency(FLASH_Latency_2); //flashÑÓʱ RCC_HCLKConfig(RCC_SYSCLK_Div1); //??AHB(HCLK)????==SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //??APB2(PCLK2)?==AHB??
RCC_PCLK1Config(RCC_HCLK_Div2); //??APB1(PCLK1)?==AHB1/2,Ô¤·ÖƵ2 72M RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //??PLL?? == 8M£¨Íⲿ£© * 9 = 72MHz
RCC_PLLCmd(ENABLE); //??PLL?? while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //??PLL????
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //?????? = PLL??
while(RCC_GetSYSCLKSource() != 0x08) //??PLL??????????
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|
RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOF|RCC_APB2Periph_GPIOG , ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
上一篇:编译android-4.3.1_r源代码并刷到自己的Galaxy Nexus I9250真机上


下一篇:asp.net mvc 之旅 —— 第五站 从源码中分析asp.net mvc 中的TempData