我需要使用串口1,3,4,5,6,按照原子的程序完成了串口3的数据收发,但是串口4-6却没成功,我不知道,程序出现什么问题了还是我的设置还有什么问题,硬件是用的原子的stm32f407的开发板,请哪位大侠能给我解惑。程序如下:
UART.c中的代码:
......
u8 USART_RX_BUF[USART_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
u8 USART_RX_BUF3[USART_REC_LEN3]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
u8 USART_RX_BUF4[USART_REC_LEN4]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
u8 USART_RX_BUF5[USART_REC_LEN5]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
u8 USART_RX_BUF6[USART_REC_LEN6]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬ ½óêÕíê3é±êÖ¾
//bit14£¬ ½óêÕμ½0x0d
//bit13~0£¬ ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
u16 USART_RX_STA=0; //½óêÕ×′쬱ê¼Ç
u16 USART_RX_STA3=0; //½óêÕ×′쬱ê¼Ç
u16 USART_RX_STA4=0; //½óêÕ×′쬱ê¼Ç
u16 USART_RX_STA5=0; //½óêÕ×′쬱ê¼Ç
u16 USART_RX_STA6=0; //½óêÕ×′쬱ê¼Ç
//3õê¼»ˉIO ′®¿ú1
//bound:2¨ìØÂê
void uart1_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//ê1ÄüUSART1ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9¸′óÃÎaUSART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10¸′óÃÎaUSART1
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9óëGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA9£¬
A10
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //êä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //Ëù¶è2MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_8);
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
void USART1_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA&0x8000)==0) //½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000) //½óêÕμ½áË0x0d
{
if(Res!=0x0a) //½óêÕ′íÎó,ÖØD¿aê¼
USART_RX_STA=0;
else //½óêÕíê3éáË
USART_RX_STA|=0x8000;
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)
USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1)) //½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
USART_RX_STA=0;
}
}
}
}
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntExit();
#endif
}
void uart3_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //ê1ÄüGPIOBê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//ê1ÄüUSART3ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); //GPIOB11¸′óÃÎaUSART3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); //GPIOB10¸′óÃÎaUSART3
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; //GPIOB11óëGPIOB10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOB,&GPIO_InitStructure); //3õê¼»ˉPA9£¬
A10
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(USART3, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART3, USART_FLAG_TC);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =4; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //êä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //Ëù¶è2MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
void USART3_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART3);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA3&0x8000)==0) //½óêÕÎ′íê3é
{
if(USART_RX_STA3&0x4000) //½óêÕμ½áË0x0d
{
if(Res!=0x0a) //½óêÕ′íÎó,ÖØD¿aê¼
USART_RX_STA3=0;
else //½óêÕíê3éáË
USART_RX_STA3|=0x8000;
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)
USART_RX_STA3|=0x4000;
else
{
USART_RX_BUF3[USART_RX_STA3&0X3FFF]=Res ;
USART_RX_STA3++;
if(USART_RX_STA3>(USART_REC_LEN3-1)) //½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
USART_RX_STA3=0;
}
}
}
}
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntExit();
#endif
}
void uart4_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //ê1ÄüGPIOBê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);//ê1ÄüUSART3ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4); //GPIOC11¸′óÃÎaUSART4
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4); //GPIOC10¸′óÃÎaUSART4
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; //GPIOB11óëGPIOB10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure); //3õê¼»ˉPA9£¬
A10
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(UART4, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(UART4, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(UART4, USART_FLAG_TC);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=4;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =4; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //êä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //Ëù¶è2MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC, GPIO_Pin_9);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
}
void UART4_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(UART4);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA4&0x8000)==0) //½óêÕÎ′íê3é
{
if(USART_RX_STA4&0x4000) //½óêÕμ½áË0x0d
{
if(Res!=0x0a) //½óêÕ′íÎó,ÖØD¿aê¼
USART_RX_STA4=0;
else //½óêÕíê3éáË
USART_RX_STA4|=0x8000;
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)
USART_RX_STA4|=0x4000;
else
{
USART_RX_BUF4[USART_RX_STA4&0X3FFF]=Res ;
USART_RX_STA4++;
if(USART_RX_STA4>(USART_REC_LEN4-1)) //½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
USART_RX_STA4=0;
}
}
}
}
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntExit();
#endif
}
void uart5_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); //ê1ÄüGPIOBê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);//ê1ÄüUSART3ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5); //GPIOB11¸′óÃÎaUSART3
GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_UART5); //GPIOB10¸′óÃÎaUSART3
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOC12
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure); //3õê¼»ˉPC12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //GPIOD2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOD,&GPIO_InitStructure); //3õê¼»ˉPD2
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(UART5, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(UART5, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(UART5, USART_FLAG_TC);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=5;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //êä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //Ëù¶è2MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_SetBits(GPIOD, GPIO_Pin_6);
GPIO_ResetBits(GPIOD, GPIO_Pin_6);
}
void UART5_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(UART5);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA5&0x8000)==0) //½óêÕÎ′íê3é
{
if(USART_RX_STA5&0x4000) //½óêÕμ½áË0x0d
{
if(Res!=0x0a) //½óêÕ′íÎó,ÖØD¿aê¼
USART_RX_STA5=0;
else //½óêÕíê3éáË
USART_RX_STA5|=0x8000;
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)
USART_RX_STA5|=0x4000;
else
{
USART_RX_BUF5[USART_RX_STA5&0X3FFF]=Res ;
USART_RX_STA5++;
if(USART_RX_STA5>(USART_REC_LEN5-1)) //½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
USART_RX_STA5=0;
}
}
}
}
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntExit();
#endif
}
void uart6_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //ê1ÄüGPIOCê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//ê1ÄüUSART3ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOB11¸′óÃÎaUSART6
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //GPIOB10¸′óÃÎaUSART6
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIOB6óëGPIOB7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure); //3õê¼»ˉPC6£¬
C7
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART6, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(USART6, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART6, USART_FLAG_TC);
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=6;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //êä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //Ëù¶è2MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
}
void USART6_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART6);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA6&0x8000)==0) //½óêÕÎ′íê3é
{
if(USART_RX_STA6&0x4000) //½óêÕμ½áË0x0d
{
if(Res!=0x0a) //½óêÕ′íÎó,ÖØD¿aê¼
USART_RX_STA6=0;
else //½óêÕíê3éáË
USART_RX_STA6|=0x8000;
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)
USART_RX_STA6|=0x4000;
else
{
USART_RX_BUF6[USART_RX_STA6&0X3FFF]=Res ;
USART_RX_STA6++;
if(USART_RX_STA6>(USART_REC_LEN6-1)) //½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
USART_RX_STA6=0;
}
}
}
}
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntExit();
#endif
}
main.c中的代码为:
......
int main(void)
{
u8 t;
u8 len;
u16 times=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //ê1óÃμÎ′eê±Öó×öÑóê±3õê¼»ˉ
uart1_init(4800); //′®¿ú3õê¼»ˉ2¨ìØÂê
uart3_init(4800);
uart4_init(4800);
uart5_init(4800);
uart6_init(9600);
for(len = 0; len < 10; len ++)
{
USART_SendData(USART6, 0x31); //Ïò′®¿ú1·¢Ëíêy¾Y
while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET);//μè′y·¢Ëí½áêø
}
LED_Init(); &nb
串口4,5的主要问题是我在初始化串口4的时候没有开GIOC的AHB总线时钟,导致串口4和5都没办法用,开了AHB总线时钟就好了;串口6比较神奇,在网上搜到一个大神的帖子说的比较详细,但是忘了网址了,实在对不起这位老大,不过解决问题的主要方法是需要在初始化串口的时候把 USART6->CR2 重新初始化,将同步串口功能全部关掉就可以了。
......
USART_ClockInitTypeDef USART_ClockInitStruct; //定义一个时钟结构的变量
......
USART_ClockStructInit(&USART_ClockInitStruct);
USART_ClockInit(USART6,&USART_ClockInitStruct); //将USART6->CR2全部赋初值0
......
本人通过实验证实该方法的确可行,只是觉得有些诧异,不过每个人可能碰到的问题不一定都跟我一样,这里只是给以后遇到同样问题的人一个参考,写的不好或不对的请诸位见谅。另外,个人感觉ST公司工作做得不够细致,明明手册上标注的默认值为0x00000000,结果却出现一个不同的结果。
你好,我写的程序串口4也是不能使用,怎么开AHB总线时钟呢?
一周热门 更多>