找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4999|回復(fù): 0
收起左側(cè)

avr單片機(jī)秒表Proteus仿真程序 tm1637數(shù)碼管顯示

[復(fù)制鏈接]
ID:342822 發(fā)表于 2020-4-14 12:10 | 顯示全部樓層 |閱讀模式
Proteus仿真電路圖如下:
tm1637.gif

單片機(jī)源程序如下:
  1. /**

  2. *版權(quán)所有(c)2018,艁ukasz Marcin Podkalicki
  3. *2009年12月13日            
  4. *簡(jiǎn)單定時(shí)器(啟動(dòng)/復(fù)位/停止),使用基于TM1637的一個(gè)按鈕和7段顯示模塊。              *            
  5. *注意,這個(gè)ATtiny13項(xiàng)目使用的內(nèi)部時(shí)鐘并不精確            
  6. *時(shí)間可以向前或向后流動(dòng),但是嘿!            
  7. *它仍然足夠做一個(gè)好的雞蛋計(jì)時(shí)器:)

  8. */


  9. //#include <stdint.h>

  10. #include <avr/io.h>

  11. #include <util/delay.h>

  12. #include <avr/interrupt.h>

  13. #include "tm1637.h"



  14. #define        BUTTON_PIN        PB2



  15. #define        TIMER_UPDATE        (1 << 1)

  16. #define        TIMER_STOP        (1 << 2)

  17. #define        TIMER_START        (1 << 3)

  18. #define        TIMER_RESET        (1 << 4)



  19. static volatile uint8_t timer_counter;

  20. static volatile uint8_t timer_events;

  21. static volatile uint8_t timer_seconds;

  22. static volatile uint8_t timer_minutes;

  23. static volatile uint8_t timer_colon;



  24. static void timer_init(void);

  25. static void timer_handler(void);

  26. static void timer_process(void);

  27. static void timer_display(const uint8_t minutes, const uint8_t seconds, const uint8_t colon);



  28. ISR(TIM0_COMPA_vect)

  29. {



  30.         timer_handler();

  31. }



  32. int main(void)

  33. {

  34.         /* setup */

  35.         timer_init();



  36.         /* loop */

  37.         while (1) {

  38.                 timer_process();

  39.         }

  40. }



  41. void  timer_init(void)

  42. {



  43.         TM1637_init(1, 4);

  44.         DDRB &= ~_BV(BUTTON_PIN); //明確設(shè)置按鈕針作為輸入

  45.         PORTB |= _BV(BUTTON_PIN); // 設(shè)置按鈕銷的上拉電阻器

  46.         TCCR0A |= _BV(WGM01); // 將計(jì)時(shí)器計(jì)數(shù)器模式設(shè)置為CTC

  47.         TCCR0B |= _BV(CS01)|_BV(CS00); // 將預(yù)分頻器設(shè)置為64(CLK=1200000Hz/64/250=75Hz)

  48.         OCR0A = 249; // 設(shè)置定時(shí)器計(jì)數(shù)器最大值(250-1)


  49. TIMSK |= _BV(OCIE0A);// 啟用定時(shí)器CTC中斷

  50.         timer_counter = timer_seconds = timer_minutes = 0; // 重置計(jì)數(shù)器

  51.         timer_events = TIMER_UPDATE | TIMER_RESET; // 重置計(jì)時(shí)器和更新顯示

  52.         timer_colon = 1; // 顯示冒號(hào)

  53.         sei(); //啟用全局中斷

  54. }



  55. void  timer_handler(void)

  56. {



  57.         if (!(timer_events & TIMER_START)) {

  58.                 return;

  59.         }



  60.         timer_counter++;

  61.         if (timer_counter == 38) {

  62.                 timer_colon = 1;

  63.                 timer_events |= TIMER_UPDATE;

  64.         } else if (timer_counter == 75) {

  65.                 timer_colon = 0;

  66.                 timer_counter = 0;

  67.                 if (++timer_seconds == 60) {

  68.                         timer_seconds = 0;

  69.                         if (++timer_minutes == 100) {

  70.                                 timer_minutes = 0;

  71.                         }

  72.                 }

  73.                 timer_events |= TIMER_UPDATE;

  74.         }

  75. }



  76. void  timer_process(void)

  77. {



  78.         /* 過程啟動(dòng)/停止/重置 */

  79.         if ((PINB & _BV(BUTTON_PIN)) == 0) {

  80.                 _delay_ms(10); // 去噪時(shí)間

  81.                 while((PINB & _BV(BUTTON_PIN)) == 0);

  82.                 if (timer_events & TIMER_START) {

  83.                         timer_colon = 1;

  84.                         timer_events = TIMER_UPDATE | TIMER_STOP;

  85.                 } else if (timer_events & TIMER_STOP) {

  86.                         timer_minutes = timer_seconds = 0;

  87.                         timer_colon = 1;

  88.                         timer_events = TIMER_UPDATE | TIMER_RESET;

  89.                 } else if (timer_events & TIMER_RESET) {

  90.                         timer_events = TIMER_START;

  91.                 }

  92.         }



  93.         /* 更新顯示 */

  94.         if (timer_events & TIMER_UPDATE) {

  95.                 timer_display(timer_minutes, timer_seconds, timer_colon);

  96.                 timer_events &= ~TIMER_UPDATE;

  97.         }

  98. }



  99. void  timer_display(const uint8_t minutes, const uint8_t seconds, const uint8_t colon)

  100. {



  101.         /* 顯示分鐘數(shù)*/

  102.         TM1637_display_digit(0, minutes / 10);

  103.         TM1637_display_digit(1, minutes % 10);



  104.         /* 顯示秒數(shù) */

  105.         TM1637_display_digit(2, seconds / 10);

  106.         TM1637_display_digit(3, seconds % 10);



  107.         /* 顯示/隱藏冒號(hào) */

  108.         TM1637_display_colon(colon);

  109. }
復(fù)制代碼

51hei下載:
25tm1637.zip (33.26 KB, 下載次數(shù): 55)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表