51单片机实现AD电压转换到串口输出的主从机源代码以及原理图。
并可以通过LED数码管显示
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
主机单片机源程序如下:
- #include<reg51.h>
- #define PDATA P2
- #define aver 2
- #define uint unsigned int
- #define uchar unsigned char
- sbit Ledlow = P1^3;
- sbit Ledhig = P1^4;
- uchar table2 []={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar table1 []={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
- //短延时函数:ShortDelay()
- void nNop(uchar temp)
- {
- uint i;
- for(i=0 ;i<temp; i++) ;
- }
- //UART初始化:模式1,8位,9600Bds at 11.059MHz
- void InitSerial()
- { // uart in mode 1 (8 bit), REN=1
- TMOD = 0x20 ; // Timer 1 in mode 2
- TH1 = 0xF4; // 9600 Bds at 11.059MHz
- TL1 = 0xF4; // 9600 Bds at 11.059MHz
- TR1 = 1;
- PCON = 0x00; // Timer 1 run
- SCON = 0x50;
- }
- //主机接受信息给从机
- uchar recivefromslave()
- {
- uchar temp;
- while(!RI);
- temp=SBUF;
- RI=0;
- return(temp);
-
- }
- // 显示
- void leddisp(uint temp )
- {
- uint i;
- uint getdata1 = 0;
- uint getdata2 = 0;
- temp=temp*aver;
- getdata1=temp/100;
- getdata2=temp/10;
- getdata2=getdata2%10;
-
- for(i=0;i<30;i++)
- {
- PDATA=table1[getdata1];
- Ledlow =0;
- Ledhig = 1;
- nNop(1000);
- Ledlow =0;
- Ledhig = 0;
- PDATA=table2[getdata2];
- Ledlow =1;
- Ledhig = 0;
- nNop(1000);
- Ledlow =0;
- Ledhig = 0;
- }
-
- }
- void main()
- {
- uint tab;
- uint tempt;
- Ledlow = 0;
- Ledhig = 0;
- PDATA = 0x00;
- InitSerial();
- while(1)
- {
- tempt = (uint) recivefromslave(); //接收从机采集的信息
- leddisp(tempt); //显示信息
- if(tempt<0x60) //判断信息
- {
- tab = 0;
- }
- else if(tempt>0xc0)
- {
- tab = 2;
- }
- else
- {
- tab = 1;
- }
- SBUF = (uchar)tab; //对采集信息进行控制
- while(!TI);
- TI = 0;
- }
- }
复制代码
丛机单片机源程序如下:
所有资料51hei提供下载:
uart.zip
(108.14 KB, 下载次数: 72)
|