hnkf118 发表于 2014-12-25 23:07
那就还是自己动手搞一下吧。 具体有什么问题,再请教可能有人帮你回答问题,你这太。。。 ...
下面这段程序怎么修改呀,为什么在keil中显示错误?
#include<reg51.h>
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
uchar Vo; //A/D转换后换算的电压值
/*Declare SFR associated with the ADC */
sfr ADC_CONTR = 0xBC; //ADC control register
sfr ADC_RES = 0xBD; //ADC high 8-bit result register
//sfr ADC_RESL = 0xBE;
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
sfr P1ASF = 0x9D; //P1 secondary function control register
/*Define ADC operation const for ADC_CONTR*/
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag 模数转换结束标志位
#define ADC_START 0x08 //ADC start control bit 模数转换 ///启动控制位
//转换速度控制位SPEED0和SPEED1,共四种状态,对应四种转换速度
#define ADC_SPEEDLL 0x00 //540 clocks
#define ADC_SPEEDL 0x20 //360 clocks
#define ADC_SPEEDH 0x40 //180 clocks
#define ADC_SPEEDHH 0x60 //90 clocks
uchar tCount;
sbit RS=P3^7; //寄存器选择位,将RS位定义为P3.7引脚
sbit RW=P3^6; //读写选择位,将RW位定义为P3.6引脚
sbit EN=P2^7; //使能信号位,将E位定义为P2.7引脚
void DelayMS(uint ms)
{
uint i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/*----------------------------
Software delay function
----------------------------*/
void Delay(uint n)
{
uint x;
while (n--)
{
x = 5000;
while (x--);
}
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
P1ASF = 0x01; //选择P1.0作为A/D输入通道
ADC_RES = 0; //清0
ADC_CONTR = ADC_POWER | ADC_SPEEDLL; //0x10|0x00=0x10:开电源和设置A/D //转换速度
Delay(2); //ADC power-on and delay
}
/*----------------------------
Get ADC result
----------------------------*/
uchar GetADCResult(uchar ch)
{
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;//0x00|0x00|ch|0x08:选择A/D输入通道,开始A/D转换
_nop_(); //Must wait before inquiry ,
_nop_(); //设置ADC_CONTR寄存器后需加4个CPU时钟周期的延时,才能保证值被写入ADC_CONTR寄存器
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
Vo=ADC_RES*5*10/256; //Return ADC result(为显示整数,这 //里将电压值扩大了十倍)
return Vo;
}
uchar Read_LCD_State()
{
uchar state;
RS=0;
RW=1;
EN=1;
DelayMS(1);
state=P2;
EN = 0;
DelayMS(1);
return state;
}
void LCD_Busy_Wait()
{
while((Read_LCD_State()&0x80)==0x80);
DelayMS(5);
}
void Write_LCD_Data(uchar dat) //写数据到1602
{
LCD_Busy_Wait();
RS=1;
RW=0;
EN=0;
P2=dat;
EN=1;
DelayMS(5);
EN=0;
}
void Write_LCD_Command(uchar cmd) //写命令
{
LCD_Busy_Wait();
RS=0;
RW=0;
EN=0;
P2=cmd;
EN=1;
DelayMS(5);
EN=0;
}
void Init_LCD() //1602 初始化
{
Write_LCD_Command(0x38);
DelayMS(5);
Write_LCD_Command(0x01); //清屏
DelayMS(5);
Write_LCD_Command(0x06); //
DelayMS(5);
Write_LCD_Command(0x0c);
DelayMS(5);
}
void Set_LCD_POS(uchar p)
{
Write_LCD_Command(p|0x80);
}
void Display_LCD_String(uchar p,uchar *s) //1602显示
{
uchar i;
Set_LCD_POS(p);
for(i=0;i<16;i++)
{
Write_LCD_Data(s);
DelayMS(5);
}
}
void Format_DateTime(uchar d,uchar *a)
{
a[0]=d/10+'0';
a[1]=d%10+'0';
}
//写入液晶第二行
void display(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10+'0';
ge=date%10+'0';
Write_LCD_Command(0x80+0x40+add);
Write_LCD_Data(shi);
Write_LCD_Data(ge);
}
//写入液晶第一行
void display1(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
Write_LCD_Command(0x80+add);
Write_LCD_Data(0x30+shi); //0x30即48(十进制)对应的ASCAII码为字符 //‘0’
Write_LCD_Data(0x30+ge);
}
void main()
{
Init_LCD();
InitADC();
while (1)
{
GetADCResult(0);
Display_LCD_String(0,"dianya:");
display1(7,Vo);
Display_LCD_String(9,"V ");
DelayMS(1000);
}
} |