找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 10863|回复: 0
打印 上一主题 下一主题
收起左侧

stm32的USART1全双工模式程序

[复制链接]
跳转到指定楼层
楼主
ID:82781 发表于 2015-6-14 01:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
对昨天的程序http://www.51hei.com/bbs/dpj-36269-1.html进行修改,完善,加入接收程序!

与天津第四项目部宿舍

  1. #include "stm32f10x.h"
  2. extern unsigned char Receiver_date;//全局变量定义接收数据
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. void mysysinit(void);//系统时钟初始
  5. void my_USART_init(void);//初始化
  6. void my_send_byte(unsigned char send_date); //发送一个字节
  7. int main(void)
  8. {
  9. mysysinit();//RCC初始化,时钟设置72MHZ
  10.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能APB2的GPIO_D时钟



  11.   /*
  12.   我说怎么老是调不好串口呢?原来是我没有使能时钟啊,光在下面设置PA口有个屁用啊。
  13.   俗话说的好啊:烧火棍子,不能一头热一头冷不是。
  14.   哎呀,我怎么就忽略了这个细节啊,多么简单的串口程序啊,竟是让我折腾了半天
  15.   是这样的你要用串口那么去看电路图,看他的串口用了那个引脚,然后开启那个引脚的时钟,
  16.   然后在进行设置,这又一次的提醒我,没有时钟啥都没有,也揭示了我在做实验时凸显的一个现象
  17.   就是下载完毕运行正常,一旦从FLASH区启动代码就没有反应,这是因为,我在用串口下载完毕后
  18.   它自动调到应用程序区执行,而此时串口已经初始化了,就是在系统中已经开启了时钟,所以显示没问题,可以
  19.   接收,而我下电在上电,改变BOOT后,直接从USER区域启动加载代码我恰好没有使能PA口,所以就有这样的现象。
  20.   现在好了,真相大白,我疏忽大意了!
  21.   不过也好吃一堑长一智。
  22.   20110812与天津第四项目部宿舍内
  23.   王均伟
  24.   */
  25.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能APB2的GPIO_A时钟
  26.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能APB1的USART2时钟
  27.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能APB1的USART2时钟

  28.    /* Configure PD0 and PD2 in output pushpull mode */
  29.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11;
  30.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  31.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  32.   GPIO_Init(GPIOD, &GPIO_InitStructure);





  33. my_USART_init();
  34. my_send_byte(0x88);
  35.   my_send_byte(0x01);
  36.    my_send_byte(0x02);
  37.     my_send_byte(0x03);
  38.         while(1)
  39.      {
  40.    
  41.          if(Receiver_date==0x19)
  42.       {
  43.         /* Set the GPIOD port pin 8*/
  44.                 GPIO_SetBits(GPIOD, GPIO_Pin_8 );
  45.       }
  46.       if(Receiver_date==0x89)
  47.       {
  48.        /* Clears the GPIOD port pin 8 */
  49.      GPIO_ResetBits(GPIOD, GPIO_Pin_8);
  50.       }
  51.    
  52.    
  53.    
  54.      }   

  55. }
  56. /***********************************

  57. 发送一个字节函数通过串口

  58. ************************************/
  59. void my_send_byte(unsigned char send_date )
  60. {

  61.   while( (USART1->SR&0x00000080)!=0x80);//发送寄存器为空
  62.    USART1->DR=send_date;


  63. }
  64. /**********************************
  65.           初始化串口

  66. **********************************/
  67. void my_USART_init()
  68. {

  69. /*USART2的优先级设为5*/
  70. NVIC->IP[37]=5;
  71. /*开启38号中断即USART2,关闭其他所有外部的中断*/
  72. NVIC->ISER[1]=0x00000020;   
  73. /*设置复用模式下的引脚模式为全双工:TX输出推挽复用,RX为输入上拉模式,速度50MHZ*/
  74.             GPIOA->CRH=0x000008b0;
  75.   /* 1.开启USART,
  76. *
  77. */
  78. USART1->CR1=0x2000;
  79. /* 1.关闭局域网模式
  80. * 2.1个停止位
  81. * 3.CK引脚禁能
  82. */
  83. USART1->CR2=0;
  84. /* 1.关闭调制解调模式
  85. * 2.关闭DMA模式
  86. * 3.关闭智能卡、红外模式
  87. *   4.关闭错误中断

  88. */
  89. USART1->CR3=0;
  90. /*     波特率设置

  91.      2011年8月11日
  92.         王均伟
  93.          天津第四项目部宿舍

  94.     BRR中的第四位(DIV_Fraction)作为小数,高12位(DIV_MANtissa)作为整数部分,
  95.   
  96.     1,根据公式:波特率=fck/16*usardiv,其中usardivBRR寄存器的值,所以变形得:USARDIV=fck/16*波特率
  97.     2.算出来BRR寄存器的值后就要把这个值变成16进制数据写入BRR寄存器中,
  98.       遵循以下规则:
  99.       小数部分*16=DIV_Fraction或者取近似的值
  100.       整数部分直接=DIV_MANtissa
  101.     3.把这个16进制值写入BRR寄存器
  102.     例如我要算波特率设成9600bps的BRR寄存器值,
  103.     1.先求USARDIV=36000000/16*9600=234.375
  104.     2.换成十六进制:DIV_Fraction=16*0.375=0x6
  105.                     DIV_MANtissa=234=0xea
  106.     3.组合并写入寄存器
  107.                      USART2->BRR=0x0ea6;值得注意的是这里是16位半字操作,所以不要以为是32位。

  108. */
  109. USART1->BRR=0x0ea6;

  110. /* 1.开启USART
  111. * 2.开启接收完毕中断
  112. * 3.开启发送功能
  113. *   4.开启接收功能
  114. */
  115. USART1->CR1=0x202c;


  116. }


  117. void mysysinit()//系统初始化程序
  118. {
  119. ErrorStatus HSEStartUpStatus;//说明标志位
  120. RCC_DeInit();//所有外设全部缺省设置

  121. /* Enable HSE */
  122. RCC_HSEConfig(RCC_HSE_ON);
  123. /* Wait till HSE is ready and if Time out is reached exit */
  124. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  125. if(HSEStartUpStatus == SUCCESS)//启动成功
  126. {
  127. /*这两条FLASH指令必须加上,不知为啥?不加上就运行几秒后出错,参照系统初始化*/
  128. /* Enable The Prefetch Buffer */
  129. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH缓存开启
  130. /* Configure the Latency cycle: Set 2 Latency cycles */
  131.   FLASH_SetLatency(FLASH_Latency_2);  //设置FLASH这些位表示SYSCLK(系统时钟)周期与闪存访问时间的比例,为010:两个等待状态,当 48MHz < SYSCLK ≤ 72MHz
  132. /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
  133. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部时钟为8M,PLL的输入时钟=8MHZ,倍频系数9,

  134. /* Configure HCLK such as HCLK = SYSCLK */
  135. RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置了啦AHB分频器的分频系数=1,即HCLK=SYSCLK=72MHZ
  136. /* Configure PCLK1 such as PCLK1 = HCLK/2 */
  137. RCC_PCLK1Config(RCC_HCLK_Div2);//设置了APB1外设的时钟频率最大是36M这里是APB1的分频器设为2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
  138. /* Configure PCLK2 such as PCLK2 = HCLK */
  139. RCC_PCLK2Config(RCC_HCLK_Div1);//设置PLCK2=HCLK=72MHZ,的APB2分频器=1
  140. /* Select the PLL as system clock source */
  141. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置了SYSCLK的提供者为PLL,频率由上面算出=72MHZ
  142. /* disable PLL Ready interrupt */
  143. RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中断关闭
  144. /* disable PLL Ready interrupt */
  145. RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中断关闭
  146. /* disable PLL Ready interrupt */
  147. RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中断关闭
  148. /* disable PLL Ready interrupt */
  149. RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中断关闭
  150. /* disable PLL Ready interrupt */
  151. RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中断关闭

  152. /* PLL clock divided by 1.5 used as USB clock source */
  153. RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//设置USB的时钟为=72、1.5=48mhz
  154. /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
  155. RCC_ADCCLKConfig(RCC_PCLK2_Div2);//设置ADC时钟=PCLK2/2= 36MHZ
  156. /* disable the LSE */
  157. RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振关闭

  158. /*DISable the RTC clock */
  159. RCC_RTCCLKCmd(DISABLE);
  160. /* DISable the Clock Security System */
  161. RCC_ClockSecuritySystemCmd(DISABLE);
  162. /* Enable the PLL */
  163. RCC_PLLCmd(ENABLE);//使能PLL







  164. /* PLL ans system clock config */
  165. }
  166. else
  167. {
  168. /* Add here some code to deal with this error */
  169. }





  170. }

  171. 中断文件


  172. #include "stm32f10x_it.h"
  173. unsigned char Receiver_date;//全局变量定义接收数据
  174. void NMI_Handler(void)
  175. {
  176. }

  177. /**
  178.   * @brief  This function handles Hard Fault exception.
  179.   * @param  None
  180.   * @retval None
  181.   */
  182. void HardFault_Handler(void)
  183. {
  184.   /* Go to infinite loop when Hard Fault exception occurs */
  185.   while (1)
  186.   {
  187.   }
  188. }

  189. /**
  190.   * @brief  This function handles Memory Manage exception.
  191.   * @param  None
  192.   * @retval None
  193.   */
  194. void MemManage_Handler(void)
  195. {
  196.   /* Go to infinite loop when Memory Manage exception occurs */
  197.   while (1)
  198.   {
  199.   }
  200. }

  201. /**
  202.   * @brief  This function handles Bus Fault exception.
  203.   * @param  None
  204.   * @retval None
  205.   */
  206. void BusFault_Handler(void)
  207. {
  208.   /* Go to infinite loop when Bus Fault exception occurs */
  209.   while (1)
  210.   {
  211.   }
  212. }

  213. /**
  214.   * @brief  This function handles Usage Fault exception.
  215.   * @param  None
  216.   * @retval None
  217.   */
  218. void UsageFault_Handler(void)
  219. {
  220.   /* Go to infinite loop when Usage Fault exception occurs */
  221.   while (1)
  222.   {
  223.   }
  224. }

  225. /**
  226.   * @brief  This function handles SVCall exception.
  227.   * @param  None
  228.   * @retval None
  229.   */
  230. void SVC_Handler(void)
  231. {
  232. }

  233. /**
  234.   * @brief  This function handles Debug Monitor exception.
  235.   * @param  None
  236.   * @retval None
  237.   */
  238. void DebugMon_Handler(void)
  239. {
  240. }

  241. /**
  242.   * @brief  This function handles PendSV_Handler exception.
  243.   * @param  None
  244.   * @retval None
  245.   */
  246. void PendSV_Handler(void)
  247. {
  248. }

  249. /**
  250.   * @brief  This function handles SysTick Handler.
  251.   * @param  None
  252.   * @retval None
  253.   */
  254. void SysTick_Handler(void)
  255. {
  256. }

  257. /******************************************************************************/
  258. /*            STM32F10x Peripherals Interrupt Handlers                        */
  259. /******************************************************************************/

  260. /**
  261.   * @brief  This function handles External line 0 interrupt request.
  262.   * @param  None
  263.   * @retval None
  264.   */
  265. void EXTI0_IRQHandler(void)
  266. {
  267.   if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  268.   {
  269.     /* Toggle LED1 */
  270.    

  271.     /* Clear the  EXTI line 0 pending bit */
  272.     EXTI_ClearITPendingBit(EXTI_Line0);
  273.   }
  274. }

  275. /**
  276.   * @brief  This function handles External lines 9 to 5 interrupt request.
  277.   * @param  None
  278.   * @retval None
  279.   */
  280. void USART1_IRQHandler(void)
  281. {

  282. if( (USART1->SR&0x00000020)==0x20) //接收到数据
  283.   Receiver_date=(USART1->DR);

  284. USART1->SR=0;
  285. }
复制代码




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表