Arduino的庫中本身沒有提供操作定時中斷的功能,但是我們以可以avr開發庫本身的特性來使用Arduino沒有提供的功能。代碼如下, 設置一個1ms的中斷, 每隔1s輸出一個$符號: - #include <avr/io.h>
- #include <avr/interrupt.h>
- // 將定時器中斷設為1ms
- void init_time()
- {
- TCCR2A |= (1 <<WGM21) | (1 << WGM20);
- TCCR2B |= (1 << CS22 ); //by clk/64
- TCCR2B &= ~((1 <<CS21) | (1 <<CS20)); //by clk/64
- TCCR2B &= ~((1 << WGM21 ) | (1 << WGM20));
- ASSR |= ( 1 << AS2 );
- TIMSK2 |= ( 1 << TOIE2 ) | ( 0 << OCIE2B );
- TCNT2 = 6;
- sei();
- }
- int count = 0;
- SIGNAL(SIG_OVERFLOW2)
- {
- TCNT2 = 6;
- ++count;
- if( count == 1000 )
- {
- Serial.print("$ ");
- count=0;
- }
- }
- //設置波特率
- void setup(void)
- {
- Serial.begin(115200);
- init_time();
- }
- //串口
- void loop( void )
- {
- Serial.println("start ... ...");
- while( 1 )
- {
- delay( 100 );
- }
復制代碼
O~J]HXTX50{B7()92SD}A23.png (24.89 KB, 下載次數: 59)
下載附件
2019-4-19 17:56 上傳
|