ivan805 發表於 2016-8-3 17:28:55

以timer1 OCIE1A 中斷方式,在LCD.print 無法顯示

各位大大,請問一下


我打了個簡單的代碼,
僅僅是要在LCD顯示一行字 hello, world 2,問題是~~ 都顯示不出來?!


因我有其他的需求,
所以,我是用中斷的方式,來叫LCD顯示,
OCIE1A 致能 ''比較中斷'',去執行上述功能,
但怎麼都無法顯示呢?


代碼很簡單,沒幾行,真的找不出來哪裡有錯,麻煩各位先進,指教一下.
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

ISR (TIMER1_COMPA_vect) {
lcd.setCursor(0, 1);
lcd.print("hello world 2");
}

void setup() {
lcd.init();
lcd.backlight();
lcd.print("hello world");

noInterrupts();
TCCR1A = 0x00;
TCCR1B = 0x00;
TCNT1 = 0;
OCR1A = 15625;//compare register放入15625, 計等於1秒的時間
TCCR1B |= (1<<WGM12); //ctc mode
TCCR1B |= (1<<CS12) | (1<<CS10);//prescaler=1024
TIMSK1 |= (1<<OCIE1A);
interrupts();
}

void loop() {
}

超新手 發表於 2016-8-3 20:18:30

我猜應該是 lcd 用到 i2c
而i2c用到 twi 中斷
然後你在timer1中斷裡等待
twi 中斷
所以就卡死在中斷中

ivan805 發表於 2016-8-4 00:01:11

回復 2# 超新手
哦~ 這樣哦~那我改一下lcd好了,不要用I2C的方式,
試試看,結果如何,
再上來跟大家update.
感謝這位大大

超新手 發表於 2016-8-4 05:17:38

可以試試加上 interrupts,如下
ISR (TIMER1_COMPA_vect) {
interrupts();
lcd.setCursor(0, 1);
lcd.print("hello world 2");
}

ivan805 發表於 2016-8-4 15:04:07

已試,ok了!
果然是這邊的問題,
再次感謝大大的協助,感謝您的熱心.
頁: [1]
查看完整版本: 以timer1 OCIE1A 中斷方式,在LCD.print 無法顯示