一个经典的液晶菜单程序(C语言)
单片机制作的12864液晶万年历_C语言程序
#include<reg52.H>#include<intrins.h>#include<math.h>////////////////////////////////////////DS18B20#define uchar unsigned char#define uint unsigned intsbit DQ = P1 ^ 0; // 定义DS18B20的单总线#define Lcd_Bus P0sbit LCD_RS=P2^7; //数据/命令选择sbit LCD_RW=P2^6; //读写控制sbit LCD_EN=P2^5; //片选信号#define LCD_DA TA P0 //8 bit数据线#define LOW 0#define HIGH 1//初始化指令#define CLEAR_SCREEN 0x01 //清屏指令:清屏且AC值为00H#define AC_INIT 0x02 //将AC设置为00H。
且游标移到原点位置#define CURSE_ADD 0x06 //设定游标移到方向及图像整体移动方向(默认游标右移,图像整体不动)#define FUN_MODE 0x30 //工作模式:8位基本指令集#define DISPLAY_ON 0x0c //显示开,显示游标,且游标位置反白#define DISPLAY_OFF 0x08 //显示关#define CURSE_DIR 0x14 //游标向右移动:AC=AC+1#define SET_CG_AC 0x40 //设置AC,范围为:00H~3FH#define SET_DD_AC 0x80#define FUN_MODEK 0x36 //扩展指令集,8位指令#define X1address 0x80 //上半屏X轴起始地址#define X2address 0x88 //下半屏X轴起始地址#define Y address 0x80 //Y轴起始地址#define Set_Column 0x40#define Set_Page 0xB8uchar code shu[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};uchar code xingqi[]="星期";uchar code hanzishu[]="一二三四五六日";unsigned char TempData = 0;double DS18B20Data;uint jiemian=0;//////////////////////////////////////////DS1302sbit DS1302_RST = P1^4;sbit DS1302_IO = P1^3;sbit DS1302_SCK = P1^2;unsigned char table[]={0X00,0X18,0X0c,0X15,0X06,0X01,0X0a};//0s 24min 12hour 14ri 5yue 5zhou 2010nianunsigned char data table1[7];//uchar code table2[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};uchar code shuzi[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,};unsigned char aa,bb;uchar miaoge,miaoshi,fenge,fenshi,shige,shishi,rige,rishi,yuege,yueshi,zhou,niange,nianshi;uchar shezhinum=0;//unsigned char shi,ge;////////////////////////////////////////////////键盘扫描sbit key_mnue = P3^7;sbit key_jia = P2^0;sbit key_jian = P2^1;sbit key_enter= P2^2;uchar key_number=0;//int nian,yue,ri,shi,fen;/////////////////////////////////////////////////红外sbit HW_IRQ = P3 ^ 2; // 定义38KHz一体化红外接收头与CPU的连接unsigned char DT;static unsigned char data IRCode[3],IRCON,IRCON2;//////////////////////////////////////////////////蜂鸣器sbit BEEP =P1^1;/****************************************************************************** *************** 函数名称:DS18B20_Delay()* 功能:DS18B20软件延时专用* 入口参数:count 延时参数,值越大,延时越长* 出口参数:无******************************************************************************* **************/void DS18B20_Delay(uint count) //延时函数{while(count--);}/****************************************************************************** *************** 函数名称:DS18B20_Init()* 功能:DS18B20初始化* 入口参数:无* 出口参数:返回值为1则初始化成功* 返回值为0则初始化失败******************************************************************************* **************/uchar DS18B20_Init(void){uchar x = 0;DQ = 1; // DQ复位DS18B20_Delay(8); // 稍做延时DQ = 0; // 单片机将DQ拉低DS18B20_Delay(80); // 精确延时,大于480usDQ = 1; // 拉高总线DS18B20_Delay(14); // 稍做延时后x = DQ; // 若x=0则初始化成功,若x=1则初始化失败DS18B20_Delay(20);return (~x);}/****************************************************************************** *************** 函数名称:DS18B20_Read1Byte()* 功能:从DS18B20读一个字节* 入口参数:无* 出口参数:从DS18B20读出的一个字节******************************************************************************* **************/uchar DS18B20_Read1Byte(void){uchar i = 0;uchar dat = 0;for(i=8;i>0;i--){DQ = 0; // 给脉冲信号dat >>= 1;DQ = 1; // 给脉冲信号if(DQ) dat |= 0x80;DS18B20_Delay(4);}return (dat);}/****************************************************************************** *************** 函数名称:DS18B20_Write1Byte()* 功能:向DS18B20写一个字节* 入口参数:dat 要写入DS18B20的一字节数据* 出口参数:无******************************************************************************* **************/void DS18B20_Write1Byte(uchar dat){uchar i = 0;for(i=8;i>0;i--){DQ = 0;DQ = dat&0x01;DS18B20_Delay(5);DQ = 1;dat>>=1;}}/****************************************************************************** *************** 函数名称:Read_Disp_Temperature()* 功能:读取并显示温度* 入口参数:无* 出口参数:无******************************************************************************* **************/double Read_Disp_Temperature(void){uint a = 0, b = 0, c = 0,d = 0,e = 0, t = 0;double tt = 0;DS18B20_Init();DS18B20_Write1Byte(0xCC); // 跳过读序号列号的操作DS18B20_Write1Byte(0x44); // 启动温度转换DS18B20_Init();DS18B20_Write1Byte(0xCC); // 跳过读序号列号的操作DS18B20_Write1Byte(0xBE); // 读取温度寄存器a = DS18B20_Read1Byte();b = DS18B20_Read1Byte();t = b;t <<= 8;t = t | a;//tt = t * 0.0625;return t;}//***************************************************************** //函数名称:delay(uint t)延时函数。
12864液晶经典驱动程序,一看就懂,有详细的注释(C语言编写,不看后悔)
这个程序写得很好,自己也是看的别人写的很好的程序,但是它代码不够完整,注释不详细,经过自己的修改加以完善。
另外我发现很多的测试代码都没有附上测试代码效果图,在本人的代码下面有自己拍的效果图,填补不足之处。
自己在找资料的过程中花掉很多的财富值,如果大家看得起该代码请评五分加以下载,在下感激不尽!/*******************************************程序名称:12864液晶经典驱动程序(带中文字库)功能:12864测试程序修改作者:王程修改时间:2014年2月5日23:22:32********************************************/#include<reg52.h>#define uchar unsigned char#define uint unsigned int// P0 为数据口sbit LCD_RS=P2^0; //数据、命令选择端口sbit LCD_RW=P2^1; //液晶读写控制sbit LCD_EN=P2^2; //液晶使能控制sbit PSB = P2^4; //模式选择1为并行0为串行sbit RES = P2^5;//复位端口低电平复位,上电之前进行复位,为了稳定!unsigned char code DAT[] = "我爱我家"; //测试显示的汉字,下面的A为测试显示的英文/*******************************************函数名称:Delay_1ms (晶振为12M)功能:延时约1ms的时间参数:无返回值:无********************************************/void delay_1ms(uint x){uint i,j;for(j = 0;j < x;j++)for(i=0;i<110;i++);}/*******************************************函数名称:write_cmd功能:向液晶中写控制命令参数:cmd--控制命令返回值:无********************************************/void write_cmd(uchar cmd){uchar lcdtemp = 0;LCD_RS=0;LCD_RW=1;do //经典判忙方式,精简{LCD_EN=1;delay_1ms(2);lcdtemp = P0;LCD_EN=0;}while(lcdtemp & 0x80);LCD_RW=0;P0 = cmd;LCD_EN=1;delay_1ms(5);LCD_EN=0;}/*******************************************函数名称:write_data功能:向液晶中写显示数据参数:dat--显示数据返回值:无********************************************/ void write_data(uchar dat){uchar lcdtemp = 0;LCD_RS=0;LCD_RW=1;do //经典判忙方式{LCD_EN=1;delay_1ms(2);lcdtemp = P0;LCD_EN=0;}while(lcdtemp & 0x80);LCD_RS=1;LCD_RW=0;P0 = dat;LCD_EN=1;delay_1ms(5);LCD_EN=0;}/*******************************************函数名称:lcd_int功能:初始化液晶模块参数:无返回值:无********************************************/void lcd_int(void){delay_1ms(40); //大于40MS的延时程序PSB=1; //设置为8BIT并口工作模式delay_1ms(1); //延时RES=0; //复位delay_1ms(1); //延时RES=1; //复位置高delay_1ms(10);write_cmd(0x30); //基本指令集delay_1ms(5);write_cmd(0x30); //基本指令集delay_1ms(5);write_cmd(0x08); // 地址归位delay_1ms(5);write_cmd(0x10); //光标设置delay_1ms(5);write_cmd(0x0c); //整体显示打开,游标关闭delay_1ms(5);write_cmd(0x01); //清除显示delay_1ms(5);write_cmd(0x06); //游标右移delay_1ms(5);write_cmd(0x80); //设定显示的起始地址delay_1ms(5);}函数名称:main功能:测试1602初始化函数模块参数:无返回值:无********************************************/void main(){uchar i;char *a = DAT;//使用指针,指向字符串地址lcd_int();write_data('A'); //每个显示都是用的两个字节,必须写两次write_data(' ');//再写一次,不然乱显示(如果不加就再显示一个别的字母)for(i=0;i<8;i++){write_data(*a);//将地址当作数据传给12864a++;//一个汉字传送两次,因为一个汉字占用两个字节,两次传完}write_cmd(0x90);//一行显示八个汉字,第一行0x80-87,第二行0x90开始a = DAT;for(i=0;i<8;i++){write_data(*a);a++;}while(1); //停住,不然重复的初始化显示,作为测试用}/*******************************************代码测试效果图片两两张由于曝光度影响,拍了两张********************************************/。
(整理)单片机液晶屏C程序.
for(i=0;i<2;i++)
{
lcd_wdat(DIS41[i]);
}
}
else
{
PORTB&=~(1<<0);
lcd_pos(4,3);
for(i=0;i<2;i++)
{
lcd_wdat(DIS42[i]);
}
}
////////*******“运行”显示**************************
}
DDRC=0xFF;
PORTD&=~(1<<3);
}
//******************************************************************
void lcd_wcmd(uchar cmd)
{
lcd_busy();
DDRD=0xF8;
PORTD&=~(1<<5);
{
lcd_wcmd(0x30); //基本指令操作
lcd_wcmd(0x30);
delay(1);
lcd_wcmd(0x0c); //显示开,关光标
delay(1);
lcd_wcmd(0x01); //清除LCD的显示内容
delay(1);
}
//*****************************************************************
lcd_pos(4,6);
(2)可能造成轻度环境影响的建设项目,编制环境影响报告表,对产生的环境影响进行分析或者专项评价;for(i=0;i<4;i++)
LCD1602液晶显示C程序
1、关于1602液晶的电气参数一般市场上的1602液晶使用的驱动器为HD44780U或HD44780S,市场上大部分液晶用的是后者。
HD44780S的供电电压为5V±10%,而HD44780U的供电电压为2.7V~5.5V。
也就是说,绝大部分1602液晶只能工作在5V电压下,其供电电压必须为5V。
经过试验发现,1602液晶的供电虽然必须为5V,但其控制总线和数据总线可以用3.3V电平(因为对于TTL电平,一般大于2.5V以上都算高电平,不过最可靠的是大于3.6V),只不过在3.3V电平下,数据的通信速度会大大降低,这一点需要在写驱动时注意。
需要时,可以将MCU的IO配置为漏极开路方式,用上拉电阻拉到5V电平;实在不能配置为漏极开路方式时,请查阅MCU的电气参数,在允许的条件下,直接使用电阻弱上拉也可以。
2、硬件连接1602液晶可以使用4位或8位通信模式,通信可以是双向的或单向的,双向通信主要是为了读取LCD忙标志和AC地址寄存器和DDRAM和CGRAM中的值,一般用处不大,因为1602不支持点阵绘图功能。
在连接时,注意4位通信方式下,LCD只使用DB7~DB4,一般情况下会使用MCU 某端口的高4位或低4位与之连接,注意写驱动时在必要的情况下对端口的其它几位要保护,防止破坏其数据;当然如果没有使用其它4位时则不必要保护(奇怪,不使用其它4位干嘛用4位通信方式嘛?)。
还要注意的是,如果需要双向通信,则必须选择既能做输入又能做输出的IO口,特别是对于RS、RW和E这三条控制线,若能选择支持位寻址的IO口则可以方便编程。
3、底层驱动问题通用1602液晶的时序如图:(1)、写时序(2)、读时序总的说来,按照这个时序图来编写驱动程序是不会出什么问题的,只是要深刻理解时序图中各参数的涵义。
编程中要特别关注E这根控制线。
写操作的时序应该是:①、RS=0(写指令寄存器)或RS=1(写数据寄存器);②、RW=0(写操作);③、将数据写到数据线上;④、E=1;⑤、E=0 。
液晶显示c语言程序
/********************************************************************* 文件名:定时器的使用.c* 描述:* 创建人:东流,2009年4月8日* 版本号:2.0***********************************************************************/ #include <reg52.h>#include <string.h>#define uchar unsigned char#define uint unsigned intuchar code IC_DAT[]。
uchar code pic1[]。
uchar code pic2[]。
uchar code pic3[]。
sbit le1 = P2^0。
sbit le2 = P2^1。
sbit RS = P2^5。
sbit WRD = P2^6。
sbit E = P2^7。
sbit PSB = P2^2。
sbit RES = P2^4。
void TransferData(char data1,bit DI)。
void display(void)。
void display_grapic(void)。
void delayms(uint n)。
void DisplayLine(uchar line1,uchar line2)。
void DisplayGraphic(uchar code *adder)。
void delay(uint m)。
void lcd_mesg(uchar code *adder1)。
uchar code IC_DAT[]={" HOT-51 "" "" 单片机开发板"" 东流电子"}。
uchar code IC_DAT2[]={" 白日依山尽," //0x80" 欲穷千里目," //0x90" 黄河入海流。
液晶显示控制(1602)c语言实例
DB1 低4位三态、 双向数据总线 1位
9
DB2 低4位三态、 双向数据总线 2位
10 DB3 低4位三态、 双向数据总线 3位
11 DB4 高4位三态、 双向数据总线 4位
12 DB5 高4位三态、 双向数据总线 5位
13 DB6 高4位三态、 双向数据总线 6位
14 DB7 高4位三态、 双向数据总线 7位 (最高位)(也是busy flag)
4 可视角度
➢大多数光都是从屏幕中垂直射出来的,所以从某一个 较大的角度观看液晶显示器时,便不能看到原本的颜 色,甚至只能看到全白或全黑。为了解决这个问题, 到目前为止有三种比较流行的技术,分别是: TN+FILM、IPS(IN-PLANE -SWITCHING)和 MVA(MULTI-DOMAIN VERTICAL align MENT)。
(8)写指令38H:显示模式设置; (9)写指令08H:显示关闭;
(10)写指令01H:显示清屏; (11)写指令06H:显示光标移动设置; (12)写指令0CH:显示开及光标设置;
2011年春
LCD 1602初始化函数实例
void Initialize_LCD( )
{
Write_LCD_Command(0x38);
4 RS RS为寄存器选择,高电平1时选择数据寄 存器、低电平0时选择指令寄存器。
5 R/W R/W为读写信号线,高电平时进行读操作, 低电平时进行写操作。
6E
E(或EN)端为使能(enable)端,下降沿使 能。
2011年春
引脚 符号
功能说明
7
DB0 低4位三态、 双向数据总线 0位
(最低位)
8
(2)写指令: 输入:RS=L;RW=L; D0~D7=指令码;E=高脉冲
PIC单片机C语言编程实例——液晶显示模块编程
PIC单片机C语言编程实例——液晶显示模块编程15.2.2 MG-12232模块的编程下面以图15.1的接口电路为例。
液晶显示区域分成E1边和E2边,下面只含E1边的程序(表15.1中E1=1,E2=0),E2边(表15.1中E1=0,E2=1)类推。
在系统程序的初始化部分,应对程序中用到的寄存器和临时变量作说明,如:unsigned char TRANS;unsigned char PAGEADD; //存放页地址寄存器unsigned char PAGENUM; //存放总页数寄存器unsigned char CLMSUM; //存放总列数寄存器unsigned char CLMADD; //存放列地址寄存器unsigned char WRITE; //存放显示数据寄存器unsigned char row; //存放显示起始行寄存器unsigned char i,k; //通用寄存器//系统各口的输入输出状态初始化子程序void INITIAL(){ADCON1=0X87; //设置PORTA口和PORTE口为数字I/O口TRISA3=0;TRISB0=0;TRISE=0X00; //设置液晶的4个控制脚为输出}//读液晶显示器状态子程序void LCDSTA1(){while(1){TRISD=0XFF; //设置D口为输入RB0=1; //E1=1RA3=0; //E2=0RE0=1; //R/W=1RE1=0; //A0=0if(RD7==0) break; //为忙状态,则继续等待其为空闲}}//对液晶显示器发指令子程序(指令保存在TRANS寄存器中)void TRANS1(){LCDSTA1(); //判断液晶是否为忙TRISD=0X00; //置D口为输出RB0=1; //E1=1RA3=0; //E2=0RE0=0; //R/W=0RE1=0; //A0=0PORTD=TRANS; //需要写入的命令字送入数据线RB0=0; //E1=0写入指令RE0=1; //R/W=1}//对液晶显示器写数据子程序(数据保存在WRITE寄存器中) void WRITE1(){TRANS=CLMADD; //设置列地址TRANS1();LCDSTA1(); //查询液晶是否为空闲TRISD=0X00; //D口为输出RB0=1;//E1=1RA3=0;//E2=0RE0=0;//R/W=0RE1=1;//A0=1PORTD=WRITE; //需要写入的数据放入D口RB0=0; //E1=0,写入数据CLMADD++; //列地址加1RE0=1; //R/W=1}//开E1显示子程序void DISP1(){while(1){TRANS=0XAF;TRANS1(); //送出控制命令LCDSTA1(); //判断液晶是否为空闲TRISD=0XFF; //设置D口为输入RB0=1; //E1=1RA3=0; //E2=0RE0=1; //R/W=1RE1=0; //A0=0if(RD5==0) break; //如果液晶没被关闭,则继续关}}//E1边清屏子程序void CLEAR1(){PAGEADD=0xB8; //设置页地址代码for(PAGENUM=0X04;PAGENUM>0;PAGENUM-){TRANS=PAGEADD;TRANS1();CLMADD=0x00; //设置起始列for(CLMSUM=0X50;CLMSUM>0;CLMSUM-){LCDSTA1(); //判断液晶是否为空闲WRITE=0X00;WRITE1(); //写入00H以清屏}PAGEADD++; //页号增1}}//关 E1显示子程序void DISOFF1(){while(1){TRANS=0XAE;TRANS1(); //发出控制命令LCDSTA1(); //判断液晶是否为空闲TRISD=0XFF; //D口设置为输入RB0=1; //E1=1RA3=0; //E2=0RE0=1; //R/W=1RE1=0; //A0=0if(RD5==1) break; //如果液晶没被关闭,则继续关}}有了以上的通用子程序,就可以构造出各种显示程序,如字符。
液晶驱动C语言.
#include "lcd.h"#include "fonts.h"static vu16 TextColor = 0x0000, BackColor = 0xFFFF;****************************************************************************** Function Name * Description* Input* Output* Return: Delay_LCD: Inserts a delay time.: nCount: specifies the delay time length. : None: None******************************************************************************* /void Delay_LCD(u16 n){ u16 i,j; for (i = 0;i<n;++i) for(j=0;j<3000;++j);}/****************************************************************************** ** Function Name * Description* Input* Output* Return: STM3210B_LCD_Init : Initializes the LCD.: None: None: None******************************************************************************* /void STM3210B_LCD_Init(void) {vu16 dummy = 0;//总线配置LCD_CtrlLinesConfig();LCD_WriteReg(0x0000,0x0001);Delay_LCD(1000);LCD_WriteReg(0x0001,0x0000);LCD_WriteReg(0x0010,0x1790);LCD_WriteReg(0x0060,0x2700);LCD_WriteReg(0x0061,0x0001);LCD_WriteReg(0x0046,0x0002);LCD_WriteReg(0x0013,0x8010);LCD_WriteReg(0x0012,0x80fe);LCD_WriteReg(0x0002,0x0500);LCD_WriteReg(0x0003,0x1030);LCD_WriteReg(0x0030,0x0303);LCD_WriteReg(0x0031,0x0303);LCD_WriteReg(0x0032,0x0303);LCD_WriteReg(0x0033,0x0300);LCD_WriteReg(0x0034,0x0003);LCD_WriteReg(0x0035,0x0303);LCD_WriteReg(0x0036,0x0014);LCD_WriteReg(0x0037,0x0303);LCD_WriteReg(0x0038,0x0303);LCD_WriteReg(0x0039,0x0303);LCD_WriteReg(0x003a,0x0300);LCD_WriteReg(0x003b,0x0003);LCD_WriteReg(0x003c,0x0303);LCD_WriteReg(0x003d,0x1400);LCD_WriteReg(0x0092,0x0200);LCD_WriteReg(0x0093,0x0303);LCD_WriteReg(0x0090,0x080d);LCD_WriteReg(0x0003,0x1018);LCD_WriteReg(0x0007,0x0173);dummy = LCD_ReadReg(0);}/******************************************************************************** Function Name : LCD_SetTextColor* Description : Sets the Text color.* Input : - Color: specifies the Text color code RGB(5-6-5).* Output : - TextColor: Text color global variable used by LCD_DrawChar * and LCD_DrawPicture functions.* Return : None*******************************************************************************/void LCD_SetTextColor(vu16 Color){TextColor = Color;}/******************************************************************************** Function Name : LCD_SetBackColor* Description : Sets the Background color.* Input * Output * : - Color: specifies the Background color code RGB(5-6-5). : - BackColor: Background color global variable used byLCD_DrawChar and LCD_DrawPicture functions.* Return : None*******************************************************************************void LCD_SetBackColor(vu16 Color){BackColor = Color;}/******************************************************************************* Function Name : LCD_ClearLine* Description : Clears the selected line.* Input : - Line: the Line to be cleared.* This parameter can be one of the following values: * - Linex: where x can be 0..9* Output : None* Return : None******************************************************************************* / void LCD_ClearLine(u8 Line){LCD_DisplayStringLine(Line, " ");}/******************************************************************************* Description* Input* Output* Return}'********************************************* ******************************** Function Name : LCD_SetCursorDescription Input : Sets the cursor position.: - Xpos: specifies the X position.* Output * Return- Ypos: specifies the Y position. : None: None******************************************************************************* /void LCD_SetCursor(u8 Xpos, u16 Ypos){LCD_WriteReg(R32, Xpos);LCD_WriteReg(R33, Ypos);'***************************************************************************** Function Name * Description* Input: LCD_DrawChar: Draws a character on LCD.: - Xpos: the Line where to display the character shape.This parameter can be one of the following values: - Linex: where x can be 0..9- Ypos: start column address.- c: pointer to the character data.* Output * Return : None : None******************************************************************************* /void LCD_DrawChar(u8 Xpos, u16 Ypos, uc16 *c){u32 index = 0, i = 0; u8 Xaddress = 0;Xaddress = Xpos;LCD_SetCursor(Xaddress, Ypos);for(index = 0; index < 24; index++){LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ for(i = 0; i< 16; i++){ if((c[index] & (1 << i)) == 0x00) {LCD_WriteRAM(BackColor);} else{LCD_WriteRAM(TextColor);}}Xaddress++;LCD_SetCursor(Xaddress, Ypos);}}/******************************************************************************* Function Name : LCD_DisplayChar* Description : Displays one character (16dots width, 24dots height).* Input : - Line: the Line where to display the character shape .* This parameter can be one of the following values:* - Linex: where x can be 0..9* - Column: start column address.* - Ascii: character ascii code, must be between 0x20 and 0x7E. * Output : None* Return : None******************************************************************************* /void LCD_DisplayChar(u8 Line, u16 Column, u8 Ascii){Ascii -= 32;LCD_DrawChar(Line, Column, &ASCII_Table[Ascii * 24]);}/******************************************************************************* Function Name : LCD_DisplayStringLine* Description : Displays a maximum of 20 char on the LCD.* Input : - Line: the Line where to display the character shape .* This parameter can be one of the following values:* - Linex: where x can be 0..9* - *ptr: pointer to string to display on LCD.* Output : None* Return : None******************************************************************************* /void LCD_DisplayStringLine(u8 Line, u8 *ptr){u32 i = 0;u16 refcolumn = 319;//319;}while ((*ptr != 0) && (i < 20))// 20{ LCD_DisplayChar(Line, refcolumn, *ptr); refcolumn -= 16;ptr++;i++;}} * - Width: display window width. * Output* Return : None : None*******************************************************************************/void LCD_SetDisplayWindow(u8 Xpos, u16 Ypos, u8 Height, u16 Width) {if(Xpos >= Height){LCD_WriteReg(R80, (Xpos - Height + 1));}else{LCD_WriteReg(R80, 0);}LCD_WriteReg(R81, Xpos);if(Ypos >= Width){LCD_WriteReg(R82, (Ypos - Width + 1));}else{LCD_WriteReg(R82, 0);}/* Vertical GRAM End Address */ LCD_WriteReg(R83, Ypos); LCD_SetCursor(Xpos, Ypos);* Function Name* Description* Input*: LCD_SetDisplayWindow : Sets a display window : - Xpos: specifies the X buttom left position. - Ypos: specifies the Y buttom left position. * - Height: display window height.*****************************************************************************'****************************************************************************Function Name : LCD_WindowModeDisable: Disables LCD Window mode. * Return******************************************************************************* /void LCD_WindowModeDisable(void){LCD_SetDisplayWindow(239, 0x13F, 240, 320);LCD_WriteReg(R3, 0x1018);}/******************************************************************************* Function Name : LCD_DrawLine* Description: Displays a line. * Input: - Xpos: specifies the X position. *- Ypos: specifies the Y position. *- Length: line length. *- Direction: line direction. *This parameter can be one of the following values: Vertical* or Horizontal.* Output : None * Return : None******************************************************************************* /void LCD_DrawLine(u8 Xpos, u16 Ypos, u16 Length, u8 Direction) {u32 i = 0; LCD_SetCursor(Xpos, Ypos); if(Direction == Horizontal) {LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ for(i = 0; i < Length; i++){ LCD_WriteRAM(TextColor);}} else {for(i = 0; i < Length; i++) LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */LCD_WriteRAM(TextColor);Xpos++;LCD_SetCursor(Xpos, Ypos);}}}/******************************************************************************Input* Output: None : NoneDescription: None{* Function Name * Description* Input*: LCD_DrawRect: Displays a rectangle.: - Xpos: specifies the X position.- Ypos: specifies the Y position.* - Height: display rectangle height. * - Width: display rectangle width.* Output * Return : None : None*******************************************************************************void LCD_DrawRect(u8 Xpos, u16 Ypos, u8 Height, u16 Width) {LCD_DrawLine(Xpos, Ypos, Width, Horizontal); LCD_DrawLine((Xpos + Height), Ypos, Width, Horizontal);LCD_DrawLine(Xpos, Ypos, Height, V ertical);LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, Vertical);}/******************************************************************************** Function Name * Description* Input*: LCD_DrawCircle: Displays a circle.: - Xpos: specifies the X position.- Ypos: specifies the Y position.* - Height: display rectangle height. * - Width: display rectangle width.* Output * Return : None : None******************************************************************************* /void LCD_DrawCircle(u8 Xpos, u16 Ypos, u16 Radius){s32 D; u32 CurX;u32 CurY;D = 3 - (Radius << 1);CurX = 0;CurY = Radius;while (CurX <= CurY){LCD_SetCursor(Xpos + CurX, Ypos + CurY);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);LCD_SetCursor(Xpos + CurX, Ypos - CurY);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);LCD_SetCursor(Xpos - CurX, Ypos + CurY);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);LCD_SetCursor(Xpos - CurX, Ypos - CurY);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);LCD_SetCursor(Xpos + CurY , Ypos + CurX);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);LCD_SetCursor(Xpos + CurY , Ypos - CurX);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);LCD_SetCursor(Xpos - CurY , Ypos + CurX);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);LCD_SetCursor(Xpos - CurY , Ypos - CurX);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor);if (D < 0){D += (CurX << 2) + 6;}else{D += ((CurX - CurY) << 2) + 10;CurY--;}CurX++;}}/****************************************************************************** ** Function Name : LCD_DrawMonoPict* Description : Displays a monocolor picture.* Input* Output * Return : - Pict: pointer to the picture array. : None : None*******************************************************************************/void LCD_DrawMonoPict(uc32 *Pict){u32 index = 0, i = 0;LCD_SetCursor(0, 319);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ for(index = 0; index < 2400; index++) {for(i = 0; i < 32; i++){if((Pict[index] & (1 << i)) == 0x00){LCD_WriteRAM(BackColor);}else{LCD_WriteRAM(TextColor);}}}} /******************************************************************************** Function Name : LCD_WriteBMP* Description : Displays a bitmap picture loaded in the internal Flash.* Input : - BmpAddress: Bmp picture address in the internal Flash.* Output: None * Return : None*******************************************************************************/ void LCD_WriteBMP(u32 BmpAddress){u32 index = 0, size = 0;size = *(vu16 *) (BmpAddress + 2);size |= (*(vu16 *) (BmpAddress + 4)) << 16;index = *(vu16 *) (BmpAddress + 10);index |= (*(vu16 *) (BmpAddress + 12)) << 16; size = (size - index)/2;BmpAddress += index;LCD_WriteReg(R3, 0x1008); LCD_WriteRAM_Prepare(); for(index = 0; index < size; index++) {LCD_WriteRAM(*(vu16 *)BmpAddress);BmpAddress += 2;}LCD_WriteReg(R3, 0x1018);/******************************************************************************: LCD_WriteReg : Writes to the selected LCD register. : - LCD_Reg: address of the selected register.- LCD_RegValue: value to write to the selected register. : None : None/void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue){GPIOB->BRR = 0x0200;//LCD_NCS_LOW(); GPIOB->BRR = 0x0100; //LCD_RS_LOW();GPIOB->BSRR = 0x0020; //LCD_NWR_HIGH();GPIOC->ODR = LCD_Reg; //GPIO_Write(LCD_DataPort,LCD_Reg);GPIOB->BRR = 0x0020; //LCD_NWR_LOW();GPIOB->BSRR = 0x0020; //LCD_NWR_HIGH();Function NameDescriptionInput * Output* Return*******************************************************************************GPIOB->BSRR = 0x0100; //LCD_RS_HIGH();GPIOC->ODR = LCD_RegValue; GPIOB->BRR = 0x0020; GPIOB->BSRR = 0x0020; GPIOB->BSRR = 0x0100; } /****************************************************************************** * //GPIO_Write(LCD_DataPort,LCD_RegValue); //LCD_NWR_LOW(); //LCD_NWR_HIGH(); //LCD_RS_HIGH(); Function Name Description Input * Output * Return : LCD_ReadReg : Reads the selected LCD Register.: None : None : LCD Register Value.*******************************************************************************u16 LCD_ReadReg(u8 LCD_Reg) { u16 temp; GPIOB->BRR = 0x0200; GPIOB->BRR = 0x0100; GPIOB->BSRR = 0x0020; //LCD_NCS_LOW(); //LCD_RS_LOW(); //LCD_NWR_HIGH(); GPIOC->ODR = LCD_Reg; //GPIO_Write(LCD_DataPort,LCD_Reg); GPIOB->BRR = 0x0020; //LCD_NWR_LOW(); GPIOB->BSRR = 0x0020; GPIOB->BSRR = 0x0100; //LCD_NWR_HIGH(); //LCD_RS_HIGH(); LCD_BusIn(); GPIOB->BRR = 0x0400; //LCD_NRD_LOW(); temp = GPIOC->IDR; //temp=GPIO_ReadInputData(LCD_DataPort); GPIOB->BSRR = 0x0400; //LCD_NRD_HIGH(); LCD_BusOut(); GPIOB->BSRR = 0x0200; //LCD_NCS_HIGH(); return temp; } /****************************************************************************** * * Function Name : LCD_WriteRAM_Prepare * Description : Prepare to write to the LCD RAM. * Input : None* Output : None* Return : None******************************************************************************* /void LCD_WriteRAM_Prepare(void){GPIOB->BRR = 0x0200; GPIOB->BRR = 0x0100; GPIOB->BSRR = 0x0020; //LCD_NCS_LOW(); //LCD_RS_LOW();//LCD_NWR_HIGH();} *GPIOC->ODR = R34;GPIOB->BRR = 0x0020;GPIOB->BSRR = 0x0020;GPIOB->BSRR = 0x0100;//GPIO_Write(LCD_DataPort,R34);//LCD_NWR_LOW();//LCD_NWR_HIGH();//LCD_RS_HIGH();GPIOB->BSRR = 0x0200; //LCD_NCS_HIGH();****************************************************************************Function Name : LCD_WriteRAMDescription Input* Output* Return : Writes to the LCD RAM.: - RGB_Code: the pixel color in RGB mode (5-6-5). : None : None*******************************************************************************void LCD_WriteRAM(u16 RGB_Code){GPIOB->BRR = 0x0200; //LCD_NCS_LOW();GPIOB->BSRR = 0x0100; //LCD_RS_HIGH();GPIOB->BSRR = 0x0020; //LCD_NWR_HIGH();GPIOC->ODR = RGB_Code; //GPIO_Write(LCD_DataPort,RGB_Code); GPIOB->BRR = 0x0020; //LCD_NWR_LOW();GPIOB->BSRR = 0x0020; GPIOB->BSRR = 0x0100; //LCD_NWR_HIGH(); //LCD_RS_HIGH();GPIOB->BSRR = 0x0200; //LCD_NCS_HIGH();}/****************************************************************************** ** Function Name : LCD_ReadRAM* Description : Reads the LCD RAM.* Input : None* Output : None* Return : LCD RAM Value.*******************************************************************************/u16 LCD_ReadRAM(void){u16 temp;GPIOB->BRR = 0x0200; //LCD_NCS_LOW();GPIOB->BRR = 0x0100; //LCD_RS_LOW();GPIOB->BSRR = 0x0020; //LCD_NWR_HIGH();GPIOC->ODR = R34; //GPIO_Write(LCD_DataPort,R34);GPIOB->BRR = 0x0020; //LCD_NWR_LOW();GPIOB->BSRR = 0x0020; //LCD_NWR_HIGH();GPIOB->BSRR = 0x0100; //LCD_RS_HIGH();LCD_BusIn();GPIOB->BRR = 0x0400; //LCD_NRD_LOW();temp = GPIOC->IDR; //temp=GPIO_ReadInputData(LCD_DataPort);GPIOB->BSRR = 0x0400; //LCD_NRD_HIGH();LCD_BusOut();GPIOB->BSRR = 0x0200; //LCD_NCS_HIGH();return temp;}/******************************************************************************** Function Name : LCD_PowerOn* Description : Power on the LCD.* Input : None* Output : None* Return : None*******************************************************************************/void LCD_PowerOn(void){LCD_WriteReg(R16, 0x0000);LCD_WriteReg(R17, 0x0000);LCD_WriteReg(R18, 0x0000);LCD_WriteReg(R19, 0x0000);Delay_LCD(20);LCD_WriteReg(R16, 0x17B0);LCD_WriteReg(R17, 0x0137);Delay_LCD(5);LCD_WriteReg(R18, 0x0139);Delay_LCD(5);LCD_WriteReg(R19, 0x1d00);LCD_WriteReg(R41, 0x0013);Delay_LCD(5);LCD_WriteReg(R7, 0x0173);}/******************************************************************************** Function Name : LCD_DisplayOn* Description : Enables the Display.* Input : None* Output: None * Return : None******************************************************************************* /void LCD_DisplayOn(void){LCD_WriteReg(R7, 0x0173);}/******************************************************************************* Function Name : LCD_DisplayOff* Description : Disables the Display.* Input: None * Output: None * Return : None******************************************************************************* /void LCD_DisplayOff(void){LCD_WriteReg(R7, 0x0);}/******************************************************************************: LCD_CtrlLinesConfig: Configures LCD Control lines.Push-Pull mode.Function Name Description Input* Output * Return: None : None: None *******************************************************************************: Configures the Parallel interface for LCD(PortC)* Input : None* Output : None* Return : None*******************************************************************************//void LCD_CtrlLinesConfig(void){GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOB, &GPIO_InitStructure);LCD_BusOut();GPIOB->BSRR |= 0x0620;}/******************************************************************************* Function Name : LCD_BusIn* Description : Configures the Parallel interface for LCD(PortC)* Input : None* Output : None* Return : None*******************************************************************************/void LCD_BusIn(void){GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_Init(GPIOC, &GPIO_InitStructure);}/******************************************************************************* Function Name : LCD_BusOut* Descriptionvoid LCD_BusOut(void)}GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOC, &GPIO_InitStructure);}/****************************************************************************** * Function Name : LCD_DrawPicture: Displays a 16 color picture. : - picture: pointer to the picture array. : None * Return : None******************************************************************************* /void LCD_DrawPicture(const u8* picture){int index;LCD_SetCursor(0x00, 0x0000);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */for(index = 0; index < 76800; index++){LCD_WriteRAM(picture[2*index+1]<<8|picture[2*index]);* Function Name : LCD_Clear* Description* Input* Output: Clears the hole LCD.: Color: the color of the background.: None: None******************************************************************************* /void LCD_Clear(u16 Color){u32 index = 0;LCD_SetCursor(0x00, 0x0000);LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ for(index = 0; index < 76800;index++) {LCD_WriteRAM(Color);}。
液晶显示c语言程序
-液晶显示-c语言程序————————————————————————————————作者:————————————————————————————————日期:ﻩ/*********************************************************************文件名:定时器的使用.c* 描述:* 创建人:东流,2009年4月8日* 版本号: 2.0***********************************************************************/#include<reg52.h>#include <string.h>#define ucharunsignedchar#define uintunsignedintuchar code IC_DA T[];ucharcode pic1[];uchar code pic2[];ucharcode pic3[];sbit le1= P2^0;sbitle2=P2^1;sbit RS = P2^5;sbit WRD =P2^6;sbit E= P2^7;sbit PSB=P2^2;sbitRES = P2^4;voidTransferData(char data1,bit DI);voiddisplay(void);void display_grapic(void);voiddelayms(uint n);void DisplayLine(uchar line1,ucharline2);void DisplayGraphic(uchar code *adder);void delay(uint m);void lcd_mesg(uchar code*adder1);uchar codeIC_DAT[]={"HOT-51 """"单片机开发板""东流电子"};uchar code IC_DAT2[]={"白日依山尽, "ﻩ//0x80" 欲穷千里目, "ﻩ//0x90"黄河入海流。
字符液晶显示器C语言
if(CYPOS==0)
lcdwc(CXPOS|0X80);
else if(CYPOS==1)
lcdwc(CYPOS|0XC0);
else if(CYPOS==2)
lcdwc((CXPOS+20)|0X80);
else
lcdwc((CXPOS+20)|0XC0);
}
voidcharcursornext(void)
}
}
voidputstrxy(ucharcx,ucharcy,ucharcode *s)
{
CXPOS=cx;
CYPOS=cy;
for(;*s!=0;s++)
{
putchar(*s);
charcursornext();
}
}
voidputstr(ucharcode *s)
{
for(;*s!=0;s++)
}
voidlcdwaitidle(void)
{
uchari;
for(i=0;i<20;i++)
// if((LCDCRREG&0X80)==0)
break;
}
{
CXPOS++;
if(CXPOS>19)
{
CXPOS=0;
CYPOS++;
CYPOS&=0X30;
}
}
voidlcdreset(void)
{
lcdwc(0x38);
delay3ms();
lcdwc(0x38);
delay3ms();
lcdwc(0x38);
液晶菜单程序
OCMJ4X8C液晶菜单显示程序(2008-08-08 09:13:13)标签:菜单程序液晶mainmenu it刚刚花了3天写了一个比较完善的菜单显示程序,分享给大家。
可能有很多不足,如果有什么b ug请发Email到yunjian4516@谢谢!未经作者允许请勿用于商业用途!菜单的数据结构定义//选项结构体的定义typedef struct option{signed ID; //选项标志()int *Label; //菜单显示字,连向wordstruct menu *next; //选项的下级菜单,若非菜单则为0}option;//菜单结构体的定义typedef struct menu{unsigned Maxopts; //该菜单所包含的选项数unsigned Maxoptsperpage; //定义LED每一页最多显示项目数(低2位有效,最多为3+1,最少为0+1)unsigned selection;struct menu *parmenu; //父菜单struct option *paropt;struct option *next; //该菜单的第一个项目}menu;//主菜单结构体的定义typedef struct Mainmenu{unsigned int Maxopts; //主菜单包含的项目数unsigned int maxhypes; //已定义的最大的层数(建立菜单时用到)struct option *curopt; //在建立菜单时记录当前的选项struct menu *curmenu; //在建立菜单时记录当前所在的菜单//上面两数据在对菜单操作时指向当前页的第一项struct menu *next; //主菜单}Mainmenu;菜单的处理函数定义/////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////#include "Menudefine.h"Mainmenu *Main_Menu;menu *Menu;option *Option;int Addmenu(unsigned *Label,unsigned maxopts,unsigned maxoptsperpage);int Addnext(unsigned *Label);Mainmenu * InitMainMenu(void);////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////添加菜单//////////////////////////////////////// /////参数简介: Label存储项目显示内容的指针,maxopts该菜单的项目数///// maxoptserpage该菜单下每页显示的项目数/////返回值类型:整型///// 正常返回0,主菜单建立完毕返回-1,内存不被分配返回1int Addmenu(unsigned *Label,unsigned maxopts,unsigned maxoptsperpage){int j;Menu = 0;if(!(Menu = (menu *)malloc(Menusize)))return 1; //内存空间申请失败,返回1Menu->Maxopts = maxopts;Menu->Maxoptsperpage = maxoptsperpage;Menu->selection = 1;Option = 0;if(!(Option = (option *)malloc(maxopts * Optionsize))) //内存空间申请失败,返回1 return 1;Menu->next = Option;Option -> ID = 0;//对Menu的初始化完毕if(Main_Menu->maxhypes) //如果不是主菜单{if(!Main_Menu->curopt->ID) //如果是该菜单第一个项目,则给项目ID赋值{Main_Menu->curopt->ID = Main_Menu->maxhypes<<4;}else{Main_Menu->curopt += 1;Main_Menu->curopt->ID = (Main_Menu->curopt - 1)->ID +1;}Main_Menu->curopt->next = Menu;Main_Menu->curopt->Label = Label;Menu->parmenu = Main_Menu->curmenu;Menu->paropt = Main_Menu->curopt;}else{Main_Menu->next = Menu;Menu->parmenu = Menu;Menu->paropt = 0;}Main_Menu->curopt = Option;Main_Menu->curmenu = Menu;Main_Menu->maxhypes ++;return 0;}//////////////////////////////////添加项目//////////////////////////////////////// /////参数简介:Label存储项目显示内容的指针/////返回值类型:整型///// 正常返回0,主菜单建立完毕返回-1int Addnext(unsigned *Label){if(!Main_Menu->curopt->ID) //如果是该菜单第一个项目,第一项ID赋值{Main_Menu->curopt->ID = Main_Menu->maxhypes<<4;}else //否则项目后移一项{Main_Menu->curopt += 1;Main_Menu->curopt->ID = (Main_Menu->curopt - 1)->ID +1;}Main_Menu->curopt->Label = Label;Main_Menu->curopt->next = 0;while((Main_Menu->curopt->ID&0x000f) == (Main_Menu->curmenu->Maxopts - 1)) //该层项目已经到达最大数,退出该层菜单{if(Main_Menu->curmenu == Main_Menu->next)return -1;Main_Menu->curopt = Main_Menu->curmenu->paropt;Main_Menu->curmenu = Main_Menu->curmenu->parmenu;}return 0;}//////////////////////////////////初始化主菜单///////////////////////////////////// Mainmenu * InitMainMenu(){Main_Menu = (Mainmenu *)malloc(sizeof(Mainmenu));Main_Menu->maxhypes = 0;Main_Menu->curmenu = 0;Main_Menu->curopt = 0;Main_Menu->next = 0;//Addmenu() Main_Menu添加菜单时Label可为0,其他参数根据用户需要自定义Addmenu(0,7,3);Addmenu("滨州学院",6,3);Addnext("物理系");Addnext("计算机系");Addnext("数学系");Addnext("政法系");Addnext("数学系");Addnext("自动化系");Addmenu("滨州政府",2,2);Addnext("市长办公室");Addnext("副市长办公室");Addnext("滨州人事局");Addnext("滨州法院");Addnext("滨州检察院");Addnext("滨州教育局");Addnext("滨州医学院");//此前代码需要用户自己填写Main_Menu->curmenu = Main_Menu->next;Main_Menu->curopt = Main_Menu->curmenu->next;return Main_Menu;}//进入选择项目//入口参数:selection为以Main_Menu->curopt为第一项,选择的项目序号unsigned Enter(int selection){unsigned ID ;if(selection > Main_Menu->curmenu->Maxoptsperpage) //选项无效,直接返回0xffff return 0xffff;selection --;ID = Main_Menu->curopt->ID + selection;if((ID&0x0f) >= Main_Menu->curmenu->Maxopts) //选项无效,直接返回0xffffreturn 0xffff;if((Main_Menu->curopt + selection)->next) //若选择菜单,则进入{Main_Menu->curmenu = (Main_Menu->curopt + selection)->next;Main_Menu->curopt = Main_Menu->curmenu->next;return 0;}else //若选择项目,则返回ID,外部函数根据返回值调用相关函数return Main_Menu->curopt->ID;}//返回上一级void Esc(){if(Main_Menu->curmenu != Main_Menu->next) //如果不是主菜单则返回上一级{Main_Menu->curmenu = Main_Menu->curmenu->parmenu;Main_Menu->curopt = Main_Menu->curmenu->next;}}//转到下一页void Nextpage(){int j;j = (Main_Menu->curopt->ID&0x0f) + Main_Menu->curmenu->Maxoptsperpage;if(j < Main_Menu->curmenu->Maxopts){Main_Menu->curopt += Main_Menu->curmenu->Maxoptsperpage;}}//转到上一页void Prepage(){int j;j = (Main_Menu->curopt->ID&0x000f) - Main_Menu->curmenu->Maxoptsperpage; //存在问题:此处不知什么原因,只要代入if就会出现错误if(j <= 0){Main_Menu->curopt = Main_Menu->curmenu->next;}else{Main_Menu->curopt -= Main_Menu->curmenu->Maxoptsperpage;}}菜单显示程序//LCD的显示函数,仅仅书写了显示函数,对于液晶驱动其他函数请大家自己补全unsigned line[4]={0x80,0x98,0x88,0x98};void menudisplay(Mainmenu *MainMenu){int i;int maxoptsperpage = MainMenu->curmenu->Maxoptsperpage;lcd_init_usual();for(i=0;i<maxoptsperpage;i++){code_write(line[i]);chinese_series((MainMenu->curopt + i)->Label);if(((MainMenu->curopt->ID&0x000f) + i + 1) >= (MainMenu->curmenu->Maxopts)) break;}code_write(0x96);}。
液晶显示C语言实现
{0x38,0x44,0x04,0x08,0x10,0x20,0x7c,0x00}, // 2
{0x78,0x84,0x04,0x38,0x04,0x84,0x78,0x00}}; // 3
// 中文字模库 16x16点阵
code unsigned char CCTAB[][32] ={
{ /*-- 文字: 相 --*/
0x10,0x10,0xD0,0xFF,0x30,0x50,0x90,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,
{/*-- 文字: 蚕 --*/
0x08,0x08,0x89,0x89,0x49,0x29,0x19,0xCF,0x09,0x19,0x29,0x49,0x89,0x89,0x88,0x00,
0x01,0x41,0x40,0x4F,0x45,0x45,0x45,0x3F,0x25,0x25,0x25,0x2F,0x70,0x21,0x00,0x00},
{/*-- 文字: 到 --*/
0x00,0x22,0x72,0x2A,0x26,0xE2,0x2A,0x72,0x22,0x00,0xF8,0x00,0x00,0xFF,0x00,0x00,
0x00,0x42,0xC2,0x42,0x22,0x3F,0x22,0x12,0x12,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00},
0x04,0x03,0x00,0xFF,0x00,0x00,0x01,0x00,0xFF,0x42,0x42,0x42,0x42,0xFF,0x00,0x00},
液晶(LCD)显示程序
************************************************************* 功能:液晶(LCD)显示程序;LCD: TRULY M19264-5A1 192*64点阵驱动器: S6B0108/S6B0107或HD61202及相兼容驱动器MPU: AT89S51开发环境: KEIL C51作者:纪长城日期: 2005年10月************************************************************/ #include <AT89X51.H>#include <ZKDot.h>#define DISP_OFF 0X3E //关闭显示#define DISP_ON 0X3F //打开显示#define START_ROW 0xC0 //设置起始行#define FIRST_PAGE 0xB8 //设置页#define START_YADD 0x40 //设置列/*----------------------------------------MPU端口定义----------------------------------------*/#define LCD_PORT P0#define LCD_RESET P2_2#define LCD_CSA P2_3#define LCD_CSB P2_4#define LCD_RS P2_5#define LCD_RW P2_6#define LCD_E P2_7/*------------------------------------------LCD的区选择程序:F为要选通的区,取值为0,1,2,对应选中三个区,取其它值选中第三个区--------------------------------------------*/void LCD_CSAB_SET(unsigned char block){if(0==block) //0 选择第一区{LCD_CSA=0;LCD_CSB=0;}else if(1==block) //1 选择第二区{LCD_CSA=0;LCD_CSB=1;}else //2 选择第三区{LCD_CSA=1;LCD_CSB=0;}}/*---------------------------------------//延时子程序----------------------------------------*/void Delay_xMs(unsigned int xMs){unsigned int i,j;for(i=0;i<xMs;i++)for(j=0;j<500;j++);}/*---------------------------------------向LCD中写入指令或指令,F=0 是指令,F=1是数据,bkock是选择写入哪个区 ---------------------------------------*/void LCD_Write( unsigned char Com,bit F){LCD_PORT = Com; //把数据输出LCD_RS = F; //确定是数据还是指令LCD_RW = 0;LCD_E = 1;LCD_E = 0; //电平下降沿数据写入控制器}/*----------------------------------------------坐标定位程序参数XPage 定位页取值0--7 共8页Y 横向定位取值0-191 共192列-----------------------------------------------*/ void LCD_XY_SET(unsigned char X,unsigned char YPage) {if(X<64) //在第一个区中{LCD_CSAB_SET(0);LCD_Write(FIRST_PAGE|YPage,0);LCD_Write(START_YADD|X,0);}else if(X<128) //在第二个区中{LCD_CSAB_SET(1);LCD_Write(FIRST_PAGE|YPage,0);LCD_Write(START_YADD|X-64,0);}else if(X<192) //在第三个区中{LCD_CSAB_SET(2);LCD_Write(FIRST_PAGE|YPage,0);LCD_Write(START_YADD|X-128,0);}}/*--------------------------------------------清除一行的子程序 H是要清的页,取值0-3--------------------------------------------*/void LCD_Clear_H(unsigned char H){unsigned char i,j,k;for (k=0;k<2;k++)for (i=0;i<3;i++){LCD_XY_SET(i*64,H*2+k);for(j=0;j<64;j++) LCD_Write(0x00,1);}}/*----------------------------------------LCD初始化程序----------------------------------------*/void LCD_Init(void){unsigned char i;LCD_E = 0;LCD_RW = 1;LCD_RS = 0;LCD_RESET= 0; //液晶模块复位Delay_xMs(10); //等待10mSLCD_RESET= 1; //液晶结束复位电平LCD_CSAB_SET(0);LCD_Write(DISP_ON,0); //模块1显示开LCD_CSAB_SET(1);LCD_Write(DISP_ON,0); //模块2显示开LCD_CSAB_SET(2);LCD_Write(DISP_ON,0); //模块3显示开for (i=0;i<4;i++) LCD_Clear_H(i); //清屏}/*----------------------------------------读display RAM---------------------------------------*/unsigned char LCD_READ_RAM(unsigned char x,unsigned char y) {unsigned char temp;LCD_XY_SET(x,y);LCD_RW = 1; //设置为读数据LCD_RS = 1;LCD_PORT =0XFF; //设置总线为高电平(空)LCD_E = 0;LCD_E = 1;temp =LCD_PORT;LCD_E =0;return temp;}/*------------------------------------------设制起始行,参数line为要起始的行,取值范围0--63,block 为要设置的块-----------------------------------------*/void LCD_LINE_SET(unsigned char line){unsigned char i;for (i=0;i<3;i++){LCD_CSAB_SET(i);LCD_Write(START_ROW|line,0);}}/*----------------------------------------------显示一个16*8的ASCII的程序 ,参数是ascii码值,x取值0-191,y取值0-3 只能显示四行------------------------------------------------*/bit LCD_PRINT_ASCII_1608(unsigned char ascii,unsigned char x,unsigned char y) {unsigned char i,j;if((x+8)>191)return 1;for(j=0;j<2;j++)for(i=j;i<16;i+=2){LCD_XY_SET(x+(i>>1),y*2+j);LCD_Write(AsciiDotBig[(ascii-0x20)*16+i],1);}return 0;}/*-------------------------------------------------查找汉字程序参数是内码的高8位与低8位---------------------------------------------------*/unsigned char LCD_SEARCH_HZ16(unsigned char h,unsigned char l){unsigned char i;for(i=0;i<17;i++)if(HZ[i].Index[0]==h&&HZ[i].Index[1]==l) return i;}/*----------------------------------------------------16点阵的汉字显示程序----------------------------------------------------*/bit LCD_PRINT_HZ16(unsigned char wz,unsigned char x,unsigned char y) {unsigned char i,j;if((x+16)>192) return 1;for(j=0;j<2;j++)for(i=0;i<16;i++){LCD_XY_SET(x+i,y*2+j);LCD_Write(HZ[wz].Msk[i+(16*j)],1);}return 0;}/*--------------------------------------------点操作函数,参数,X/Y,取值为192/64 F 0-1,0是画点,1是取消点---------------------------------------*/void LCD_POINT(unsigned char X,unsigned char Y,bit F){unsigned char i,j,k;i=Y&&0xF8;//i是X/8的余数Y>>=3; //x是X/8的商j=LCD_READ_RAM(X,Y);//空读操作j=LCD_READ_RAM(X,Y);//读出8位数据LCD_XY_SET(X,Y);k=1<<i;if(F){k=~k;LCD_Write(j&k,1);}elseLCD_Write(j|k,1);}/*----------------------------------------------- 显示图像的一个例子---------------------------------------------*/ void LCD_TU(void){unsigned char i,j;for(i=0;i<6;i++)for(j=0;j<100;j++){LCD_XY_SET(j,i);LCD_Write(TP[i*100+j],1);}}/*---------------------------------------------测试主函数-------------------------------------------------*/ main(){unsigned char i,j,k,m;unsigned char code c[]="我老婆最可爱最漂亮的";LCD_Init();LCD_TU();for(i=0;i<18;i+=2){m=LCD_SEARCH_HZ16(c[i],c[i+1]); LCD_PRINT_HZ16(m,i*8,3);}while(1){for(i=0;i<64;i++){LCD_LINE_SET(i);if(i==0)Delay_xMs(500); //等待elseDelay_xMs(20); //等待}}}。
可以直接显示汉字的19264液晶驱动C语言程序
注:本程序是经过KEIL编译通过,并通过实验板测试通过的,直接按硬件电路连接好显示屏然后将程序代码烧写入单片机即可点亮,将显示武汉2 字,并且随意设置坐标,就可以在不同的位置显示,使用很方便。
如果不能显示请将软件中的接口定义按照本电路图中的定义修改一下即可。
19264液晶显示屏的硬件连接图本人修改过的19264液晶驱动程序C51 示例.//***********硬件的连接*******************//RS=P3.0 R/W=P3.1 E=P3.2 CS1=P3.3 CS2=P3.4 CS3=P3.5 HZ=12M DB0-D7=P1.0-P1.7 Reset=InBosrd//***********************************************#include <REGX51.H>#include <stdlib.H>#include <intrins.H>#include <stdio.H>//****************引脚定义***********************sbit RS=P2^0;sbit RW=P2^1;sbit E=P2^2;sbit CSL=P2^3;sbit CSM=P2^4;sbit CSR=P2^5;sbit LED=P2^6;#define Col_Add 0x40 ///Y地址#define Page_Add 0xb8char code wu[]={/*-- 文字: 武--*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x10,0x10,0x14,0x14,0x14,0xD4,0x14,0x14,0x10,0x7F,0x90,0x12,0x14,0x10,0x10,0x00, 0x40,0x40,0x7F,0x20,0x20,0x1F,0x11,0x11,0x01,0x00,0x07,0x18,0x20,0x40,0xF0,0x00 };char code han[]={/*-- 文字: 汉--*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x10,0x60,0x01,0x86,0x60,0x04,0x1C,0xE4,0x04,0x04,0x04,0xE4,0x1C,0x04,0x00,0x00, 0x04,0x04,0x7E,0x01,0x40,0x20,0x20,0x10,0x0B,0x04,0x0B,0x10,0x30,0x60,0x20,0x00 };//********声明函数**********************************void Delay(unsigned int MS);void wtcom(void);//********检查Busy 测忙****************************void BusyL(void){CSL=1;CSM=0;CSR=0;wtcom();}void BusyM(void){CSL=0;CSM=1;CSR=0;wtcom();}void BusyR(void){CSL=0;CSM=0;CSR=1;wtcom();}void wtcom(void){RS=0;RW=1;P1=0xFF;E=1;_nop_();while(P1&0x80);E=0;_nop_();}//***********写指令************************************** void WriteCommangL(unsigned char CommandByte) //写左屏程序{void wtcom(); ///侧忙程序。
C语言源程序LCD
C语言源程序//******** 小波电子工作室 All rights reserved****** //******** 个人主页:/niejinbo **//******** 文件名: lcd_0712.c **************//******** 功能概要: LCD液晶显示字符 ***********//******** MCU: STC89C52 晶振:11.0592Mhz ***********//******** 设计者:聂金波 ************//******** 完成日期: 2008-07-12 ************//******** 当前版本: 0712_1 ************//******** 改进说明:暂无 ************//**********头文件区************#include<reg52.h>#include<math.h>#include<absacc.h>#include<intrins.h>//*********宏定义方便使用*******#define uchar unsigned char#define uint unsigned int//*********定义变量区***********sbit dula=P2^6; //数码管段选sbit wela=P2^7; //数码管位选sbit lcden=P3^4; //LCD使能信号sbit lcdrs=P3^5; //LCD数据/命令选择信号uchar code tab_nie[]="Nie Jinbo";uchar code tab_index[]="";//*********函数声明区***********void lcd_dis(); //lcd显示函数void lcd_init(); //LCD初始化函数void write_com(uchar); //写命令函数void write_data(uchar); //写数据函数void delay(uint); //延时函数//*********主函数开始***********void main(){lcd_dis();while(1);}//*********lcd显示函数开始******void lcd_dis(){uchar n;lcd_init();write_com(1);write_com(0x80+4); //设置光标位置for(n=0;n<9;n++){write_data(tab_nie[n]);delay(2);}write_com(0x80+0x40); // 设置光标位置for(n=0;n<15;n++){write_data(tab_index[n]);delay(2);}}//*********LCD初始化函数开始****void lcd_init(){dula=0;wela=0; // 关闭数码管显示lcden=0;write_com(0x38); //设置显示模式:16X2,5X7,8位数据接口write_com(0x0f); //开显示,显示光标,光标闪烁write_com(0x06); //读写一个字符后,地址指针及光标加一,且光标加一整屏显示不移动write_com(0x80); //设置光标指针}//*********写命令函数开始*******void write_com(uchar com){lcdrs=0; //低电平写命令P0=com; //写入命令delay(3); //延时约3mslcden=1; //LCD使能端置高电平delay(5); //延时约5mslcden=0; //LCD使能端拉低电平}//*********写数据函数开始*******void write_data(uchar dat){lcdrs=1; //低电平写数据P0=dat; //写入命令delay(3); //延时约3mslcden=1; //LCD使能端置高电平delay(5); //延时约5mslcden=0; //LCD使能端拉低电平}//*********延时函数开始*********void delay(uint k){uint i,j;for(i=k;i>0;i--)for(j=110;j>0;j--);}实验原理51单片机的一个I/O口控制speaker发声,演奏老鼠爱大米。
1602液晶滚动显示 c语言
#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned intsbit RS = P2^0;sbit RW = P2^1;sbit EN = P2^2;void Delayms(uint ms){uchar i;while(ms--){for(i=0;i<120;i++);}}uchar Busy_Check(){uchar LCD_Status;RS = 0;RW = 1;EN = 1;Delayms(1);LCD_Status = P0;EN = 0;return LCD_Status;}void Write_LCD_Command(uchar cmd) {while((Busy_Check()&0x80)==0x80);RS = 0;RW = 0;EN = 0;P0 = cmd;EN = 1;Delayms(1);EN = 0;}void Write_LCD_Data(uchar dat){while((Busy_Check()&0x80)==0x80);RS = 1;RW = 0;EN = 0;P0 = dat;EN = 1;Delayms(1);EN = 0;}void Initialize_LCD(){Write_LCD_Command(0x38);Delayms(1);Write_LCD_Command(0x01);Delayms(1);Write_LCD_Command(0x06);Delayms(1);Write_LCD_Command(0x0c);Delayms(1);}void ShowString(uchar x,uchar y,uchar *str) {uchar i = 0;if(y == 0)Write_LCD_Command(0x80 | x);if(y == 1)Write_LCD_Command(0xc0 | x);for(i=0;i<16;i++){Write_LCD_Data(str[i]);}}#include <reg52.h>#include <string.h>#define uchar unsigned char#define uint unsigned intvoid Initialize_LCD();void ShowString(uchar,uchar,uchar *);sbit K1 = P3^0;sbit K3 = P3^2;uchar code Prompt[]="PRESS K1--K4 TO START DEMO PROG"; uchar const Line_Count = 6;uchar code Msg[][80]={"Many CAD users dismiss","process of creating PCB","of view.with PCB layout","placement and track routing,","can often be the most time","And if you use circuit simulation","you are going to spend even more"};uchar Disp_Buffer[32];void Delayxms(uint ms){uchar i;while(ms--){for(i=0;i<120;i++);}}void V_Scroll_Display(){uchar i,j,k = 0;uchar *p = Msg[0];uchar *q = Msg[Line_Count] + strlen(Msg[Line_Count]);while(p<q){for(i=0;(i<16)&&(p<q);i++){if(((i==0)||(i==15))&& *p == ' ')p++;if(*p != '\0'){Disp_Buffer[i] = *p++;}else{if(++k>Line_Count)break;p = Msg[k];Disp_Buffer[i] = *p++;}}for(j=i;j<16;j++)Disp_Buffer[j]=' ';while(F0)Delayxms(5);ShowString(0,0,"");Delayxms(150);while(F0)Delayxms(5);ShowString(0,1,Disp_Buffer);Delayxms(150);while(F0)Delayxms(5);ShowString(0,0,Disp_Buffer);ShowString(0,1,"");Delayxms(150);}ShowString(0,0,"");ShowString(0,1,"");}void main(){uint Count = 0;IE = 0x81;IT0 = 1;F0 = 0;Initialize_LCD();ShowString(0,0,Prompt);ShowString(0,1,Prompt+16);while(1){if(K1 == 0){V_Scroll_Display();Delayxms(200);}}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一个经典的液晶菜单程序
转载:ahong007
unsigned char tempin,tempout,temprun;
unsigned char Key_Value();
unsigned char Change_Value(unsigned char);
void Delay_1(unsigned int m);
void Run_Satus();
char * MainMenuTitle = "主菜单";
const char * MainMenuItems[] = {
"1.室内温度调整",
"2.室外温度调整",
"3.运行时间调整",
"4.ESC 退出",
};
TMenu MainMenu;
void OnSelectMainMenu(int index)
{
if (index < 0 || index >= sizeof(MainMenuItems) / sizeof(char *)) return;
ClearScreen();
switch (index) {
case 0:
TextOut(0, 1, "室内调整开始");
tempin=Change_V alue(tempin);
TextOut(0, 1, "室内调整完毕");
Delay_1(300000);
break;
case 1:
TextOut(0, 1, "室外调整开始");
tempout=Change_V alue(tempout);
TextOut(0, 1, "室外调整完毕");
Delay_1(300000);
break;
case 2:
TextOut(0, 1, "运行时间调整开始");
tempout=Change_V alue(temprun);
TextOut(0, 1, "运行时间完毕");
Delay_1(300000);
break;
case 3:
break;
}
ClearScreen();
}
UINT OnMainMenuOk(void * arg)
{
TMenu * pMenu = (TMenu *)arg;
if (pMenu) OnSelectMainMenu(pMenu->Current);
return 0;
}
UINT OnMainMenuCancel(void * arg)
{
TMenu * pMenu = (TMenu *)arg;
if (pMenu) {
CloseMenu(pMenu);
}
return 0;
}
UINT OnMainMenuOtherKey(void * arg)
{
TMenu * pMenu = (TMenu *)arg;
char key;
if (pMenu) {
key = pMenu->EventArg;
OnSelectMainMenu(key - '1');
DrawMenu(pMenu);
}
return 0;
}
void InitMenu()
{
InitializeMenu(&MainMenu, MainMenuTitle, MainMenuItems, sizeof (MainMenuItems) / sizeof(char *));
MainMenu.OnOk = OnMainMenuOk;
MainMenu.OnOtherKey = OnMainMenuOtherKey;
MainMenu.OnCancel = OnMainMenuCancel;
}
unsigned char Key_Value()
{
unsigned char KeyTemp;
KeyTemp=GetKey();
switch(KeyTemp)
{ case(VK_ESCAPE):
return(0);
break;
case(VK_ENTER):
return(1);
break;
case(VK_UP):
return(2);
break;
case(VK_DOWN):
return(3);
break;
}
}
unsigned char Change_Value(unsigned char temp2) { unsigned char temp3;
temp3=temp2;
while((Key_Value()!=0)&&(Key_Value()!=1))
{
if(Key_Value()==2)temp3++;
else if(Key_Value()==3)temp3--;
ClearLine(1);
TextOut(0, 1, "调整状态%d", temp3);
}
if(Key_Value()==1)return(temp3);
else if(Key_Value()==0) return(temp2); }
void Delay_1(unsigned int m)
{while(m--);
}
void Run_Satus()
{
ClearScreen();
TextOut(0, 0, "室内温度%d",tempin); TextOut(0, 1, "室外温度%d",tempout); TextOut(0, 2, "运行时间%d",temprun); TextOut(0, 3, "ENTER 进入调整");
}
void tmain()
{
tempin=30;
tempout=30;
temprun=30;
unsigned char key;
InitMenu();
while(1)
{
Run_Satus();
key=GetKey();
if(key==VK_ENTER)ShowMenu(&MainMenu); }
}。