STM32串口通信配置(USART1+USART2+USART3+UART4)
摘要:
本文是通过分享几个通用的串口函数,实现对STM32芯片的多个串口操作和多种收发数据方式,实现串口通信的功能,本文的各个驱动代码经测试都可以直接使用,本人写的这部分代码开源 (。_。)
一、串口一的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include "stm32f10x_usart.h" #include "stm32f10x.h" #include "stm32f10x_iwdg.h"
u8 USART1_RX_BUF[21]; u8 USART1_RX_CNT=0;
void IWDG_Configuration(void);
void Usart1_Init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound; 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); }
void USART1_Send_Data(u8 *buf,u16 len) { u16 t; GPIO_SetBits(GPIOC,GPIO_Pin_9);
for(t=0;t<len;t++) { while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); USART_SendData(USART1,buf[t]); } while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); GPIO_ResetBits(GPIOC,GPIO_Pin_9);
} void main(void) { Usart1_Init(9600); IWDG_Configuration(); while(1) { IWDG_ReloadCounter(); if(USART1_RX_CNT==21) { USART1_RX_CNT=0; USART1_Send_Data(USART1_RX_BUF,21); } } }
void USART1_IRQHandler(void) { u8 Res; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { Res =USART_ReceiveData(USART1); if(USART1_RX_CNT<21) { USART1_RX_BUF[USART1_RX_CNT]=Res; USART1_RX_CNT++; } } if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) == SET) { USART_ReceiveData(USART1); USART_ClearFlag(USART1,USART_FLAG_ORE); } USART_ClearFlag(UART1,USART_IT_RXNE); }
void IWDG_Configuration(void) { IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); IWDG_SetPrescaler(IWDG_Prescaler_256); IWDG_SetReload(781); IWDG_SetReload(3125); IWDG_Enable(); IWDG_ReloadCounter(); }
|
二、串口二的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include "stm32f10x_usart.h" #include "stm32f10x.h" #include "stm32f10x_iwdg.h"
u8 USART2_RX_BUF[250]; u8 USART2_RX_CNT=0; u16 USART2_RX_STA=0;
void Usart2_Init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound; 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(USART2, &USART_InitStructure); ; USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2, ENABLE);
}
void USART2_Send_Data(u8 *buf,u16 len) { u16 t; for(t=0;t<len;t++) { while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); USART_SendData(USART2,buf[t]); } while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); }
void USART2_Receive_Data(u8 *buf) { u8 rxlen=USART2_RX_CNT; u8 i=0; delay_ms(10); while(rxlen!=USART2_RX_CNT) { rxlen=USART2_RX_CNT; delay_ms(10); } for(i=0;i<(USART2_RX_CNT);i++) { buf[i] = USART2_RX_BUF[i]; USART2_RX_BUF[i] = 0; } USART2_RX_CNT=0; }
void main(void) { Usart2_Init(9600); while(1) { if(USART2_RX_STA) { USART2_RX_STA=0; USART2_Send_Data(USART2_RX_BUF,USART2_RX_CNT); USART2_RX_CNT=0; } } }
void USART2_IRQHandler(void) { u8 res; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { res =USART_ReceiveData(USART2); if(USART2_RX_STA==0) { USART2_RX_BUF[USART2_RX_CNT] = res; if(USART2_RX_BUF[USART2_RX_CNT-1]==0xA0&&USART2_RX_BUF[USART2_RX_CNT]==0xA1) USART2_RX_STA=1; USART2_RX_CNT++; } } } if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) == SET) { USART_ReceiveData(USART2); USART_ClearFlag(USART2,USART_FLAG_ORE); } USART_ClearFlag(UART2,USART_IT_RXNE); }
|
三、串口三的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include "stm32f10x_usart.h" #include "stm32f10x.h"
#define USART3_TIMEOUT_Setting 800
u8 USART3_RX_BUF[250]; u16 USART3_RX_CNT=0; u16 USART3_RX_TIMEOUT=0;
void Timer1CountInitial(void);
void USART3_Init(u32 baud) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baud; 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(USART3, &USART_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); USART_Cmd(USART3, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void USART3_Sned_Char(u8 temp) { USART_SendData(USART3,(u8)temp); while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET); }
void USART3_Sned_Char_Buff(u8 buf[],u32 len) { u32 i; for(i=0;i<len;i++) USART3_Sned_Char(buf[i]); }
void main(void) { Timer1CountInitial(); Usart3_Init(9600); while(1) { if(USART3_RX_TIMEOUT==USART3_TIMEOUT_Setting) { USART3_RX_TIMEOUT=0; USART3_Sned_Char_Buff(USART3_RX_BUF,USART3_RX_CNT); USART3_RX_CNT=0; } } } void USART3_IRQHandler(void) { u8 Res; if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { USART3_RX_TIMEOUT=0; USART3_RX_BUF[USART3_RX_CNT++] = USART_ReceiveData(USART3); } if(USART_GetFlagStatus(USART3,USART_FLAG_ORE) == SET) { USART_ReceiveData(USART3); USART_ClearFlag(USART3,USART_FLAG_ORE); } USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}
void Timer1CountInitial(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); TIM_TimeBaseStructure.TIM_Period = 100-1; TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); TIM_ClearFlag(TIM1,TIM_FLAG_Update); TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); TIM_Cmd(TIM1, ENABLE); } void TIM1_UP_IRQHandler(void) { if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) { if(USART3_RX_TIMEOUT<USART3_TIMEOUT_Setting) USART3_RX_TIMEOUT++; } TIM_ClearITPendingBit(TIM1,TIM_IT_Update); }
|
四、串口四的配置
注意串口四的中断优先级没有贴出来,和前面的三个一样的配置,为了不占用过多的篇幅就不贴中断优先级配置了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include "stm32f10x_usart.h" #include "stm32f10x.h"
#define USART4_TIMEOUT_Setting 800
u8 USART4_RX_BUF[250]; u16 USART4_RX_CNT=0; u16 USART2_RX_STA=0;
void Systick_delay_init(u8 SYSCLK); u8 virtual_delay(u32 num,u8 unit);
void UART4_Init(u32 bound) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
GPIO_InitStructure.GPIO_Pin = RS485_TX_EN_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RS485_TX_EN_PORT, &GPIO_InitStructure);
RS485_TX_EN=0;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound; 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(UART4, &USART_InitStructure); USART_Cmd(UART4, ENABLE); USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_ClearFlag(UART4,USART_FLAG_TC); }
void UART4_Receive_Data(u8 *buf) { u8 rxlen=21; u8 i=0; delay_ms(10); RS485_RX_FLAG = 0; if((UART4_RX_BUF[0]==0x01)&&(UART4_RX_BUF[1]==0x03)) { for(i=0;i<rxlen;i++) { buf[i]=UART4_RX_BUF[i]; UART4_RX_BUF[i] = 0; } RS485_RX_FLAG = 1; } UART4_RX_CNT=0; }
void UART4_Send_Data(u8 *buf,u16 len) { u16 t; RS485_TX_EN=1; for(t=0;t<len;t++) { while(USART_GetFlagStatus(UART4,USART_FLAG_TC)==RESET); USART_SendData(UART4,buf[t]); } while(USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET); RS485_TX_EN=0; }
void main(void) { Systick_delay_init(72); Usart4_Init(9600); while(1) { if(USART2_RX_STA) { if(virtual_delay(USART4_TIMEOUT_Setting,MS)) { UART4_Send_Data(UART4_RX_BUF,UART4_RX_CNT); USART2_RX_STA=0; UART4_RX_CNT=0; } } } } void UART4_IRQHandler(void) { u8 Res; if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) { Res =USART_ReceiveData(UART4); UART4_RX_BUF[UART4_RX_CNT&0XFF]=Res; UART4_RX_CNT++; USART2_RX_STA=1; } if( USART_GetITStatus(UART4, USART_IT_TC) == SET ) { USART_ClearFlag(UART4, USART_FLAG_TC); } if(USART_GetFlagStatus(UART4,USART_FLAG_ORE) == SET) { USART_ReceiveData(UART4); USART_ClearFlag(UART4,USART_FLAG_ORE); }
USART_ClearFlag(UART4,USART_IT_RXNE); }
void Systick_delay_init(u8 SYSCLK) { SysTick->CTRL&=0xfffffffb;
fac_us=SYSCLK/8; fac_ms=(u16)fac_us*1000; }
u8 virtual_delay(u32 num,u8 unit) { u32 temp; if(virtual_delay_status==RESET) { if(unit==MS) { SysTick->LOAD=(u32)num*Delay_SYSCLK*125; SysTick->VAL =0x00; SysTick->CTRL=0x01 ; }else if(unit==US) { SysTick->LOAD=num*Delay_SYSCLK/8; SysTick->VAL=0x00; SysTick->CTRL=0x01 ; } virtual_delay_status=SET; return 0; } else { temp=SysTick->CTRL; if(!(temp&0x01&&!(temp&(1<<16)))) { SysTick->CTRL=0x00; SysTick->VAL =0X00; virtual_delay_status=RESET; return 1; }else return 0; } }
|
附上本人这篇博客地址