我用的是普中51双核A6开发板,STC89C52芯片,我首先编写了LCD1602显示AD模块中的电位器电压,我用电位器调节,一切正常,显示范围是0-5V。
但是我现在想用单片机产生的PWM波来控制这个电压,我设定PWM波为10KHZ,占空比为10%,可是LCD1602显示却变为了3.125V,我调整电位器,这个数字一直不变,10%的占空比,不应该最大显示为0.5V吗?
下面是我的主程序:
#include"reg52.h"
#include"lcd.h"
sbit PWM=P2^1;
sbit add=P3^1;
sbit dec=P3^0;
typedef unsigned int u8;
typedef unsigned char u16;
typedef unsigned long u32;
void delay(u16 i)
{
while(i--);
}
#define SYSCLOCK 12000000
u8 disp[4]={0,0,0,0};
u16 Volt;
u8 highHR0,highLR0;
u8 lowHR0,lowLR0;
u8 cont=10;
void datapros()
{ u16 temp;
static u8 i;
if(i==50)
{i=0;
temp=Read_AD_Data(0x94);
}
i++;
Volt=temp*5/4096.0*1000+0.5;
disp[0]=Volt/1000;
disp[1]=Volt%1000/100;
disp[2]=Volt%1000%100/10;
disp[3]=Volt%1000%100%10;
}
void PWM_Config(u16 fr,u8 dc)
{ u32 tmp;
u16 high,low;
tmp=SYSCLOCK/12;
tmp=tmp/fr;
high=(tmp*dc)/100;
low=tmp-high;
high=65536-high;
low=65536-low;
highHR0=(u8)(high>>8);
highLR0=(u8)high;
lowHR0=(u8)(low>>8);
lowLR0=(u8)low;
TMOD&=0XF0;
TMOD|=0X10;
TH1=highHR0;
TL1=highLR0;
PWM=1;
TR1=1;
ET1=1;
EA=1;
}
void PWM_Close()
{ TR1=0;
ET1=0;
PWM=1;
}
void ADD()
{ if(add==0)
{delay(1000);
if(add==0)
{cont+=10;
if(cont>=50)
{cont=50;}
}
while(!add);
}
}
void DEC()
{ if(dec==0)
{delay(1000);
if(dec==0)
{cont-=10;
if(cont<=10)
{cont=10;}
}
while(!dec);
}
}
void main()
{u16 i;
LcdInit();
while(1)
{datapros();
LcdWriteCom(0x80);
LcdWriteData("V");
LcdWriteCom(0x81);
LcdWriteData("o");
LcdWriteCom(0x82);
LcdWriteData("u");
LcdWriteCom(0x83);
LcdWriteData("t");
LcdWriteCom(0x84);
LcdWriteData("=");
LcdWriteCom(0x85);
for(i=0;i<4;i++)
{LcdWriteData("0"+disp[i]);
if(i==0)
LcdWriteData(".");
}
LcdWriteCom(0x8a);
LcdWriteData("V");
PWM_Config(13158,cont);
for(i=0;i<30000;i++);
ADD();
DEC();
PWM_Close();
}
}
void Timer0_ISP() interrupt 3
{ if(PWM)
{TH1=lowHR0;
TL1=lowLR0;
PWM=0;
}
else
{TH1=highHR0;
TL1=highLR0;
PWM=1;
}
}
就是感觉XPT2046芯片转换的AD模块电位器的电压和单片机芯片产生的PWM波无法关联,怎么用PWM波占空比控制(AD模块转换出的)电位器电压从而显示在液晶屏上?求指导!!!!