16位单片机 LCD程序代码
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#include "shujuku.h" //专门放的数据 图片
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define uint unsigned int
#define uchar unsigned char
#define x1 0x80
#define x2 0x88
#define y 0x80
#define cs PORTA_PA0
#define sid PORTA_PA1
#define sclk PORTA_PA2
#define comm 0
#define dat 1
void Lcd_Write (uchar dat_comm,uchar content); //申明写入函数
/*-----------------延时程序----------------*/
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
单片机printf函数void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
/*------------------初始化-----------------*/
void init_lcd (void)
{
Lcd_Write (comm,0x30); /*30---基本指令动作*/
Lcd_Write (comm,0x01); /*清屏,地址指针指向00H*/
delay_nus(100); //100us
Lcd_Write (comm,0x06); /*光标的移动方向*/
Lcd_Write (comm,0x0c); /*开显示,关游标*/
}
/*--------------清DDRAM------------------*/
void Lcd_Clrram(void)
{
Lcd_Write (comm,0x30);
Lcd_Write (comm,0x01);
delay_nus(180); //delay 180us
}
/*---------------------------------------*/
/*---写入命令或者数据,内容-----如果需要设定
地址写入的就是命令状态,如果需要写入数据就是数据状态--*/
void Lcd_Write(uchar dat_comm,uchar content)
{ uchar a,i,j;
delay_nus(50);
a=content;
cs=1;
sclk=0;
sid=1;
for(i=0;i<5;i++)
{ sclk=1;
delay_nus(10);
sclk=0;
delay_nus(10);
}
sid=0;
sclk=1;
sclk=0;
if(dat_comm)
sid=1;
else
sid=0;
sclk=1;
delay_nus(10);
sclk=0;
delay_nus(10);
sid=0;
sclk=1;
delay_nus(10);
sclk=0;
delay_nus(10);
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{ if(a>=0x80) sid=1;
else sid=0;
a=a<<1;
sclk=1;
delay_nus(10);
sclk=0;
delay_nus(10);
}
sid=0;
for(i=0;i<4;i++)
{
sclk=1;
delay_nus(10);
sclk=0;
delay_nus(10);
}
}
}
/*----------------显示图形-----------------*/
void Lcd_Img_display (uchar *img)
{
uchar i,j;
for(j=0;j<32;j++)
{ for(i=0;i<8;i++)
{
Lcd_Write (comm,0x34);
Lcd_Write (comm,y+j);
Lcd_Write (comm,x1+i);
Lcd_Write (comm,0x30);
Lcd_Write (dat,img[j*16+i*2]);
Lcd_Write (dat,img[j*16+i*2+1]);
}
}
for(j=32;j<64;j++)
{
for(i=0;i<8;i++)
{
Lcd_Write (comm,0x34);
Lcd_Write (comm,y+j-32);
Lcd_Write (comm,x2+i);
Lcd_Write (comm,0x30);
Lcd_Write (dat,img[j*16+i*2]);
Lcd_Write (dat,img[j*16+i*2+1]);
}
}
Lcd_Write (comm,0x36);
}
/*--------------显示点阵----------------*/
void lat_disp (uchar data1,uchar data2)
{ uchar i,j,k,x;
x=x1;
for(k=0;k<2;k++)
{
for(j=0;j<16;j++)
{
for(i=0;i<8;i++)
{
Lcd_Write(comm,0x34);
Lcd_Write(comm,y+j*2);
Lcd_Write(comm,x+i);
Lcd_Write(comm,0x30);
Lcd_Write(dat,data1);
Lcd_Write(dat,data1);
}
for(i=0;i<8;i++)
{ Lcd_Write(comm,0x34);
Lcd_Write(comm,y+j*2+1);
Lcd_Write(comm,x+i);
Lcd_Write(comm,0x30);
Lcd_Write(dat,data2);
Lcd_Write(dat,data2);
}
}
x=x2;
}
Lcd_Write (comm,0x36);
}
/*-----------------------------------------------*/
/*----------------显示汉字的函数---------------*/
void Lcd_Printf(uchar xx,uchar yy,char *Lcd_Pr,uchar Num)
{
uchar i,addr;
switch(yy)
{ case 0:
addr=0x80+xx;
break;
case 1:
addr=0x90+xx;
break;
case 2:
addr=0x88+xx;
break;
case 3:
addr=0x98+xx;
break;
default:
addr=0x80+xx;
break;
}
Lcd_Write(comm,addr); //设置DDRAM地址
for (i=0;i<Num;i++)
{
Lcd_Write(dat,Lcd_Pr[i]); //本行显示
}
}
/*-----------------------显示字-----------------------*/
void Lcd_chinese_display(uchar *chn)
{
uchar i,j;
Lcd_Write (comm,0x30);
Lcd_Write (comm,0x80);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论