欢迎来到天天文库
浏览记录
ID:41158291
大小:22.01 KB
页数:4页
时间:2019-08-17
《单片机测量电压的程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、主要程序如下:#include"stc15.h"#defineFOSC11059200L//系统频率#defineBAUD115200//串口波特率sbitP23=P2^3;sbitP22=P2^2;sbitP21=P2^1;unsignedchardataH,dataL;unsignedintVolut,Shangxian,Xiaxian;unsignedcharRecieve[4];unsignedchari=0,m=0;unsignedchartime0count;unsignedcharqian,bai,shi,ge;bitthflag=0
2、;voidSendData(unsignedchardat){SBUF=dat;//写数据到UART数据寄存器while(TI==0);TI=0;}voidSendString(char*s){while(*s)//检测字符串结束标志{SendData(*s++);//发送当前字符}}voidClearAll(unsignedchar*p){charn;for(n=0;n<5;n++){*p++='';}}voiddelay(intz){intx,y;for(x=z;x>0;x--)for(y=10000;y>0;y--);}/*---------
3、------------------------------------------------------------*/voidmain(void){P21=1;//初始化LED21灭P22=1;//初始化LED22灭Shangxian=500;Xiaxian=100;//////////////////////////////////////////////////////////SCON=0x50;//8位可变波特率AUXR=0x40;//定时器1为1T模式TMOD=0x00;//定时器1为模式0(16位自动重载)TL1=(65536-(F
4、OSC/4/BAUD));//设置波特率重装值TH1=(65536-(FOSC/4/BAUD))>>8;TR1=1;//定时器1开始启动ES=1;//使能串口中断//////////////////////////////////////////////////////////ADC_CONTR
5、=0X80;delay(1);P1ASF=0x04;//P12为模拟量输入口ADC_CONTR=0x82;CLK_DIV
6、=0x20;delay(1);////////////////////////////////////////////////////
7、//////TH0=(65536-50000)/256;//定时50msTL0=(65536-50000)%256;//定时50msTR0=1;//使能定时器0ET0=1;//使能定时器0中断time0count=0;EA=1;//开总中断//////////////////////////////////////////////////////////while(1){if(thflag){thflag=0;ADC_CONTR
8、=0X08;//启动转换while(!(ADC_CONTR&0x10));ADC_CONTR&=0xE7;//转换结束标
9、志位清零dataL=ADC_RESL;dataH=ADC_RES&0x03;Volut=(dataH*256+dataL);qian=Volut/1000+48;bai=Volut/100%10+48;shi=Volut/10%10+48;ge=Volut%10+48;SendData(qian);SendData(bai);SendData(shi);SendData(ge);SendData('');}}}/*----------------------------定时器0中断服务程序-----------------------------*
10、/voidT0_ISR(void)interrupt1{i=0;thflag=1;time0count++;if(time0count==10){P23=~P23;time0count=0;if(Volut>Shangxian)P21=~P21;elseif(Volut11、Recieve[m]=SBUF;m++;if(m==4){unsignedinttempp=(Recieve[1]-48)
11、Recieve[m]=SBUF;m++;if(m==4){unsignedinttempp=(Recieve[1]-48)
此文档下载收益归作者所有