http://coopermaa2nd.blogspot.tw/2011/07/4-timers.html
http://coopermaa2nd.blogspot.tw/2011/07/41-blink-with-timer.html
利用time2除頻256 16MHZ 剩62500HZ 1clok要0.000016秒 1ms需62.5個clock約63 63clok=0.001008秒
time2除頻與time0、time1不一樣
CS22 CS21 CS20 功能
0 0 0 停止
0 0 1 clk/1
0 1 0 clk/8
0 1 1 clk/32
1 0 0 clk/64
1 0 1 clk/128
1 1 0 clk/256
1 1 1 clk/1024
volatile unsigned int delay_ms_count; void delay_ms(unsigned int i);
void setup() { // put your setup code here, to run once: DDRB=0b00100000;//設定P13為output TCCR2A=0x00; TCCR2B=0b00000110;//設定除頻256 TCNT2=-63; }
void loop() { // put your main code here, to run repeatedly: //設定P13 hight PORTB =PORTB | 0B00100000; delay_ms(1000); //設定P13 low PORTB=PORTB & 0B11011111; delay_ms(1000); }
void delay_ms(unsigned int i) { delay_ms_count=i; TCNT2=-63; TIMSK2 |=0x01;//啟動中斷time2 while(delay_ms_count > 0){ } TIMSK2 &=0xfe;//關閉中斷time2 }
ISR(TIMER2_OVF_vect) { delay_ms_count--; TCNT2=-63; }
沒有留言:
張貼留言