#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
//write 0 read 1
//command 0 data 1
#define lcd_data PORTD
#define lcd_data_dir DDRD
#define lcd_ctrl PORTB
#define lcd_ctrl_dir DDRB
#define RS PB1
#define RW PB2
#define EN PB3
//set position array
uint8_t position_y[2] = {0,64};
//function prototypes
void peekaboo(void);
void checkbusy(void);
void send_command(unsigned char command);
void send_data(unsigned char data);
void send_string(char *string);
void set_cursor(uint8_t x,uint8_t y);
int main(void)
{
lcd_ctrl_dir |= 1 << RS | 1 << RW | 1 << EN;
_delay_ms(15);
send_command(0x01); //clear lcd
_delay_ms(2);
send_command(0x38); //8-bit mode line-2 5x7 dots
_delay_ms(50);
send_command(0x0E); //display on cursor on
_delay_ms(50);
char positionstring[2];
while(1)
{
for(int y=0;y<2;y++)
{
for(int x=0;x<16;x++)
{
itoa(x,positionstring,10);
set_cursor(7,0);
send_string("X = ");
send_string(positionstring);
itoa(y,positionstring,10);
set_cursor(7,1);
send_string("Y = ");
send_string(positionstring);
set_cursor(x,y);
send_string("x");
_delay_ms(400);
set_cursor(x,y);
send_string(" ");
itoa(x,positionstring,10);
set_cursor(7,0);
send_string(" ");
itoa(y,positionstring,10);
set_cursor(7,1);
send_string(" ");
}
}
}
return 0;
}
void peekaboo(void)
{
lcd_ctrl |= 1 << EN;
asm volatile("nop");
asm volatile("nop");
lcd_ctrl &= ~(1 << EN);
}
void checkbusy(void)
{
lcd_data_dir = 0;
lcd_ctrl |= (1 << RW);
lcd_ctrl &= ~(1 << RS);
while(lcd_data >= 0x80)
{
peekaboo();
}
lcd_data_dir = 0xFF;
}
void send_command(unsigned char command)
{
checkbusy();
lcd_data = command;
lcd_ctrl &= ~(1 << RW | 1 << RS);
peekaboo();
lcd_data = 0;
}
void send_data(unsigned char data)
{
checkbusy();
lcd_data = data;
lcd_ctrl |= (1 << RS);
lcd_ctrl &= ~(1 << RW);
peekaboo();
lcd_data = 0;
}
void send_string(char *string)
{
while(*string > 0) //*string != NULL
{
send_data(*string++);
}
}
void set_cursor(uint8_t x,uint8_t y)
{
send_command(0x80 + position_y[y] + x);
}