弄了好久都解决不了,AD值都正常,但是转换成电压值就不正常了,会不会是软件的原因(使用KEIL3)?
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sfr P1ASF=0X9D; //P1口模拟功能控制寄存器
sfr ADC_CONTR=0XBC; //ADC控制寄存器
sfr ADC_RES=0XBD; //AD转换结果存放
sfr ADC_RESL=0XBE; //AD转换结果存放
sfr AUXR1=0XA2;
sbit SCL=P0^0;
sbit SDA=P0^1;
uchar a,b,c,d;
uint j,k,l;
unsigned long g;
unsigned char code Hzk[][32]={
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},/*"0",0*/
{0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00},/*"1",1*/
{0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},/*"2",2*/
{0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00},/*"3",3*/
{0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24},/*"4",4*/
{0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00},/*"5",5*/
{0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00},/*"6",6*/
{0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00},/*"7",7*/
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},/*"8",8*/
{0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00},/*"9",9*/
{0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20},/*"A",10*/
{0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00},/*"B",11*/
{0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00},/*"C",12*/
{0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00},/*"D",13*/
{0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00},/*"E",14*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"空白",15*/
{0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00},/*">",16*/
{0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00},/*"<",17*/
{0x00,0x40,0xC0,0x78,0x40,0xC0,0x78,0x00,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x00},/*"#",18*/
{0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00},/*"*",19*/
{0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00},/*"%",20*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00},/*".",21*/
};
/**********************************************
//IIC Start
**********************************************/
void IIC_Start() //开始信号
{
SCL = 1;
SDA = 1;
SDA = 0;
SCL = 0;
}
/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop() //停止信号
{
SCL = 0;
SDA = 0;
SCL = 1;
//Delay_us(1);
SDA = 1;
//Delay_us(1);
}
/**********************************************
// IIC Write byte
**********************************************/
void Write_IIC_Byte(unsigned char IIC_Byte) //写1个字节
{
unsigned char i;
for(i=0;i<8;i++)
{
if(IIC_Byte & 0x80) //1?0?
SDA=1;
else
SDA=0;
//Delay_us(1);
SCL=1;
//Delay_us(1);
SCL=0;
//Delay_us(1);
IIC_Byte<<=1; //loop
}
SDA=1;
//Delay_us(1);
SCL=1;
//Delay_us(1);
SCL=0;
//Delay_us(1);
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78); //Slave address,SA0=0
Write_IIC_Byte(0x00); //write command
Write_IIC_Byte(IIC_Command);
IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78);
Write_IIC_Byte(0x40); //write data
Write_IIC_Byte(IIC_Data);
IIC_Stop();
}
void picture_1(uint y)
{
unsigned char x;
Write_IIC_Command(0xb4);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
for(x=0;x<8;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
Write_IIC_Command(0xb5);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
for(x=8;x<16;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
}
void picture_2(uint y)
{
unsigned char x;
Write_IIC_Command(0xb4);
Write_IIC_Command(0x08);
Write_IIC_Command(0x10);
for(x=0;x<8;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
Write_IIC_Command(0xb5);
Write_IIC_Command(0x08);
Write_IIC_Command(0x10);
for(x=8;x<16;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
}
void picture_3(uint y)
{
unsigned char x;
Write_IIC_Command(0xb4);
Write_IIC_Command(0x00);
Write_IIC_Command(0x11);
for(x=0;x<8;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
Write_IIC_Command(0xb5);
Write_IIC_Command(0x00);
Write_IIC_Command(0x11);
for(x=8;x<16;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
}
void picture_4(uint y)
{
unsigned char x;
Write_IIC_Command(0xb4);
Write_IIC_Command(0x08);
Write_IIC_Command(0x11);
for(x=0;x<8;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
Write_IIC_Command(0xb5);
Write_IIC_Command(0x08);
Write_IIC_Command(0x11);
for(x=8;x<16;x++)
{
Write_IIC_Data(Hzk[y][x]);
}
}
void fill_picture(unsigned char fill_Data) //清屏
{
unsigned char m,n;
for(m=0;m<8;m++)
{
Write_IIC_Command(0xb0+m);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
for(n=0;n<128;n++)
{
Write_IIC_Data(fill_Data);
}
}
}
void Initial_LY096BG30()
{
Write_IIC_Command(0xAE); //display off
Write_IIC_Command(0x20); //Set Memory Addressing Mode
Write_IIC_Command(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
Write_IIC_Command(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
Write_IIC_Command(0xc8); //Set COM Output Scan Direction
Write_IIC_Command(0x00);//---set low column address
Write_IIC_Command(0x10);//---set high column address
Write_IIC_Command(0x40);//--set start line address
Write_IIC_Command(0x81);//--set contrast control register
Write_IIC_Command(0x7f);
Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
Write_IIC_Command(0xa6);//--set normal display
Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
Write_IIC_Command(0x3F);//
Write_IIC_Command(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
Write_IIC_Command(0xd3);//-set display offset
Write_IIC_Command(0x00);//-not offset
Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
Write_IIC_Command(0xf0);//--set divide ratio
Write_IIC_Command(0xd9);//--set pre-charge period
Write_IIC_Command(0x22); //
Write_IIC_Command(0xda);//--set com pins hardware configuration
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);//--set vcomh
Write_IIC_Command(0x20);//0x20,0.77xVcc
Write_IIC_Command(0x8d);//--set DC-DC enable
Write_IIC_Command(0x14);//
Write_IIC_Command(0xaf);//--turn on oled panel
}
/***********************Delay****************************************/
void Delay_50ms(unsigned int Del_50ms) // 延时
{
unsigned int m;
for(;Del_50ms>0;Del_50ms--)
for(m=6245;m>0;m--);
}
void delay1ms(uint z) //误差 0us
{
unsigned char a,b,c;
for(c=z;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
void ADinit()
{
P1ASF=0x01; //P1^0口作为AD输入口
ADC_CONTR=0x20; //P1^0口作为AD输入口,360个时钟周期转换一次
AUXR1=0x00; //高八位存放于ADC_RES,低二位存放于ADC_RESL
IE=0XA0; //开总中断允许AD中断
}
void ADser() interrupt 5
{
_nop_();
_nop_();
_nop_();
_nop_(); //延时4个时钟周期,使其趋于稳定
ADC_CONTR=0xC0; //转换完成,将ADC_FLAG清零
j=ADC_RES; //AD高位数据
k=ADC_RESL; //AD低位数据
l=(j<<2)+k;
}
void main()
{
ADinit();
Initial_LY096BG30();
delay1ms(1);
fill_picture(0x00);
Delay_50ms(50);
while(1)
{
ADC_CONTR=0XA8;//开始AD转换
delay1ms(1);
ADC_CONTR=0X20;//关闭AD转
g=l*50/1023*100; //这里的转换值不正确,不知道是不是软件原因还是计算错误?
d=g/1%10; //个位
c=g/10%10; //十位
b=g/100%10; //百位
a=g/1000%10; //千位
picture_1(a);
picture_2(b);
picture_3(c);
picture_4(d);
}
}
|