一、实验名称
ADC实验 模拟转数字实验
二、设计思路
电路设计
1.选用AT89C51单片机作为电路核心单元,外接8位单通道AD转换器ADC0804芯片和LM016L显示器以及滑动变阻器等其它常用元器件构成电路。
2.将ADC0804芯片的控制引脚RD,WR,INTR接到AT89C51芯片对应引脚,再将ADC0804电压输入引脚接到滑动变阻器上,作为输入的模拟电压,最后将其数据引脚DB0至DB7接到AT89C51芯片上。
3.将AT89C51单片机所选引脚与LCD控制引脚相连,再将数据通过引脚与LCD接收引脚相连。
代码设计:
1.对AT89C51单片机所用引脚进行定义。
2.构造基本的功能函数:延时函数delay()、LCD控制函lcd_cmd()、LCD数据函lcd_data()、显示函数display()。
3.主函数:先将芯片的引脚和LCD屏幕进行初始化,然后循环执行以下操作:首先进行模拟信号到数字信号的转换,待转换完成之后打开读信号引脚,将转换完成的数据读入芯片,然后将数据的每一位分割出来,关闭读引脚打开写引脚,将每一位依次显示在LCD显示器上,关闭写引脚。
三、原理图
1.将芯片代码进行编译测试,结果正确无任何错误。
- 将编译之后的HEX文件加载到芯片内,进行软件仿真。当电阻取值为100%、50%、0% 三个节点时所对应的数值应为255、128、0,由仿真结果显示正确无误。
- 电阻为100%:
- 电阻为50%:
- 电阻为0%:
-
六、软件源码
C语言代码:
#include<reg51.h>
sfr mydata = 0x90;
sbit rd= P2^5;
sbit wr= P2^6;
sbit intr= P2^7;
sbit RS = P2^0;
sbit EN = P2^1;
//(0x90)is address of port1, else you can write as : #defintre mydata P1
void delay(int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<255;j++);
}
void lcd_cmd(char a)
{
P3 = a;
RS = 0;
EN = 1;
delay(10);
EN = 0;
}
void lcd_data(char a)
{
P3 = a;
RS = 1;
EN = 1;
delay(10);
EN = 0;
}
void display(char *ptr)
{
while(*ptr != '\0')
{
lcd_data(*ptr);
ptr++;
}
}
void main()
{
unsigned char value;
char temp[4];
int i=0;
lcd_cmd(0x01); //clear screen
lcd_cmd(0x0E); //Display On, Cursor Blinking
lcd_cmd(0x38); //2 lines and 5*7 matrix
lcd_cmd(0x80); //Force Cursor to beginning of first line
display("ADC Value=");
lcd_cmd(0xC0); //Force Cursor to beginning of second line
//mydata = 0xEF;
intr = 1;
rd = 1;
wr = 1;
while(1)
{
i = 0;
while(intr == 1);
rd = 0;
value = mydata;
while(i < 3)
{
temp[i] = (value % 10) + '0';
value = value / 10;
i++;
}
for (i = 2; i >= 0; i--)
{
lcd_cmd(0x06); //Force Cursor to the next address(move to right)
lcd_data(temp[i]);
}
rd = 1;
delay(100);
lcd_cmd(0xC0); //Force Cursor to beginning of second line
wr = 0;
wr = 1;
}
}
六、软件源码 C语言代码: #include<reg51.h> sfr mydata = 0x90; sbit rd= P2^5; sbit wr= P2^6; sbit intr= P2^7; sbit RS = P2^0; sbit EN = P2^1;//(0x90)is address of port1, else you can write as : #defintre mydata P1void delay(int n) {int i,j;for(i=0;i<n;i++)for(j=0;j<255;j++); } void lcd_cmd(char a) {P3 = a;RS = 0;EN = 1;delay(10);EN = 0; }void lcd_data(char a) {P3 = a;RS = 1;EN = 1;delay(10);EN = 0; }void display(char *ptr) {while(*ptr != '\0'){lcd_data(*ptr);ptr++;} } void main() {unsigned char value;char temp[4];int i=0;lcd_cmd(0x01); //clear screenlcd_cmd(0x0E); //Display On, Cursor Blinkinglcd_cmd(0x38); //2 lines and 5*7 matrixlcd_cmd(0x80); //Force Cursor to beginning of first linedisplay("ADC Value=");lcd_cmd(0xC0); //Force Cursor to beginning of second line//mydata = 0xEF;intr = 1;rd = 1;wr = 1;while(1){i = 0;while(intr == 1);rd = 0;value = mydata;while(i < 3){temp[i] = (value % 10) + '0';value = value / 10;i++;}for (i = 2; i >= 0; i--){lcd_cmd(0x06); //Force Cursor to the next address(move to right)lcd_data(temp[i]);}rd = 1;delay(100);lcd_cmd(0xC0); //Force Cursor to beginning of second linewr = 0;wr = 1;} }