adc0809不能连续采集1,2,3,4 ,会从19跳变到39,为什么?
时间:10-02
整理:3721RD
点击:
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include<intrins.h>
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
#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 uchar unsigned char
#define DataPort P1
/*------------------------------------------------
mS延时函数
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
判忙函数
------------------------------------------------*/
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()); //忙则等待
DelayMs(5);
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
//while(LCD_Check_Busy()); //忙则等待
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); /*显示开及光标设置*/
}
#define uint unsigned int
sbit P27=P2^7;
sbit EOC=P2^3;
sbit ST=P3^0;
sbit OE=P3^1;
uchar getdata = 0;
uchar code table[]="0123456789";//定义显示的数组
uchar q,b,s,g;
void adc0809(void)
{
uchar i;
float temp=0.0;
ST=0;//ADC0809初始化
ST=1;
_nop_();
_nop_();
ST=0;
for(i=0;i<20;i++)
while(EOC==0)//等待转换完成
{
OE=1;
getdata=P0;//p2选择为ADC的数据口
OE=0;
}
temp=(float)getdata*5.0/256;//数据处理
temp=temp/100.0;
temp=temp*100000.0;
q=(int)temp/1000;
b=(int)temp%1000/100;
s=(int)temp%1000%100/10;
g=(int)temp%10;
}
void main(void)
{
LCD_Init();
OE=0;
while (1)
{
LCD_Write_String(0,0,"current value:");
adc0809();
LCD_Write_Com(0x80+0x40);//1602显示处理好的数据
{
LCD_Write_Data(table[q]);
DelayMs(20);
LCD_Write_Data(table[b]);
DelayMs(20);
LCD_Write_Data(table[s]);
DelayMs(20);
LCD_Write_Data(table[g]);
DelayMs(20);
}
}
应该如何修改呢?
#include<intrins.h>
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
#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 uchar unsigned char
#define DataPort P1
/*------------------------------------------------
mS延时函数
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
判忙函数
------------------------------------------------*/
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()); //忙则等待
DelayMs(5);
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
//while(LCD_Check_Busy()); //忙则等待
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); /*显示开及光标设置*/
}
#define uint unsigned int
sbit P27=P2^7;
sbit EOC=P2^3;
sbit ST=P3^0;
sbit OE=P3^1;
uchar getdata = 0;
uchar code table[]="0123456789";//定义显示的数组
uchar q,b,s,g;
void adc0809(void)
{
uchar i;
float temp=0.0;
ST=0;//ADC0809初始化
ST=1;
_nop_();
_nop_();
ST=0;
for(i=0;i<20;i++)
while(EOC==0)//等待转换完成
{
OE=1;
getdata=P0;//p2选择为ADC的数据口
OE=0;
}
temp=(float)getdata*5.0/256;//数据处理
temp=temp/100.0;
temp=temp*100000.0;
q=(int)temp/1000;
b=(int)temp%1000/100;
s=(int)temp%1000%100/10;
g=(int)temp%10;
}
void main(void)
{
LCD_Init();
OE=0;
while (1)
{
LCD_Write_String(0,0,"current value:");
adc0809();
LCD_Write_Com(0x80+0x40);//1602显示处理好的数据
{
LCD_Write_Data(table[q]);
DelayMs(20);
LCD_Write_Data(table[b]);
DelayMs(20);
LCD_Write_Data(table[s]);
DelayMs(20);
LCD_Write_Data(table[g]);
DelayMs(20);
}
}
应该如何修改呢?
?