高中生
最后登录1970-1-1
在线时间 小时
注册时间2016-1-7
|
本帖最后由 灰太狼的伤疤 于 2017-4-6 14:40 编辑
- void uart_init(u32 bound){
- //GPIO¶Ë¿úéèÖÃ
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüUSART1£¬GPIOAê±Öó
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- //USART1_TX GPIOA.9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
- GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.9
-
- //USART1_RX GPIOA.103õê¼»ˉ
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
- GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.10
-
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- // GPIO_Init(GPIOC, &GPIO_InitStructure);
- // GPIO_SetBits(GPIOC,GPIO_Pin_8);
- //Usart1 NVIC ÅäÖÃ
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 2 ;//ÇàÕ¼óÅÏ輶3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×óóÅÏ輶3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
- NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
-
- //USART 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(USART2, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
- USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú1
- }
- void USART2_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
- {
- u8 Res;
- #if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
- OSIntEnter();
- #endif
- GPIO_ResetBits(GPIOC,GPIO_Pin_8);
- /* USER CODE END UART4_IRQn 0 */
- if (USART_GetITStatus(USART2, USART_IT_RXNE)==SET)
- {
-
- // while (USART_GetITStatus(USART1, USART_FLAG_RXNE) !=SET);
- Res = USART_ReceiveData(USART2);
- // SaveSendDataChar(buf);
- // SaveReciveData(buf);
-
- USART_RX_BUF[USART_RX_STA]=Res;
- USART_RX_STA ++;
- if(USART_RX_STA == USART_REC_LEN)
- USART_RX_STA = 0;
-
-
- }
- GPIO_SetBits(GPIOC,GPIO_Pin_8);
- // if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
- // {
- // Res =USART_ReceiveData(USART1); //¶á衽óêÕμ½μÄêy¾Y
- //
- // if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
- // {
- // if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
- // {
- // if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
- // else USART_RX_STA|=0x8000; //½óêÕíê3éáË
- // }
- // 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))USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
- // }
- // }
- // }
- // }
- #if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
- OSIntExit();
- #endif
- }
- #endif
复制代码
|
|