找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4610|回复: 1
收起左侧

STM8F103串口发送及接收程序

[复制链接]
ID:393699 发表于 2019-3-6 18:45 | 显示全部楼层 |阅读模式
最近在用STM8做东西,看到论坛上关于STM8的例程少之又少,故发一个串口例程
串口1波特率9600,不断发送数据,同时可响应接收数据,注释较少。
开发环境为IAR,可直接烧录生成的hex文件。
tu1.JPG

tu2.JPG

单片机源程序如下:
  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)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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