本帖最后由 a2455788055 于 2013-7-24 18:09 编辑
我是一个初学者,通过查阅资料编写了C51程序,运行没错误,
仿真图如图所示也应该没问题,就是我一按按键就出现乱码,我想是不是显示程序那块问题,我检查了好久都没检查出来,希望高手能看下并指出错误。。。小弟不胜感激~~~也可加小弟QQ:654309668,希望高手们能带带我!先谢谢了。程序如下:
#include<reg51.h>
#include<intrins.h>
#define ledr P2;
#define ledb P1;
sbit DIN=P2^2; //串行数据输入引脚
sbit CLK=P2^1; //同步时钟
sbit LOAD=P2^0; //数据输入使能
sbit BB1=P1^0;
sbit BB2=P1^1;
sbit BB3=P1^2;
sbit BB4=P1^3;
int keyc(unsigned long int n);//判断按键//
int delay10ms(int k);//延时
void send(unsigned char dataout);
void WriteMAX7219(unsigned char reg_number,unsigned char dataout);//输入
int display(unsigned long int n);//显示
unsigned long int key;
void main()
{
unsigned long int first;
unsigned long int second;
unsigned long int result;//最终运算结果
unsigned char op;//用来存储+、-、*、/操作符键的值
unsigned char flag,flag1;
first=0;second=0;result=0;op=0;flag=1;flag1=0;
while(1)
{
key=keyc(n);
if(key<11&&key>0&&flag==1)
{
if(key==10)
key=0;
first=first*10+key;key=0;
display(first);
}
delay10ms(30);
key=keyc(n);
if(key>12&&key<=16)
{
op=key;key=0;
flag=2;
}
key=keyc(n);
if(key<11&&key>0&&flag==2)
{
if(key==10)
key=0;
second=second*10+key;key=0;
display(second);
flag1=1;
}
delay10ms(30);
key=keyc(n);
if(key==12&&flag1==1)
{
switch(op)
{
case 13:result=first+second;break;
case 14:result=first-second;break;
case 15:result=first*second;break;
case 16:result=first/second;break;
}
first=0;
second=0;
display(result);
key=0;
flag=1;
flag1=0;
}
key=keyc(n);
if(key==11)
first=0;second=0;result=0;op=0;flag=1;flag1=0;
}
}
int display(unsigned long int n)//显示程序
{
unsigned char n0;
unsigned char n1;
unsigned char n2;
unsigned char n3;
unsigned char n4;
unsigned char n5;
unsigned char n6;
unsigned char n7;
n0=n/10000000;
n1=(n/1000000)%10;
n2=(n/100000)%10;
n3=(n/10000)%10;
n4=(n/1000)%10;
n5=(n/100)%10;
n6=(n/10)%10;
n7=n%10;
if(n0==0)
{
n0=0x0f;
if(n1==0)
{
n1=0x0f;
if(n2==0)
{
n2=0x0f;
if(n3==0)
{
n3=0x0f;
if(n4==0)
{
n4=0x0f;
if(n5==0)
{
n5=0x0f;
if(n6==0)
n6=0x0f;
}
}
}
}
}
}
WriteMAX7219(0x0c,0x01); //掉电控制寄存器设置为正常操作
WriteMAX7219(0x0b,0x07); //扫描边界,显示8位
WriteMAX7219(0x0a,0xf5); //亮度控制
WriteMAX7219(0x09,0xff); //译码方式
WriteMAX7219(0x01,n0); //最大显示结果65535;最高位不显示 ,灭零 ;
WriteMAX7219(0x02,n1);
WriteMAX7219(0x03,n2);
WriteMAX7219(0x04,n3);
WriteMAX7219(0x05,n4);
WriteMAX7219(0x06,n5);
WriteMAX7219(0x07,n6);
WriteMAX7219(0x08,n7);
}
void send(unsigned char dataout)//发送数据
{
char i;
for(i=8;i>0;i--)
{
unsigned char mask=1<<(i-1);
CLK=0;
if(dataout&mask)
DIN=1;
else
DIN=0;
CLK=1;
}
}
void WriteMAX7219(unsigned char reg_number,unsigned char dataout)
{
LOAD=1;
send(reg_number);
send(dataout);
LOAD=0;
LOAD=1;
}
int keyc(unsigned long int n)//判断按键程序
{
{
unsigned char temp;
P1=0xFF;
BB1=0;
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
delay10ms(10);
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0xE0:{n=7;}break;
case 0xd0:{n=8;}break;
case 0xb0:{n=9;}break;
case 0x70:{n=16;}break;//"除号"
}
}
}
P1=0xFF;
BB2=0;
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
delay10ms(10);
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0xE0:{n=4;}break;
case 0xd0:{n=5;}break;
case 0xb0:{n=6;}break;
case 0x70:{n=15;}break;//"乘号"
}
}
}
P1=0xFF;
BB3=0;
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
delay10ms(10);
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0xE0:{n=1;}break;
case 0xd0:{n=2;}break;
case 0xb0:{n=3;}break;
case 0x70:{n=14;}break;//"减号"
}
}
}
P1=0xFF;
BB4=0;
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
delay10ms(10);
temp=P1;
temp&=0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0xE0:{n=11;}break;//复位 ON/C
case 0xd0:{n=10;}break;//0
case 0xb0:{n=12;}break;//等号
case 0x70:{n=13;}break;//加号
}
}
}
}return n;
}
int delay10ms(int k)//20ms延时//
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<12;j++)
{;}
}
}
一周热门 更多>