#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <string.h>
#define RS PB1
#define E PB0
#define LCD_RS_SIGNAL 0 //PD0 pin37 of uC to LCD RS - pin 4 of LCD
#define LCD_RW_SIGNAL 1 //PD1 pin36 of uC (don't connect) to LCD R/W - currently grounded
#define LCD_ENABLE_SIGNAL 2 //PD2 pin36 of uC to LCD E - pin 6 of LCD
#define LCD_C4_SIGNAL 4 //PC4 pin33 of uC to LCD D3 - pin 11 of LCD
#define LCD_C5_SIGNAL 5 //PC5 pin32 of uC to LCD D4 - pin 12 of LCD
#define LCD_C6_SIGNAL 6 //PC6 pin31 of uC to LCD D5 - pin 13 of LCD
#define LCD_C7_SIGNAL 7 //PC7 pin30 of uC to LCD D6 - pin 14 of LCD
#define LCD_dir DDRB
#define LCD_Port PORTB
void LCDcmd(unsigned char cmd) // command mode
{
LCD_Port =(LCD_Port&0x0f)|(cmd&0xf0); // set the first 4 high bit to LCDport
LCD_Port &=~(1<<RS);
LCD_Port |=(1<<E);
_delay_us(1);
LCD_Port&=~(1<<E);
_delay_us(200);
LCD_Port =(LCD_Port&0x0f)|(cmd<<4); // set the first 4 low bit to LCDport
LCD_Port |=(1<<E);
_delay_us(1);
LCD_Port&=~(1<<E);
_delay_ms(2);
}
void LCDchar(unsigned char data) //send data
{
LCD_Port =(LCD_Port&0x0f)|(data&0xf0); // set the first 4 high bit of data to LCDport
LCD_Port |=(1<<RS);
LCD_Port |=(1<<E);
_delay_us(1);
LCD_Port&=~(1<<E);
_delay_us(200);
LCD_Port =(LCD_Port&0x0f)|(data<<4); // set the first 4 low bit to LCDport
LCD_Port |=(1<<E);
_delay_us(1);
LCD_Port&=~(1<<E);
_delay_ms(2);
}
void LCD_string(char *s, int LCD_line, int LCD_cursor_position)
{
if(LCD_line==1)
LCDcmd(0x80+LCD_cursor_position);
else LCDcmd(0xc0+LCD_cursor_position);
while (*s)
LCDchar(*s++);
}
void LCD_cursor_blink()
{
LCDcmd(0x0f);
}
void LCD_float_string(float LCD_number, int LCD_line, int LCD_cursor_position)
{
char buffer_LCD[13];
dtostre(LCD_number, buffer_LCD,5, 4);
LCD_string(buffer_LCD,LCD_line,LCD_cursor_position);
}
void LCD_int_string(int LCD_number, int LCD_line, int LCD_cursor_position)
{
char buffer_LCD[10];
itoa(LCD_number, buffer_LCD, 10);
LCD_string(buffer_LCD, LCD_line, LCD_cursor_position);
}
void LCDinit()
{
LCD_dir =0xff;
_delay_ms(20);
LCDcmd(0x33);
LCDcmd(0x32);
LCDcmd(0x01);
LCDcmd(0x28);
LCDcmd(0x0C);
LCDcmd(0x06);
LCDcmd(0x1c);
_delay_ms(2);
LCDcmd(0x80);
}
void LCD_String (char *str) /* Send string to LCD function */
{
//int i;
//for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
//{
//LCDchar (str[i]);
//}
while (*str)
LCDchar(*str++);
}
void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD with xy position */
{
if (row == 0 && pos<16)
LCDcmd((pos & 0x0F)|0x80); /* Command of first row and required position<16 */
else if (row == 1 && pos<16)
LCDcmd((pos & 0x0F)|0xC0); /* Command of first row and required position<16 */
LCD_String(str); /* Call LCD string function */
}
void LCD_clear()
{
LCDcmd(0x01);
_delay_ms(2);
LCDcmd(0x80);
}
void LCD_home()
{
LCDcmd(1<<1);
}
void ADC_init()
{
DDRA=0x00;
ADCSRA=0;
ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(1<<ADEN)|(1<<ADIE);; //adc clock=freq/64, enable ADC,
ADMUX|=(0<<REFS1)|(1<<REFS0)|(1<<ADLAR)|(0<<MUX4)|(0<<MUX3)|(0<<MUX0)|(0<<MUX1)|(0<<MUX2); // set pin A0 as analog input
}
int ADC_read()
{
ADCSRA|=(1<<ADSC);
while(!(ADCSRA&(1<<ADIF)))
{
ADCSRA|=(1<<ADIF);
_delay_ms(1);
return ADCW;
}
}
void T1delay()
{
TCNT1=0x00;
OCR1A=3125;
TCCR1A=0x00;
TCCR1A|=(1<<WGM01);
TCCR1B=0x00;
TCCR1B|=(1<<CS12);
while((TIFR1&(1<<TOV1))==0);
TCCR1B=0x00;
TIFR1|=(1<<TOV1);
}
void port_init(){
EICRA=0x00;
DDRD=0x00;
PORTD|=(1<<PD0);
EICRA|=(1<<ISC01);
EIMSK|=(1<<INT0);
}
void init_devices(){
cli();
port_init();
sei();
}
int main()
{
int value_check =0;
float time=0;
//LCDinit();
//DDRF=0x00;
while(1){
}
}
SIGNAL(INT0_vect, ISR_NOBLOCK){
init_devices();
LCDinit();
int count =0;
while((EIMSK&(1<<INT0))==1)
{
LCD_string("The timer is counting:", 1, 1)
LCD_int_string(count, 2, 1);
T1delay();
count++;
}
}