https://static.assets-stash.eet-china.com/album/old-resources/2008/10/28/18398a2e-c738-4b04-913a-8537e55c1d15.rarAT24C02+LCD1602显示
/*-----------------------------------------*/
//File: AT24C2.H
//Data: 2008.6.9//3:06 PM
//Amend: 08-8-1 19:31
//Version: Ver0.2
//Thanks: Web
//Code: I love C51~
/*-----------------------------------------*/
#ifndef __AT24C02_H__
#define __AT24C02_H__
/*-----------------------------------------*/
#define unchar unsigned char
#define unint unsigned int
#define unlong unsigned long
/*-----------------------------------------*/
#define WriteDeviceAddress 0xa0 //写
#define ReadDeviceAddress 0xa1 //读
sbit SCL = P2^3; //串行接口定义
sbit SDA = P2^4;
/*-----------------------------------------*/
//函数声明
void delay1ms(void); //延时1ms
void Start(void); //开始总线
void Stop(void); //结束总线
void NoAck(void); //发送ACk0
bit TestAck(void); //测试ACk
void Write8Bit(unchar input); //写入8个Bit 到24C02
void Write24C02(unchar ch,unchar address);//写入一个字到24C02中的指定地址中
unchar Read8Bit(void); //从24C02中读出8个Bit
unchar Read24C02(unchar address); //从24C02指定地址位置中读出1个字节
/*-----------------------------------------*/
//延时1000us
void delay1ms(void)
{
unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=28;j>0;j--)
for(k=2;k>0;k--);
}
/*-----------------------------------------*/
//开始总线
void Start(void)
{
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
/*-----------------------------------------*/
//结束总线
void Stop(void)
{
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
/*-----------------------------------------*/
//发送ACk0
void NoAck(void)
{
SDA=1;
SCL=1;
SCL=0;
}
/*-----------------------------------------*/
//测试ACk
bit TestAck(void)
{
bit ErrorBit;
SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return (ErrorBit);
}
/*-----------------------------------------*/
//写入8个Bit 到24C02
void Write8Bit(unchar input)
{
unchar temp;
for(temp=8;temp!=0;temp--)
{
SDA=(bit)(input&0x80);
SCL=1;
SCL=0;
input<<=1;
}
}
/*-----------------------------------------*/
//写入一个字到24C02中的指定地址中
void Write24C02(unchar ch,unchar address)
{
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(address);
TestAck();
Write8Bit(ch);
TestAck();
Stop();
delay1ms(); //延迟时间1ms!!!
}
/*-----------------------------------------*/
//从24C02中读出8个Bit
unchar Read8Bit(void)
{
unchar temp,rbyte=0;
for(temp=8;temp!=0;temp--)
{
SCL=1;
rbyte<<=1;
rbyte|=((unchar)(SDA));
SCL=0;
}
return (rbyte);
}
/*-----------------------------------------*/
//从24C02指定地址位置中读出1个字节
unchar Read24C02(unchar address)
{
unchar ch;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(address);
TestAck();
Start();
Write8Bit(ReadDeviceAddress);
TestAck();
ch=Read8Bit();
NoAck();
Stop();
return (ch);
}
/*-----------------------------------------*/
/*
//应用举例
void main(void)
{
unsigned char i;
for(i=0;i<256;i++)
{
Write24C02(i,i);
P1=Read24C02(i);
delayms(1000);
}
while(1);
}
*/
/*-----------------------------------------*/
#endif
//LCD1602头文件mini驱动
/*------------------------------------------------------------------------------*/
//File: LCD1602_8.H
//Date: 08-7-29
//Time: 16:09
//Note: LCD1602 Mini Driver.
/*------------------------------------------------------------------------------*/
#ifndef __LCD1602_8_H__
#define __LCD1602_8_H__
/*------------------------------------------------------------------------------*/
//LCD接口定义
sfr io = 0x80; //P0-0x80,P1-0x90,P2-0xA0,P3-0xB0;
sbit rs = P2^0; //LCD数据/命令选择端(H/L)
sbit rw = P2^1; //LCD读/写选择端(H/L)
sbit ep = P2^2; //LCD使能控制
sbit bz = io^7; //LCD忙标志位
/*------------------------------------------------------------------------------*/
//函数声明
void lcd_busy(void); //测试LCD忙碌状态程序
void lcd_wcmd(unsigned char cmd); //写入指令到LCD程序
void lcd_wdat(unsigned char dat); //写入数据到LCD程序
void lcd_pos (unsigned char x, bit y); //LCD数据指针位置程序
void printc(unsigned char CHAR); //显示字符
//void prints(unsigned char *string); //显示字符串
void lcd_init(void); //LCD初始化设定程序
/*------------------------------------------------------------------------------*/
//测试LCD忙碌状态
void lcd_busy(void)
{
do{
ep = 0;
rs = 0; //指令
rw = 1; //读出
io = 0xff;
ep = 1;
_nop_(); //高电平读出 1us
}while(bz); //bz=1表示忙,bz=0表示空闲
ep = 0;
}
/*------------------------------------------------------------------------------*/
//写入指令到LCD
void lcd_wcmd(unsigned char cmd)
{
lcd_busy(); //检测忙
rs = 0; //指令
rw = 0; //写入
ep = 1;
io = cmd; //指令
ep = 0; //下降沿有效
}
/*------------------------------------------------------------------------------*/
//写入数据函数
void lcd_wdat(unsigned char Data)
{
lcd_busy(); //检测忙
rs = 1; //数据
rw = 0; //写入
ep = 1;
io = Data; //数据
ep = 0; //下降沿有效
}
/*------------------------------------------------------------------------------*/
//LCD数据指针位置程序
void lcd_pos(unsigned char x, bit y)
{
if(y)lcd_wcmd(x|0xc0); //y=1,第二行显示;y=0,第一行显示 0<=x<16
else lcd_wcmd(x|0x80); //数据指针=80+地址码(00H~27H,40H~67H)
}
/*------------------------------------------------------------------------------*/
//显示字符
void printc(unsigned char CHAR)
{
lcd_wdat(CHAR);
}
/*------------------------------------------------------------------------------*/
//显示字符串
/*
void prints(unsigned char *string)
{
unsigned char i;
//do{lcd_wdat(string);i++;}while(string!='\0');//生成代码比for循环大
for(i=0;string!='\0';i++)lcd_wdat(string); //有时用for循环也好哈
}
*/
/*------------------------------------------------------------------------------*/
//LCD初始化设定
void lcd_init()
{
lcd_wcmd(0x38); //设置LCD为16X2显示,5X7点阵,八位数据接口
lcd_wcmd(0x06); //LCD显示光标移动设置(光标地址指针加1,整屏显示不移动)
lcd_wcmd(0x0c); //LCD开显示及光标设置(光标不闪烁,不显示"_")
lcd_wcmd(0x01); //清除LCD的显示内容
}
#endif
//主函数
#include<reg52.h>
#include<intrins.h>
#include"LCD1602.H"
#include"AT24C02.H"
//函数定义
void delay(unsigned char x, unsigned char y, unsigned char z)
{
unsigned char tempx,tempy,tempz=z;
do{tempy=y;do{tempx=x;while(--tempx);}while(--tempy);}while(--tempz);
}
void main(void)
{
unsigned char i;
unsigned char temp_char;
//for(i=0;i<250;i++)Write24C02(i,i); //写入数据到AT24C02中
i=32;
lcd_init();
lcd_pos(0,0);
while(1)
{
temp_char=Read24C02(i++);
if(i==250)i=32;
printc(temp_char);
delay(20,25,150);
if(i%16==0){lcd_pos(0,1);if(i%32==0)lcd_pos(0,0);}
}
}
文章评论(0条评论)
登录后参与讨论