找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4171|回复: 0
打印 上一主题 下一主题
收起左侧

avr单片机秒表Proteus仿真程序 tm1637数码管显示

[复制链接]
跳转到指定楼层
楼主
Proteus仿真电路图如下:


单片机源程序如下:
  1. /**

  2. *版权所有(c)2018,艁ukasz Marcin Podkalicki
  3. *2009年12月13日            
  4. *简单定时器(启动/复位/停止),使用基于TM1637的一个按钮和7段显示模块。              *            
  5. *注意,这个ATtiny13项目使用的内部时钟并不精确            
  6. *时间可以向前或向后流动,但是嘿!            
  7. *它仍然足够做一个好的鸡蛋计时器:)

  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); //明确设置按钮针作为输入

  45.         PORTB |= _BV(BUTTON_PIN); // 设置按钮销的上拉电阻器

  46.         TCCR0A |= _BV(WGM01); // 将计时器计数器模式设置为CTC

  47.         TCCR0B |= _BV(CS01)|_BV(CS00); // 将预分频器设置为64(CLK=1200000Hz/64/250=75Hz)

  48.         OCR0A = 249; // 设置定时器计数器最大值(250-1)


  49. TIMSK |= _BV(OCIE0A);// 启用定时器CTC中断

  50.         timer_counter = timer_seconds = timer_minutes = 0; // 重置计数器

  51.         timer_events = TIMER_UPDATE | TIMER_RESET; // 重置计时器和更新显示

  52.         timer_colon = 1; // 显示冒号

  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.         /* 过程启动/停止/重置 */

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

  80.                 _delay_ms(10); // 去噪时间

  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.         /* 显示分钟数*/

  102.         TM1637_display_digit(0, minutes / 10);

  103.         TM1637_display_digit(1, minutes % 10);



  104.         /* 显示秒数 */

  105.         TM1637_display_digit(2, seconds / 10);

  106.         TM1637_display_digit(3, seconds % 10);



  107.         /* 显示/隐藏冒号 */

  108.         TM1637_display_colon(colon);

  109. }
复制代码

51hei下载:
25tm1637.zip (33.26 KB, 下载次数: 53)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表