STM32 例程-4 Proteus下串口发送数据
1、配置端口代码如下
void usart1_config() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO,ENABLE); GPIO_InitTypeDef a; a.GPIO_Pin=GPIO_Pin_9; a.GPIO_Speed=GPIO_Speed_50MHz; a.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOA,&a); a.GPIO_Pin=GPIO_Pin_10; a.GPIO_Mode=GPIO_Mode_IN_FLOATING; a.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&a); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitTypeDef b; b.NVIC_IRQChannel = USART1_IRQn; b.NVIC_IRQChannelPreemptionPriority = 1; b.NVIC_IRQChannelSubPriority = 1; b.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&b); USART_InitTypeDef c; c.USART_BaudRate=9600; c.USART_HardwareFlowControl=USART_HardwareFlowControl_None; c.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; c.USART_WordLength=USART_WordLength_8b; c.USART_Parity=USART_Parity_No; c.USART_StopBits=USART_StopBits_1; USART_Init(USART1,&c); USART_Cmd(USART1,ENABLE); }
2、完整代码如下
#include "stm32f10x.h" __IO uint32_t TimingDelay; u16 i=0; #include "stm32f10x.h" #include "stm32f10x_gpio.h" #include "stm32f10x_usart.h" #include "misc.h" #include "stdio.h" void usart1_config() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO,ENABLE); GPIO_InitTypeDef a; a.GPIO_Pin=GPIO_Pin_9; a.GPIO_Speed=GPIO_Speed_50MHz; a.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOA,&a); a.GPIO_Pin=GPIO_Pin_10; a.GPIO_Mode=GPIO_Mode_IN_FLOATING; a.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&a); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitTypeDef b; b.NVIC_IRQChannel = USART1_IRQn; b.NVIC_IRQChannelPreemptionPriority = 1; b.NVIC_IRQChannelSubPriority = 1; b.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&b); USART_InitTypeDef c; c.USART_BaudRate=9600; c.USART_HardwareFlowControl=USART_HardwareFlowControl_None; c.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; c.USART_WordLength=USART_WordLength_8b; c.USART_Parity=USART_Parity_No; c.USART_StopBits=USART_StopBits_1; USART_Init(USART1,&c); USART_Cmd(USART1,ENABLE); } void LED_GPIO_Config(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU ; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); } void NVIC_Configuare(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM3_Configuare(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 10-1; TIM_TimeBaseStructure.TIM_Prescaler =(200-1); TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_Trigger,ENABLE); TIM_Cmd(TIM3, ENABLE); } void delay_nms(__IO uint32_t nTime) { TimingDelay=nTime; while(TimingDelay!=0); } void Init_SysTick(void){ if(SysTick_Config(SystemCoreClock/1000)) while(1); } int main(void) { u8 kent=0; SystemInit(); RCC_ClockSecuritySystemCmd(ENABLE); LED_GPIO_Config(); NVIC_Configuare(); TIM3_Configuare(); Init_SysTick(); usart1_config(); USART_SendData(USART1,'0'); while(1) { if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)==0) { delay_nms(10); if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)==0) kent++; if(kent%2) USART_SendData(USART1,'1'); else {USART_SendData(USART1,'2');kent=0;} while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)==0); } } }
3、Proteus界面
4、注意仿真芯片频率至少要设置为8M,低于8M乱码。 ,
本博客是个人工作中记录,更深层次的问题可以提供有偿技术支持。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。