Advertisement
a5768549

ATmega8_p5

Jul 27th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. //ICC-AVR application builder
  2. //atmega8_p5
  3. /*
  4.     功能:計時器
  5.         S1按鈕:七段功能依序(七段繞圈圈檢測)
  6.         S2按鈕:
  7.                 第一下:顯示目前日期(月,日)
  8.                 第二下:顯示目前時間(時,分)
  9.                 第三下:由0000計數到0010(間隔一秒),結束計數時回到S1按鈕功能
  10. */
  11.  
  12. #include<iom8v.h>
  13. #include<macros.h>
  14.  
  15. #define S_1      PORTC &= ~BIT(0);PORTC|=BIT(1) ;PORTC|=BIT(2) ;PORTC|=BIT(3);
  16. #define S_2      PORTC |= BIT(0) ;PORTC&=~BIT(1);PORTC|=BIT(2) ;PORTC|=BIT(3);
  17. #define S_3      PORTC |= BIT(0) ;PORTC|=BIT(1) ;PORTC&=~BIT(2);PORTC|=BIT(3);  
  18. #define S_4      PORTC |= BIT(0) ;PORTC|=BIT(1) ;PORTC|=BIT(2) ;PORTC&=~BIT(3);
  19. #define S_OFF    PORTC |= BIT(0) ;PORTC|=BIT(1) ;PORTC|=BIT(2) ;PORTC|=BIT(3);  
  20. #define S_ON     PORTC &= ~BIT(0);PORTC&=~BIT(1);PORTC&=~BIT(2);PORTC&=~BIT(3);
  21.  
  22. unsigned int seg[11] =
  23. {
  24.     0x3f,0x06,0x5b,0x4f,
  25.     0x66,0x6d,0x7d,0x07,
  26.     0x7f,0x6f,0x71
  27. };
  28.  
  29. //初始化 變量 聲明//////////////////////////////////////////////////////////////
  30. int data = 0;
  31. int data2 = 0;
  32.  
  33. int count2 = 0;
  34.  
  35. void btn2(void)
  36. {
  37.     S_OFF;
  38.     switch(count2)
  39.     {
  40.         case 0:PORTD = seg[0];S_ON;count2=1;break;
  41.         case 1:PORTD = seg[10];S_ON;count2=0;break;
  42.     }
  43. }
  44.  
  45. void key_scan(void)
  46. {
  47.     if((PINB&0x3F)!=0x3F)     //假如1~8顆按鈕有被按下去(=0)
  48.     {
  49.         if((PINB&0x3F)!=0x3F)
  50.         {
  51.             delay_ms(20);
  52.             switch(PINB)
  53.             {
  54.                 case 0X3E:
  55.                    
  56.                 break;//S1
  57.                
  58.                 case 0X3B:
  59.                     timer0_open();
  60.                 break;//S2
  61.                 default:break;
  62.             }              
  63.             delay_ms(1);
  64.         }
  65.     }
  66. }
  67.  
  68. void delay_us(unsigned int microsecond)
  69. {      
  70.     do
  71.     {
  72.         microsecond--;
  73.     }        
  74.     while (microsecond>1);
  75. }
  76.  
  77. void delay_ms(unsigned int millisecond)
  78. {      
  79.     while (millisecond--)
  80.     {
  81.         delay_us(999);
  82.     }  
  83. }
  84.  
  85. //I、O口初始化
  86. void port_init(void)
  87. {
  88.     DDRB  = 0XC0;
  89.     PORTB = 0XFF;//按鈕
  90.  
  91.     PORTC = 0xFF;
  92.     DDRC  = 0xFF;
  93.     PORTD = 0xFF;
  94.     DDRD  = 0xFF;//七段顯示器
  95. }
  96.  
  97. void timer0_open(void)
  98. {
  99.     CLI();
  100.     TCNT0 = 0x05;
  101.     TIMSK |= BIT(TOIE0);
  102.     TCCR0 |= BIT(CS01) | BIT(CS00);
  103.     SEI();
  104. }
  105.  
  106. //CPU總體初始化
  107. void init_devices(void)
  108. {
  109.     CLI();
  110.     port_init();
  111.     MCUCR = 0x00;
  112.     GICR  = 0x00;
  113.    
  114.     TIMSK = 0x00;
  115.     TCCR0 = 0x00;
  116.     TCNT0 = 0x05;
  117.     SEI();
  118. }
  119. //向量值
  120. #pragma interrupt_handler timer0_ovf_isr:10    
  121. void timer0_ovf_isr(void)
  122. {    
  123.     TCNT0 = 0X05;
  124.     data++;
  125.     if(500==data)
  126.     {
  127.         data = 0;
  128.         btn2();
  129.     }
  130. }
  131.  
  132. void main(void)
  133. {
  134.     init_devices();
  135.     while(1)
  136.     {  
  137.         key_scan();
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement