|
如题,今天调试STM32F107VCT6。结果发现串口的数据一只不对。0x00变成0xc0. 0x10变成0xd0。。。。。只有0x55,0xff,才对,不知道是什么问题,折磨疯了。大家有遇到过这个问题么?求大牛指点
下面是我的代码。
- #include "stm32f10x.h"
- void USART1_GPIO_Init(void);
- void USART1_Configuration(void);
- void USART1_NVIC_Init(void);
- void InitInitComRS232(void);
- void InitialSysClock(void);
- void SendData1(unsigned char i);
- int main(void)
- {
- InitialSysClock();//初始化系统时钟
- InitInitComRS232();
-
- while(1)
- SendData1(0xaa);
- }
- void BSP_NVIC_Init(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //先占优先级2位,从优先级2位
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
- }
- static volatile ErrorStatus HSEStartUpStatus = SUCCESS;
- void InitialSysClock(void)
- {
- /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/
- /* RCC system reset(for debug purpose) */
- RCC_DeInit(); //设置RCC寄存器重新设置为默认值
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON); //打开外部高速时钟晶振
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速时钟晶振工作
- if (HSEStartUpStatus == SUCCESS) //外部就绪
- {
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /*************************************************************************/
- /* HSE=8MHz,HCLK=72MHz,PCLK2=72MHz,PCK1=36MHz */
- /*************************************************************************/
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //设置AHB时钟不分频
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1); //设置APB2时钟不分频
- /* PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2); //设置APB1时钟二分频
- RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC时钟2分频 12Mhz
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
- /* Enable PLL */
- RCC_PLLCmd(ENABLE); //使能PLL
- /* Wait till PLL is ready */
- while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {
- }
- /* Select PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- while (RCC_GetSYSCLKSource() != 0x08) {
- }
- }
- /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG
- | RCC_APB2Periph_AFIO, ENABLE);
- BSP_NVIC_Init();
-
- }
- void InitInitComRS232(void)
- {
- USART1_GPIO_Init(); //初始化端口
- USART1_Configuration();
- USART1_NVIC_Init();
- }
- void USART1_Configuration(void) //串口初始化函数
- {
- USART_InitTypeDef USART_InitStructure;
- //串口参数初始化
- //串口设置恢复默认参数
- //初始化参数设置
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //打开串口1时钟
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //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; //打开Rx接收和Tx发送功能
- USART_Init(USART1, &USART_InitStructure); //初始化
-
-
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//TXE接收中断,TC传输完成中断,RXNE接收中
- USART_Cmd(USART1, ENABLE); //启动串口
- }
- void USART1_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // 使能PortA
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化
- }
- void USART1_NVIC_Init(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=4;//先占优先级0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART1_IRQHandler(void)
- {
- u8 RX_dat; //定义字符变量
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- /* 清楚接收中断标志*/
- USART_ClearITPendingBit(USART1, USART_IT_RXNE);
- RX_dat = USART_ReceiveData(USART1);
- SendData1(RX_dat);
- }
- if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
- {
- /* 清楚发送中断标志*/
- USART_ClearITPendingBit(USART1, USART_IT_TXE);
- }
- }
- void SendDatas1(unsigned char *output,unsigned char Outlength)
- {
- while(Outlength--)
- {
- USART1->DR=*(output++);
- while((USART1->SR&0X40)==0);//等待发送结束
- }
- }
- void SendData1(unsigned char i)
- {
- USART1->DR=i;
- while((USART1->SR&0X40)==0);//等待发送结束
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
复制代码 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|