📄 密码锁.c
字号:
//==================================================================
//工程名称:
//工程功能:
//作者:
//审核:
//版本: 0001.02.03
//日期:2007.04.12
//==================================================================
#include "reg51.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
//==================液晶12864 端口定义
sbit E=P1^0;
sbit RW=P1^1;
sbit RS=P1^2;
//====================================
//==================IIC 24c04 端口定义
sbit SDA=P1^0;
sbit SCL=P1^0;
//====================================
uchar code table1[]={"B :密码锁系统"};
uchar code table2[]={"作者:潘素琴"};
uchar code table3[]={"日期:2007.05.16"};
/*****************************/
/*延时子程序*/
/****************************/
void delayms (uchar k)
{
uchar j;
while((k--)!=0)
{
for(j=0;j<115;j++)
{;}
}
}
//=============================================================
//函数名称:void check_busy(void)
//函数功能:检测LCM是不是处于忙状态,并等待忙结束
//作者:
//日期:2007.03.31
//=============================================================
void check_busy(void)
{
uchar i;
RS=0;
RW=1;
E=1;
P0=0xff;
while(P0&0x01);
i=6;
while(i--);
E=0;
}
//=====================================================
//======================================================
//函数名称:void sendorder(uchar order)
//函数功能:给LCM发送指令
//作者:
//日期:2007.03.31
//======================================================
void sendorder(uchar order)
{
uchar i,temp1=0,temp2=0;
check_busy();
RS=0;
RW=0;
for(i=0;i<8;i++)
{
temp1=order&0x01;
temp2|=temp1;
order>>=1;
temp2<<=1;
}
P0=temp2;
i=7;
while(i--);
E=1;
E=0;
}
//=====================================================
//=====================================================
//函数名称:void sendbyte(uchar byte)
//函数功能:给LCM发送数据
//作者:
//日期:2007.03.31
//=====================================================
void sendbyte(uchar byte)
{
uchar i,temp1=0,temp2=0;
check_busy();
for(i=0;i<8;i++)
{
temp1=byte&0x01;
temp2|=temp1;
byte>>=1;
temp2<<=1;
}
RS=1;
RW=0;
P0=temp2;
i=7;
while(i--);
E=1;
E=0;
}
//=======================================================
/*----------------------------------------------------------------------
调用方式:void start_bit(void)
函数说明:开始位-
-----------------------------------------------------------------------*/
void start_bit(void)
{
SCL=1;
_nop_();
SDA=1;
_nop_();
SDA=0;
_nop_();
SCL=0;
_nop_();
}
/*-------------------------------------------------------------------
调用方式:void stop_bit(void)
函数说明:停止位
--------------------------------------------------------------------*/
void stop_bit(void)
{
SDA=0;
_nop_();
SCL=1;
_nop_();
SDA=1;
_nop_();
}
/*-----------------------------------------------------------------
调用方式:void mast_ack(void)-
函数说明:主答函数
-----------------------------------------------------------------*/
void mast_ack(void)
{
SCL=0;
_nop_();
SDA=0;
_nop_();
SCL=1;
_nop_();
SCL=0;
_nop_();
SDA=1;
_nop_();
}
/*-----------------------------------------------
调用方式:void ack(void)
函数说明:应答函数
-----------------------------------------------*/
void ack(void)
{
SDA=1;
SCL=0;
_nop_();
SCL=1;
_nop_();
while(SDA){;}
SCL=0;
_nop_();
}
/*-----------------------------------------------
调用方式:void no_ack(void)
函数说明:无需应答位,在读程序中用到
-----------------------------------------------*/
void no_ack(void)
{
SDA=1;
_nop_();
SCL=1;
_nop_();
SCL=0;
_nop_();
}
/*-----------------------------------------------
调用方式:write_8bit(unsigned char ch)
函数说明:写一个字节(8位)数据
-----------------------------------------------*/
write_8bit(unsigned char ch)
{
unsigned char i=8;
SCL=0;
_nop_();
while(i--)
{
SDA=(bit)(ch&0x80);
_nop_();
ch<<=1;
SCL=1;
_nop_();
SCL=0;
_nop_();
}
}
/*----------------------------------------------
调用方式:unsigned char read24c16(unsigned int address)
函数说明:读24c16指定地址数据(字节读)
-----------------------------------------------*/
unsigned char read24c16(uchar addh)
{
unsigned char data rdata;
unsigned char i=8;
start_bit();
write_8bit(0xa0);
ack();
write_8bit(addh);
ack(); //伪写
start_bit();
write_8bit(0xa1);
ack();
while(i--)
{
rdata<<=1;
SCL=0;
_nop_();
SCL=1;
_nop_();
if(SDA) rdata|=0x01;
}
no_ack();
stop_bit();
return(rdata);
}
/*-----------------------------------------------
调用方式:void write24c16(unsigned int address,unsigned char ddata)
函数说明:写数据到24c16的指定地址(字节写)
-----------------------------------------------*/
void write24c16(unsigned int addr_h,unsigned char ddata)
{
start_bit();
write_8bit(0xa0);
ack();
write_8bit(addr_h);
ack();
write_8bit(ddata);
ack();
stop_bit();
delayms(4);
}
//=======================================================================
//=======================================================================
//函数名:void LCD_init()
//函数功能:
//参数:
//作者:
//日期:
//=======================================================================
void LCD_init()
{
uchar i;
sendorder(0x30); ///功能设置 8位控制界面 基本指令
sendorder(0x0c); ///显示打开 光标关 反白显示关
sendorder(0x01); ///清除屏幕显示 将DDRAM的地址计数器清零
sendorder(0x80); ///设置DDRAM地址为0000000
for(i=0;i<14;i++)
{
sendbyte(table1[i]);
}
sendorder(0x88);
for(i=0;i<12;i++)
{
sendbyte(table2[i]);
}
sendorder(0x98);
for(i=0;i<16;i++)
{
sendbyte(table3[i]);
}
}
//=======================================================================
//=======================================================================
//函数名:
//函数功能:
//参数:
//作者:
//日期:
//=======================================================================
void main()
{
LCD_init();
while(1);
}
//=======================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -