|
這是我最近寫的程式,但是無法中斷,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
} |
|