My final prototype consists of an ATmega328-AU (the SMT version). Due to layout problems, I've been forced to change some pins compared to previous (completely) working setup.
Everything works fine, except the touchscreen of the 2.8" TFT. I'm using the Adafruit_ILI9341 library to use the LCD and the UTouch library to get the information from the touchscreen.
I've figured out that the function touch.dataAvailable() causes the project to "crash": the LCD becomes white. I'm using digital pin 5 as T_IRQ (the pin dataAvailable is called on).
The function touch.dataAvailable() comes down to the following:
void UTouch::InitTouch(byte orientation)
{
...
...
P_IRQ = portInputRegister(digitalPinToPort(T_IRQ));
B_IRQ = digitalPinToBitMask(T_IRQ);
...
pinMode(T_IRQ, OUTPUT);
...
sbi(P_IRQ, B_IRQ);
}
bool UTouch::dataAvailable()
{
bool avail;
pinMode(T_IRQ, INPUT);
avail = !(rbi(P_IRQ, B_IRQ));
pinMode(T_IRQ, OUTPUT);
return avail;
}
Just calling dataAvailable() already crashes the system. I don't know what causes this. Even commenting out all other code doesn't help.
So, to summarize:
- Using barebone ATmega328-AU with internal clock and variant "eightanaloginputs"
- 8 MHz on 3.3V
- Everything works fine, except dataAvailable on pin 5, I can't check if it works on other pins
- If I comment out the if (touch.dataAvailable()) loop, the sketch runs without problems
Below is my sketch:
/*
EEPROM usage:
0: last frequency from previous run (unsigned int)
1: amount of saved stations (byte)
2: array of saved stations
- 2.1a Name of station (8 bytes)
- 2.1b Frequency of station (unsigned int)
...
- 2.8a Name of station (8 bytes)
- 2.8b Frequency of station (unsigned int)
*/
#include <EEPROMex.h>
#include <Arduino.h>
#include <Wire.h>
#include <radio.h>
#include <RDA5807M.h>
#include <RDSParser.h>
#include <SoftwareSerial.h>
#include <UTouch.h>
#define LOG_OUT 1
#define FHT_N 32
#include <FHT.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
//Adafruit_ILI9341 tft = Adafruit_ILI9341(10, 2, 3);
Adafruit_ILI9341 tft = Adafruit_ILI9341(10, A0, A1);
//UTouch touch = UTouch(6, 7, 8, A3, 9);
UTouch touch = UTouch(9, 8, 7, 6, 5);
int x, y;
RDA5807M radio;
RDSParser rds;
#define BINS 15
byte spectrum[BINS], spectrum_previous[BINS];
unsigned int frequency = 0, frequency_old = 0;
byte signal = 0, signal_old = 0;
RADIO_INFO info;
unsigned long info_timer_counter = 0, info_timer_interval = 200;
byte seeking = 0;
char rds_text[64 + 2];
boolean rds_text_active = false;
int text_position = 0, text_iterator = 0;
unsigned long text_timer_counter = 3, text_timer_interval = 5000;
int address_frequency, address_stations, address_stations_name[8], address_stations_frequency[8];
void setup() {
address_frequency = EEPROM.getAddress(sizeof(int));
address_stations = EEPROM.getAddress(sizeof(byte));
for (byte i = 0; i < 8; i ++) {
address_stations_name[i] = EEPROM.getAddress(sizeof(byte)*10);
address_stations_frequency[i] = EEPROM.getAddress(sizeof(int));
}
EEPROM.updateInt(address_frequency, 9960);
EEPROM.updateByte(address_stations, 1);
EEPROM.writeBlock(address_stations_name[0], "3FM", 9);
EEPROM.writeInt(address_stations_frequency[0], 9960);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setRotation(3);
touch.InitTouch();
touch.setPrecision(PREC_LOW);
radio.init();
frequency = (unsigned int)EEPROM.readInt(address_frequency);
radio.setBandFrequency(RADIO_BAND_FM, frequency);
radio.setVolume(10); //360 mV per level
radio.setMono(false);
radio.setMute(false);
radio.attachReceiveRDS(rdsProcess);
rds.attachServicenNameCallback(serviceName);
rds.attachTextCallback(text);
...
//TIMSK0 = 0; // turn off timer0 for lower jitter
ADCSRA = 0xe5; // set the adc to free running mode
ADMUX = 0x47; // use adc0
//DIDR0 = 0x01;
}
void loop() {
...
if (touch.dataAvailable()) {
touch.read();
unsigned short x = 319 - touch.getX(), y = 219 - touch.getY();
if (x != 320 && y != 240) {
//tft.fillCircle(319 - touch.getX(), 239 - touch.getY(), 2, ILI9341_RED);
if (x < 120 && seeking == 0) {
clearServiceName();
clearText();
radio.seekDown(true);
seeking = 1;
} else if (x > 200 && seeking == 0) {
clearServiceName();
clearText();
radio.seekUp(true);
seeking = 2;
} else if (seeking == 0) {
menu();
}
}
}
...
}
[/li]