/*
Title: LCD-16sm.c
Connection:
Factory fixed setting:
PORTA:
PA0-PA2 LCD control
PA3-PA7 4x7-segment display control
Drive LED group2 (the right group of LED)
PORTB:
Shared by LCD and 4x7-segment displays
output 8-bit data to LCD or 8-bit data to 4x7-segment displays
PORTC:
shared by 8-bit dipswitch and 4 x touch switches + 4 buttons
receive inputs from dipswitch, touch switches and buttons
PORTD:
Drive LED group1 (the left group of LED)
Attention:
1. J12 should be capped (connectted)
2. J5 is the Jump for LCD back light power
Operation:
LCD panel display messages
Four messages can be displayed
which message displayed is controlled by SW4 and SW5
LED group1 display status of SW4 and SW5
*/
#include <iom16v.h>
#include <macros.h>
#include <stdlib.h>
unsigned char lcd_enable = 0b00000001, lcd_disable = 0b11111110;
unsigned char in_instr_wr = 0b00000000, in_data_wr = 0b00000100;
unsigned char in_instr_rd = 0b00000010, in_data_rd = 0b00000110;
void wr_instruction(unsigned char instr);
void wr_data(unsigned char data);
int indexhead=0, indextail=0, indexcount;
int cursor_index=0;
const char message1[]= {" Hello World!"};
const char message2[]= {"www.inovacs.com"};
const char message3[]= {" Happy New Year"};
const char message4[]= {" 2008 is coming"};
const char message5[]= {" Greeting From"};
const char message6[]= {"www.inovacs.com"};
const char message7[]= {"Contact Inova"};
const char message8[]= {"Tel:0592 5563570"};
void cursor_home(void);
void cursor_line1(void);
void cursor_line2(void);
int ifr_decode(void);
unsigned char swin, swin2;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Initilization file
void port_init(void)
{
DDRA = 0xff; //PA0-PA2 for LCD control
DDRB = 0xff; //PB0-PB7 for LCD data
DDRD = 0xff; //Control LEDs on left group
DDRC = 0x00; //Receive from dip-switch and button
PORTC=0xff;
}
void WDT_off(void)
{
WDR();
WDTCR=0b00010111;
WDTCR=0b00000111;
}
void delay(int count)
{
int a,b;
b=2*count;
for(a=0; a<b; a++)
;
}
void ldelay(int ms)
{
int i;
for(i=0; i<=ms; i++)
delay(0x2000); //2000-->4000
}
// ************************************************************
// LCD functions follows
//PORTB to LCD data port
//PORTA to LCD control port
//PA0--> R/S
//PA1--> R/W
//PA2--> E
void wr_instruction(unsigned char instr)
{
DDRA=0xff;
delay(1);
PORTA=0b00000000;
PORTB=instr; //send out instruction
delay(2);
PORTA=0b00000100; //PA2<--1 LCD-E ON (enable)
delay(2);
PORTA=0b00000000; //PA2<--0 LCD-E OFF (disable)
delay(1);
}
void wr_data(unsigned char data)
{
DDRA=0xff;
PORTA=0b00000001; //PA0 <-- 1, Select data register
PORTB=data; //Send out data
delay(1);
PORTA=0b00000101; //Enable write to Data register
delay(2);
PORTA=0b00000000; //disable ????
delay(1);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Following functions for LCD panel control
void wr_instruction2(unsigned char instr)
{
unsigned char flags;
delay(10);
wr_instruction(instr);
delay(10);
}
void wr_data2(unsigned char data)
{
unsigned char flags;
if(cursor_index==0x10)
cursor_line2();
else if(cursor_index==0x50)
cursor_line1();
wr_data(data);
cursor_index++;
delay(50);
}
void cursor_home(void) //Move cursor to Home
{
wr_instruction2(0x01);
delay(10);
wr_instruction2(0x80);
ldelay(5);
cursor_index=0;
}
void cursor_line1(void) //Move cursor to the first line
{
wr_instruction2(0x80);
cursor_index=0;
ldelay(5);
}
void cursor_line2(void) //Move cursor to the second line
{
wr_instruction2(0b11000000);
cursor_index=0x40;
ldelay(2);
}
void space(void) //Write space, ' ', on the panel
{
wr_data2(' ');
}
void out_string(const char array[]) //Send a string stored in array to LCD panel
{
int i=0;
unsigned char onechar;
i=0;
while(array[i])
{
onechar=array[i++];
wr_data2(onechar);
}
}
void lcd_on(void)
{
unsigned char i, ctrl_data;
ldelay(20);
PORTD=0x01;
ldelay(40);
wr_instruction(0b00110000); //Function Set:set 8-bit, 2line, 5x7 fonts
ldelay(2);
wr_instruction(0b00110000); //
ldelay(2);
wr_instruction(0b00110000); //
delay(80);
wr_instruction(0b00111100); //8-bit, 2 lines, 5x7 font
delay(20);
wr_instruction(0b00001110); //display on, cursor on, blink off
delay(20);
wr_instruction(0b00001110); //display on,
delay(20);
wr_instruction(0b00000001); //clear display
ldelay(5);
wr_instruction(0b00000110); //Entry mode
delay(10);
wr_instruction(0b00000001); //clear display
delay(20);
}
//***********************************************************
// **********************************************************
// Main program
void main(void)
{
unsigned char data, pfin, dark, darkfg=0xff;
int i=0;
unsigned char j, jsave, array1[10];
int a;
CLI();
port_init();
WDT_off();
ldelay(200);
lcd_on();
PORTD=0x50;
ldelay(50);
PORTD=0x05;
ldelay(50);
out_string(message1);
cursor_line2();
out_string(message2);
PORTD=0x55;
PORTA=0xF8;
PORTB=0x00;
ldelay(100);
while(1)
{
WDR();
swin=PINC&0x30;
swin=(swin>>4)&0x03;
if(swin2!=swin)
{
PORTD=swin;
swin2=swin;
if(swin==0x00)
{
cursor_home();
out_string(message1);
cursor_line2();
out_string(message2);
}
else if(swin==0x01)
{
cursor_home();
out_string(message3);
cursor_line2();
out_string(message4);
}
else if(swin==0x02)
{
cursor_home();
out_string(message5);
cursor_line2();
out_string(message6);
}
else
{
cursor_home();
out_string(message7);
cursor_line2();
out_string(message8);
}
}
ldelay(10);
}
}
评论0