AVR单片机制作计算器(共5页).doc
精选优质文档-倾情为你奉上Proteus中的模拟电路:C语言程序: #include<mega32.h> #include<delay.h> unsigned int put = 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f; unsigned int key; unsigned int led_1,led_2,led_3,led_4,fuhao,num,num_1; interrupt EXT_INT0void ext_int0_isr() /按键中断 低电平触发 PORTD.4=0; PORTA = 0x01; if(PINA.4) key = 16; / 清零 if(PINA.5) key = 3; if(PINA.6) key = 2; if(PINA.7) key = 1; PORTA = 0x02; if(PINA.4) key = 15; / 等于 if(PINA.5) key = 6; if(PINA.6) key = 5; if(PINA.7) key = 4; PORTA = 0x04; if(PINA.4) key = 0; if(PINA.5) key = 9; if(PINA.6) key = 8; if(PINA.7) key = 7; PORTA = 0x08; if(PINA.4) key = 14; /除法 if(PINA.5) key = 13; /乘法 if(PINA.6) key = 12; /减法 if(PINA.7) key = 11; /加法 delay_ms(20); PORTA = 0x0f; void xianshi() /数码管显示 函数 while(PIND.1) PORTB = 0x0e; PORTC = putled_1; delay_ms(1); PORTB = 0x0f; PORTB = 0x0d; PORTC = putled_2; delay_ms(1); PORTB = 0x0f; PORTB = 0x0b; PORTC = putled_3; delay_ms(1); PORTB = 0x0f; PORTB = 0x07; PORTC = putled_4; delay_ms(1); void jisuan() /加减乘除计算函数 switch(fuhao) case 11 : num=num_1+num; break; case 12 : num=num_1-num; break; case 13 : num=num_1*num; break; case 14 : num=num_1/num; break; void main(void) /主函数 DDRB=0x0f; DDRC=0xff; DDRD=0xf0; DDRA=0x0f; GICR|=0x40; MCUCR = 0x02; MCUCSR = 0x00; GIFR = 0x40; #asm("sei") while(1) PORTA = 0x0f; if(key < 10) num=num*10+key; led_4 = led_3; led_3 = led_2; led_2 = led_1; led_1 = key; xianshi(); PORTD.4=1; if(key > 10) led_1=0; led_2=0; led_3=0; led_4=0; if(key!=15) num_1=num; num=0; if(key=11) fuhao=11; xianshi(); PORTD.4=1; if(key=12) fuhao=12; xianshi(); PORTD.4=1; if(key=13) fuhao=13; xianshi(); PORTD.4=1; if(key=14) fuhao=14; xianshi(); PORTD.4=1; if(key=16) num_1=0; num=0; xianshi(); PORTD.4=1; if(key=15) jisuan(); led_1 = num%10; num = num/10; led_2 = num%10; num = num/10; led_3 = num%10; num = num/10; led_4 = num%10; xianshi(); PORTD.4=1; led_1=0; led_2=0; led_3=0; led_4=0; 专心-专注-专业