|
在V5开发板出厂程序进行修改的程序,串口配置基本一样。
但是有一个奇怪的现象:当主循环里每秒执行一次串口发送时,串口接收中断正常;如果主循环里没用串口发送程序,就不能接收数据,进不了串口接收中断。
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 串口1 TX = PC10 RX = PC11 */
/* 打开 GPIO 时钟 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* 打开 UART 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
/* 配置 USART Tx 为复用功能 */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* 输出类型为推挽 */
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; /* 内部上拉电阻使能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; /* 复用模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 配置 USART Rx 为复用功能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 第2步: 配置串口硬件参数 */
USART_InitStructure.USART_BaudRate = 9600; /* 波特率 */
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); /* 使能串口 */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
如下语句解决第1个字节无法正确发送出去的问题 */
USART_ClearFlag(USART1, USART_FLAG_TC); /* 清发送完成标志,Transmission Complete flag */
}
void USART1_IRQHandler(void)
{
u8 i,j;
/* 处理接收中断 */
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
for(i=0;i<29;i++)
RxBuffer1=RxBuffer1[i+1];
RxBuffer1[29] = USART_ReceiveData(USART1);
if(RxBuffer1[6]==0x04&&RxBuffer1[7]==0x0A&&RxBuffer1[29]==0x05)
{
switch(RxBuffer1[8])
{
case 1:
if(SYS_STATUS==MS_IDLE||SYS_STATUS==MS_SLEEP||SYS_STATUS==MS_SYSCHECK)
{
Upper=1;
}
break;
case 2:
if(SYS_STATUS==MS_COUNT||SYS_STATUS==MS_SYSCHECK)
{
Upper=2;
}
break;
case 3:
for(j=0;j<18;j++)
patientName[j]=RxBuffer1[j+9];
patientSex=RxBuffer1[27];
patientAge=RxBuffer1[28];
break;
case 0x10:
TXserial(0x10,YH04AS>>24,YH04AS>>16&0xFF,YH04AS>>8&0xFF,YH04AS&0xFF,SYS_STATUS,-1);
break;
default:break;
}
USART_ClearFlag(USART1, USART_FLAG_RXNE);
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
// USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
}
/* 处理发送缓冲区空中断 */
if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
{
if (TxIndex < 24)
{
USART_SendData(USART1, TxBuffer1[TxIndex++]);
}
else
{
TxIndex=0;
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
} |
|