📄 lcd.c.svn-base
字号:
/* ****ROBOCON 2009 | BUPT TEAM******* * ------------------------------------------------------------------------ * FileName : lcd.c * Version : 1.0 * Brief : * Code by : Leaf * Date : Tue Mar 31 00:04:43 2009 * Note : * * * ------------------------------------------------------------------------ */
#include "lib_bupt.h"
#ifdef LCD_H_INCLUDED#define LCD_DELAY_INIT 15
#define LCD_DELAY_DATA 40
#define LCD_DELAY_EN 10static void lcdWriteEn();
static LcdConnType lcdConnType = LCD_CONN_TYPE;static UINT8 lcdDataMask;
#ifdef _STDIO_H_
/*LCD的文件操作,使可以在lcd显示时可以使用fprintf*/
UINT8 lcdCurLine;
FILE *lcd;
int lcd_putchar(char c, FILE *strean){
if(c == '\n' || c == '\r'){
lcdSetXy(0, !lcdCurLine);
return 0;
}
lcdSendData(c);
return 0;
}
#endif
/*LCD 发送数据的函数,根据不同的连接执行不同的动作*/
void lcd_trans(UINT8 data){
if(lcdConnType == LCD_CONNTYPE_L4){
UINT8 half;
half = data >> 4; //get higher 4 bits
LCD_DATA_PORT &= 0xf0; //clear 4-bits send port
LCD_DATA_PORT |= half; //send higher 4-bits
lcdWriteEn();
half = data & 0x0f; //get lower 4 bits
LCD_DATA_PORT &= 0xf0; //clear 4-bits send port
LCD_DATA_PORT |= half; //send lower 4-bits
lcdWriteEn();
}
else if(lcdConnType == LCD_CONNTYPE_H4){
UINT8 half;
half = data & 0xf0; //get higher half
LCD_DATA_PORT &= 0x0f; //clear 4-bits send port
LCD_DATA_PORT |= half; //send higher half
lcdWriteEn();
half = data << 4; //get lower half
LCD_DATA_PORT &= 0x0f; //clear 4-bits send port
LCD_DATA_PORT |= half; //send lower half
lcdWriteEn();
}
else if(lcdConnType == LCD_CONNTYPE_8){
LCD_DATA_PORT = data;
lcdWriteEn();
}
}
/*初始化,液晶寝化时要发一串串的指令*/
void lcdInit(void) //Һ����ʼ��
{
unsigned char i; if(lcdConnType == LCD_CONNTYPE_L4){ lcdDataMask = 0x0f; }else if(lcdConnType == LCD_CONNTYPE_H4){ lcdDataMask = 0xf0; }else if(lcdConnType == LCD_CONNTYPE_8){ lcdDataMask = 0xff; } LCD_DATA_DDR |= lcdDataMask;
LCD_EN_DDR|=LCD_EN; //����EN�������
LCD_RS_DDR|=LCD_RS; //����RS�������
if(lcdConnType == LCD_CONNTYPE_8){
lcdSendCommand(0x38);
}
else{
lcdSendCommand(0x28);
}
lcdWriteEn();
_delay_us(LCD_DELAY_INIT);
if(lcdConnType == LCD_CONNTYPE_8){
lcdSendCommand(0x38);
}
else{
lcdSendCommand(0x28);
}
for (i=0;i<20;i++)
{
if(lcdConnType == LCD_CONNTYPE_8){
lcdPosChar(0,0,0x38); //4bit test
}
else{
lcdPosChar(0,0,0x28); //4bit test
}
_delay_ms(LCD_DELAY_INIT);
}
lcdSendCommand(0x0c); //显示开
lcdSendCommand(0x01); //清屏
_delay_ms(LCD_DELAY_INIT);
#ifdef _STDIO_H_
lcd = fdevopen(lcd_putchar, NULL);
#endif
}
/*在LCD的EN脚上产生一个下降沿*/
static void lcdWriteEn(void)
{
LCD_EN_PORT|=LCD_EN;
_delay_us(LCD_DELAY_EN);
LCD_EN_PORT&=~LCD_EN;
}
/*向lcd发个指令*/
void lcdSendCommand(unsigned char command)
{
_delay_us(LCD_DELAY_DATA);
LCD_RS_PORT&=~LCD_RS; //RS=0
lcd_trans(command);
}
/*向lcd发个数据*/
void lcdSendData(unsigned char data)
{
_delay_us(LCD_DELAY_DATA);
LCD_RS_PORT|=LCD_RS; //RS=1
lcd_trans(data);
}
/*设置光标位置*/
void lcdSetXy( unsigned char x, unsigned char y )
{
unsigned char address;
#ifdef _STDIO_H_
lcdCurLine = y;
#endif
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
lcdSendCommand( address);
}
/*写个字符*/
void inline lcdPutc(const char c){
lcdSendData(c);
}
/*写字串,\0 结尾*/
void lcdPuts(const char *s){
while (*s)
{
lcdPutc( *s );
s ++;
}
}
/*向指定的位置写个字符*/void inline lcdPosChar(unsigned char X,unsigned char Y, char data){ lcdSetXy( X, Y ); lcdSendData( data);}
/*向指定的位置写个字串*/
void inline lcdPosString(unsigned char X,unsigned char Y,const char *s)
{
lcdSetXy( X, Y );
lcdPuts(s);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -