lcd显示数字c语言程序,0802LCD 4位显示程序 PIC单片机C语言程序

//#include"head.h"

#define uchar unsigned char

#define uint unsigned int

#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)

#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)

#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)

#pragma config LVP = ON        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)

#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)

#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)

#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

//char tab[]="0123456789";

#define uchar unsigned char

#define uint unsigned int

void LCD_write_cmd(uchar command);

void LCD_write_command(uchar command);

void LCD_en_write(void);

void LCD_set_xy( unsigned char x, unsigned char y );

void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);

void LCD_write_data(unsigned char data);

void delay_nus(unsigned int n);

void delay_nms(unsigned int n);

#define RS RC2

#define EN RC3

#define DB7 RC7

#define DB6 RC6

#define DB5 RC5

#define DB4 RC4

#define LCD_DATA_PORT PORTC

#define LCD_DATA_TRIS TRISC

/*------------------------------------------------------------------------------

函数说明

------------------------------------------------------------------------------*/

void LCD_init()

{

TRISD=0x00; //数据口方向为输出

PORTD=0x00;

TRISC=0x00; //数据口方向为输出

PORTC=0x00;//设置EN、RS/数据为输出

LCD_write_cmd(0x30);//4位的指令

delay_nms(5);

LCD_write_cmd(0x30);

delay_nus(200);

LCD_write_cmd(0x30);

delay_nms(1);

LCD_write_cmd(0x20);

LCD_write_cmd(0x20); //4位显示

LCD_write_cmd(0x80);

LCD_write_cmd(0x00); //显示开 游标、闪烁不显示

LCD_write_cmd(0x80);

LCD_write_cmd(0x00); //清屏

LCD_write_cmd(0x01);

LCD_write_cmd(0x00); //两行 5*7

LCD_write_cmd(0xc0);

}

void LCD_write_cmd(uchar command) //写指令

{

delay_nus(10);

RS=0;

EN=0;//使能清零

LCD_DATA_PORT&=0x0f; //清除端口

delay_nus(2);

EN=1;

LCD_DATA_PORT |= (command & 0xf0);//高4位不用改

delay_nus(2);

EN=0;

}

void LCD_write_command(uchar command) //写指令

{

LCD_write_cmd(command);

LCD_write_cmd(command<<4);

}

void LCD_write_data(unsigned char data) //写数据

{

delay_nus(10);

RS=1;

EN=0;//使能清零

LCD_DATA_PORT&=0x0f;

EN=1;

LCD_DATA_PORT |= (data & 0xf0);//高4位不用改

delay_nus(2);

EN=0;

delay_nus(2);

LCD_DATA_PORT&=0x0F; //清低四位

EN=1;

LCD_DATA_PORT |= ((data << 4) & 0xf0);//发送低4位

delay_nus(2);

EN=0;

}

void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1

{

LCD_set_xy( X, Y ); //写地址

while (*s) // 写显示字符

{

LCD_write_data( *s );

s ++;

}

}

void LCD_set_xy( unsigned char x, unsigned char y )  //写地址函数

{

unsigned char address;

if (y == 0) address = 0x80 + x;

else        address = 0xc0 + x;

LCD_write_command( address);

}

void delay_nms(unsigned int n) //N ms延时函数

{

uint a,b;

for(a=n;a>0;a--)

for(b=80;b>0;b--);

}

void delay_nus(unsigned int n) //N us延时函数

{

unsigned int i;

for (i=0;i}

void main()

{

LCD_init();

//    LCD_write_command(0x0d); //光标开

while(1)

{

RD0=1;

delay_nms(1000);

RD0=0;

delay_nms(1000);

LCD_write_string(0,0,"ceshiLCD");

LCD_write_string(0,1,"hahahaha");

delay_nms(2000);

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值