void init_port(void);
void out_data(char);
void out_ctrl(char);
void delay1(int);
void lcd_ctrl_write(void);
void init_lcd(void);
void write_data(char);
void write_string(char *ptr);
void setup() {
// put your setup code here, to run once:
init_port();
init_lcd();
write_string("Hello World!");
}
void write_string(char *ptr)
{
while(*ptr != 0)
{
write_data(*ptr);
ptr++;
}
}
void init_port()
{
volatile char* portf_dir = (volatile char*) 0x30;
volatile char* portk_dir = (volatile char*) 0x107;
*portf_dir = 0xFF; // Set PORTF as output
*portk_dir = 0x03; // Set PORTK lower 2 bits as output
}
void out_data(char outdata)
{
volatile char* portf_out = (volatile char*) 0x31;
*portf_out = outdata; // Output data to PORTF
}
void out_ctrl(char outctrl)
{
volatile char* portk_ctrl = (volatile char*) 0x108;
*portk_ctrl = outctrl; // Output control signals to PORTK
}
void init_lcd()
{
out_data(0x38); // Function set: 8-bit, 2 line, 5x8 dots
lcd_ctrl_write();
delay1(2);
out_data(0x0F); // Display on, cursor blinking
lcd_ctrl_write();
delay1(2);
out_data(0x01); // Clear display
lcd_ctrl_write();
delay1(2);
out_data(0x06); // Entry mode set: increment cursor, no shift
lcd_ctrl_write();
delay1(2);
}
void write_data(char wrdata)
{
out_data(wrdata); // Send data to PORTF
out_ctrl(0x02); // RS = 1, RW = 0 (write data)
delay1(1);
out_ctrl(0x03); // Enable = 1
delay1(1);
out_ctrl(0x02); // Enable = 0
delay1(1);
}
void lcd_ctrl_write()
{
out_ctrl(0x01); // RS = 0, RW = 0 (write instruction)
delay1(1);
out_ctrl(0x00); // Enable = 0
delay1(1);
}
void delay1(int ms)
{
// Simple delay loop (not accurate but illustrative)
for(int i = 0; i < ms * 1000; i++)
{
asm volatile ("nop");
}
}
void loop() {
// put your main code here, to run repeatedly:
}