Source file: /~heha/Mikrocontroller/Displays/utft/Kalender.zip/ILI9341/TouchScreen2.cpp

#include "TouchScreen.h"
// Diese Version für "2.4″TFT LCD SHIELD" mit D0 und D1

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>

//#define NUMSAMPLES 2

// Interruptbedienroutine für Pin-Change (Touchscreen)
// Tut nichts als den Prozessor aufzuwecken

EMPTY_INTERRUPT(PCINT0_vect);
EMPTY_INTERRUPT(ADC_vect);

void TouchScreen::Init() {	// nichts tun!
// Vorausgesetzt sei, dass die beteiligten Pins für das Display
// bereits initialisiert (für Ausgabe) sind
 PCMSK0|=0x01;	// PCINT0 heißmachen
}

// "Ruhezustand" aktivieren, Portpins sind noch Ausgang
static void setIdling() {
 DDRB&=~0x01; PORTB|= 0x01;	// X- = Hi-Z
	      PORTC&=~0x08;	// Y- = L
 DDRC&=~0x04; PORTC&=~0x04;	// X+ = Z
	      PORTB&=~0x02;	// Y+ = L
 _delay_us(0.5);
}

// Ausgangszustand wiederherstellen
static void restorePorts() {
 PORTC|= 0x04;	// RS high oder low??
 PORTC&=~0x08;	// CS high oder low??
 DDRB |= 0x01;	// X- (D0): Ausgang
 DDRC |= 0x08;	// Y- (CS): Ausgang
 DDRC |= 0x04;	// X+ (RS): Ausgang
 DDRB |= 0x02;	// Y+ (D1): Ausgang
}

bool TouchScreen::isTouching() {
 setIdling();
 bool ret=!(PINB&0x01);	// Drück-Zustand einlesen (TRUE wenn LOW)
 restorePorts();
 return ret;
}

uint16_t TouchScreen::readTouch(uint8_t b) {
 ADCSRA=0x9F;	// A/D-Wandler aktivieren mit Teiler 128 -> Taktfrequenz 125 kHz
 if (b) {	// Y messen
  DDRB&=~0x01; PORTB&=~0x01;	// X- = Z
  DDRC|= 0x08; PORTC&=~0x08;	// Y- = L
  DDRC&=~0x04; PORTD&=~0x04;	// X+ = Z
  DDRB|= 0x02; PORTB|= 0x02;	// Y+ = H
  ADMUX=0x42;	// ADC2, sonst wie unten
 }else{		// X messen
  DDRB|= 0x01; PORTB&=~0x01;	// X- = L
  DDRC&=~0x08; PORTC&=~0x08;	// Y- = Z
  DDRC|= 0x04; PORTC|= 0x04;	// X+ = H
  DDRB&=~0x02; PORTB&=~0x02;	// Y+ = Z
  ADMUX=0x43;	// ADC3, Referenz 5 V, rechts ausgerichtet
 }
 _delay_us(0.5);
 ADCSRA=0xDF;	// A/D-Wandler starten mit Interrupt
 do sleep_cpu();
 while (ADCSRA&0x40);	// warten bis fertig
 uint16_t ret=ADC;
 ADCSRA=0;	// Keine weiteren Interrupts
 restorePorts();
 return ret;
}

// Hier: Nur für Argument -2: Sleep
bool TouchScreen::wait(int ms) {
 setIdling();
 if (PINB&0x01) {	// Finger nicht drauf?
  PCIFR=0x01;	// Evtl. vorhandenen anhängigen Interrupt löschen
  uint8_t o=PCICR;
  PCICR=o|0x01;	// Pegelwechselinterrupt für Touchscreen aktivieren
  sleep_cpu();
  PCICR=o;
 }
 restorePorts();
 return false;
}
Detected encoding: UTF-80