📄 lcd-16sn.c
字号:
/*
Title: LCD-16sn.c
Function program used by DEMOB-16sm.c
*/
#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[]= {" RS232 Demo "};
const char message2[]= {"Baudrate=38400 "};
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;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 clear_line1(void)
{
int i;
cursor_line1();
for(i=0; i<=15; i++)
space();
cursor_line1();
}
void clear_line2(void)
{
int i;
cursor_line2();
for(i=0; i<=15; i++)
space();
cursor_line2();
}
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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -