Hello,
I am using a TFT touchscreen (Adafruit's one) and I am trying to implement a timer interrupt to run through several for loops once a second but the interrupts are (I think) being used for the touch screen. I am using the mega 2560 which has additional timers and I cannot figure out how to configure the timers to use them instead of timer0 timer1 timer2 which I believe the touch screen uses in the background. I am posting my code and the timers are included just below the header "Interrupts"
Please let me know what I need to change to use the additional timers in the MEGA
/*********************************************************************************************************
* Include Statements
/********************************************************************************************************/
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#include <Timer.h>
/*********************************************************************************************************
* Define Statements
/********************************************************************************************************/
#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 7 // can be a digital pin
#define XP 6 // can be a digital pin
//The min and max pixels of touch screen
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define S3_pin 53
#define S2_pin 51
#define S1_pin 49
#define S0_pin 47
#define E0_pin 45
#define E1_pin 43
#define E2_pin 41
#define E4_pin 39
Adafruit_TFTLCD tft;
#define BOXSIZE 100
/*********************************************************************************************************
* Varaible Declaration Statements
/********************************************************************************************************/
int Isready = 0; // ready to start building
int screen = 0; // selects the screen
int Analog_voltage; //Analog voltage read
/*********************************************************************************************************
* Arduino Setup Statements
*
/********************************************************************************************************/
void setup(void) {
Serial.begin(9600);
/**********************************************************************************************************
* Turn on Timer ISR
***********************************************************************************************************/
cli();//stop interrupts
//set timer0 interrupt at 2kHz
TCCR0A = 0;// set entire TCCR2A register to 0
TCCR0B = 0;// same for TCCR2B
TCNT0 = 0;//initialize counter value to 0
// set compare match register for 2khz increments
OCR0A = 124;// = (16*10^6) / (2000*64) - 1 (must be <256)
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS11 and CS10 bits for 64 prescaler
TCCR0B |= (1 << CS11) | (1 << CS10);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
//set timer2 interrupt at 8kHz
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
// set compare match register for 8khz increments
OCR2A = 249;// = (16*10^6) / (8000*8) - 1 (must be <256)
// turn on CTC mode
TCCR2A |= (1 << WGM21);
// Set CS11 bit for 8 prescaler
TCCR2B |= (1 << CS11);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei();//allow interrupts
// reset something?
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
pinMode(13, OUTPUT);
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
/*********************************************************************************************************
/ Arduino Loop Statements
/*******************************************************************************************************/
void loop()
{
digitalWrite(13, HIGH);// Get point on TFT board
Point p = ts.getPoint();
digitalWrite(13, LOW);
// if sharing pins, you'll need to fix the directions of the touchscreen pins
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
Serial.print("\tPressure = "); Serial.println(p.z);
// scale from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
//Setup Complete
//Start Screen
if (screen == 0){
Isready = 0;
tft.fillScreen(BLACK);
tft.fillRect(20, 20, 200, 40, RED);
tft.drawRect(20, 20, 200, 40, WHITE);
tft.setCursor(30, 25);
tft.setTextColor(WHITE); tft.setTextSize(2);
tft.println("START BULILDING");
if (((20 < p.y) && (p.y < 60 )) && ((20 < p.x) && (p.x < 220))) {
Serial.println("hello screen 0");
screen = 1;
delay(100);
}
else{
Serial.println("Not Red Button");
}
}
else if (screen == 1){
Isready = 1;
tft.fillScreen(BLACK);
tft.fillRect(20, 20, 200, 40, GREEN);
tft.drawRect(20, 20, 200, 40, WHITE);
tft.setCursor(40, 30);
tft.setTextColor(WHITE); tft.setTextSize(2);
tft.println(" Ghost Block");
tft.fillRect(20, 80, 200, 40, BLUE);
tft.drawRect(20, 80, 200, 40, WHITE);
tft.setCursor(40, 90);
tft.setTextColor(WHITE); tft.setTextSize(2);
tft.println(" UnGhost Block");
if (((20 < p.y) && (p.y < 60 )) && ((20 < p.x) && (p.x < 220))) {
Serial.println("hello screen 1");
screen = 1;
delay(100);
}
else if (((80 < p.y) && (p.y < 120 )) && ((20 < p.x) && (p.x < 220))) {
Serial.println("hello screen 3");
screen = 0;
delay(100);
}
else{
Serial.println("Not green Button");
}
}// else if screen = 1
}//If pressure is correct
}
//timer1 interrupt 1Hz
ISR(TIMER1_COMPA_vect){
for(int quadrent = 0; quadrent < 4; quadrent++){
for(int channel= 0; channel < 16; channel++){
if (quadrent == 0){
set_channel(channel); // turns on channel
Serial.println(quadrent);
int Analog_voltage = analogRead(A15);
}
else if (quadrent == 1){
set_channel(channel); // turns on channel
int Analog_voltage = analogRead(A14);
Serial.println(quadrent);
}
else if (quadrent == 2){
set_channel(channel); // turns on channel
int Analog_voltage = analogRead(A13);
Serial.println(quadrent);
}
else {
set_channel(channel); // turns on channel
int Analog_voltage = analogRead(A12);
Serial.println(quadrent);
delay(1000);
}
}//second for loop
}// first for loop
}//Interrupt
/*********************************************************************************************************
/ Function Statements
/********************************************************************************************************/
Thanks for your time