chipKIT® Development Platform

Inspired by Arduino™

lcd display and timer interrupt hang program

Created Sat, 03 Mar 2012 04:38:07 +0000 by sonic


sonic

Sat, 03 Mar 2012 04:38:07 +0000

This program will eventually contain a countdown timer that will be displayed on an lcd display. Anyway, the timer interrupt functions properly unless the lcd display functions are accessed. At the start of the program "line1" and "line2" are displayed on the lcd and the timer and interrupt will initiate and function as expected. After the timer interrupts are initiated, any lcd display functions called will hang the program. I have tried to write to the lcd with a pushbutton as well as write to the lcd with a function.

//lcd functions hang code

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <sys/attribs.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // define LCD pinout
const int startButtonPin = 26;     // the number of the start pushbutton pin
const int enterButtonPin = 27;     // the number of the enter pushbutton pin
const int plusButtonPin = 28;     // the number of the plus pushbutton pin
const int minusButtonPin = 29;     // the number of the minus pushbutton pin
const int menuButtonPin = 30;     // the number of the menu pushbutton pin
const int Led4 = 13;  // assign Led4 to pin13
const int Led5 = 43;  // assign Led4 to pin13
int startButtonState = 0;
int enterButtonState = 0;
long elapsedTime = 0;
int oneSecond = 0;

void setup() {                
  // initialize the LCD
  lcd.begin(16, 2);
  lcd.println("     line1      ");
  lcd.setCursor(0,1);  // print row 1, column 0
  lcd.println("     line2      ");
  // initialize serial port
  Serial.begin(9600);
  // reserve 200 bytes for the inputString
  //inputString.reserve(200); check reserve function
  // initialize the pushbutton pins as inputs
  pinMode(startButtonPin, INPUT);
  pinMode(enterButtonPin, INPUT);
  pinMode(plusButtonPin, INPUT);
  pinMode(minusButtonPin, INPUT);
  pinMode(menuButtonPin, INPUT);
  // initialize the digital pin as an output
  pinMode(13, OUTPUT);
  pinMode(Led5, OUTPUT);
  delay(2000);
  // Fpb = SYS_FREQ = 80Mhz (From configuration in bootloader code)
  // Timer Prescale = 256
  // PR2 = 0xF423 = 62,499
  // interrupts every 200 ms
  // 200 ms = (PR2 + 1) * TMR Prescale / Fpb = (62499 + 1) * 256 / 80000000
  T2CON =0x0;  // stop timer and clear control register
  T2CONSET = 0x0070;  //set prescaler to 256
  TMR2 = 0x0;  // clear timer register
  PR2 = 0xF423;  // load period register
  IPC2SET = 0x0000000D;  // set priority level to 3 and subpriority level to 1
  IFS0CLR = 0x00000100;  // clear timer interupt status flag
  IEC0SET = 0x00000100;  // enable timer interrupts
  T2CONSET = 0x8000;  // start timer
  digitalWrite(Led4, LOW);
}

void loop() {
  // read the state of the pushbutton value:
  startButtonState = digitalRead(startButtonPin);
  enterButtonState = digitalRead(enterButtonPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (enterButtonState == HIGH) {     
    // test print    
    lcd.println("something");  
  }
  if (oneSecond >= 5) {     
    // turn LED5 on    
    digitalWrite(Led5, HIGH);
    elapsedTime = elapsedTime + 1;
    oneSecond = 0;
    writeTimeRemaining();    
  } 
  else {
    // turn LED5 off
    digitalWrite(Led5, LOW); 
  }
  delay(100);  // makes no difference  
}

void writeTimeRemaining(){
  // does not hang if the following lines are commented
  lcd.home();
  lcd.clear();
  lcd.println(" Time Remaining ");
  lcd.setCursor(0,1);  // print row 1, column 0
  lcd.println(" Time Remaining ");
  // lcd.println(millis());  
}

#ifdef __cplusplus
extern "C" {
#endif

void __ISR(_TIMER_2_VECTOR,IPL3AUTO) comms_handler(void)
{
  //mT2ClearIntFlag();  // Clear interrupt flag
  IFS0CLR = 0x00000100;
  digitalWrite(Led4, HIGH);
  oneSecond++;
}
#ifdef __cplusplus
}
#endif

knChip

Mon, 19 Mar 2012 20:18:50 +0000

did you try toggleing LED4 which is in ISR, does it stop toggling when the board is in "hung" state?


pmourao

Tue, 08 May 2012 17:53:11 +0000

Sonic, did you already put the program run with interrupts and the lcd? thanks