当前位置:网站首页>STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
2022-07-02 05:52:00 【不知何人】
串口发送菜单界面选择不同功能
一、题目
使用串口1、定时器T1的模式0作为波特率发生器实现STC实验箱和主机电脑的串口通信,STC通过串口1向主机发送菜单界面,主机空STC实验箱的LED灯。
1)在主机上,按1键,用于控制STC实验箱的标记为LED17(接在P6.7上)的LED灯;
2) 按2键,用于控制STC实验箱上标记为LED16(接在P6.6上)的LED灯;
3) 按其他键显示退出程序的信息。
二、代码
#include "STC8h.h"
#include <intrins.H>
typedef unsigned char uchar;
typedef unsigned int uint;
bit busy;
u8 Flag;
uchar GetC;
sbit control=P4^0;
void InitUART(void);
void SendData(uchar dat);
void SendString(uchar *s);
void gpio() //gpio初始化为准双向口,刚开始除了P30,P31其他均为高阻态
{
P0M1 = 0x00; P0M0 = 0x00; //设置为准双向口
P1M1 = 0x00; P1M0 = 0x00; //设置为准双向口
P2M1 = 0x00; P2M0 = 0x00; //设置为准双向口
P3M1 = 0x00; P3M0 = 0x00; //设置为准双向口
P4M1 = 0x00; P4M0 = 0x00; //设置为准双向口
P5M1 = 0x00; P5M0 = 0x00; //设置为准双向口
P6M1 = 0x00; P6M0 = 0x00; //设置为准双向口
P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
}
void main()
{
control=0;
gpio();
P0=0xff;
InitUART();
SendString("---------main menu----------\r\n input 1: Control LED7\r\n input 2: Control LED6\r\n other : exit program\r\n---------end menu----------\r\n");
while(1)
{
if(Flag==1)
{
SendData(GetC);
Flag=0;
}else if(Flag==2)
{
SendString("Exited program\r\n");
Flag=0;
}
}
}
/*---------------------------- UART1初始化 -----------------------------*/
void InitUART(void)
{
SCON = 0x50; //8位数据
P_SW1= P_SW1 & 0x3F;
AUXR |= 0x40; //定时器1T模式
AUXR &= 0xFE;
TMOD &= 0x0F;
TMOD |= 0x20; //8位自动重装载模式
TL1 = 0xDC; //
TH1 = 0xDC;
TR1 = 1; //开启定时器1
ES = 1; //开启串口中断
EA = 1;
}
/*---------------------------- UART 中断 -----------------------------*/
void Uart() interrupt 4 using 1
{
if (RI)
{
RI = 0; //接收到字符后,RI清0
GetC = SBUF;
if (GetC=='1') {
P6=0x7f;P0=0X7F;Flag=1;}
if (GetC=='2') {
P6=0xbf;P0=0XBF;Flag=1;}
if(GetC!='1'&&GetC!='2') {
Flag=2;}
}
if (TI)
{
TI = 0; //发送完字符后TI清0
busy = 0; //发送完一个字符后busy清0
}
}
/*---------------------------- 发送字节 ----------------------------*/
void SendData(uchar dat)
{
while (busy);
busy = 1;
SBUF = dat; //要发送的数据存入SBUF
}
/*---------------------------- 发送字符串函数 ----------------------------*/
void SendString(uchar *s)
{
while (*s !='\0') //字符串被读取完才停止
{
SendData(*s++); //每发送完一个字节s++
}
}
总结
本人目前更新的单片机系列文章较多,欢迎各位前往首页浏览。
非常感谢各位观看!!!
系列文章——STC8H8K系列汇编51实战
基于ESP8266与STC单片机的天气时钟(包括DS18B20、TFT、串口、外部中断、ESP8266、STC、API等)
STC8H8K系列汇编和C51实战——实现键控不同方式数码管动态显示(C51版与汇编版)
STC8H8K系列汇编和C51实战——开关控制定时器秒表(C51版)
STC8H8K系列汇编和C51实战——开关控制定时器秒表(汇编版)
STC8H8K系列汇编和C51实战——双中断控制定时器流水灯
STC8H8K系列汇编和C51实战——秒倒计时器(可自行设定初值)(51版)
STC8H8K系列汇编和C51实战——按键允许按键计数(51版)
STC8H8K系列汇编和C51实战——按键允许按键计数(汇编版)
STC8H8K系列汇编和C51实战——按键允许按键计数(定时器去抖动51版)
STC8H8K系列汇编和C51实战——按键允许按键计数(利用下降沿中断控制)
STC8H8K系列汇编和C51实战——计算机串口控制单片机LED
边栏推荐
猜你喜欢
brew install * 失败,解决方法
Cube magique infini "simple"
[golang syntax] be careful with the copy of slices
Vscode paste image plugin saves image path settings
RGB 无限立方体(高级版)
Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
Lantern Festival gift - plant vs zombie game (realized by Matlab)
Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
idea開發工具常用的插件合集匯總
随机推荐
1036 Boys vs Girls
mysql的约束总结
Cube magique infini "simple"
Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
Stick to the big screen UI, finereport development diary
492.构造矩形
How to change the IP address of computer mobile phone simulator
Pytorch Chinese document
Zzuli:1064 encrypted characters
2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
brew install * 失败,解决方法
TypeScript的泛型和泛型约束
[Chongqing Guangdong education] selected reading reference materials of British and American literature of Nanyang Normal University
深度学习分类网络--VGGNet
servlet的web.xml配置详解(3.0)
Fundamentals of software testing
mock-用mockjs模拟后台返回数据
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond
PHP extensions