Proteus仿真電路圖如下:
tm1637.gif (1.04 MB, 下載次數: 73)
下載附件
2020-4-14 12:05 上傳
單片機源程序如下:- /**
- *版權所有(c)2018,艁ukasz Marcin Podkalicki
- *2009年12月13日
- *簡單定時器(啟動/復位/停止),使用基于TM1637的一個按鈕和7段顯示模塊。 *
- *注意,這個ATtiny13項目使用的內部時鐘并不精確
- *時間可以向前或向后流動,但是嘿!
- *它仍然足夠做一個好的雞蛋計時器:)
- */
- //#include <stdint.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include "tm1637.h"
- #define BUTTON_PIN PB2
- #define TIMER_UPDATE (1 << 1)
- #define TIMER_STOP (1 << 2)
- #define TIMER_START (1 << 3)
- #define TIMER_RESET (1 << 4)
- static volatile uint8_t timer_counter;
- static volatile uint8_t timer_events;
- static volatile uint8_t timer_seconds;
- static volatile uint8_t timer_minutes;
- static volatile uint8_t timer_colon;
- static void timer_init(void);
- static void timer_handler(void);
- static void timer_process(void);
- static void timer_display(const uint8_t minutes, const uint8_t seconds, const uint8_t colon);
- ISR(TIM0_COMPA_vect)
- {
- timer_handler();
- }
- int main(void)
- {
- /* setup */
- timer_init();
- /* loop */
- while (1) {
- timer_process();
- }
- }
- void timer_init(void)
- {
- TM1637_init(1, 4);
- DDRB &= ~_BV(BUTTON_PIN); //明確設置按鈕針作為輸入
- PORTB |= _BV(BUTTON_PIN); // 設置按鈕銷的上拉電阻器
- TCCR0A |= _BV(WGM01); // 將計時器計數器模式設置為CTC
- TCCR0B |= _BV(CS01)|_BV(CS00); // 將預分頻器設置為64(CLK=1200000Hz/64/250=75Hz)
- OCR0A = 249; // 設置定時器計數器最大值(250-1)
- TIMSK |= _BV(OCIE0A);// 啟用定時器CTC中斷
- timer_counter = timer_seconds = timer_minutes = 0; // 重置計數器
- timer_events = TIMER_UPDATE | TIMER_RESET; // 重置計時器和更新顯示
- timer_colon = 1; // 顯示冒號
- sei(); //啟用全局中斷
- }
- void timer_handler(void)
- {
- if (!(timer_events & TIMER_START)) {
- return;
- }
- timer_counter++;
- if (timer_counter == 38) {
- timer_colon = 1;
- timer_events |= TIMER_UPDATE;
- } else if (timer_counter == 75) {
- timer_colon = 0;
- timer_counter = 0;
- if (++timer_seconds == 60) {
- timer_seconds = 0;
- if (++timer_minutes == 100) {
- timer_minutes = 0;
- }
- }
- timer_events |= TIMER_UPDATE;
- }
- }
- void timer_process(void)
- {
- /* 過程啟動/停止/重置 */
- if ((PINB & _BV(BUTTON_PIN)) == 0) {
- _delay_ms(10); // 去噪時間
- while((PINB & _BV(BUTTON_PIN)) == 0);
- if (timer_events & TIMER_START) {
- timer_colon = 1;
- timer_events = TIMER_UPDATE | TIMER_STOP;
- } else if (timer_events & TIMER_STOP) {
- timer_minutes = timer_seconds = 0;
- timer_colon = 1;
- timer_events = TIMER_UPDATE | TIMER_RESET;
- } else if (timer_events & TIMER_RESET) {
- timer_events = TIMER_START;
- }
- }
- /* 更新顯示 */
- if (timer_events & TIMER_UPDATE) {
- timer_display(timer_minutes, timer_seconds, timer_colon);
- timer_events &= ~TIMER_UPDATE;
- }
- }
- void timer_display(const uint8_t minutes, const uint8_t seconds, const uint8_t colon)
- {
- /* 顯示分鐘數*/
- TM1637_display_digit(0, minutes / 10);
- TM1637_display_digit(1, minutes % 10);
- /* 顯示秒數 */
- TM1637_display_digit(2, seconds / 10);
- TM1637_display_digit(3, seconds % 10);
- /* 顯示/隱藏冒號 */
- TM1637_display_colon(colon);
- }
復制代碼
51hei下載:
25tm1637.zip
(33.26 KB, 下載次數: 55)
2020-4-14 12:09 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|