目前程序能够实现8路AD检测数据但是显示有问题,并且使用按键进行切换时总会不知道跳到什么地方去。
/* 功能:使用12C5A60S2内部AD读取外部8 路电压,显示在1602上 */ /* 最多可同时输入8路电压,设置P1ASF */
制作出来的实物图如下:
单片机源程序如下:
#include <STC12C5A60S2.H>
#include<intrins.h>
sbit RS = P0^7; //定义1602使用的端口
sbit RW = P0^6;
sbit EN = P0^5;
#define uchar unsigned char;
#define uint unsigned int;
#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 P2 //连接1602数据口 P2
//uchar b,i,ge,shi,bai;
uchar da1=0,da2=0,da3=0,da4=0,da5=0,da6=0,da7=0,da8=0;
double Data,c;
char a[6]="";
uchar ADC_Chanul_Turn=0;
sbit key1=P0^0;
sbit key2=P0^1;
int tongdao=0;
int sum=0;
/*------------------------------------------------
判忙函数
------------------------------------------------*/
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)
{
while(LCD_Check_Busy()); //忙则等待
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com; //
_nop_();
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //忙则等待
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
while(TH0<9);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)//y为行数,x为横坐标,最后一个是字符
{
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); /*显示模式设置*/
while(TH0<9);
LCD_Write_Com(0x38);
while(TH0<9);
LCD_Write_Com(0x38);
while(TH0<9);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
while(TH0<9);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
/*------------------------------------------------
ADC函数
------------------------------------------------*/
void InitADC()//初始AD寄存器
{
P1ASF=0Xff; //0xff设置P1口全部为ADC通道,0x07=0000,0111 即使用P10,P11,P12作为输入
ADC_RES=0X00; //清除高8位缓冲数据
if(ADC_Chanul_Turn%8==0)//外部基准电压
{
ADC_CONTR=0xF0; //AD转换控制寄存器,1111,0000 P10口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xE8; //1110,1000(清零Flag,置位Start)
}
if(ADC_Chanul_Turn%8==1)
{
ADC_CONTR=0xF1; // 1111,0001 P11口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xE9; //1110,1001
}
if(ADC_Chanul_Turn%8==2)
{
ADC_CONTR=0xF2; //1111,0010 P12口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xEA; //1110,1010
}
if(ADC_Chanul_Turn%8==3)
{
ADC_CONTR=0xF3; //1111,0011 P13口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xEB; //1110,1011
}
if(ADC_Chanul_Turn%8==4)
{
ADC_CONTR=0xF4; //1111,0100 P14口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xEC; //1110,1100
}
if(ADC_Chanul_Turn%8==5)
{
ADC_CONTR=0xF5; //1111,0101 P15口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xED; //1110,1101
}
if(ADC_Chanul_Turn%8==6)
{
ADC_CONTR=0xF6; //1111,0110 P16口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xEE; //1110,1110
}
if(ADC_Chanul_Turn%8==7)
{
ADC_CONTR=0xF7; //1111,0111 P17口
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xEF; //1110,1111
}
}
void timer0() interrupt 1 //interrupt 1: 定时器0,interrupt3:定时器3
{
TH0=-9; //高八位,(需要表示Xms的定时,计数器由65536-X数到65536,由于16位,只能分高低位)
EA = 1;
ET0 = 1;
TR0 = 1;
InitADC();
}
void adc_isr() interrupt 5 //FLAG标志位置位触发中断,没有设优先级,但是同优先级下定时器0更高
{
//V_5REF=V_1REF*256/da_ref;
if(ADC_Chanul_Turn%8==0) //外部基准电压
{
da1=ADC_RES; //获取转换结果
Data=((double)da1/256)*5; //取八位计算基准电压Data,
c =Data;
}
if(ADC_Chanul_Turn%8==1)
{
da2=ADC_RES; //获取转换结果
Data=((double)da2/256)*5; //取八位计算实际值Data,
c =Data;
}
if(ADC_Chanul_Turn%8==2)
{
da3=ADC_RES; //获取转换结果
Data=((double)da3/256)*5; //取八位计算实际值Data,
c =Data;
}
if(ADC_Chanul_Turn%8==3)
{
da4=ADC_RES; //获取转换结果
Data=((double)da4/256)*5; //取八位计算实际值Data,
c =Data;
}
if(ADC_Chanul_Turn%8==4)
{
da5=ADC_RES; //获取转换结果
Data=((double)da5/256)*5; //取八位计算实际值Data,
c =Data;
}
if(ADC_Chanul_Turn%8==5)
{
da6=ADC_RES; //获取转换结果
Data=((double)da6/256)*5; //取八位计算实际值Data,
c =Data;
}
if(ADC_Chanul_Turn%8==6)
{
da7=ADC_RES; //获取转换结果
Data=((double)da7/256)*5; //取八位计算实际值Data,
c =Data;
}
if(ADC_Chanul_Turn%8==7)
{
da8=ADC_RES; //获取转换结果
Data=((double)da8/256)*5; //取八位计算实际值Data,
c =Data;
}
a[0]=((int)c%10+0x30);//个位(电压<5,仅有个) //0x30: ASCAI码里代表“0”,必须转换成字符存在字符型数组里才可以在1602液晶屏上显示
a[1]=0x2e; //小数点
a[2]=((int)(c*10)%10+0x30); // 十分位
a[3]=((int)(c*100)%10+0x30);// 百分位
a[4]=((int)(c*1000)%10+0x30);// 千分位
// a[5]=((int)(c*10000)%10+0x30);// 万分位
// a[6]=((int)(c*100000)%10+0x30);// 十万分位
a[5]='\0'; // 加了串尾符才成了字符串哦
if(key1==0)
{
while(TH0<9);
if(key1==0)
{
tongdao++;
}
if(tongdao==8)
{
tongdao=0;
}
}
if(key2==0)
{
while(TH0<9);
if(key2==0)
{
tongdao--;
}
if(tongdao==-1)
{
tongdao=7;
}
}
switch(tongdao)
{
case 0: if(ADC_Chanul_Turn%8==0)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x30);
}
break;
case 1: if(ADC_Chanul_Turn%8==1)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x31);
}
break;
case 2: if(ADC_Chanul_Turn%8==2)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x32);
}
break;
case 3: if(ADC_Chanul_Turn%8==3)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x33);
}
break;
case 4: if(ADC_Chanul_Turn%8==4)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x34);
}
break;
case 5: if(ADC_Chanul_Turn%8==5)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x35);
}
break;
case 6: if(ADC_Chanul_Turn%8==6)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x36);
}
break;
case 7: if(ADC_Chanul_Turn%8==7)
{
LCD_Write_String(0,0,a);
LCD_Write_String(5,1,0x37);
}
break;
}
ADC_CONTR&=0xEF; //标志位清零
ADC_Chanul_Turn++;
if(ADC_Chanul_Turn==252)
ADC_Chanul_Turn=0;
}
void main()
{
LCD_Init();
LCD_Clear(); //清屏
LCD_Write_String(0,1,"P");
LCD_Write_String(1,1,"A");
LCD_Write_String(2,1,"S");
LCD_Write_String(3,1,"S");
LCD_Write_String(4,1,":");
TMOD=0x11;
TH0=0;
TL0=; //开定时器0 TR0=1;
EA=1; //开全局中断
ET0=1;
EX0 = 1; //允许定时器零中断
EADC=1; //允许ADC中断
while(1);
}
全部资料51hei下载地址:
AD003.rar
(35.58 KB, 下载次数: 13)
|