2、51单片机:
- LCD1602.h
- #include "LCD1602.h"
- /******************************************************************************
- * 函数名称:void Delay_ms(unsigned int nms) *
- * 函数功能:写命令函数 *
- * 输入参数: //nms为要延时的ms数 *
- * 返回值 :无 *
- * 其他说明: *
- ******************************************************************************/
- void Delay_ms(unsigned int nms) //@11.0592MHz{
- unsigned char i, j; //用单片机小工具根据自己的单片机类型及晶振直接生成对应的延时函数即可
- while(nms--)
- {
- i = 15;
- j = 90;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- /******************************************************************************
- * 函数名称:void LCD1602_write_cmd(unsigned char cmd) *
- * 函数功能:写命令函数 *
- * 输入参数: cmd 命令 *
- * 返回值 :无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_write_cmd(unsigned char cmd) {
- LCD1602_DB=cmd;
- LCD_RES=0;
- LCD_EN=1;
- Delay_ms(10);
- LCD_EN=0;
- }
- /******************************************************************************
- * 函数名称:void LCD1602_write_data(unsigned char date) *
- * 函数功能:写数据函数 *
- * 输入参数: date 数据 *
- * 返回值 :无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_write_data(unsigned char date) {
- LCD1602_DB=date;
- LCD_RES=1;
- LCD_EN=1;
- Delay_ms(10);
- LCD_EN=0;
- }
- /******************************************************************************
- * 函数名称:void LCD1602_Init(void) *
- * 函数功能:1602初始化函数 *
- * 输入参数: 无 *
- * 返回值 :无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_Init() {
- LCD1602_BKLSet(LCD1602_BKL_ON); //背光开启
- LCD1602_write_cmd(0x01); //显示清屏
- LCD1602_write_cmd(0x38); //显示模式设置
- LCD1602_write_cmd(0x0C); //显示开及光标设置
- LCD1602_write_cmd(0x06); //显示光标移动位置
- }
- /******************************************************************************
- * 函数名称:void LCD1602_Clear(void) *
- * 函数功能:1602清屏函数 *
- * 输入参数: 无 *
- * 返回值 :无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_Clear(){
- LCD1602_write_cmd(0x01);
- }
- /******************************************************************************
- * 函数名称:void LCD1602_BKLSet(unsigned char val) *
- * 函数功能:打开1602背光函数 *
- * 输入参数: LCD1602_BKL_ON 开LCD1602_BKL_OFF 关 *
- * 返回值 :无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_BKLSet(unsigned char val){
- Lcd1602_light = val;
- }
- /******************************************************************************
- * 函数名称:unsigned char LCD1602_BKLGet() *
- * 函数功能:获取1602背光函数 *
- * 输入参数:无 *
- * 返回值 : 0 开1 关 *
- * 其他说明: *
- ******************************************************************************/
- unsigned char LCD1602_BKLGet(){
- return Lcd1602_light;
- }
- /******************************************************************************
- * 函数名称:void LCD1602_SetCursor(unsigned char x,unsigned char y) *
- * 函数功能:设置1602位置函数 *
- * 输入参数:x 横坐标 y 纵坐标 *
- * 返回值 : 无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_SetCursor(unsigned char x,unsigned char y){
- unsigned char addr;
- if(y==0)
- addr=0x00+x;
- else
- addr=0x40+x;
- LCD1602_write_cmd(addr | 0x80);
- }
- /******************************************************************************
- * 函数名称:void LCD1602_ShowNum(char x, char y, unsigned int num) *
- * 函数功能:指定位置显示数字函数 *
- * 输入参数:x 横坐标 y 纵坐标 num 数字 *
- * 返回值 : 无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_ShowNum(char x, char y, unsigned int num){
- unsigned int i,j,k,l,n;
- i=num/10000;
- j=(num-10000*i)/1000;
- k=(num-10000*i-1000*j)/100;
- l=(num-10000*i-1000*j-100*k)/10;
- n=num%10;
- LCD1602_SetCursor(x,y);
- if(i!=0)LCD1602_write_data(i+0x30);
- if((i!=0)||(j!=0))LCD1602_write_data(j+0x30);
- if((i!=0)||(j!=0)||(k!=0))LCD1602_write_data(k+0x30);
- if((i!=0)||(j!=0)||(k!=0)||(l!=0))LCD1602_write_data(l+0x30);
- LCD1602_write_data(n+0x30);
- }
- /******************************************************************************
- * 函数名称:void LCD1602_ShowChar(unsigned char xpos,unsigned char ypos,char xsz) *
- * 函数功能:指定位置显示字符函数 *
- * 输入参数:xpos 横坐标 ypos 纵坐标 xsz 字符 *
- * 返回值 : 无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_ShowChar(unsigned char xpos,unsigned char ypos,char xsz) {
- ypos%=2;
- if(ypos==0)
- {
- LCD1602_write_cmd(0x80+xpos);
- }
- else
- {
- LCD1602_write_cmd(0x80+0x40+xpos);
- }
- LCD1602_write_data(xsz);
- }
- /******************************************************************************
- * 函数名称:void LCD1602_ShowStr(unsigned char xpos,unsigned char ypos,char *p) *
- * 函数功能:指定位置显示字符串函数 *
- * 输入参数:xpos 横坐标 ypos 纵坐标 *p 字符串 *
- * 返回值 : 无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_ShowStr(unsigned char xpos,unsigned char ypos,char *p){
- if(ypos>1)return;
- while(*p!='\0')
- {
- LCD1602_ShowChar(xpos++,ypos,*p++);
- if(*p=='\n')
- {
- xpos=0;
- ypos++;
- p++;
- }
- }
- }
- unsigned char code code_Y[8]={0x11,0x0A,0x04,0x04,0x04,0x04,0x04,0x00};//类似Y的字模
- /******************************************************************************
- * 函数名称:void LCD1602_wtire_CGRAM(void) *
- * 函数功能:自定义显示字符函数 *
- * 输入参数: 无 *
- * 返回值 : 无 *
- * 其他说明: *
- ******************************************************************************/
- void LCD1602_wtire_CGRAM(){
- unsigned char num;
- LCD1602_write_cmd(0x40); //对CGRAM第一个自定义字符操作,若是第二个则为0x48,
- //其次类推(上面有对顶的关系)
- for(num=0;num<8;num++)
- {
- LCD1602_write_data(code_Y[num]);
- }
- LCD1602_write_cmd(0x80); //规定显示在第一行第一个位置
- LCD1602_write_data(0x00); //显示第一个自定义字符 (0x40对应第一个:0x00)
- }
- /********************************************************************/
复制代码
|