这是一个加法运算,运行到最后得出结果是出行乱码,显示数字是动态乱码,求解!!!
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- uchar code table[]={0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f};
- void delay( uint m ) //延时函数
- {
- uint x , y ;
- for( x = m ; x > 0 ; x--)
- for( y = 110 ; y > 0 ; y--);
- }
- sbit beep = P2^2 ; //蜂鸣器
- uchar firstflot = 0 ;
- int keyscan() //扫描键盘
- {
- int temp, num ;
- P2=0x7f;
- temp=P2;
- temp=temp&0x0f; //判断第一行按键
- if(temp != 0x0f )
- {
- delay( 10 ) ;
- temp = P2 ; //重读P2口值
- temp = temp & 0x0f ;
- if( temp != 0x0f )
- {
- temp = P2 ;
- switch(temp)
- {
- case 0x77: num=1; //1
- firstflot=1 ;
- break;
- case 0x7b: num=2; //2
- firstflot=1 ;
- break;
- case 0x7d: num=3; //3
- firstflot=1 ;
- break;
- case 0x7e: num = '+' ; //+
- break;
- }
- while(temp != 0x0f) //等待按键释放
- {
- temp = P2;
- temp = temp & 0x0f;
- }
- }
- }
- P2 = 0xbf ; //扫描第二行键盘
- temp = P2 ;
- temp = temp & 0x0f;
- if( temp != 0x0f )
- {
- delay( 10 ) ; //消抖
- temp = P2 ;
- temp = temp & 0x0f ;
- if( temp != 0x0f )
- {
- temp = P2 ;
- switch( temp )
- {
- case 0xb7: num=4; //4
- firstflot=1 ;
- break;
- case 0xbb: num=5; //5
- firstflot=1 ;
- break;
- case 0xbd: num=6; //6
- firstflot=1 ;
- break;
- case 0xbe: num = '-' ; //-
- break;
- }
- while( temp != 0x0f )
- {
- temp = P2 ;
- temp = temp & 0x0f ;
- }
- }
- }
- P2 = 0xdf ; //扫描第三行键盘
- temp = P2 ;
- temp = temp & 0x0f ;
- if( temp != 0x0f )
- {
- delay( 10 ) ; //消抖
- temp = P2 ;
- temp = temp & 0x0f ;
- if( temp != 0x0f )
- {
- temp = P2 ;
- switch( temp )
- {
- case 0xd7: num=7; //7
- firstflot=1 ;
- break;
- case 0xdb: num=8; //8
- firstflot=1 ;
- break;
- case 0xdd: num=9; //9
- firstflot=1 ;
- break;
- case 0xde: num = '*' ; //*
- break;
- }
- while( temp != 0x0f )
- {
- temp = P2;
- temp = temp & 0x0f ;
- }
- }
- }
- P2 = 0xef ; //扫描第四行键盘
- temp = P2 ;
- temp = temp & 0x0f ;
- if( temp != 0x0f )
- {
- delay( 10 ) ; //消抖
- temp = P2 ;
- temp = temp & 0x0f;
- if( temp != 0x0f )
- {
- temp = P2 ;
- switch( temp )
- {
- // case 0xe7: printf("num%s",onn) //归0
- // break;
- case 0xeb: num = 0 ; //0
- firstflot=1 ;
- break;
- case 0xed: num = '=' ; //=
- break;
- case 0xee: num = '/' ; //除
- break;
- }
- }
- }
- return num ;
- }
- int modern() //输入与计算
- {
- int second = 0 , m , n ; //操作符
- P0 = 0x7f ; //初始化数码管 0
- P1 = table[0];
- delay(1);
- while(1)
- {
- keyscan() ;
- init: //返回标记处
- if(firstflot==1) //给m赋值
- {
- P0 = 0x7f ;
- m = keyscan() ;
- P1 = table[ m ] ;
- delay( 1 ) ;
- second=1 ;
- firstflot = 0 ;
- }
- if(second==1)
- {
- while(1)
- {
- keyscan() ;
- if(firstflot==1)
- {
- P0 = 0x7f ;
- n = keyscan() ;
- P1 = table[ n ] ;
- beep = 0 ; //蜂鸣器置加点
- delay( 1 ); //关
- firstflot = 0 ; //重新设
- second = 0 ;
- goto init ;
- }
- }
- }
- if(keyscan() == '+' || keyscan() == '-' || keyscan() == '*' || keyscan() == '/' )
- {
- P0 = 0xff ; //关闭
- beep = 1 ; //蜂鸣器开
- second = 1 ;
- delay( 1 ) ;
- goto init ;
- }
- if( keyscan() == '=' ) //等号
- {
- m += n ; //计算加法
- return m ;
- }
- }
- }
- void display_1(int a)
- {
- P0 = 0xbf ;
- P1= table[a/10] ; //十位
- delay(1) ;
- P0 = 0x7f ;
- P1 = table[a%10] ; //个位
- delay(1) ;
- }
- int main()
- {
- while(1)
- {
- display_1( modern());
- }
- }
复制代码 |