|
#include <pic.h>
#define DEFBIT_0(reg, name) static volatile bit name @ (unsigned)®*8 + 0;
#define DEFBIT_1(reg, name) static volatile bit name @ (unsigned)®*8 + 1;
#define DEFBIT_2(reg, name) static volatile bit name @ (unsigned)®*8 + 2;
#define DEFBIT_3(reg, name) static volatile bit name @ (unsigned)®*8 + 3;
#define DEFBIT_4(reg, name) static volatile bit name @ (unsigned)®*8 + 4;
#define DEFBIT_5(reg, name) static volatile bit name @ (unsigned)®*8 + 5;
#define DEFBIT_6(reg, name) static volatile bit name @ (unsigned)®*8 + 6;
#define DEFBIT_7(reg, name) static volatile bit name @ (unsigned)®*8 + 7;
DEFBIT_5(PORTA, LCD_E)
#define NOP asm("nop")
#define LCD_REGSEL 0x10
#define DATA_TO_LCD TRISD=0X00
#define DATA_FROM_LCD TRISD=0x0f
#define LCD_REGSEL 0x10#define LCD_PORT PORTD
//#define DATA_TO_LCD set_tris_d(ALL_OUT)
//#define DATA_FROM_LCD set_tris_d(0x0f)
#define LCD_SETPOS 0x80
#define LCD_MODE 0x28 //
#define LCD_LINE2 0x40
#define LCD_WIDTH 16
char disp_decword(int w);
void putch(char b);
void lcd_cmd(char b);
void lcd_char(char b);
void lcd_nyb(char b);
#if CPU_CLK < 20000000L
#define delay_us(x) { unsigned char _del; _del = x>>1; while(_del--) ; }
#else
#define delay_us(x) { unsigned char _del; _del = x; while(_del--) ; }
#endif
//01清屛
//0000 01 I/D s
//I/D=1 光标右移一位,I/D=0光标左移一位 S=0时I/D无效
//0000 1 D C B D=1开显示,D=0关显示。C=1光标显示C=0光标消失。B 2.4HZ闪烁开关
//
void delay_ms(unsigned char val) // 大约MS的延时
{
unsigned char i;
while (val--)
{
for (i=0; i<4; i++)
delay_us(250);
}
}
//************************************
//初始化LCD
void init_lcd(void)
{
LCD_E = 0; /* 清LCD时钟 */
DATA_FROM_LCD; /* TRISD=0X0F */
delay_ms(15); /* 上电延时 */
lcd_cmd(LCD_MODE);
delay_ms(5); //>1.64ms
lcd_cmd(LCD_MODE);
delay_us(100);
lcd_cmd(LCD_MODE);
delay_us(40);
lcd_cmd(LCD_MODE); /* Set 4-bit mode, 2 lines, 5x7 dots */
lcd_cmd(0x04); /* 光标 */
lcd_cmd(0x0e); /* Display on,not 闪烁 */
lcd_cmd(0x01); /* Clear display, 回光标 */
lcd_cmd(LCD_SETPOS); /* 数据地址 */
}
/* 显示位置 */
void lcd_gotoxy(char x, char y)
{
if (y != 1)
x += LCD_LINE2;
lcd_cmd(LCD_SETPOS - 1 + x);
}
/* 清一行(1 = 顶行), 回到光标位 */
void lcd_clearline(char y)
{
char x, n;
x = y>1 ? LCD_LINE2 : 0;
lcd_cmd(LCD_SETPOS + x);
for (n=0; n<LCD_WIDTH; n++)
lcd_char(' ');
lcd_cmd(LCD_SETPOS + x);
}
/* 传送字符到 LCD */
void lcd_char(char b)
{
DATA_TO_LCD;
lcd_nyb((b>>4) | LCD_REGSEL);//传送
lcd_nyb((b&0xf) | LCD_REGSEL);
DATA_FROM_LCD;
delay_us(40);
}
/* 传送命令位至 LCD */
void lcd_cmd(char b)
{
DATA_TO_LCD;
lcd_nyb(b >> 4);
lcd_nyb(b & 0x0f);
if ((b & 0xfc) == 0)
delay_ms(2);
DATA_FROM_LCD;
delay_us(40);
}
/* 传送4位到 LCD, 包含 RD和 RS 位 */
void lcd_nyb(char b)
{
LCD_E = 1;
LCD_PORT = b;
NOP;
NOP;
LCD_E = 0;
NOP;
}
//显示r,n或1字节数据
void putch(char b)
{
if (b == ' ')
lcd_cmd(LCD_SETPOS);
else if (b == ' ')
lcd_cmd(LCD_SETPOS + LCD_LINE2);
else
lcd_char(b);
}
//显示
char disp_decword(int w)
{
char count, n=0, d[5];
do
{
d[n++] = (char)(w % 10) + '0'; //BCD转换
w /= 10;
} while(w);
count = n;
while (n)
putch(d[--n]);
return(count);
}
main()
{
init_lcd();
putch('r');
disp_decword(123);
} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|