一款简单编写的数字电压表,可供参考
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <stc12c5a60s2.h>
- #include <intrins.h>
- #include "lcd1602.h"
- #include "AD_2543.h"
- #define uchar unsigned char
- #define uint unsigned int
- /*---------------------------------------
- AD芯片采集定义I/O口
- ----------------------------------------- */
- sbit dout=P1^0;
- sbit din=P1^1;
- sbit cs=P1^2;
- sbit clk=P1^3;
- uchar dat2[]={"TL2543 ADC"};
- uchar dat3[]={"Voltage="};
- uchar tab[] = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9'};
- uint ad;
- uint Ad_value;
- /*--------------------------------------------------
- 函数名:read
- 输入参数:void
- 功能: 进行AD采集并把采集的数据传输给单片机
- ---------------------------------------------------*/
- void delay(uchar N)
- {
- while(N--);
- }
- uint AD_2543(uchar CON_WORD)
- {
- uchar i;
- clk=0;
- cs=0;
- CON_WORD<<=4;
- for(i=0;i<12;i++)
- {
- if(dout==1)
- Ad_value|=0x01;
- if(CON_WORD&0x80)
- din=1;
- else
- din=0;
- clk=1;
- delay(10);
- clk=0;
- delay(10);
- CON_WORD<<=1;
- Ad_value<<=1;
- }
- cs=1;
- Ad_value>>=1;
- return Ad_value;
- }
- ///**********************************************
- //函数名:display()
- //参数: 无
- //功能: 时钟显示函数
- //**********************************************/
- void display1()
- {
- uchar m0=0,m1=0,m2=0,m3=0;
- ad=(Ad_value/4.095)*5;
- m0=ad/1000;
- m1=ad%1000/100;
- m2=ad%1000%100/10;
- m3=ad%10;
- lcd_write_string(0,0,dat2); //整数显示部分
- lcd_write_string(1,0,dat3); //整数显示部分
- lcd_write_char(1,8, tab[m0]);
- lcd_write_char(1,10, tab[m1]);
- lcd_write_char(1,9, '.' );
- lcd_write_char(1,11, tab[m2]); //小数显示部分
- lcd_write_char(1,12, tab[m3]);
- lcd_write_char(1,13, 'V');
- }
复制代码
所有资料51hei提供下载:
电压表.rar
(135.49 KB, 下载次数: 43)
|