使用CH340G進(jìn)行usart通訊,使用微處理STM32V407VG 關(guān)鍵通過WCH串口工具,接收到數(shù)據(jù)就是00,HSM晶振設(shè)置8M,但是依舊接收到00
代碼如下:
#include? #include? static?USART_InitTypeDef?USART_InitStructure; static?GPIO_InitTypeDef?GPIO_InitStructure; static?NVIC_InitTypeDef?NVIC_InitStructure; //?位帶操作 #define?displacementPortAOut(pin)?*(volatile?uint32_t?*)(0x42000000?+?(GPIOA_BASE+?0x14?-?0x40000000)*32?+?pin*4) //?位帶操作 #define?displacementPortAIn(pin)?*(volatile?uint32_t?*)(0x42000000?+?(GPIOA_BASE+?0x10?-?0x40000000)*32?+?pin*4) static?void?UsartInitConfig(void){ //啟用串口硬件時鐘 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //啟用PA的硬件時鐘 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //初始引腳9,引腳10?、復(fù)用模式 ?GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_9?|?GPIO_Pin_10;??//串口綁定引腳號??????????? ?GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF;??//?復(fù)用模式 ?????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_100MHz; ?????GPIO_InitStructure.GPIO_OType?=?GPIO_OType_PP; ?????GPIO_InitStructure.GPIO_PuPd?=?GPIO_PuPd_NOPULL; //GPIO初始 ?GPIO_Init(GPIOA,?&GPIO_InitStructure); //引腳硬件綁定硬件為復(fù)用模式 ?GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_USART1); ?GPIO_PinAFConfig(GPIOD,GPIO_PinSource10,GPIO_AF_USART1); //配置串口參數(shù)?:比特率、字節(jié)、停止位、是否校驗奇偶、收發(fā)模式、是否需要硬件控制 ?USART_InitStructure.USART_BaudRate?=?115200;?//?比特率 ?USART_InitStructure.USART_WordLength?=?USART_WordLength_8b;????//?8字節(jié)?????? ?USART_InitStructure.USART_StopBits?=?USART_StopBits_1;????//?停止位 ?USART_InitStructure.USART_Parity?=?USART_Parity_No;????//是否校驗奇偶 ?USART_InitStructure.USART_Mode?=?USART_Mode_Rx?|?USART_Mode_Tx;??//收發(fā)模式 ?USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None;?//是否需要硬件控制 //串口號綁定初始 ?USART_Init(USART1,&USART_InitStructure); ? ?//配置串口接受數(shù)據(jù)觸發(fā)一個中斷函數(shù) ?USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); ? //嵌套向量中斷控機器優(yōu)先級組配置: ?NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //嵌套向量中斷控制器 ?NVIC_InitStructure.NVIC_IRQChannel?=?USART1_IRQn;??????????????? ?????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?0;? ?????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0;??????? ?????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; //嵌套向量中斷控制器初始化 ?NVIC_Init(&NVIC_InitStructure); //啟動串口 ?USART_Cmd(USART1,?ENABLE); } void?USART_SendString(USART_TypeDef*?USARTx,?char?*DataString) { int?i?=?0; USART_ClearFlag(USARTx,USART_FLAG_TC); //發(fā)送字符前清空標(biāo)志位(否則缺失字符串的第一個字符) while(DataString[i]?!=?'\0') //字符串結(jié)束符 { USART_SendData(USARTx,DataString[i]); //每次發(fā)送字符串的一個字符 while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)?==?0); //等待數(shù)據(jù)發(fā)送成功 USART_ClearFlag(USARTx,USART_FLAG_TC); //發(fā)送字符后清空標(biāo)志位 i++; } } //?入口函數(shù) int?main(void) { ????//?初始串口配置 UsartInitConfig(); //?發(fā)送串口信息 USART_SendString(USART1,"Hello?world!\r\n"); ???while(1) ???{ ???} }
晶振配置:
#if?!defined??(HSE_VALUE)? ??#define?HSE_VALUE????((uint32_t)8000000)?/*!<?Value?of?the?External?oscillator?in?Hz?*/ ?? #endif?/*?HSE_VALUE?*/ /*************************?PLL?Parameters?*************************************/ /*?PLL_VCO?=?(HSE_VALUE?or?HSI_VALUE?/?PLL_M)?*?PLL_N?*/ #define?PLL_M??????8 /*?USB?OTG?FS,?SDIO?and?RNG?Clock?=??PLL_VCO?/?PLLQ?*/ #define?PLL_Q??????7 #if?defined?(STM32F40_41xxx) #define?PLL_N??????336 /*?SYSCLK?=?PLL_VCO?/?PLL_P?*/ #define?PLL_P??????2 #endif?/*?STM32F40_41xxx?*/ #if?defined?(STM32F427_437xx)?||?defined?(STM32F429_439xx) #define?PLL_N??????360 /*?SYSCLK?=?PLL_VCO?/?PLL_P?*/ #define?PLL_P??????2 #endif?/*?STM32F427_437x?||?STM32F429_439xx?*/ #if?defined?(STM32F401xx) #define?PLL_N??????336 /*?SYSCLK?=?PLL_VCO?/?PLL_P?*/ #define?PLL_P??????2 #endif?/*?STM32F401xx?*/
搞不懂到底為什么收到00
熱門產(chǎn)品 :
CH32V317: 互聯(lián)型青稞RISC-V MCU