Robofun 機器人論壇
標題:
新手請教多棵LED同時閃爍後計時中斷~~誰能幫我看看><
[打印本頁]
作者:
j246810j
時間:
2015-9-9 14:53
標題:
新手請教多棵LED同時閃爍後計時中斷~~誰能幫我看看><
這是我最近寫的程式,但是無法中斷,update後怎麼計時中斷,換下個燈號,另外我有用到74hc595 所以是用shift()
unsigned long previous_time;
int latchPin = 26;
int clockPin = 24;
int dataPin = 22;
#define ULONG_MAX 0xffffffffL
#define latchPin2 A1
#define clockPin2 A0
#define dataPin2 A2
volatile unsigned long Count = 0;
void setup() {
// 將 latchPin, clockPin, dataPin 設置為輸出
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
previous_time = millis();
Serial.begin(9600);
TCCR1A = 0x00; // Normal mode, just as a Timer
TCCR1B |= _BV(CS12); // prescaler = CPU clock/1024
TCCR1B &= ~_BV(CS11);
TCCR1B |= _BV(CS10);
TIMSK1 |= _BV(TOIE1); // enable timer overflow interrupt
TCNT1 = -15625; // Ticks for 1 second @16 MHz,prescale=1024
}
void update2(unsigned long time_passed){
static unsigned long time = 0;
time += time_passed;
if((time / 200) % 2 == 0){ // 第一組的LED明滅頻率為0.2秒
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 1);
digitalWrite(latchPin, HIGH);
}
else{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
}
void update3(unsigned long time_passed){
static unsigned long time = 0;
time += time_passed;
if((time / 300) % 2 == 0){ // 第二組LED明滅頻率為0.3秒
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, 1);
digitalWrite(latchPin2, HIGH);
}
else{
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
digitalWrite(latchPin2, HIGH);
}
}
void loop() {
unsigned long current_time = millis();
unsigned long time_passed;
if(current_time > previous_time ){
time_passed = current_time - previous_time;
}
else{
time_passed = ULONG_MAX - previous_time + current_time;
}
if(time_passed >= 100){
update2(time_passed);
update3(time_passed);
previous_time = current_time;
}
}
ISR (TIMER1_COMP_vect)
{
Count++;
if (Count == 5) { //5秒後中斷
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
digitalWrite(latchPin2, HIGH);
}
TCNT1 = -15625; // Ticks for 1 second @16 MHz,prescale=1024
}
作者:
超新手
時間:
2015-9-10 08:37
你致能的是 OVERFLOW 的中斷
TIMSK1 |= _BV(TOIE1);
但用的郤是 COMPARE 中斷
ISR (TIMER1_COMP_vect)
所以只要把 ISR (TIMER1_COMP_vect)
改為 ISR (TIMER1_OVF_vect)
就可以了
作者:
j246810j
時間:
2015-9-11 15:59
回復
2#
超新手
還是無法停止,我有個想法是會不會是用到mills() 導致不能使用count來處理 或者isr內程式太複雜, 到底要如何閃爍一段時間來中斷做另一件事?求救
作者:
超新手
時間:
2015-9-11 17:16
如果只是要做另一件事
不用那麼麻煩
把中斷都拿掉(含SETUP 中的致能)
然後LOOP改成
if(time_passed >= 100) {
if(Count >= 50) {
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
digitalWrite(latchPin2, HIGH);
}
else {
++Count;
update2(time_passed);
update3(time_passed);
}
previous_time = current_time;
}
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2