要选择其他通道按同理写按键函数即可,采用1602显示.
main.c文件#include <reg52.h> #include <stdio.h>#include "1602.h"#include "delay.h"#define uint unsigned int #define uchar unsigned char /****ADC0809引脚定义********************/sbit CLK=P2^6;sbit ST=P2^3;sbit ALE=P2^7; sbit OE=P2^5; sbit EOC=P2^4; sbit ADDA=P3^5;sbit ADDB=P3^6;sbit ADDC=P3^7;/*****按键定义*****/sbit KEY1=P3^2;sbit KEY2=P3^3;sbit KEY3=P3^4;/*****º函数声明*************/void ADC0809_init();//ADC0809初始化函数unsigned char Read_AD(); //读取AD数据函数unsigned char keyscan(); //通道选择函数void main(){ uchar num=0; unsigned char temp[7];//显示区域临时存储数组 float Voltage; //浮点变量 ADC0809_init(); LCD_Init(); //LCD初始化 DelayMs(20); LCD_Clear(); //清屏 LCD_Write_String(3,0,"PYH_DVM /V");//设置表头 ADC0809_init(); //AD0809初始化 ST=0;while(1){ST=1;//启动ADC0809ST=0; keyscan(); //0~2num=Read_AD(); //读取AD0809数据Voltage=(float)num*5/256; //数据转换,float是强制转换符号,将结果转换为浮点型 sprintf(temp,"VO:%3.2f ",Voltage); //格式输出电压值,%3.2表示浮点输出,小数点后两位 LCD_Write_String(2,1,temp); //将数据写入1602显示}}void timer0(void) interrupt 1 //中断处理函数,产生约500KHz时钟供0809工作{TH0=(65536-200)/256;TL0=(65536-200)%256;CLK=~CLK; }void ADC0809_init() //ADC0809初始化{TMOD=0x01;TH0=(65536-200)/256;TL0=(65536-200)%256;ET0=1;TR0=1;EA=1;DelayMs(2);}unsigned char Read_AD() //读ADC0809数据{uchar temp=0; //存放AD采集的数据while(EOC==0);//等待转换完成 OE=1; temp=P1; //读取数据 DelayMs(1); OE=0; return temp;}unsigned char keyscan(){ if(KEY1==0)//选择通道0{ ALE=1; //允许写入地址 ADDA=0; ADDB=0; ADDC=0; ALE=0;} else if(KEY2==0) //选择通道1 {ALE=1;ADDA=1;ADDB=0;ADDC=0;ALE=0; }else if(KEY3==0) //选择通道2 {ALE=1;ADDA=0;ADDB=1;ADDC=0;ALE=0;}/****其他通道同理写入地址即可********/}#ifndef __DELAY_H__#define __DELAY_H__/*------------------------------------------------us延时函数,含参,t值范围0~255------------------------------------------------*/void DelayUs2x(unsigned char t);/*------------------------------------------------ms延时函数,含参,t值范围0~255------------------------------------------------*/void DelayMs(unsigned char t);#endif#include "delay.h"void DelayUs2x(unsigned char t){ while(--t);}void DelayMs(unsigned char t){ while(t--) { //大致延时1ms DelayUs2x(245); DelayUs2x(245); }}#include<reg52.h> //#include<intrins.h>#ifndef __1602_H__#define __1602_H__ bit LCD_Check_Busy(void) ; void LCD_Write_Com(unsigned char com) ; void LCD_Write_Data(unsigned char Data) ; void LCD_Clear(void) ; void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) ; void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) ; void LCD_Init(void) ; #endif1602.c文件#include "1602.h"#include "delay.h"sbit RS = P2^0; //端口定义sbit RW = P2^1;sbit EN = P2^2;#define RS_CLR RS=0 #define RS_SET RS=1#define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0#define EN_SET EN=1#define DataPort P0/*------------------------------------------------ 判忙函数------------------------------------------------*/ bit LCD_Check_Busy(void) { DataPort= 0xFF; RS_CLR; RW_SET; EN_CLR; _nop_(); EN_SET; return (bit)(DataPort & 0x80); }/*------------------------------------------------ 写入命令函数------------------------------------------------*/ void LCD_Write_Com(unsigned char com) { DelayMs(5); RS_CLR; RW_CLR; EN_SET; DataPort= com; _nop_(); EN_CLR; }/*------------------------------------------------ 写入数据函数------------------------------------------------*/ void LCD_Write_Data(unsigned char Data) { DelayMs(5); RS_SET; RW_CLR; EN_SET; DataPort= Data; _nop_(); EN_CLR; }/*------------------------------------------------ 清屏函数------------------------------------------------*/ void LCD_Clear(void) { LCD_Write_Com(0x01); DelayMs(5); }/*------------------------------------------------ 写入字符串函数------------------------------------------------*/ void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { LCD_Write_Com(0x80 + x); //表示第一行 } else { LCD_Write_Com(0xC0 + x); //表示第二行 } while (*s) { LCD_Write_Data( *s); s ++; } }/*------------------------------------------------ 写入字符函数------------------------------------------------*//* void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) { if (y == 0) { LCD_Write_Com(0x80 + x); } else { LCD_Write_Com(0xC0 + x); } LCD_Write_Data( Data); }*//*------------------------------------------------ 初始化函数------------------------------------------------*/ void LCD_Init(void) { LCD_Write_Com(0x38); /*显示模式设置*/ DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); LCD_Write_Com(0x08); /*显示关闭*/ LCD_Write_Com(0x01); /*显示清屏*/ LCD_Write_Com(0x06); /*显示光标移动设置*/ DelayMs(5); LCD_Write_Com(0x0C); /*Ï显示开关及光标设置*/ }