大家好我是刚学习单片机编程,下面有一段7279控制显示的程序,麻烦大家帮忙改改,现在编译完了以后就在第三个数码管显示0,实际上我想实现的目的是,有三个数码管最小显示5最大显示100.通过按键0X00来实现以5为步具增长到100停止,通过按键键码值为0X01实现以5为步具减小直到最小5,下面断程序本来想表达的这个意思,但最后实现不了,晶振为8.192MHZ,请高手帮忙;
#include <stdio.h>
#include <stdlib.h>
#include <regx52.h>
#include <intrins.h>
#include <absacc.h>
sbit HD7279_CS=P1^5;
sbit HD7279_CLK=P1^1;
sbit HD7279_DATE=P1^2;
sbit HD7279_KEY=P1^3;
#define HD7279_DECODE0 0X80;
#define HD7279_READ 0X15;
#define HD7279_RESET 0Xa4;
void HD7279_SendByte(unsigned char);
unsigned char baiwei;
unsigned char shiwei;
unsigned char gewei,key;
unsigned int a=0 ;
unsigned char HD7279_DATA ;
void HD7279_LongDelay(void)
{unsigned char i;
for(i=0;i<6;i++);
}
void HD7279_ShortDelay(void)
{;}
void Delay10ms(unsigned char time)
{
unsigned char i;
unsigned int j;
for(i=0; i<time;i++)
{
for(j=0;j<931;j++)
{;}
}
}
void HD7279_SendByte(unsigned char out_byte)
{unsigned char i;
HD7279_CS=0;
HD7279_LongDelay();
for(i=0;i<8;i++)
{if(out_byte&0x80)
{HD7279_DATE=1;}
else{HD7279_DATE=0;}
HD7279_CLK=1;
HD7279_ShortDelay();
HD7279_CLK=0;
HD7279_ShortDelay();
out_byte=out_byte<<1;
}
HD7279_DATE=0;
}
void Write7279(unsigned char command ,unsigned char dta)
{HD7279_SendByte(command);
HD7279_SendByte(dta);
}
unsigned char HD7279_ReceiveByte(void)
{unsigned char i,in_byte;
HD7279_DATA=1;
HD7279_LongDelay();
for(i=0;i<8;i++)
{HD7279_CLK=1;
HD7279_ShortDelay();
in_byte=in_byte<<1;
if(HD7279_DATE)
{in_byte=in_byte|0x01;}
HD7279_CLK=0;
HD7279_ShortDelay();
}
HD7279_DATE=0;
return(in_byte);
}
unsigned char HD7279_GetKey(void)
{unsigned char key;
if(!HD7279_KEY)
{HD7279_SendByte(0x15);检测按键
key=HD7279_ReceiveByte();
switch( key )
{case 0x00: // 按键码
a=a+5; //以5为步具增加
Delay10ms(10);
break;
case 0x01:
a=a-5; 以5为步具减少
Delay10ms(10) ;
break;
default:return(0xff);
}
}
return(0xff);
}
void main(void)
{ P1=0X18; //对P1口初始定义
HD7279_SendByte(0xa4);
if(a>=5&&a<=100)
{baiwei=a/100;
shiwei=a%100/10;
gewei=a%10;
}
else
{key=0xff;}
if(baiwei==0)
{Write7279(0x80,0x0f);}
else
{Write7279(0x80,baiwei);} //百位为0数码管0什么都不显示
if(shiwei==0&&baiwei==0) //十位和百位为0就什么也不显示
{Write7279(0x81,0x0f);}
else
{Write7279(0x81,shiwei);}
Write7279(0x82,gewei);
}
end; |