LCD显示

主程序:

/*
标题:范例5-2
版本:1.0
Target:89S51
程序描述:这个范例说明如何使用8051 的Port 1连接LCD显示器,PORT 3.3、P3.4和P3.5则连接到LCD显示器的控制线。程序执行时将显示出Hello Keil C和LCD  display */
/* ***************************************************** */
#include <REGX51.H>
#include "lcd.h"
void main (void)  {
  init_LCD();
  clear_LCD();
  display_LCD_string("Hello Keil C");
  gotoxy(2,1);
  display_LCD_string("LCD  display");
  while(1);          /* 无穷循环 */
}

 

lcd.c

 #include <REGX51.H>
#include <lcd.h>

char code int2charLCD[]="0123456789";
 
void write_LCD_command(unsigned command)
{
 rw=WRITE;
 rs=COMMAND;
 enable=ENABLE;
 P1=command;
 delay100us(20);
 enable=DISABLE;
 rw=1;
}
void write_LCD_data(unsigned LCDdata)
{
 rw=WRITE;
 rs=DATA;
 enable=ENABLE;
 P1=LCDdata;
 delay100us(20);
 enable=DISABLE;
 rw=1;
}
void init_LCD(void)
{
 write_LCD_command(TwoLine_8bit);// 0x38   
 write_LCD_command(CURSOR_OFF); // 0x0C
 write_LCD_command(CURSOR_RIGHT);// 0x06
}
void clear_LCD()
{
 write_LCD_command(CLEAR); 
 write_LCD_command(CURSOR_HOME); 
}
void display_LCD_string(char *p)
{
 while(*p)
 {
  write_LCD_data(*p);
  p++;
 }
}
void display_LCD_number(char number)
{
 unsigned char x,y;
 x=number/10;
 y=number-10*x;
 write_LCD_data(int2charLCD[x]);
 write_LCD_data(int2charLCD[y]);
}
void gotoxy(unsigned x,unsigned y)
{
 if(x==1) 
  write_LCD_command(GOTO_LINE_1+y);
 else
     write_LCD_command(GOTO_LINE_2+y);
}

 

 

lcd.h

 

/* LCD command */
#define  TwoLine_8bit 56  // 0x38
#define  CLEAR    1  // 0b00000001
#define  CURSOR_HOME  2  // 0b00000010
#define  CURSOR_LEFT  4  // 0b00000100
#define  CURSOR_RIGHT 6  // 0b00000110
#define  CURSOR_OFF  12  // 0b00001100
#define  CURSOR_ON  14  // 0b00001110
#define  CURSOR_BLINK 15  // 0b00001111
#define  GOTO_LINE_2  192
#define  GOTO_LINE_1  128

/* LCD control lines */
#define  ENABLE   1
#define  DISABLE  0
#define  READ  1
#define  WRITE  0
#define  COMMAND  0
#define  DATA  1
#define  rs   P3_5
#define  rw   P3_4
#define  enable  P3_3

void delay100us(unsigned);
void write_LCD_command(unsigned);
void write_LCD_data(unsigned);
void init_LCD(void);
void clear_LCD(void);
void display_LCD_string(char *);
void gotoxy(unsigned,unsigned);
void display_LCD_number(char);

 

 

delay100us.c

void delay100us(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++) //我加上去的循环 把n用上
{
#pragma asm
more: mov  R3,#48
  djnz R3,$
  djnz R7,more
#pragma endasm
}
} /*这段程序要记得把delay100us.c这个文件的 OPTION 里 勾选 汇编 assembly相关的选项
    否则出错,提示  “DELAY100US.C(7): error C272: 'asm/endasm' requires src-control to be active”

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值