• STM8S903K3T6C基于ST Visual Develop开发串口数据收发示例


    STM8S903K3T6C基于ST Visual Develop开发串口数据收发示例


    • 利用ST Visual Develop配置环境和工程,利用ST Visual Programmer工具来烧录程序。安装cosmic stm8 32k C编译器 v4.3.4插件,是为了实现在ST Visual Develop环境下使用C语言来开发stm8系列单片机,在默认没有安装第三方插件情况下,ST Visual Develop工具是不支持C语言开发的。

    🛠开发环境搭建

    • ST Visual Programmer STM8官方地址
    • https://www.st.com/zh/development-tools/stvp-stm8.html
      在这里插入图片描述
    • cosmic stm8 32k C编译器 v4.3.4插件
    • https://www.jb51.net/softs/626867.html#downintro2
    1.ST Visual Develop开发和IAR开发环境对比相同的地方是,都需要先创建一个工作空间,然后再创建工程。

    在这里插入图片描述

    2. 添加头文件。
    根据自己stm8型号选择对应的头文件。

    头文件位置:C:\Program Files (x86)\STMicroelectronics\st_toolset\include
    在这里插入图片描述

    • 安装了ST Visual Programmer STM8才有的。(上面提供有链接)

    在这里插入图片描述

    工程概览

    在这里插入图片描述

    • mian.cstm8_interrupt_vector.c文件是在创建时,会自动生成。头文件需要自己的芯片型号添加进来。
    • 芯片型号可在工程当中重新修改型号:
      在这里插入图片描述
      在这里插入图片描述
    • 串口波特率计算内容请参考文章前面的相关篇内容。

    🌼串口数据收发示例

    #include"stm8s903k3.h"
    #include"stdio.h"
    
    #define _COSMIC_
    /*
     由于不同的编译器 putcha 和 getchar 形参和返回值略有不同。
     因此此处采用宏定义的方式区别。
     _RAISONANCE_ 和 _COSMIC_ 这2个宏是由编译器自动添加的预编译宏
    */
    #ifdef _RAISONANCE_
     #define PUTCHAR_PROTOTYPE int putchar (char c)
     #define GETCHAR_PROTOTYPE int getchar (void)
    #elif defined (_COSMIC_)
     #define PUTCHAR_PROTOTYPE char putchar (char c)
     #define GETCHAR_PROTOTYPE char getchar (void)
    #else /* _IAR_ */
     #define PUTCHAR_PROTOTYPE int putchar (int c)
     #define GETCHAR_PROTOTYPE int getchar (void)
    #endif /* _RAISONANCE_ */
    
    
    PUTCHAR_PROTOTYPE
    {
     /* 发送一个字符 c 到UART1 */
    	UART1_DR = c;
     /* 等待发送完毕 */
     while (!(UART1_SR&0x40));
     return (c);
    }
    
    
    GETCHAR_PROTOTYPE
    {
     #ifdef _COSMIC_
      char c = 0;
     #else
      int c = 0;
     #endif
     /* 等待新数据到达  */
     while (!(UART1_SR&0x40));
     /* 读取数据寄存器 */
     c = UART1_DR;
     return (c);
    }
    
    void CLK_Init(void)
    {
    	//  CLK_ECKR = 0x01; //开启外部时钟寄存器
    //  CLK_SWR = 0xb4; //HSE外部时钟源作为主时钟源
    //  CLK_CKDIVR = 0x00;//不分频
      
      CLK_ICKR |= 0X01; //使能内部高速时钟 HSI
      CLK_CKDIVR = 0x00; // 不分频,16M
     //    CLK_CKDIVR = 0x08; // 16M内部RC经2分频后系统时钟为8M 
       while(!(CLK_ICKR&0x02)); //HSI准备就绪
         CLK_SWR = 0xE1;//HSI内部时钟源作为主时钟源(复位值)
    }
    void UART1_Init(void)
    {
    	 UART1_CR3 = 0x00;
    	 /*disable LIN mode 
    	-one stop bit
    	-disable SCK
    	*/
        UART1_CR2  = 0x00;
    		/*disable TX interrupt
    	  disable TX completion interrupt
    		disable RX interrupt
    		disable idle interrupt
    		disable TX and RX
    		没有发送断开帧
    	*/
        UART1_CR3  = 0x00;
    		/*one start bit 
    	eight data bits 
    	 wake up by idle bus
    	 disable ECC and EEC interrupt
    	 UART enable
    	*/
    	
    /* 波特率:9600 */  
       UART1_BRR2 = 0x02;//
       UART1_BRR1 = 0x68;//
      /* 波特率:115200 */   
    //    UART1_BRR2 = 0x0b;
     //   UART1_BRR1 = 0x08;
     UART1_CR2  = 0x0C; //enable TX and RX
    
    }
    /***
    char putchar(char c)
    {
    	while(!(UART1_SR&0x40));
    	UART1_DR = c;
    	while(!(UART1_SR&0x40));
        return (c);
    }
    ***/
    void delay (unsigned int x)
    {
    	unsigned int i,j;
    	for(i=x;i>0;i--)
    		for(j=300;j>0;j--);
    }
    void main()
    {	
    	unsigned char c;
    	CLK_Init();
    	UART1_Init();
    	UART1_CR2  = 0x0c;
    	while(1)
    	{
    		printf("\n\r请输入一个按键");
    		while(!(UART1_SR&0x20));
    		c = UART1_DR;
    		printf("\n\r输入按键为%c。",c);
    		
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120

    📚工程源码

    链接:https://pan.baidu.com/s/1vQcTd21biHqHV45Ya0UC7g 
    提取码:p3iw
    
    • 1
    • 2
  • 相关阅读:
    kubeadm快速部署kubernetes集群(1.24.2版本)
    京东数据分析(京东数据采集):2023年10月京东平板电视行业品牌销售排行榜
    可以攻击国王的皇后(JS)
    【每天学习一点新知识】XXE(XML外部实体注入)从入门到放弃
    解决使用npm出现Cannot find module ‘XXXnode_modulesnpmbinnpm-cli.js‘错误
    MySQL索引事务存储引擎
    来自华为的暴击?传高通裁员赔偿N+7 | 百能云芯
    新手如何使用Cheat Engine (CE) 来修改“我的世界“?
    【Java程序员面试专栏 算法思维】五 高频面试算法题:贪心算法
    Python Web开发的未来
  • 原文地址:https://blog.csdn.net/weixin_42880082/article/details/127845477