|
單片機數碼管閃動,發暗是為什么?而且打開數碼管后,led也會跟著閃動,兩個數碼管全開,有些led燈會直接黑了,測了發現變黑的led燈電壓變低。
這是代碼,剛接觸一段時間,不是很懂,網上查了資料,改了時延,都不行,大神求教
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint cyc0=1200;
uchar codevalue[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //êy1üê,ê0~9oíèáá
uchar codevalue_dot[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78, 0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};
uchar codecho[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //
uint count_s=60;
uint tvalue;
uchar button[8]={0,0,0,0,0,0,0,0}; //啟動 設置 + - 低檔 高檔 照明 殺菌燈
uchar Jmark[5]={0,0,0,0,0};//低檔 高檔 風扇 燈 殺菌燈
uchar Ledmark[5]={0,0,0,0,0};//啟動 低檔 高檔 照明 殺菌燈
uchar num;
dis[5]=0;
bit timeOpen=0;
sbit DQ=P3^7;//溫度傳輸引腳定義
sbit duan=P3^2;
sbit wei=P3^3;
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
sbit led5=P2^4;
sbit led6=P3^6;
sbit j1=P2^5;
sbit j2=P2^6;
sbit j3=P2^7;
sbit j4=P3^0;
sbit j5=P3^1;
sbit Button1=P1^0;
sbit Button2=P1^1;
sbit Button3=P1^2;
sbit Button4=P1^3;
sbit Button5=P1^4;
sbit Button6=P1^5;
sbit Button7=P1^6;
sbit Button8=P1^7;
void t_clr()
{
TMOD |= 0x01; //使用模式1,16位定時器,使用"|"符號可以在使用多個定時器時不受影響
//TH0=0x00; //給定初值
//TL0=0x00;
EA=1; //總中斷打開
ET0=1; //定時器中斷打開
TR0=1; //定時器開關打開
TH0=0X3c;
TL0=0Xaf;
}
void dataCompute(){
dis[0]=codevalue[tvalue%1000/100];
dis[1]=codevalue_dot[tvalue%100/10];
dis[2]=codevalue[tvalue%10];
if(timeOpen==1){
dis[3]=codevalue[count_s/10];
dis[4]=codevalue[count_s%10];
}else{
dis[3]=0xff;
dis[4]=0xff;
}
}
void delay500us(void) //誤差 0us
{
unsigned char a,b;
for(b=71;b>0;b--)
for(a=2;a>0;a--);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//設置
void setMark(){
//低檔
if(Jmark[0]==0){
j1=1;
}else{
j1=0;
}
//高檔
if(Jmark[1]==0){
j2=1;
}else{
j2=0;
}
//風扇
if(Jmark[2]==0){
j3=1;
}else{
j3=0;
}
//-----------led控制-----------
//啟動
if(Ledmark[0]==0){
led1=1;
led6=1;
}else{
led1=0;
led6=0;
}
//低檔
if(Ledmark[1]==0){
led2=1;
}else{
led2=0;
}
//高檔
if(Ledmark[2]==0){
led3=1;
}else{
led3=0;
}
//照明
if(Jmark[3]==0){
j4=1;
}else{
j4=0;
}
//殺菌燈
if(Jmark[4]==0){
j5=1;
}else{
j5=0;
}
//照明
if(Ledmark[3]==0){
led4=1;
}else{
led4=0;
}
//殺菌燈
if(Ledmark[4]==0){
led5=1;
}else{
led5=0;
}
}
void display()
{
uchar k;
for(k=0;k<5;k++) //LEDê
{
wei=1;
P0=codecho[k];
wei=0;
duan=1;
P0=dis[k];
duan=0;
delay(10);
wei=0;
duan=0;
}
}
void key(){
if(!Button1){
delay(120);
if(!Button1){
if(button[1]==1){
button[1]=0;
Ledmark[0]=0;
Ledmark[1]=0;
Ledmark[2]=0;
Ledmark[4]=0;
Jmark[0]=0;
Jmark[1]=0;
Jmark[2]=0;
timeOpen=0;
TR0=0; //關中斷 暫停倒計時
}else{
button[1]=1;
Ledmark[0]=1;
Jmark[2]=1;
Jmark[0]=1;
if(timeOpen==1){
TR0=1; //開中斷
}
}
}
}
if(!Button2){ //時間設置按鈕
delay(120);
if(!Button2){
if(button[2]==1){
button[2]=0;
timeOpen=0; //時間顯示部分可能需要修改
}else{
button[2]=1;
dataCompute();
if(button[1]==1){
timeOpen=1;
}
}
}
}
if(!Button3){ //+
delay(120);
if(!Button3){
if(button[2]==1){
if(count_s<99){
count_s++;
}
}
}
}
if(!Button4){ //-
delay(120);
if(!Button4){
if(button[2]==1){
if(count_s){
count_s--;
}
}
}
}
if(!Button5){
delay(120);
if(!Button5){
if(button[1]==1){
if(button[4]==1){
button[4]=0;
}else{
button[4]=1;
Ledmark[1]=1;
Ledmark[2]=0;
Jmark[0]=1;
Jmark[1]=0;
}
}
}
}
if(!Button6){
delay(120);
if(!Button6){
if(button[1]==1){
if(button[5]==1){
button[5]=0;
Ledmark[1]=1;
Ledmark[2]=0;
Jmark[0]=1;
Jmark[1]=0;
}else{
button[5]=1;
Ledmark[1]=0;
Ledmark[2]=1;
Jmark[0]=1;
Jmark[1]=1;
}
}
}
}
if(!Button7){
delay(120);
if(!Button7){
if(button[7]==1){
button[7]=0;
Jmark[3]=0;
Ledmark[3]=0;
}else{
button[7]=1;
Jmark[3]=1;
Ledmark[3]=1;
}
}
}
if(!Button8){
delay(120);
if(!Button8){
if(button[8]==1){
button[8]=0;
Jmark[4]=0;
Ledmark[4]=0;
}else{
button[8]=1;
Jmark[4]=1;
Ledmark[4]=1;
}
}
}
}
void init(){ //初始化
led1=1;
led2=1;
led3=1;
led4=1;
led5=1;
led6=1;
j1=1;
j2=1;
j3=1;
j4=1;
j5=1;
timeOpen=0;
count_s=60;
}
/******************************ds18203***************************************/
void delay_18B20(unsigned int i)//óê±1¢
{
while(i--);
}
void ds1820rst()/*ds1820′*/
{ unsigned char x=0;
DQ = 1; //DQ′
delay_18B20(4); //óê±
DQ = 0; //DQà-μí
delay_18B20(100); //è·óê±′óóú480us
DQ = 1; //à-
delay_18B20(40);
}
uchar ds1820rd()
{ unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{ DQ = 0; //3Do
dat>>=1;
DQ = 1; //3Do
if(DQ)
dat|=0x80;
delay_18B20(10);
}
return(dat);
}
void ds1820wr(uchar wdata)
{unsigned char i=0;
for (i=8; i>0; i--)
{ DQ = 0;
DQ = wdata&0x01;
delay_18B20(10);
DQ = 1;
wdata>>=1;
}
}
read_temp()
{uchar a,b;
ds1820rst();
ds1820wr(0xcc);
ds1820wr(0x44);
ds1820rst();
ds1820wr(0xcc);
ds1820wr(0xbe);
a=ds1820rd();
b=ds1820rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
tvalue=tvalue*(0.625);
return(tvalue);
}
void main()
{
t_clr();
init();
while(1)
{
read_temp();
dataCompute();
key();
setMark();
display();
}
}
void t0() interrupt 1
{
TR0=0;
TH0=0X3c;
TL0=0Xaf;
cyc0--;
if (cyc0==0)
{cyc0=1200;
count_s--;
if (count_s!=0 && timeOpen==1)
{
TR0=1;
}
else if(count_s==0){
TR0=0;
init();
}
}
else
TR0=1;
}
|
|