博客
关于我
51单片机串口的使用
阅读量:335 次
发布时间:2019-03-04

本文共 2261 字,大约阅读时间需要 7 分钟。

#include
/********************非精确延时*********************/void delay(long d){ while(d--) ;}/***************************************** 串口单字节发送********************************************/void uar1tByte(char byte){ SBUF = byte ; while(!TI); TI = 0 ;}/***************************************** 串口字符串发送********************************************/void uart1String(char *s){ while(*s) { uar1tByte(*s++) ; }}/***************************************** 串口数字发送********************************************/void uart1Figure(long figure){ if(figure >= 100000000) uar1tByte('0'+figure/100000000%10); if(figure >= 10000000) uar1tByte('0'+figure/10000000%10); if(figure >= 1000000) uar1tByte('0'+figure/1000000%10); if(figure >= 100000) uar1tByte('0'+figure/100000%10); if(figure >= 10000) uar1tByte('0'+figure/10000%10); if(figure >= 1000) uar1tByte('0'+figure/1000%10); if(figure >= 100) uar1tByte('0'+figure/100%10); if(figure >= 10) uar1tByte('0'+figure/10%10); if(figure >= 0) uar1tByte('0'+figure/1%10);}/***************************************** 中断初始化函数 使用定时器1初始化*******************************************/// void uart1TiINIT()// { // unsigned int INITValue = 256-(11059200/12/32/9600) ; // EA = 1 ; //打开总中断 // ES = 1 ; //打开串口中断 // SCON = 0x50; //8位数据,可变波特率 // TMOD &= 0x0F; //清除定时器1模式位 // TMOD |= 0x20; //设定定时器1为8位自动重装方式 // TH1 = TL1 = INITValue; // ET1 = 0; //禁止定时器1中断 // TR1 = 1; //启动定时器1// }/***************************************** 中断初始化函数 使用定时器2初始化*******************************************/void uart1T2INIT() //串口初始化{ unsigned int INITValue = 65536 - (11059200/32/9600) ; EA=1; ES = 1; SCON = 0x50; T2CON = 0x34; TH2 = RCAP2H = INITValue/256; TL2 = RCAP2L = INITValue%256; TR2 = 1;}int i = 0 ;/*************************************** main函数 程序入口***************************************/void main(){ // uart1TiINIT(); //中断初始化 uart1T2INIT() ; while(1) { i++; uart1String("i = ") ; uart1Figure(i); uart1String("\r\n") ; delay(10000) ; }}//^^main函数^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/*----------------------------UART interrupt service routine----------------------------*/void uart1Receive() interrupt 4{ if (RI) { RI = 0; //Clear receive interrupt flag }}

转载地址:http://fbrq.baihongyu.com/

你可能感兴趣的文章
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>