1、sbit KeyOut3 = P21;sbit KeyOut4 = P20;sbit ADDR0 = P10;sbit ADDR1 = P11;sbit ADDR2 = P12;sbit ADDR3 = P13;sbit ENLED = P14;sbit BUZZ = P16;#define FADD 10#define FSUB 11#define FMUL 12#define FDIV 13#define FRES 14#define FEQU 15#define KEY_DELAY 300#define BUZ_DELAY 80code uint8 Ledcode13=0xc0,0xf9
2、,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0x86;uint8 Led_n=0;uint8 Led_buf6;float Tmp1=0, Tmp2=0;int8 C_flag=0; * 延时void delay(uint16 n) while (n-); * 蜂鸣器发声void buzzer_sound(void) uint16 i; for (i=0; i 0) buzzer_sound(); return val; * 验证数据有效性bit check_num(float f_num) if (f_num = 100000) re
3、turn 1; return 0; * 制作数码管错误标志void make_led_error(void) int8 i;5; Led_bufi = Ledcode10; Led_buf5 = Ledcode12; * 制作数码管整数数据void make_led_inumber(int32 i_num) bit s_flag=0; int16 sit; if (i_num =1; i-, sit/=10) if (i_num = sit) break; i_num -= i_num/sit*sit; for (;i Led_bufi = Ledcodei_num/sit; Led_buf0
4、 = Ledcodei_num & 0x7F; if (s_flag) Led_buf5 = Ledcode11; else Led_buf5 = Ledcode10; ET0 = 1; * 制作数码管浮点数据void make_led_fnumber(float f_num) int32 num; int8 i, decimal, dot_sit=0; if (f_num 0; decimal-) if (num % 10 != 0) num /= 10; dot_sit = decimal;= 10000) dot_sit = 0; else if (f_num = 1000) if (d
5、ecimal = 1) dot_sit = 1;= 100)= 2) dot_sit = 2;= 10)= 3) dot_sit = 3;dot_sit; f_num *= 10; num = (int32)(f_num+0.5); if (num if (i = dot_sit) num -= num/sit*sit; Led_bufi = Ledcodenum/sit; Led_buf0 = Ledcodenum; Led_bufdot_sit &= 0x7F; * 数码管显示void show_num(uint8 *buf) ENLED = 1; switch (Led_n) case
6、0: ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = buf0; case 1: ADDR0 = 1; P0 = buf1; case 2: ADDR1 = 1; P0 = buf2; case 3: P0 = buf3; case 4: ADDR2 = 1; P0 = buf4; case 5: P0 = buf5; ENLED = 0; if (Led_n = 5) Led_n = 0; Led_n+; * 计算程序void calculate(int8 key_val) float ans; bit err=0; if (key_val = FADD) & (
7、key_val = FDIV) C_flag = key_val; else if (key_val = FEQU) switch (C_flag) case FADD: ans = Tmp1+Tmp2; break; case FSUB: ans = Tmp1-Tmp2; case FMUL: ans = Tmp1*Tmp2; case FDIV: if (Tmp2 = 0) err = 1; break; else ans = Tmp1/Tmp2; if (check_num(ans) err = 1; if (err) make_led_error(); else make_led_fn
8、umber(ans); C_flag = 0; Tmp1 = 0; Tmp2 = 0; else if (key_val = FRES) make_led_fnumber(0); if (C_flag = 0) ans = Tmp1; ans *= 10; ans += key_val; if (check_num(ans) return; else Tmp1 = ans; make_led_inumber(Tmp1); ans = Tmp2; Tmp2 = ans; make_led_inumber(Tmp2);main() int8 key_val; ADDR3 = 1; make_led_inumber(0); TMOD = 0x01; TH0 = 0xF8; TL0 = 0xCC; TR0 = 1; EA = 1; while (1) key_val = scan_key(); if (key_val = -1) continue; calculate(key_val);void time0() interrupt 1 TR0 = 0; show_num(Led_buf);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1