display()采集的数据都能在lcd上显示了,可是display2(是按键调节数值(电压))却不能显示在lcd上.显示程序我感觉没有出错啊,为何display可以显示,但display2就显示不了
#include<reg52.h>
#include"lcd.h"
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
sbit P1_6 = P1^6;
sbit P1_7 = P1^7;
sbit ADC_CS=P1^0;
sbit ADC_CLK=P1^1;
sbit ADC_DIN=P1^2;
sbit ADC_DOUT=P1^3;
sbit ADC_DRDY=P1^4;
uchar t = 0,c = 0,k,v =53;//k为脉冲周期
sbit S1 = P3^5;
sbit S2 = P3^4;
uchar date[3]={0,0,0};
uint result0[8]={0,0,0,0,0,0,0,0};
uint result1[8]={0,0,0,0,0,0,0,0};
void Display(uint AD_data);
void Display1(uint AD_data);
void Display2();
void PWM_Control1( uchar Width, uchar DutyCycle,uchar count,uchar partern);
void PWM_Control2( uchar Width, uchar DutyCycle,uchar count,uchar partern);
void Delay_1ms(uint c)
{
uchar a,b;
for(;c>0;c--)
{
for(b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void WriteToReg_ADC(uint byteword ) //发送控制字
{
uint AD16bit,Token;
uchar i;
ADC_CLK=1;
ADC_CS=0;
Token=0x80;
AD16bit=byteword;
for(i=0;i<8;i++)
{
if(AD16bit&Token) // 逐一取出各位上的数据
ADC_DIN=1;
else
ADC_DIN=0;
ADC_CLK=1;
ADC_CLK=0; //为发送数据做准备
ADC_CLK=1;
Token=Token>>1;
}
ADC_CLK=1;
ADC_DIN=1;
}
uint ReadADC_16BitValue(uchar channel) //发送控制字准备读取的通道,接着读取数据
{
uint AD16bit;
uchar i;
WriteToReg_ADC(0x38|channel);
while(ADC_DRDY==1); // 等待DRDY下降沿读数据
ADC_CLK=1;
ADC_CS=0;
AD16bit=0x00;
for(i=0;i<16;i++) //巡历
{
AD16bit=AD16bit<<1;
ADC_CLK=0;
if(ADC_DOUT==1) //等待DRDY低电平时间
AD16bit=AD16bit|0x01; //最低位开始
ADC_CLK=1;
}
ADC_CLK=1;
ADC_DIN=1;
return(AD16bit); //返回数据
}
void InitADC()
{
uchar i;
ADC_CLK=1;
ADC_DIN=1;
ADC_CS=0;
for(i=0;i<40;i++) //自校验
{
ADC_CLK=0;
ADC_CLK=1;
}
WriteToReg_ADC(0x20); //先写通信寄存器,下一步时钟寄存器
WriteToReg_ADC(0x02); //100HZ CLKIDS=0 CLKDIV=0(器件内部频率)
WriteToReg_ADC(0x10); //设置寄存器
WriteToReg_ADC(0x44); //自校准
WriteToReg_ADC(0x21); //通道2
WriteToReg_ADC(0x02);
WriteToReg_ADC(0x11);
WriteToReg_ADC(0x44);
Delay_1ms(50);
}
void PWM_Control1( uchar Width, uchar DutyCycle,uchar count,uchar partern) //1ms为最小单位
{ uchar i,j;
//脉宽设置 Wdith
//占空比 X0% DutyCycle
//脉冲个数 count
// pin = ;
k = (Width*10)/DutyCycle; //PWM整个周期
if(partern == 6)
{
for(i=0;i<k*count;i++)
{
P1_6= 0;
delay(Width);
P1_6= 1;
delay(k-Width);
}
}
if(partern == 7)
{
for(j=0;j<k*count;j++)
{
P1_7 = 0;
delay(Width);
P1_7 = 1;
delay(k-Width);
}
}
}
void PWM_Control2( uchar Width, uchar DutyCycle,uchar count,uchar partern) //1ms为最小单位
{ uchar i,j;
//脉宽设置 Wdith
//占空比 X0% DutyCycle
//脉冲个数 count
// pin = ;
k = (Width*10)/DutyCycle; //PWM整个周期
if(partern == 6)
{
for(i=0;i<k*count;i++)
{
P1_6= 0;
delay(Width);
P1_6= 1;
delay(k-Width);
}
}
if(partern == 7)
{
for(j=0;j<k*count;j++)
{
P1_7= 0;
delay(Width);
P1_7= 1;
delay(k-Width);
}
}
}
/***************按键S1,S2调节电压************/
unsigned int Set()
{
if( S1 == 0)
{
delay(5);
if(S1 == 0)
{
v++;
}
}
if(S2 == 0)
{
delay(5);
if(S2 == 0)
{
v--;
}
}
return v;
}
/*******************设定值与检测值比较****************/
void compare()
{
if(v<50)PWM_Control1(7,7,1,7);else PWM_Control2(7,7,1,6);
}
void main()
{
uchar i;
ulong sum=0;
uint result;
float h;
lcd1602init();
InitADC();
// v = Set();
lcd_pos(0x00);
lcd1602writedata('V');
lcd1602writedata('=');
lcd1602writedata(5+0x30);
lcd1602writedata(0x2e);
lcd1602writedata(0x30);
lcd1602writedata(0x30);
lcd1602writedata('V');
lcd_pos(0x08);
lcd1602writedata('S');
lcd1602writedata('e');
lcd1602writedata('t');
lcd1602writedata('=');
/* lcd1602writedata((v/10)+0x30);
lcd1602writedata(0x2e);
lcd1602writedata((v%10)+0x30); */
lcd_pos(0x40);
lcd1602writedata('T');
lcd1602writedata('e');
lcd1602writedata('s');
lcd1602writedata('t');
lcd1602writedata('=');
while(1)
{
WriteToReg_ADC(0x20); //先写通信寄存器,下一步时钟寄存器
WriteToReg_ADC(0x02); //100HZ CLKIDS=0 CLKDIV=0(器件内部频率)
WriteToReg_ADC(0x10); //设置寄存器
WriteToReg_ADC(0x44); //自校准后DRDY返回低电平
Delay_1ms(60);
for(i=0;i<8;i++) //读取数据并转换成电压
{
result0[i]=ReadADC_16BitValue(0);
sum=sum+result0[i];
}
sum=sum/8;
result=(uint)sum;
sum=0;
h=result;
result=h*5000/65536;
Display(result);
result=0;
Delay_1ms(50);
/**********************就是这个函数********************/
/*******************同样在主循环中,display正常,但这个display2不显示***********/
v = Set();
Display2(); //Display2函数写在主循环中但LCD却不显示
Delay_1ms(5);
compare();// 判定设定值与检测值大小,从而输出PWM1或PWM2 (P1^6或P1^7)
Delay_1ms(5);
// Delay_1ms(500);
/* WriteToReg_ADC(0x21);
WriteToReg_ADC(0x02);
WriteToReg_ADC(0x11);
WriteToReg_ADC(0x44);
Delay_1ms(60);
for(i=0;i<8;i++)
{
result1[i]=ReadADC_16BitValue(1);
sum=sum+result1[i];
}
sum=sum/8;
result=(uint)sum;
sum=0;
h=result;
result=h*5000/65536;
Display1(result);
result=0;
// Delay_1ms(500); */
}
}
void Display(uint AD_data)
{
date[2]= (AD_data)/1000;
date[1]= (AD_data)/100%10;
date[0]= (AD_data)/10%10;
lcd_pos(0x45);
lcd1602writedata(0x30+date[2]);
lcd1602writedata(0x2e);
lcd1602writedata(0x30+date[1]);
lcd1602writedata(0x30+date[0]);
lcd1602writedata('V');
}
/*void Display1(uint AD_data)
{
date[2]= (AD_data)/1000;
date[1]= (AD_data)/100%10;
date[0]= (AD_data)/10%10;
lcd_pos(0x49);
lcd1602writedata(0x30+date[2]);
lcd1602writedata(0x2e);
lcd1602writedata(0x30+date[1]);
lcd1602writedata(0x30+date[0]);
} */
/****************就是这个Display2函数*********************/
void Display2()
{ v = Set();
lcd_pos(0x13);
lcd1602writedata((v/10)+0x30);
lcd1602writedata(0x2e);
lcd1602writedata((v%10)+0x30);
} |