Advertisement
a5768549

Untitled

Jul 25th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 KB | None | 0 0
  1. //ICC-AVR application builder
  2. //atmega8_p4
  3. /*
  4.     功能:計時器
  5.         S1按鈕:設定時間(從0000向上數,放開即停)
  6.         S2按鈕:開始倒數(timer開啟)
  7.         S4按鈕:暫停倒數(timer關閉)
  8.         S6按鈕:控制倒數速度(從0000向上數,放開即停,數值可從0~100)
  9.         倒數至0000時閃爍三下
  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.  
  23. #define w1       PORTD =  0x01;
  24. #define w2       PORTD =  0x02;
  25. #define w3       PORTD =  0x04;
  26. #define w4       PORTD =  0x08;
  27. #define w5       PORTD =  0x10;
  28. #define w6       PORTD =  0x20;
  29. #define w7       PORTD =  0x40;
  30. #define w8       PORTD =  0x80;
  31.  
  32.  
  33. unsigned int seg[10] =
  34. {
  35.     0x3f,0x06,0x5b,0x4f,
  36.     0x66,0x6d,0x7d,0x07,
  37.     0x7f,0x6f
  38. };
  39.  
  40. //初始化 變量 聲明//////////////////////////////////////////////////////////////
  41. int data = 0;
  42. int flag = 0;
  43. int th = 0;
  44. int hu = 0;
  45. int ten = 0;
  46. int num = 0;
  47. int i = 0;
  48. int speed = 50;
  49. int again = 0;
  50.  
  51. int count = 1;
  52. int count_num = 1;
  53. int btn2_count = 1;
  54. int btn2_3_count = 0;
  55.  
  56. void btn2_1(void)
  57. {
  58.     S_OFF;
  59.     for(i = 0;i<500;i++)
  60.     {
  61.     PORTD = seg[0];S_1;
  62.     delay_ms(1);
  63.     PORTD = seg[7];S_2;
  64.     delay_ms(1);
  65.     PORTD = seg[2];S_3;
  66.     delay_ms(1);
  67.     PORTD = seg[4];S_4;
  68.     delay_ms(1);
  69.     }
  70.     S_OFF;
  71.     btn2_count += 1;
  72. }
  73.  
  74. void btn2_2(void)
  75. {
  76.     S_OFF;
  77.     for(i = 0;i<500;i++)
  78.     {
  79.     PORTD = seg[0];S_1;
  80.     delay_ms(1);
  81.     PORTD = seg[2];S_2;
  82.     delay_ms(1);
  83.     PORTD = seg[5];S_3;
  84.     delay_ms(1);
  85.     PORTD = seg[6];S_4;
  86.     delay_ms(1);
  87.     }
  88.     S_OFF;
  89.     btn2_count += 1;
  90. }
  91.  
  92. void btn2_3(void)
  93. {
  94.     S_OFF;
  95.     th = btn2_3_count / 1000;
  96.     PORTD = seg[th];
  97.     S_1;
  98.     delay_us(900);
  99.     S_OFF;
  100.     hu = (btn2_3_count / 100) % 10;
  101.     PORTD = seg[hu];
  102.     S_2;
  103.     delay_us(900);
  104.     S_OFF;
  105.     ten = (btn2_3_count / 10) % 10;
  106.     PORTD = seg[ten]|0x80;
  107.     S_3;
  108.     delay_us(900);
  109.     S_OFF;
  110.     num = btn2_3_count % 10;
  111.     PORTD = seg[num];
  112.     S_4;
  113.     delay_us(900);
  114. }
  115.  
  116. void btn1_fuc(void)
  117. {
  118.    
  119.      switch(count_num)
  120.     {
  121.         case 1 : S_1;break;
  122.         case 2 : S_2;break;
  123.         case 3 : S_3;break;
  124.         case 4 : S_4;break;
  125.     }
  126.    
  127. }
  128.  
  129. void btn1(void)
  130. {
  131.     switch(count)
  132.     {
  133.         case 1 : w1;break;
  134.         case 2 : w2;break;
  135.         case 3 : w3;break;
  136.         case 4 : w4;break;
  137.         case 5 : w5;break;
  138.         case 6 : w6;break;
  139.         case 7 : w7;break;
  140.         case 8 : w8;break;
  141.     }
  142.     btn1_fuc();
  143.    
  144. }
  145.  
  146. void key_scan(void)
  147. {
  148.     if((PINB&0x3F)!=0x3F)
  149.     {
  150.         if((PINB&0x3F)!=0x3F)
  151.         {
  152.             delay_ms(20);
  153.             switch(PINB)
  154.             {
  155.                 case 0X3E:
  156.                     timer0_open();
  157.                 break;//S1
  158.                
  159.                 case 0X3B:
  160.                     switch(btn2_count)
  161.                     {
  162.                      case 1:btn2_1();break;
  163.                      case 2:btn2_2();break;
  164.                      case 3:
  165.                         delay_ms(1000);
  166.                     timer0_close();
  167.                         timer2_open();
  168.                         btn2_count =1;
  169.                         break;
  170.                     }
  171.                 break;//S2
  172.                
  173.                 case 0X37:
  174.                     timer2_open();
  175.                 break;//S6             
  176.                 default:break;
  177.             }              
  178.             delay_ms(1);
  179.         }
  180.     }
  181. }
  182.  
  183. void delay_us(unsigned int microsecond)
  184. {      
  185.     do
  186.     {
  187.         microsecond--;
  188.     }        
  189.     while (microsecond>1);
  190. }
  191.  
  192. void delay_ms(unsigned int millisecond)
  193. {      
  194.     while (millisecond--)
  195.     {
  196.         delay_us(999);
  197.     }  
  198. }
  199.  
  200. //I、O口初始化
  201. void port_init(void)
  202. {
  203.  DDRB  = 0XC0;
  204.  PORTB = 0XFF;//按鈕
  205.  
  206.  PORTC = 0xFF;
  207.  DDRC  = 0xFF;
  208.  PORTD = 0xFF;
  209.  DDRD  = 0xFF;//七段顯示器
  210. }
  211.  
  212. void timer0_close(void)
  213. {
  214.     CLI();
  215.     TIMSK = 0x01;
  216.     TCCR0 =(0<<CS01) | (0<<CS00);
  217.     TCNT0 = 0X05;//0XFF   256    (0xFF-0X05) *8M/64
  218.     SEI();
  219. }
  220.  
  221. void timer0_open(void)
  222. {
  223.     CLI();
  224.     TIMSK = 0x01;
  225.     TCCR0 =(1<<CS01) | (1<<CS00);
  226.     TCNT0 = 0X05;                   //timer起始值(255-5)
  227.     SEI();
  228. }
  229.  
  230. void timer2_close(void)
  231. {
  232.     CLI();
  233. TCCR2 = 0X03;//0000 0011
  234.      TCNT2 = 0X83;//初值
  235.     SEI();
  236. }
  237.  
  238. void timer2_open(void)
  239. {
  240.     CLI();
  241. TCCR2 = 0X03;//0000 0011
  242.      TCNT2 = 0X83;//初值
  243.  
  244.     SEI();
  245. }
  246.  
  247. //CPU總體初始化
  248. void init_devices(void)
  249. {
  250.  CLI();
  251.  port_init();
  252.  MCUCR = 0x00;
  253.  GICR  = 0x00;
  254.  
  255.  TIMSK = 0x43;
  256.  TCCR0 =(0<<CS01) | (0<<CS00);
  257.  TCNT0 = 0X05;
  258.  
  259. TCCR2 = 0X00;//0000 0011
  260.      TCNT2 = 0X83;//初值
  261.  
  262.  SEI();
  263. }
  264. //向量值
  265. #pragma interrupt_handler timer0_ovf_isr:10    
  266. void timer0_ovf_isr(void)
  267. {    
  268.     TCNT0 = 0X05;
  269.     data++;
  270.    
  271.     if(data == 50)              // 50*(255-5)*(8M/64)=0.1s
  272.     {
  273.     if(count == 9)
  274.         {
  275.         count = 1;
  276.         count_num += 1;
  277.         }
  278.         if(count_num == 5)
  279.     {
  280.         count_num = 1;
  281.     }
  282.         data = 0;
  283.         S_OFF;
  284.         btn1();
  285.         count = count + 1;
  286.        
  287.     }
  288. }
  289.  
  290. #pragma interrupt_handler timer2_ovf_isf:5
  291. void timer2_ovf_isf(void)
  292. {
  293.   TCNT2 = 0X83;//初值
  294.   data++;
  295.    
  296.     if(data = 50)               // 50*(255-5)*(8M/64)=0.1s
  297.     {
  298.         data = 0;
  299.         btn2_3_count += 1;
  300.        
  301.         btn2_3();
  302.         S_OFF;
  303.     }
  304. }
  305.  
  306. void main(void)
  307. {
  308. SREG = 0X80;
  309.    init_devices();
  310.    while(1)
  311.    {  
  312.         key_scan();
  313.    }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement