STC单片机基于VSCode PlatformIO开发STC15W408AS软串口示例程序

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STC单片机基于VSCode PlatformIO开发STC15W408AS软串口示例程序相关的知识,希望对你有一定的参考价值。

【STC单片机】基于VSCode PlatformIO开发STC15W408AS软串口示例程序


采用了外部16MHz的晶振

  • 终于实现了在基于VSCode PlatformIO开发STC15W408AS软串口正常的信息输出,前面有写文章,VSCode 代码和Keil开发环境下,一样的代码,VSCode编译烧录进去就输出的是乱码,百思不得其解,这次应该是不断修改参数摸索出来的完全没有理论依据作为支撑。

主程序代码

#include<lint.h>//包含SDCC_mcs51定义
#include "stdio.h"
#include <stc15.h>//51头文件
#include "soft_uart.h"
#include "delay.h"
#define led P1_0 
#define MAIN_Fosc 16000000uL //晶振频率
/*重映射串口打印函数printf   */
int putchar(int c)

    TxSend((uint8_t)c);
    return c;


int main(void)
       
   // EA = 1;
    led = 0;
    while (1)
    
      printf("hello world! \\n");    
      Delay(1000);
      led = !led;
      printf("perseverance51! \\n"); 
    

  • delay.h
#ifndef	__DELAY_H
#define	__DELAY_H
#include "stdint.h"//包含数据类型名缩写
#include "stdio.h"
#define _nop_()   __asm NOP__endasm


#define MAIN_Fosc 16000000uL //晶振频率
void Delay(unsigned int ms);
void Delay1us()	;	//@16MHz


#endif
  • delay.c
#include <delay.h>

void Delay(unsigned int ms)

    unsigned int i;
    do
    
        i = MAIN_Fosc / 13000;
        while (--i)
            ;
     while (--ms);

void Delay1us()		//@16MHz

	unsigned char i;
        i = 1;
    __asm nop__endasm;//内嵌汇编指令:NOP,相当于_nop_()
   __asm nop __endasm;
	//_nop_();
	
	while (--i);


  • soft_uart.c
#include "soft_uart.h"

// void BitTime(void)
// 
//   volatile  uint16_t i;
//     i = (16 * 80) / 13- 2; //根据主时钟来计算位时间16
//     while (--i)
//         ;
// 

void	BitTime(void)

	 uint16_t  i;
     i = (16 * 80) / 13 - 2; //根据主时钟来计算位时间16
	// i = ((MAIN_Fosc / 100) * 104) / 140000 - 1;  //根据主时钟来计算位时间
	while(--i);


void TxSend(uint8_t dat)

    uint8_t i;
    EA = 0;
    P_TXD = 0;
    BitTime();
    for (i = 0; i < 8; i++)
    
        if (dat & 1)
            P_TXD = 1;
        else
            P_TXD = 0;
        dat >>= 1;
        BitTime();
    
    P_TXD = 1;
    EA = 1;
    BitTime();
    BitTime();



  • soft_uart.h
#ifndef _H_SOFT_UART
#define _H_SOFT_UART

/**
 * 波特率:9600,位宽度:8,停止位:1
*/

#include "stdint.h"
#include<lint.h>//包含SDCC_mcs51定义
//#include <8052.h>//51头文件
#include <stc15.h>//51头文件
#define P_TXD P2_0 //定义模拟串口发送端,可以是任意IO

#define CLOCK		16	//定义主时钟16MHz 
#define MAIN_Fosc 16000000uL //晶振频率
void TxSend(uint8_t dat);

#endif


位时间处理函数

(16 * 80) / 13 - 2这个参数真的是费了我好多时间。在官方的库函数中也没找到关于此函数的相关计算方法,这个软串口的波特率是9600,目前没有有掌握其具体的计算方法,只能固定此波特率作为软串口输出。在Keil环境下,相同的9600波特率,参数有不一样,这一点特别注意。

void	BitTime(void)

	 uint16_t  i;
     i = (16 * 80) / 13 - 2; //根据主时钟来计算位时间16
	// i = ((MAIN_Fosc / 100) * 80) / 130000 - 2;  //根据主时钟来计算位时间
	while(--i);

Keil环境下,相同的9600波特率,位时间处理函数如下:

//========================================================================
// 函数: void	BitTime(void)
// 描述: 位时间函数。
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注: 
//========================================================================
void	BitTime(void)

	u16 i;
	i = ((MAIN_Fosc / 100) * 104) / 130000L - 1;		//根据主时钟来计算位时间	104
	while(--i);

  • platformio.ini配置文件
[env:stc15w408as]
platform = intel_mcs51
board = stc15w408as
upload_port = COM6					;下载程序端口号
upload_speed = 115200				;下载波特率
monitor_port = COM10				;串口控制台端口号
monitor_speed = 9600				;串口控制台波特率

以上是关于STC单片机基于VSCode PlatformIO开发STC15W408AS软串口示例程序的主要内容,如果未能解决你的问题,请参考以下文章

STC单片机VSCode PlatformIO开发环境详细配置过程

STC单片机VSCode PlatformIO Led呼吸灯示例程序

VSCode PlatformIO开发STC单片机注意事项

VSCode PlatformIO开发STC单片机头文件和常用外设驱动头文件获取方式

vscode+platformIO开发STM32

vscode+platformIO开发STM32