Chinaunix首页 | 论坛 | 博客
  • 博客访问: 172482
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-01 12:35
个人简介

不断超越自己,将更强大!

文章分类

全部博文(35)

文章存档

2022年(1)

2017年(5)

2016年(29)

我的朋友

分类: 嵌入式

2016-05-22 16:49:33

        原则上,像USART1等这些基本的外设,根据官方的库,应该直接成功了,可是,我试了一下,就是不成功。虽然有STM32F10X的基础,但是感觉打印的就是乱码,并且发送时,竟然中断接收函数里,中断进不了!!

       后来查看原理图,原来STLink V2有引脚与PA9 PA10连接。应该是USB转串口吧,因此,再把USB-TTL 接到PA9 PA10上,就会发生 两个TXD->TXD相互干扰的现象,因此不通!!我这里直接去掉连接的短接电阻,程序正常了。



      

       注意开始时需要修改一下库里的晶振设置:8M外部晶振与PLL_M=8。官方库好像是基于25MHz晶振的,但是实际的焊接的为:8MHz晶振。


      修改后:测试成功了。收发正常了。


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /********************     (C)  2016     *************************** 
  2.  * 文件名  :usart1.c 
  3.  * 描述       :将printf函数重定向到USART3。这样就可以用printf函数将单片机的数据 
  4.  *            打印到PC上的超级终端或串口调试助手。          
  5.  * 实验平台 :STM32F429 
  6.  * 硬件连接 :------------------------ 
  7.  *          | PA9  - USART1(Tx)      | 
  8.  *          | PA10  - USART1(Rx)      | 
  9.  *           ------------------------ 
  10.  * 库版本      :V1.6.1 
  11.  * 编写日期 :2016-05-22 
  12.  * 修改日期 : 
  13.  * 作者       : 
  14. *****************************************************************************/  
  15. #include "usart1.h"  
  16. #include   
  17.   
  18. /* 
  19.  * 函数名:USART1_Config 
  20.  * 描述  :USART1 GPIO 配置 
  21.  * 输入  :uint32_t uBaud 
  22.  * 输出  : 无 
  23.  * 调用  :外部调用 
  24.  */  
  25. void USART1_Config(uint32_t uBaud)  
  26. {  
  27.     USART1_Configuration(uBaud);  
  28.     USART1_NVIC_Configuration();  
  29. }  
  30.   
  31.   
  32. /* 
  33.  * 函数名:USART1_Configuration 
  34.  * 描述  :USART1 GPIO 配置,工作模式配置。uBaud 8-N-1 
  35.  * 输入  :uint32_t uBaud 
  36.  * 输出  : 无 
  37.  * 调用  :外部调用 
  38.  */  
  39. void USART1_Configuration(uint32_t uBaud)  
  40. {  
  41.     GPIO_InitTypeDef GPIO_InitStructure;  
  42.     USART_InitTypeDef USART_InitStructure;  
  43.       
  44.     /* config USART1 clock */  
  45.   
  46.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);  
  47.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);  
  48.       
  49.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);     
  50.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);  
  51.       
  52.     /* USART1 GPIO config */  
  53.     /* Configure USART1 Tx (PA.9) as alternate function push-pull */  
  54.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  
  55.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  56.     //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  57.     //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  58.     //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  59.     GPIO_Init(GPIOA, &GPIO_InitStructure);      
  60.     /* Configure USART1 Rx (PA.10) as input floating */  
  61.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  
  62.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  63.     //GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;  
  64.     //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  
  65.     //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  66.     GPIO_Init(GPIOA, &GPIO_InitStructure);  
  67.         
  68.     /* USART1 mode config */  
  69.     USART_InitStructure.USART_BaudRate = uBaud;  
  70.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
  71.     USART_InitStructure.USART_StopBits = USART_StopBits_1;  
  72.     USART_InitStructure.USART_Parity = USART_Parity_No ;  
  73.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
  74.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
  75.     USART_Init(USART1, &USART_InitStructure);   
  76.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  
  77.     USART_Cmd(USART1, ENABLE);  
  78. }  
  79.   
  80.   
  81. void USART1_NVIC_Configuration(void)  
  82. {  
  83.     NVIC_InitTypeDef NVIC_InitStructure;   
  84.     /* Configure the NVIC Preemption Priority Bits */    
  85.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  
  86.       
  87.     /* Enable the USARTy Interrupt */  
  88.     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;      
  89.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  
  90.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  91.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  92.     NVIC_Init(&NVIC_InitStructure);  
  93. }  
  94.   
  95.   
  96.   
  97. /* 
  98.  * 函数名:fputc 
  99.  * 描述  :重定向c库函数printf到USART1 
  100.  * 输入  :无 
  101.  * 输出  :无 
  102.  * 调用  :由printf调用 
  103.  */  
  104. int fputc(int ch, FILE *f)  
  105. {  
  106.     /* 将Printf内容发往串口 */  
  107.     USART_SendData(USART1, (unsigned char) ch);  
  108.     while (!(USART1->SR & USART_FLAG_TXE));  
  109.     //while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);    
  110.     return (ch);  
  111. }  



[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.   * @brief  This function handles USART1 Handler. 
  3.   * @param  None 
  4.   * @retval None 
  5.   */  
  6. void USART1_IRQHandler(void)  
  7. {  
  8.     uint8_t c;  
  9.     if(USART_GetITStatus(USART1,USART_IT_RXNE))  
  10.     {  
  11.       USART_ClearITPendingBit(USART1, USART_IT_RXNE);  
  12.         c=(uint8_t)USART_ReceiveData(USART1);     
  13.         printf("%c",c);  
  14.     }  
  15. }  



   Keil V5.10工程下载:


阅读(2867) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~