基于PIC16F877A单片机设计的万年历程序
5.液晶显示程序
/***************************************************
* 标题:LCD操作 *
* 作者:Wujieflash *
* 日期:2008年1月12日 *
* 说明:KS0108系列液晶不带字库 *
***************************************************/
#include
#define E RA0 //液晶使能端
#define RW RA1 //读写控制端
#define DI RA2 //数据/指令通道
#define CSA RA5 //片选
#define CSB RA3 //片选
#define nop() asm("nop")
////////////////////////基本子函数///////////////////////
//延时
void delay(char x,char y)
{
char z;
do{
z=y;
do{;}while(--z);
}while(--x);
}
//其指令时间为:7+(3*(Y-1)+7)*(X-1)如果再加上函数调用的call 指令、页面设定、传递参数花掉的7 个指令。
//则是:14+(3*(Y-1)+7)*(X-1)。
//--------------------------------------------------
void LCDdelay()
{
unsigned int i=500;
while(i--)
{;}
}
/*----------------------------------------------------*/
//状态检查
void CheckState()
{
unsigned int dat;
RW=1; //读状态
DI=0;
TRISB=0xff; //设置口为输入
while(1)
{
E=1; //高电平读数据
dat=PORTB0x90; //当4、7位为0时为正常状态
E=0;
if(dat==0x00)break;
}
TRISB=0x00; //恢复口为输出
}
/*----------------------------------------------------*/
//写显示数据
//dat:显示数据
void WriteData(unsigned char dat)
{
CheckState();
DI=1; //写数据
RW=0;
PORTB=dat;
E=1;
E=0;
}
/*---------------------------------------------------*/
//写指令
//command:操作LCD的指令
void WriteCommand(unsigned char dat)
{
CheckState();
RW=0; //写指令
PORTB=dat;
E=1;
E=0;
}
/*---------------------------------------------------*/
//设定页地址----X:0--7
void SetLine(unsigned char line)
{
line=line0x07; //0
//设定列地址----Y:0--63
void SetColumn(unsigned char column)
{
column=column0x3f; //0
//设定开始行
void SetStartLine(unsigned char startline)
{
startline=startline0x3f; //0
//开关显示
//1:开显示 0:关显示
void SetOnOff(unsigned char state)
{
state=state|0x3e;
WriteCommand(state);
}
/*--------------------------------------------------*/
//选择屏幕
//screen: 1:左屏 2:右屏
void SlectScreen(unsigned char screen)
{
switch(screen)
{
case 1: CSA=0;
CSB=1;//左屏
break;
case 2: CSA=1;
CSB=0;//右屏
break;
}
}
/*--------------------------------------------------*/
//清屏
//screen: 1:左屏 2:右屏
void ClearScreen(unsigned char screen)
{
unsigned char i,j;
SlectScreen(screen);
for(i=0;i
//显示一个16X16的汉字
void Show16X16(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp;
column_temp=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt); //写数据
*pt++;//
column++;
}
SlectScreen(1);
SetLine(line+1); //设置汉字下半部分显示页
column=column_temp;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//显示一个16X16的汉字带上划线
void Show16X16_2(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp;
column_temp=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt|0x01); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1); //设置汉字下半部分显示页
column=column_temp;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//显示一个16X16的汉字带下划线
void Show16X16_3(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp;
column_temp=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1); //设置汉字下半部分显示页
column=column_temp;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt|0x80);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//显示一个8X16的字符
void Show8X16(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp;
column_temp=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1); //设置汉字下半部分显示页
column=column_temp;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//显示一个8X16的字符带上划线
void Show8X16_2(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp;
column_temp=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt|0x01); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1); //设置汉字下半部分显示页
colu mn=column_temp;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//显示一个16X32的字符
void Show16X32(unsigned char line,unsigned char column,const unsigned char *pt)
{
unsigned char i,column_temp1,column_temp2,column_temp3;
column_temp1=column;
column_temp2=column;
column_temp3=column;
SlectScreen(1); //选通左屏
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i=64) //如果列>=64,则选通右屏
{
SlectScreen(2);
column=column%64; //设置右屏起始列
SetColumn(column);
SetLine(line);
}
WriteData(*pt); //写数据
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+1);
column=column_temp1;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+1);
}
WriteData(*pt);
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+2);
column=column_temp2;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+2);
}
WriteData(*pt);
*pt++;
column++;
}
SlectScreen(1);
SetLine(line+3);
column=column_temp3;
SetColumn(column);
for(i=0;i=64)
{
SlectScreen(2);
column=column%64;
SetColumn(column);
SetLine(line+3);
}
WriteData(*pt);
*pt++;
column++;
}
}
/*-------------------------------------------------------*/
//LCD初始化子程序
void LCDinit()
{
ADCON1=0x06;
TRISA=0x00;
TRISB=0x00;
PORTB=0x00;
SetOnOff(1);
}
/*----------------------------------------------------------*/
//调用LCD显示子程序
void ChangeToLCD(uch line,uch column,uch data)
{
int D_ge,D_shi;
D_ge=data%10; //取个位
D_shi=data%100/10; //取十位
switch(D_ge)
{
case 0:{Show8X16(line,column+8,s0);break;}
case 1:{Show8X16(line,column+8,s1);break;}
case 2:{Show8X16(line,column+8,s2);break;}
case 3:{Show8X16(line,column+8,s3);break;}
case 4:{Show8X16(line,column+8,s4);break;}
case 5:{Show8X16(line,column+8,s5);break;}
case 6:{Show8X16(line,column+8,s6);break;}
case 7:{Show8X16(line,column+8,s7);break;}
case 8:{Show8X16(line,column+8,s8);break;}
case 9:{Show8X16(line,column+8,s9);break;}
}
switch(D_shi)
{
case 0:{Show8X16(line,column,s0);break;}
case 1:{Show8X16(line,column,s1);break;}
case 2:{Show8X16(line,column,s2);break;}
case 3:{Show8X16(line,column,s3);break;}
case 4:{Show8X16(line,column,s4);break;}
case 5:{Show8X16(line,column,s5);break;}
case 6:{Show8X16(line,column,s6);break;}
case 7:{Show8X16(line,column,s7);break;}
case 8:{Show8X16(line,column,s8);break;}
case 9:{Show8X16(line,column,s9);break;}
}
}
/*-----------------------------------------------------------------*/
void ChangeToLCD2(uch line,uch column,uch data)//带上划线
{
int D_ge,D_shi;
D_ge=data%10; //取个位
D_shi=data%100/10; //取十位
switch(D_ge)
{
case 0:{Show8X16_2(line,column+8,s0);break;}
case 1:{Show8X16_2(line,column+8,s1);break;}
case 2:{Show8X16_2(line,column+8,s2);break;}
case 3:{Show8X16_2(line,column+8,s3);break;}
case 4:{Show8X16_2(line,column+8,s4);break;}
case 5:{Show8X16_2(line,column+8,s5);break;}
case 6:{Show8X16_2(line,column+8,s6);break;}
case 7:{Show8X16_2(line,column+8,s7);break;}
case 8:{Show8X16_2(line,column+8,s8);break;}
case 9:{Show8X16_2(line,column+8,s9);break;}
}
switch(D_shi)
{
case 0:{Show8X16_2(line,column,s0);break;}
case 1:{Show8X16_2(line,column,s1);break;}
case 2:{Show8X16_2(line,column,s2);break;}
case 3:{Show8X16_2(line,column,s3);break;}
case 4:{Show8X16_2(line,column,s4);break;}
case 5:{Show8X16_2(line,column,s5);break;}
case 6:{Show8X16_2(line,column,s6);break;}
case 7:{Show8X16_2(line,column,s7);break;}
case 8:{Show8X16_2(line,column,s8);break;}
case 9:{Show8X16_2(line,column,s9);break;}
}
}
/*-----------------------------------------------------------------*/
void ChangeToLCD3(uch line,uch column,uch data)//带下划线
{
int D_ge,D_shi;
D_ge=data%10; //取个位
D_shi=data%100/10; //取十位
switch(D_ge)
{
case 0:{Show16X32(line,column+16,ss0);break;}
case 1:{Show16X32(line,column+16,ss1);break;}
case 2:{Show16X32(line,column+16,ss2);break;}
case 3:{Show16X32(line,column+16,ss3);break;}
case 4:{Show16X32(line,column+16,ss4);break;}
case 5:{Show16X32(line,column+16,ss5);break;}
case 6:{Show16X32(line,column+16,ss6);break;}
case 7:{Show16X32(line,column+16,ss7);break;}
case 8:{Show16X32(line,column+16,ss8);break;}
case 9:{Show16X32(line,column+16,ss9);break;}
}
switch(D_shi)
{
case 0:{Show16X32(line,column,ss0);break;}
case 1:{Show16X32(line,column,ss1);break;}
case 2:{Show16X32(line,column,ss2);break;}
case 3:{Show16X32(line,column,ss3);break;}
case 4:{Show16X32(line,column,ss4);break;}
case 5:{Show16X32(line,column,ss5);break;}
case 6:{Show16X32(line,column,ss6);break;}
case 7:{Show16X32(line,column,ss7);break;}
case 8:{Show16X32(line,column,ss8);break;}
case 9:{Show16X32(line,column,ss9);break;}
}
}
/*-----------------------------------------------------------------*/
void ChangeToLCD4(uch line,uch column,uch data)//星期转换
{
switch(data)
{
case 1:{Show16X16_3(line,column,ri);break;}
case 2:{Show16X16_3(line,column,yi);break;}
case 3:{Show16X16_3(line,column,er);break;}
case 4:{Show16X16_3(line,column,san);break;}
case 5:{Show16X16_3(line,column,si);break;}
case 6:{Show16X16_3(line,column,wu);break;}
case 7:{Show16X16_3(line,column,liu);break;}
}
}
/*-----------------------------------------------------------------*/
void ChangeToLCD5(uch line,uch column,uch data)//生肖转换
{
data=data%12;
switch(data)
{
case 0:{Show16X16_3(line,column,llong);break;}
case 1:{Show16X16_3(line,column,she);break;}
case 2:{Show16X16_3(line,column,ma);break;}
case 3:{Show16X16_3(line,column,yang);break;}
case 4:{Show16X16_3(line,column,hou);break;}
case 5:{Show16X16_3(line,column,ji);break;}
case 6:{Show16X16_3(line,column,gou);break;}
case 7:{Show16X16_3(line,column,zhu);break;}
case 8:{Show16X16_3(line,column,shu);break;}
case 9:{Show16X16_3(line,column,niu);break;}
case 10:{Show16X16_3(line,column,hu);break;}
case 11:{Show16X16_3(line,column,tu);break;}
}
}
/*-----------------------------------------------------------------*/
void ChangeToLCD6(uch line,uch column,uch data)//阴历月转换
{
data=data%12;
switch(data)
{
case 0:{Show16X16_2(line,column,zheng);break;}
case 1:{Show16X16_2(line,column,er);break;}
case 2:{Show16X16_2(line,column,san);break;}
case 3:{Show16X16_2(line,column,si);break;}
case 4:{Show16X16_2(line,column,wu);break;}
case 5:{Show16X16_2(line,column,liu);break;}
case 6:{Show16X16_2(line,column,qqi);break;}
case 7:{Show16X16_2(line,column,ba);break;}
case 8:{Show16X16_2(line,column,jiu);break;}
case 9:{Show16X16_2(line,column,shi);break;}
case 10:{Show16X16_2(line,column,shiyi);break;}
case 11:{Show16X16_2(line,column,la);break;}
}
}
/*-----------------------------------------------------------------*/
void ChangeToLCD7(uch line,uch column,uch data)//阴历日
{
data=data%12;
switch(data)
{
case 0:{Show16X16_2(line,column,yi);break;}
case 1:{Show16X16_2(line,column,er);break;}
case 2:{Show16X16_2(line,column,san);break;}
case 3:{Show16X16_2(line,column,si);break;}
case 4:{Show16X16_2(line,column,wu);break;}
case 5:{Show16X16_2(line,column,liu);break;}
case 6:{Show16X16_2(line,column,qqi);break;}
case 7:{Show16X16_2(line,column,ba);break;}
case 8:{Show16X16_2(line,column,jiu);break;}
case 9:{Show16X16_2(line,column,shi);break;}
}
}
/*-----------------------------------------------------------------*/
void ChangeToLCD8(uch line,uch column,uch data)//阴历日十位
{
data=data%3;
switch(data)
{
case 0:{Show16X16_2(line,column,chu);break;}
case 1:{Show16X16_2(line,column,er);break;}
case 2:{Show16X16_2(line,column,san);break;}
}
}
/*-----------------------------------------------------------------*/
程序结束
PIC16F877A 万年历 KS0108系列 相关文章:
- pic16f877a的电平变化中断(interrupt on change)使用误区(12-03)
- 用单片机pic16f877a实现鼠标数据的采集(12-03)
- PIC16F877A一路舵机参数化控制程序(12-02)
- PIC16F877A与PC机串行通信C语言源程序(12-02)
- PIC16F877A和AVR的单片机的PWM输出程序(12-02)
- PIC16F877的A/D模块(11-24)