诺基亚5110显示屏51单片机驱动程序2

诺基亚5110显示屏51单片机驱动程序2
诺基亚5110显示屏51单片机驱动程序2

诺基亚5110显示屏51单片机驱动程序

#include

#include "intrins.h" //_nop_();延时函数用

#include //字符代码表

#define uchar unsigned char

#define uint unsigned int

sbit sce = P3^4; //片选

sbit res = P0^0; //复位,0复位

sbit dc = P3^5; //1写数据,0写指令

sbit sdin = P3^7; //数据

sbit sclk = P3^6; //时钟

uint data temp_data[2]={0x00,0x00}; //读出温度暂放

//================18b20数据口定义===============

sbit DQ=P0^0; //温度输入口uint h;

uint temp;

unsigned char presence,flash=0;

/*****************延时函数*************************/

void delay(uint t)

{

for (;t>0;t--);

}

//================延时函数=====================

void delay_1ms(void)//1ms延时函数

{

unsigned int i;

for (i=0;i<500;i++);

}

/*---------------lcd显示函数开始----------------

LCD_write_byte: 使用SPI接口写数据到LCD

输入参数:dt:写入的数据;

command :写数据/命令选择;

编写日期:20080918

----------------------------------------------*/

void LCD_write_byte(unsigned char dt, unsigned char command)

{

unsigned char i;

sce=0;

dc=command;

for(i=0;i<8;i++)

{

if(dt&0x80)

sdin=1;

else

sdin=0;

dt=dt<<1;

sclk=0;

sclk=1;

}

dc=1;

sce=1;

sdin=1;

}

/*---------------------------------------

LCD_init: 5110LCD初始化

编写日期:20080918

----------------------------------------- */

void LCD_init(void)

{

res=0;

delay_1ms();

res=1;

LCD_write_byte(0x21,0);//初始化Lcd,功能设定使用扩充指令LCD_write_byte(0xd0,0);//设定液晶偏置电压

LCD_write_byte(0x20,0);//使用基本指令

LCD_write_byte(0x0C,0);//设定显示模式,正常显示

}

/*-------------------------------------------

LCD_set_XY: 设置LCD坐标函数

输入参数:X:0-83 Y:0-5

编写日期:20080918

---------------------------------------------*/

void LCD_set_XY(unsigned char X, unsigned char Y)

{

LCD_write_byte(0x40 | Y, 0);// column

LCD_write_byte(0x80 | X, 0);// row

}

/*------------------------------------------

LCD_clear: LCD清屏函数

编写日期:20080918

--------------------------------------------*/

void LCD_c lear(void)

{

unsigned char t;

unsigned char k;

LCD_set_XY(0,0);

for(t=0;t<6;t++)

{

for(k=0;k<84;k++)

{

LCD_write_byte(0x00,1);

}

}

}

/*---------------------------------------------

LCD_write_shu: 显示8(宽)*16(高)点阵列数字字母符号等半角类

输入参数:c:显示的字符;

编写日期:20080918

-----------------------------------------------*/

void LCD_write_shu(unsigned char row, unsigned char page,unsigned char c) //row:列page:页dd:字符

{

unsigned char i;

LCD_set_XY(row*8, page);// 列,页

for(i=0; i<8;i++)

{

LCD_write_byte(shuzi[c*16+i],1);

}

LCD_set_XY(row*8, page+1);// 列,页

for(i=8; i<16;i++)

{

LCD_write_byte(shuzi[c*16+i],1);

}

}

/*---------------------------------------------

LCD_write_hanzi: 显示16(宽)*16(高)点阵列汉字等半角类

输入参数:c:显示的字符;

编写日期:20080918

-----------------------------------------------*/

void LCD_write_hanzi(unsigned char row, unsigned char page,unsigned char c) //row:列page:页dd:字符

{

unsigned char i;

LCD_set_XY(row*8, page);// 列,页

for(i=0; i<16;i++)

{

LCD_write_byte(hanzi[c*32+i],1);

}

LCD_set_XY(row*8, page+1);// 列,页

for(i=16; i<32;i++)

{

LCD_write_byte(hanzi[c*32+i],1);

}

}

//=================lcd显示函数结束================

//=================18b20相关函数开始==============

/****************DS18B20复位函数************************/

ow_reset(void)

{

char presence=1;

while(presence)

{

while(presence)

{

DQ=1;_nop_();_nop_(); //从高拉倒低

DQ=0;

delay(50); //550 us

DQ=1;

delay(6); //66 us

presence=DQ; //presence=0 复位成功,继续下一步}

delay(45); //延时500 us

presence=~DQ;

}

DQ=1; //拉高电平

}

//

//

/****************DS18B20写命令函数************************/ void write_byte(uchar val) //向1-WIRE 总线上写1个字节{

uchar i;

for(i=8;i>0;i--)

{

DQ=1;_nop_();_nop_(); //从高拉倒低

DQ=0;_nop_();_nop_();_nop_();_nop_(); //5 us

DQ=val&0x01; //最低位移出

delay(6); //66 us

val=val/2; //右移1位

}

DQ=1;

delay(1);

}

/****************DS18B20读1字节函数************************/ uchar read_byte(void) //从总线上取1个字节

{

uchar i;

uchar value=0;

for(i=8;i>0;i--)

{

DQ=1;_nop_();_nop_();

value>>=1;

DQ=0;_nop_();_nop_();_nop_();_nop_(); //4 us

DQ=1;_nop_();_nop_();_nop_();_nop_(); //4 us

if(DQ)value|=0x80;

delay(6); //66 us

}

DQ=1;

return(value);

}

//

/****************读出温度函数************************/

read_temp()

{

ow_reset(); //总线复位

if(presence==1)

{flash=1;} //DS18B20不正常,蜂鸣器报警delay(200);

write_byte(0xcc); //发命令

write_byte(0x44); //发转换命令

ow_reset();

delay(1);

write_byte(0xcc); //发命令

write_byte(0xbe);

temp_data[0]=read_byte(); //读温度值的第字节

temp_data[1]=read_byte(); //读温度值的高字节

temp=temp_data[1];

temp<<=8;

temp=temp|temp_data[0]; // 两字节合成一个整型变量。return temp; //返回温度值

}

//=================18b20相关函数结束==============

//=================主函数=========================

void main(void)

{

unsigned char k;

res=0;

for(k=0;k<250;k++);

res=1;

LCD_init(); //初始化LCD模块LCD_clear(); //清屏幕

LCD_write_hanzi(1,0,0); //

LCD_write_hanzi(3,0,1); //

LCD_write_hanzi(5,0,2); //

LCD_write_hanzi(7,0,3); //

LCD_write_hanzi(1,2,4); //

LCD_write_hanzi(4,2,5); //

LCD_write_hanzi(7,2,6); //

LCD_write_shu(0,4,5); // LCD_write_shu(1,4,1); //

LCD_write_shu(2,4,35); // LCD_write_shu(3,4,33); // LCD_write_shu(4,4,13); // LCD_write_shu(5,4,25); // LCD_write_shu(6,4,19); // LCD_write_shu(7,4,45); // LCD_write_shu(8,4,12); // LCD_write_shu(9,4,23); //

while(1)

{

}

}

相关主题
相关文档
最新文档