标题: STM8F103串口发送及接收程序 [打印本页]

作者: 1092648746    时间: 2019-3-6 18:45
标题: STM8F103串口发送及接收程序
最近在用STM8做东西,看到论坛上关于STM8的例程少之又少,故发一个串口例程
串口1波特率9600,不断发送数据,同时可响应接收数据,注释较少。
开发环境为IAR,可直接烧录生成的hex文件。




单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm8s.h"

  3. /* Private defines -----------------------------------------------------------*/
  4. /* Private function prototypes -----------------------------------------------*/
  5. /* Private functions ---------------------------------------------------------*/
  6. #define LED_GPIO_PORT  (GPIOB)
  7. #define LED_GPIO_PINS  (GPIO_PIN_5)
  8. void Delay(uint16_t nCount)
  9. {
  10.   /* Decrement nCount value */
  11.   while (nCount != 0)
  12.   {   
  13.     nCount--;
  14.   }
  15. }
  16. void Init_UART1(void)
  17. {
  18.         UART1_DeInit();
  19.         UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
  20.         
  21.         UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
  22.         enableInterrupts();
  23.         UART1_Cmd(ENABLE);
  24. }
  25. void Send(uint16_t dat)
  26. {
  27.   while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
  28.        
  29.         UART1_SendData9(dat);
  30.        
  31. }

  32. void main(void)
  33. {

  34.   uint8_t i=0;
  35.   uint16_t j=0;
  36.                 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  37.                 Init_UART1();
  38.                
  39. /* Infinite loop */
  40.   while (1)
  41.   {
  42.     for(j=0;j<60000;j++);
  43.                 Send(i);
  44.     //UART1_SendData8(0x01);
  45.                 i++;
  46.   }
  47.   
  48. }

  49. #ifdef USE_FULL_ASSERT

  50. /**
  51.   * @brief  Reports the name of the source file and the source line number
  52.   *   where the assert_param error has occurred.
  53.   * @param file: pointer to the source file name
  54.   * @param line: assert_param error line source number
  55.   * @retval : None
  56.   */
  57. void assert_failed(u8* file, u32 line)
  58. {
  59.   /* User can add his own implementation to report the file name and line number,
  60.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  61.   /* Infinite loop */
  62.   while (1)
  63.   {
  64.   }
  65. }
  66. #endif
复制代码

所有资料51hei提供下载:
UARTX-RX.7z (3.96 MB, 下载次数: 60)


作者: 剑剑剑剑来!    时间: 2021-3-18 17:31
您好,我是一名机械电子工程专业的学习者,由于选择了总线传感器的组网与程序设计这一项目,与所学专业知识有偏差,现如今十分迷茫。但在看到了您对传感器驱动一帖子的评论后,逐渐找了一点灵感,想与您请教交流。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1