博客
关于我
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 命令和内置函数
查看>>
mysql 四种存储引擎
查看>>
MySQL 在并发场景下的问题及解决思路
查看>>
MySQL 基础架构
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 备份 Xtrabackup
查看>>
mYSQL 外键约束
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>