设计图如下,
设计图
由于硬件已经实现,无法更改,还望大神多多帮忙。
源代码如下
//---------------------------------------
// AD0809 by maomaochong
// Date: 2011/8/31
// Time: 15:54
// Function: ADDA-ADDC connect to P2.0-P2.2 to choose AD channel
//---------------------------------------
#include "reg51.h"
sbit ST_ALE=P3^0; //START and ALE both connect to P3.0
sbit EOC=P3^1; //EOC connect to P3.1
sbit OE=P3^2; //OE conect to P3.2
sbit CLK=P3^3; //CLK connect to P3.3
unsigned char CHANNEL; //AD channel variable
unsigned char LED[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//common cathode LED code
unsigned char CHECK[4]={0xFE,0xFD,0xFB,0xF7}; //choose LED channel
void delay(unsigned char time)
{
unsigned char i,j;
for(i=time;i>0;i--)
for(j=250;j>0;j--)
;
}
void display()
{
unsigned long temp;
unsigned char DATA,digit[4];
unsigned int k,l;
DATA=P1; //save AD data
temp=DATA; //the temporary variable
temp=temp*1000/51; //AD formula:D=A*5/255
digit[0]=temp/1000; //the thousands place
digit[1]=temp%1000/100; //the hundreds place
digit[2]=temp%1000%100/10; //the tens place
digit[3]=temp%1000%100%10; //the ones place
DATA=P2; //save P2
for(k=0;k<1000;k++)
{
/* lighten each LED and equal the below for loop
P1=LED[digit[0]]+0x80;
P2_4=0;
P2_4=1;
P1=LED[digit[1]];
P2_5=0;
P2_5=1;
P1=LED[digit[2]];
P2_6=0;
P2_6=1;
P1=LED[digit[3]];
P2_7=0;
P2_7=1;
*/
for(l=0;l<4;l++)
{
P2=P2|0x0F; //avoid LED residual
P0=LED[digit[l]]+0x80*(l==0); //LED code
P2=DATA&CHECK[l]; //choose LED channel
}
}
P2=DATA; //restore P2
}
void main()
{
ST_ALE=0; //START and ALE default
OE=0; //OE default
TMOD=0x02; //T0 operate in mode 2
EA=1; //all interrupt enable
ET0=1; //T0 interrupt enable
TR0=1; //T0 run
while(1)
{
ST_ALE=1; //lock the AD channel address
ST_ALE=0; //AD run
while(EOC==0); //wait until AD over
OE=1; //enable to transmit AD data
display(); //view the AD result and diaplay LED
}
}
void T1_TIME() interrupt 1 using 0
{
CLK=~CLK; //CLK 500 kHz
}
问题:滑动变阻器显示的电压和数码管显示的电压数值不等,不知道具体问题是哪里,还望大神多多指教。新生小白一只,刚刚接触单片机,还望多多指教!
此帖出自
小平头技术问答
一周热门 更多>