tahertinu
Member level 4
- Joined
- Jun 11, 2011
- Messages
- 74
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Location
- Ahmedabad
- Activity points
- 1,752
hello every-buddy,
I am working on project interfacing touch screen with ATmega32. i have used this code
IT'S not working please tell me where im wrong?
I am working on project interfacing touch screen with ATmega32. i have used this code
Code:
#ifndef F_CPU
#define F_CPU 8000000UL // or whatever may be your frequency
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#include "lcd.h"
#define LTHRES 500
#define RTHRES 500
// initialize adc
void adc_init()
{
// AREF = AVcc
ADMUX = (1<<REFS0);
// ADC Enable and prescaler of 128
// 16000000/128 = 125000
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
// read adc value
uint16_t adc_read(uint8_t ch)
{
// select the corresponding channel 0~7
// ANDing with '7' will always keep the value
// of 'ch' between 0 and 7
ch &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
// start single conversion
// write '1' to ADSC
ADCSRA |= (1<<ADSC);
// wait for conversion to complete
// ADSC becomes '0' again
// till then, run loop continuously
while(ADCSRA & (1<<ADSC));
return (ADC);
}
int main()
{
uint16_t adc_result0, adc_result1;
char int_buffer[10];
DDRC = 0x01; // to connect led to PC0
// initialize adc and lcd
adc_init();
lcd_init();
// print some text
lcd_set_cursor(0, 16);
lcd_putstr("128x64 LCD");
lcd_set_cursor(2, 16);
lcd_putstr("HELLO");
lcd_set_cursor(3, 16);
lcd_putstr("WORLD");
_delay_ms(500);
while(1)
{ DDRA |= (1<<DDA0);
DDRA |= (1<<DDA2);
DDRA &= ~(1<<DDA3);
PORTA |= (1<<PA2);
PORTA &= ~(1<<PA2);
adc_result0 = adc_read(1); // read X
DDRA |= (1<<DDA0);
DDRA |= (1<<DDA2);
DDRA &= ~(1<<DDA3);
PORTA |= (1<<PA2);
PORTA &= ~(1<<PA2);
adc_result1 = adc_read(0); // read Y
// condition for led to glow
if (adc_result0 < LTHRES && adc_result1 < RTHRES)
PORTC = 0x01;
else
PORTC = 0x00;
// now display on lcd
itoa(adc_result0, int_buffer, 10);
lcd_set_cursor(4, 11);
lcd_putstr(int_buffer);
itoa(adc_result1, int_buffer, 10);
lcd_set_cursor(5, 11);
lcd_putstr(int_buffer);
_delay_ms(50);
}
}