|
Proteus仿真電路圖如下:
單片機(jī)源程序如下:- /**
- *版權(quán)所有(c)2018,艁ukasz Marcin Podkalicki
- *2009年12月13日
- *簡單定時(shí)器(啟動(dòng)/復(fù)位/停止),使用基于TM1637的一個(gè)按鈕和7段顯示模塊。 *
- *注意,這個(gè)ATtiny13項(xiàng)目使用的內(nèi)部時(shí)鐘并不精確
- *時(shí)間可以向前或向后流動(dòng),但是嘿!
- *它仍然足夠做一個(gè)好的雞蛋計(jì)時(shí)器:)
- */
- //#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); //明確設(shè)置按鈕針作為輸入
- PORTB |= _BV(BUTTON_PIN); // 設(shè)置按鈕銷的上拉電阻器
- TCCR0A |= _BV(WGM01); // 將計(jì)時(shí)器計(jì)數(shù)器模式設(shè)置為CTC
- TCCR0B |= _BV(CS01)|_BV(CS00); // 將預(yù)分頻器設(shè)置為64(CLK=1200000Hz/64/250=75Hz)
- OCR0A = 249; // 設(shè)置定時(shí)器計(jì)數(shù)器最大值(250-1)
- TIMSK |= _BV(OCIE0A);// 啟用定時(shí)器CTC中斷
- timer_counter = timer_seconds = timer_minutes = 0; // 重置計(jì)數(shù)器
- timer_events = TIMER_UPDATE | TIMER_RESET; // 重置計(jì)時(shí)器和更新顯示
- 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)
- {
- /* 過程啟動(dòng)/停止/重置 */
- if ((PINB & _BV(BUTTON_PIN)) == 0) {
- _delay_ms(10); // 去噪時(shí)間
- 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)
- {
- /* 顯示分鐘數(shù)*/
- TM1637_display_digit(0, minutes / 10);
- TM1637_display_digit(1, minutes % 10);
- /* 顯示秒數(shù) */
- TM1637_display_digit(2, seconds / 10);
- TM1637_display_digit(3, seconds % 10);
- /* 顯示/隱藏冒號 */
- TM1637_display_colon(colon);
- }
復(fù)制代碼
51hei下載:
25tm1637.zip
(33.26 KB, 下載次數(shù): 55)
2020-4-14 12:09 上傳
點(diǎn)擊文件名下載附件
|
評分
-
查看全部評分
|