Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ICC-AVR application builder
- //atmega8_p4
- /*
- 功能:計時器
- S1按鈕:設定時間(從0000向上數,放開即停)
- S2按鈕:開始倒數(timer開啟)
- S4按鈕:暫停倒數(timer關閉)
- S6按鈕:控制倒數速度(從0000向上數,放開即停,數值可從0~100)
- 倒數至0000時閃爍三下
- */
- #include<iom8v.h>
- #include<macros.h>
- #define S_1 PORTC &= ~BIT(0);PORTC|=BIT(1) ;PORTC|=BIT(2) ;PORTC|=BIT(3);
- #define S_2 PORTC |= BIT(0) ;PORTC&=~BIT(1);PORTC|=BIT(2) ;PORTC|=BIT(3);
- #define S_3 PORTC |= BIT(0) ;PORTC|=BIT(1) ;PORTC&=~BIT(2);PORTC|=BIT(3);
- #define S_4 PORTC |= BIT(0) ;PORTC|=BIT(1) ;PORTC|=BIT(2) ;PORTC&=~BIT(3);
- #define S_OFF PORTC |= BIT(0) ;PORTC|=BIT(1) ;PORTC|=BIT(2) ;PORTC|=BIT(3);
- #define S_ON PORTC &= ~BIT(0);PORTC&=~BIT(1);PORTC&=~BIT(2);PORTC&=~BIT(3);
- #define w1 PORTD = 0x01;
- #define w2 PORTD = 0x02;
- #define w3 PORTD = 0x04;
- #define w4 PORTD = 0x08;
- #define w5 PORTD = 0x10;
- #define w6 PORTD = 0x20;
- #define w7 PORTD = 0x40;
- #define w8 PORTD = 0x80;
- unsigned int seg[10] =
- {
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f
- };
- //初始化 變量 聲明//////////////////////////////////////////////////////////////
- int data = 0;
- int flag = 0;
- int th = 0;
- int hu = 0;
- int ten = 0;
- int num = 0;
- int i = 0;
- int speed = 50;
- int again = 0;
- int count = 1;
- int count_num = 1;
- int btn2_count = 1;
- int btn2_3_count = 0;
- void btn2_1(void)
- {
- S_OFF;
- for(i = 0;i<500;i++)
- {
- PORTD = seg[0];S_1;
- delay_ms(1);
- PORTD = seg[7];S_2;
- delay_ms(1);
- PORTD = seg[2];S_3;
- delay_ms(1);
- PORTD = seg[4];S_4;
- delay_ms(1);
- }
- S_OFF;
- btn2_count += 1;
- }
- void btn2_2(void)
- {
- S_OFF;
- for(i = 0;i<500;i++)
- {
- PORTD = seg[0];S_1;
- delay_ms(1);
- PORTD = seg[2];S_2;
- delay_ms(1);
- PORTD = seg[5];S_3;
- delay_ms(1);
- PORTD = seg[6];S_4;
- delay_ms(1);
- }
- S_OFF;
- btn2_count += 1;
- }
- void btn2_3(void)
- {
- S_OFF;
- th = btn2_3_count / 1000;
- PORTD = seg[th];
- S_1;
- delay_us(900);
- S_OFF;
- hu = (btn2_3_count / 100) % 10;
- PORTD = seg[hu];
- S_2;
- delay_us(900);
- S_OFF;
- ten = (btn2_3_count / 10) % 10;
- PORTD = seg[ten]|0x80;
- S_3;
- delay_us(900);
- S_OFF;
- num = btn2_3_count % 10;
- PORTD = seg[num];
- S_4;
- delay_us(900);
- }
- void btn1_fuc(void)
- {
- switch(count_num)
- {
- case 1 : S_1;break;
- case 2 : S_2;break;
- case 3 : S_3;break;
- case 4 : S_4;break;
- }
- }
- void btn1(void)
- {
- switch(count)
- {
- case 1 : w1;break;
- case 2 : w2;break;
- case 3 : w3;break;
- case 4 : w4;break;
- case 5 : w5;break;
- case 6 : w6;break;
- case 7 : w7;break;
- case 8 : w8;break;
- }
- btn1_fuc();
- }
- void key_scan(void)
- {
- if((PINB&0x3F)!=0x3F)
- {
- if((PINB&0x3F)!=0x3F)
- {
- delay_ms(20);
- switch(PINB)
- {
- case 0X3E:
- timer0_open();
- break;//S1
- case 0X3B:
- switch(btn2_count)
- {
- case 1:btn2_1();break;
- case 2:btn2_2();break;
- case 3:
- delay_ms(1000);
- timer0_close();
- timer2_open();
- btn2_count =1;
- break;
- }
- break;//S2
- case 0X37:
- timer2_open();
- break;//S6
- default:break;
- }
- delay_ms(1);
- }
- }
- }
- void delay_us(unsigned int microsecond)
- {
- do
- {
- microsecond--;
- }
- while (microsecond>1);
- }
- void delay_ms(unsigned int millisecond)
- {
- while (millisecond--)
- {
- delay_us(999);
- }
- }
- //I、O口初始化
- void port_init(void)
- {
- DDRB = 0XC0;
- PORTB = 0XFF;//按鈕
- PORTC = 0xFF;
- DDRC = 0xFF;
- PORTD = 0xFF;
- DDRD = 0xFF;//七段顯示器
- }
- void timer0_close(void)
- {
- CLI();
- TIMSK = 0x01;
- TCCR0 =(0<<CS01) | (0<<CS00);
- TCNT0 = 0X05;//0XFF 256 (0xFF-0X05) *8M/64
- SEI();
- }
- void timer0_open(void)
- {
- CLI();
- TIMSK = 0x01;
- TCCR0 =(1<<CS01) | (1<<CS00);
- TCNT0 = 0X05; //timer起始值(255-5)
- SEI();
- }
- void timer2_close(void)
- {
- CLI();
- TCCR2 = 0X03;//0000 0011
- TCNT2 = 0X83;//初值
- SEI();
- }
- void timer2_open(void)
- {
- CLI();
- TCCR2 = 0X03;//0000 0011
- TCNT2 = 0X83;//初值
- SEI();
- }
- //CPU總體初始化
- void init_devices(void)
- {
- CLI();
- port_init();
- MCUCR = 0x00;
- GICR = 0x00;
- TIMSK = 0x43;
- TCCR0 =(0<<CS01) | (0<<CS00);
- TCNT0 = 0X05;
- TCCR2 = 0X00;//0000 0011
- TCNT2 = 0X83;//初值
- SEI();
- }
- //向量值
- #pragma interrupt_handler timer0_ovf_isr:10
- void timer0_ovf_isr(void)
- {
- TCNT0 = 0X05;
- data++;
- if(data == 50) // 50*(255-5)*(8M/64)=0.1s
- {
- if(count == 9)
- {
- count = 1;
- count_num += 1;
- }
- if(count_num == 5)
- {
- count_num = 1;
- }
- data = 0;
- S_OFF;
- btn1();
- count = count + 1;
- }
- }
- #pragma interrupt_handler timer2_ovf_isf:5
- void timer2_ovf_isf(void)
- {
- TCNT2 = 0X83;//初值
- data++;
- if(data = 50) // 50*(255-5)*(8M/64)=0.1s
- {
- data = 0;
- btn2_3_count += 1;
- btn2_3();
- S_OFF;
- }
- }
- void main(void)
- {
- SREG = 0X80;
- init_devices();
- while(1)
- {
- key_scan();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement