lcd输出学号c语言函数怎么写,用51单片机+LCD12864屏编写的“ID卡号读/写器”

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

用51单片机+LCD12864屏编写的“ID卡号读/写器”

#include                                     //包含头文件

#define uchar unsigned char                            //宏定义

#define uint unsigned int                            //宏定义

#define LCD_data  P0                                 //数据口

sbit int0=P3^0;                                        //串口读入

sbit gd  =  P1^3;             //置P1^3端口为高/低电平的转换开关 ,即读/写转换开关

sbit LCD_RS  =  P1^0;            //数据/地址寄存器选择输入 ,lcd为12864

sbit LCD_RW  =  P1^1;            //液晶读/写控制

sbit LCD_EN  =  P1^2;            //液晶使能控制

sbit LCD_gd  =  P1^3;             //置P1^3端口为高/低电平的转换开关 ,即读/写转换开关

sbit LCD_PSB =  P2^7;            //串/并方式控制

sbit LCD_RST  =  P2^6;              //    lcd复位端

uint flag ,i;                            //串口中断标志位

char a[9];                            //字符数组a,a[9]为系统自动增设的终结符(\0)

void init();                         //

/*******************************************************************/

/*                                                                 */

/* z毫秒延时函数                                                  */

/*                                                                 */

/*******************************************************************/

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

/*******************************************************************/

/*                                                                 */

/*  50微秒延时函数                                                 */

/*                                                                 */

/*******************************************************************/

void delay_50us    (uchar t)

{

uchar j;

for(;t>0;t--)

for(j=19;j>0;j--);

}

/*******************************************************************/

/*                                                                 */

/*检查LCD忙状态                                                    */

/*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。      */

/*                                                                 */

/*******************************************************************/

bit lcd_busy()

{

bit result;

LCD_RS = 0;

LCD_RW = 1;

LCD_EN = 1;

delay_50us(1);

result = (bit)(P0&0x80);

LCD_EN = 0;

return(result);

}

/*******************************************************************/

/*                                                                 */

/*写指令数据到LCD                                                  */

/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。                             */

/*                                                                 */

/*******************************************************************/

void lcd_wcmd(uchar cmd)

{

while(lcd_busy());

LCD_RS = 0;

LCD_RW = 0;

LCD_EN = 0;

delay_50us(2);

P0 = cmd;

LCD_EN = 1;//下降沿写入

delay_50us(8);

LCD_EN = 0;

delay_50us(2);

}

/*******************************************************************/

/*                                                                 */

/*写显示数据到LCD                                                  */

/*RS=H,RW=L,E=高脉冲,D0-D7=数据。                               */

/*   //显示字符                                                    */

/*******************************************************************/

void lcd_wdat(uchar dat)

{

while(lcd_busy());

LCD_RS = 1;

LCD_RW = 0;

LCD_EN = 0;

delay_50us(1);

P0 = dat;

LCD_EN = 1;//下降沿写入

delay_50us(10);

LCD_EN = 0;

delay_50us(2);

}

/*********************************************************/

/*                                                       */

/* 设定显示位置                                          */

/*                                                       */

/*********************************************************/

void lcd_pos(uchar X,uchar Y)                //显示地址

{

uchar  pos;

if (X==0)

{X=0x80;}

else if (X==1)

{X=0x90;}

else if (X==2)

{X=0x88;}

else if (X==3)

{X=0x98;}

pos = X+Y ;

lcd_wcmd(pos);     //显示地址

}

/*******************************************************************/

/*                                                                 */

/*  LCD初始化设定                                                  */

/*                                                                 */

/*******************************************************************/

void lcd_init()                      //lcd初始化

{

LCD_PSB = 1;         //并口方式

LCD_EN=0;

LCD_RST=0;             //复位

delay(5);

LCD_RST=1;             //复位恢复

lcd_wcmd(0x34);      //扩充指令操作

delay(5);

lcd_wcmd(0x30);      //基本指令操作

delay(5);

lcd_wcmd(0x0C);      //显示开,关光标

}

/****************串行口字节发送****************/

void UART_SendOneByte(uchar c)

{

SBUF = c;

while(!TI);              //若TI=0,在此等待

TI = 0;

}

/****************串行口字符发送****************/

void SendStr(uchar *s)

{

while(*s!='\0')         //通过检测是否字符串末尾

{

UART_SendOneByte(*s);

s++;

}

}

/****************串行口字节接收****************/

uchar UART_ReceiveOneByte()

{

uint b;               //定义无符号型变量b

b = SBUF;

while(!RI);       //若RI=0,在此等待

RI = 0;

return b;

}

/****************串行口数据字符接收****************/

void ReceiveStr()

{

uint b;                 //定义无符号型变量b

uint bi = 0;                   //初始化为零

while(b!='\0')    //通过检测是否字符串末尾

{

b = UART_ReceiveOneByte();

a[bi] = b;

bi++;                      //接受的数据存放于数组 a[] 中

}

}

void main()

{

init();                                          //调用串口初始化子函数

while(1)

{

if(flag==1)                                    //若串口中断标志位打开

{

flag=0;                                //清除flag标志位 (即关闭串口中断标志位)

ES=0;                                //关串口中断

lcd_wcmd(0x01);              //清除LCD的显示内容

delay(5);

i=0;                        // i清零

lcd_pos(0,0);               //设置显示位置为第一行的第1个字符

while(a[i]!= '\0')

{

lcd_wdat(a[i]);

i++    ;

}

}

}

}

void ser() interrupt 4                                //串口中断程序

{

ES=0;                                        //关串口中断

RI=0;                                        //清零串口接收标志位,即允许串口接收

if(gd ==1)                                 //等于1时,为读的状态

{

SendStr("\nR3,0,2\r");      //发送: R3,0,2      R3:读取USER空间,0:0地址位开始读取,2:数据长度2

delay(150);

ReceiveStr();             //    读取数据暂存于a中    ;

flag=1;                                        //开串口中断标志位

ES=1;                                        //开串口中断

}

else                                               //若gd =0 为写的状态

{

SendStr("\nW3,0,2,246813EF\r");         //发送: W1,0,2      W3:写入USER空间,0:0地址位开始写入,2:数据长度2

delay(150);

delay(150);

SendStr("\nR3,0,2\r");

delay(150);

ReceiveStr();  //该返回信息暂存于a中

lcd_wcmd(0x01);              //清除LCD的显示内容

delay(5);

i=0;                        // i清零

lcd_pos(0,0);               //设置显示位置为第一行的第1个字符

while(a[i]!= '\0')

{

lcd_wdat(a[i]);

i++    ;

}

while(1);            //程序处于死机状态

}

}

void init()                                             //单片机初始化

{

TMOD = 0x20;                                   // 定时器T1工作于8位自动重载模式, 用于产生波特率

TH1 = 0xfd;                                 // 波特率9600

TL1 = 0xfd;                                    // 波特率9600

TR1 = 1;                                     // 启动定时器T1

SCON = 0x50;                                // 设定串行口工作方式1,(SM0=0;SM1=1)及REN=1

EA=1;                                        //开总中断

ES=1;                                        //开串口中断

}

问题如下:

Build target 'Target 1'

compiling ID卡号读写-3.c...

ID卡号读写-3.C(1): warning C500: LICENSE ERROR (R208: RENEW LICENSE ID CODE (LIC))

linking...

*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS

SEGMENT: ?PR?LCD_INIT?ID卡号读写_3

*** WARNING L15: MULTIPLE CALL TO SEGMENT

SEGMENT: ?PR?LCD_BUSY?ID卡号读写_3

CALLER1: ?PR?SER?ID卡号读写_3

CALLER2: ?C_C51STARTUP

Program Size: data=22.2 xdata=0 code=578

"ID卡号读写-3" - 0 Error(s), 3 Warning(s).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你一些思路。首先你需要连接好51单片机LCD1602液晶显示,然后在单片机中编写程序。以下是一个简单的示例程序,可以让LCD1602显示一行的英文字符缩和学生学号: ``` #include <reg52.h> #define LCD_DB P0 sbit RS = P2^0; // RS引脚 sbit RW = P2^1; // RW引脚 sbit EN = P2^2; // EN引脚 void delay(unsigned int t) { while(t--); } void write_cmd(unsigned char cmd) { RS = 0; // 告诉LCD这是指令 RW = 0; // 告诉LCD这是LCD_DB = cmd; // 发送指令 EN = 1; // 使能 delay(10); EN = 0; // 关闭使能 } void write_data(unsigned char dat) { RS = 1; // 告诉LCD这是数据 RW = 0; // 告诉LCD这是LCD_DB = dat; // 发送数据 EN = 1; // 使能 delay(10); EN = 0; // 关闭使能 } void init_lcd() { write_cmd(0x38); // 8位,2行,5x7点阵 write_cmd(0x0c); // 开启显示,关闭光标 write_cmd(0x06); // 右移光标 write_cmd(0x01); // 清 } void display_string(unsigned char x, unsigned char y, unsigned char *str) { unsigned char i; if(x < 16) { if(y == 0) { write_cmd(0x80 + x); // 第一行 } else { write_cmd(0xc0 + x); // 第二行 } } for(i = 0; str[i] != '\0'; i++) { write_data(str[i]); // 逐个发送字符 } } void main() { unsigned char str[] = "CSDN 123456"; // 要显示的字符串 init_lcd(); display_string(0, 0, str); // 在第一行显示 while(1); } ``` 在这个程序中,我们使用了51单片机的P0口作为LCD1602的数据口,P2.0、P2.1、P2.2分别作为RS、RW、EN引脚。程序中的init_lcd()函数用于初始化LCD1602,display_string()函数用于在LCD1602上显示字符串。 你可以根据需要修改display_string()函数,来显示你想要的英文字符缩和学生学号
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值