1. 为了能够支持printf函数
定义如下
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
复制代码
2. UART IO及设置 初始化
- void USART1_Configuration(uint32_t iBaudRate)//´®¿Ú³õʼ»¯º¯Êý
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
- /*
- * USART1_TX -> PA2 , USART1_RX -> PA3
- */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = iBaudRate;//115200;//ÉèÖô®¿Ú²¨ÌØÂÊ
- 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_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART1, ENABLE);//ʹÄÜ´®¿Ú1
- }
复制代码
3. 设置UART接收中断
- void USART1_INT_Config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* UART1 clock enable */
- // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
- /* UART1 ÖжÏǶÌ×Éè¼Æ*/
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
复制代码
4. 发送一个字节
- void UART1_SendByte(uint8_t byte) //·¢ËÍ1×Ö½ÚÊý¾Ý
- {
- // RS485_C_HI;
- while(!((USART1->ISR)&(1<<7)));
- USART1->TDR=byte;
- // RS485_C_LW;
- }
- 5. 发送BUFFER
- void UART1_SendData(uint8_t *Buffer, uint32_t Length)
- {
- // RS485_C_HI;
- while(Length != 0)
- {
- while(!((USART1->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê
- USART1->TDR= *Buffer;
- Buffer++;
- Length--;
- }
- // RS485_C_LW;
- }
复制代码
6. 接收一个字节- uint8_t UART_Recive(void)
- {
- while(!(USART1->ISR & (1<<5)));//µÈ´ý½ÓÊÕµ½Êý¾Ý
- return(USART1->RDR); //¶Á³öÊý¾Ý
- }
- 7. PUCHAR_PROTOTYPE 函数
- PUTCHAR_PROTOTYPE
- {
- /* ½«PrintfÄÚÈÝ·¢Íù´®¿Ú */
- USART_SendData(USART1,(uint8_t) ch);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
- {}
- return (ch);
- }
复制代码
8. UART接收中断函数
- void USART1_IRQHandler(void)
- {
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- if(TimeInterval > 4 && TimeInterval < 6)
- {
- Uart1_Cnt = 0;
- }
- if(Uart1_Cnt > 60)
- {
- Uart1_Cnt =0;
- }
- /* if(Uart1_Cnt == 0)
- {
- Uart1_Cnt =0;
- Uart1_RxBuf[Uart1_Cnt]=USART_ReceiveData(USART1);//¶ÁÈ¡UART1½ÓÊÕµ½µÄÊý¾Ý²¢Êä³ö
- }
- else */
- {
- Uart1_RxBuf[Uart1_Cnt]=USART_ReceiveData(USART1);//¶ÁÈ¡UART1½ÓÊÕµ½µÄÊý¾Ý²¢Êä³ö
- Uart1_Cnt++;
- }
- // Uart1_Cnt++;
- TimeInterval = 0;
- TimeRxFlag = 1;
- }
- }
复制代码
|