⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcdand24c02.c

📁 AT24C02 C语言驱动程序通过LCD1602 显示所存放的内容
💻 C
字号:
//AT24C02 EEPROM drive program
//for 51 mcu with lcd1602 as display 
//designed by zhaoliang
//2005-6-14 21:02

#include <reg51.h>
#include <intrins.h>
/********************************************************************/
//lcd part
#define LINE1     0
#define LINE2     1
#define LINE1_HEAD    0x80
#define LINE2_HEAD    0xC0
#define LCD_DELAY_TIME   40
#define DATA_MODE    0x38
#define OPEN_SCREEN    0x0C
#define DISPLAY_ADDRESS   0x80
#define CLEARSCREEN    LCD_en_command(0x01)
//common part 
#define HIGH   1
#define LOW    0
#define TRUE    1
#define ZERO    0 
//at24c02 part
#define WRITE24C02    0xA0
#define     READ24C02    0xA1
//I2C part
#define ACK    0
#define     NO_ACK    1
#define MSB    0x80
/********************************************************************/
//change this part at different board
#define LCDIO     P0
sbit LCD1602_RS=P3^5;   //data command select 1 data 0 command pin 4 
sbit LCD1602_RW=P3^6;   //read write select   1 read   0 write     pin 5
sbit LCD1602_EN=P3^7;   //LCD enable signal             pin 6

sbit SDA=P2^1;      //AT24C02 serial data    pin 5 
sbit SCLK=P2^0;     //AT24C02 serial clock    pin 6
/********************************************************************/
void LCD_delay(void);//lcd delay function 
void LCD_en_command(unsigned char command);//write command function
void LCD_en_dat(unsigned char temp);//write data function
void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function
void LCD_init(void);//lcd initize function
/********************************************************************/
void I2C_delay(void);//I2C delay function 
void I2C_start(void);//I2C start function
void I2C_stop(void);//I2C stop function
void I2C_send_ack(bit k);//I2C send responsion function 
void I2C_write_byte(unsigned char dat);//I2C bus write byte function 
unsigned char I2C_read_byte(void);//I2C bus read byte function
/********************************************************************/
void AT24C02_write(unsigned char address,unsigned char dat);//write 24c02 information function
unsigned char AT24C02_read(unsigned char address);//read 24c02 information function
/********************************************************************/
void Mcu_init(void);//system initize funcition
void delay_nms(unsigned int n);//delay functinon
unsigned char count[2];
/********************************************************************/
void main()
{
unsigned char temp; 
Mcu_init();
while(1)    
{
AT24C02_write(0x00,count[1]);
temp=AT24C02_read(0x00);
LCD_write_string(0x00,LINE1,"AT24C02 TEST");
     LCD_write_char(0x0e,LINE1,(temp/10)|0x30);
     LCD_write_char(0x0f,LINE1,(temp%10)|0x30);
     LCD_write_string(0x00,LINE2,"TIMER0 JISHU");
     LCD_write_char(0x0e,1,(count[1]/10)|0x30);
     LCD_write_char(0x0f,1,(count[1]%10)|0x30);
     AT24C02_write(0x00,count[1]); 
}
} 
/***********************************************************************/
void timer0(void) interrupt 1 using 1 
{
TH0=-(12000/256);
    TL0=-(12000%256);
count[0]=count[0]+1;
if(count[0]==100)
    { 
count[0]=0;
count[1]=count[1]+1;
if(count[1]==99) 
   count[1]=0;
} 
}
/***********************************************************************/
void Mcu_init(void) 
{
TMOD=0x11;
TH0=-(12000/256);
TL0=-(12000%256);
EA=HIGH;
ET0=HIGH;
TR0=HIGH;
LCD_init(); 
}
/***********************************************************************/
/******************************** I2C PART **************************/
void I2C_delay(void)
{
_nop_();_nop_();_nop_();_nop_();
}
/***********************************************************************/
void I2C_start(void)
{
SDA=HIGH;
_nop_();
SCLK=HIGH;
_nop_();
SDA=LOW;
_nop_();
SCLK=LOW;
_nop_();
}
/***********************************************************************/
void I2C_stop(void)
{
SDA=LOW;
_nop_();
SCLK=HIGH;
   _nop_();
SDA=HIGH;
_nop_();
SCLK=LOW;
_nop_();
}
/***********************************************************************/
void I2C_send_ack(bit k)
{
SDA=k;
I2C_delay();
SCLK=HIGH;
I2C_delay();
SCLK=LOW;
}
/***********************************************************************/
void I2C_write_byte(unsigned char dat)
{
unsigned char i;
   for (i=8;i>0;i--)
    {
     SCLK=LOW;
     I2C_delay();
     SDA=(bit)(dat&MSB);
     dat<<=1;
     I2C_delay();
     SCLK=HIGH;
     I2C_delay();
    }
    SCLK=LOW;   
}
/***********************************************************************/
unsigned char I2C_read_byte(void)
{
unsigned char i,dat;
   for (i=0;i<8;i++)
    {
     SCLK=LOW;
     I2C_delay();
     SDA=HIGH;
     I2C_delay();
     SCLK=HIGH;
     dat<<=1;
        I2C_delay();
     if(SDA)
        dat++;
    } 
    SCLK=LOW; 
     
   return (dat); 
}
/***********************************************************************/
/************************ 24C02 PART **********************************/
void AT24C02_write(unsigned char address,unsigned char dat)
{
unsigned char temp;
temp=dat/10; 
temp<<=4;
temp=dat%10+temp;

I2C_start(); 
I2C_write_byte(WRITE24C02);     
I2C_send_ack(ACK);
I2C_write_byte(address);   
I2C_send_ack(ACK);    
I2C_write_byte(temp);     
I2C_send_ack(NO_ACK); 
I2C_stop();
}
/***********************************************************************/
unsigned char AT24C02_read(unsigned char address)
{
unsigned char temp,dat;
I2C_start();
I2C_write_byte(WRITE24C02);
I2C_send_ack(ACK);
I2C_write_byte(address); 
I2C_send_ack(NO_ACK);
I2C_stop();

I2C_start();
I2C_write_byte(READ24C02);
I2C_send_ack(ACK);
dat=I2C_read_byte();
I2C_send_ack(NO_ACK);
I2C_stop();        

temp=dat/16;
dat=dat%16;
dat=dat+temp*10;

return (dat);
}
/******************** LCD PART *************************************/
void LCD_delay(void)   
{
unsigned char i;
for(i=LCD_DELAY_TIME;i>ZERO;i--)//be sure lcd reset
   ;
}
/********************************************************************/ 
void LCD_en_command(unsigned char command)
{
LCDIO=command;
LCD1602_RS=LOW;   
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
LCDIO=dat;
LCD1602_RS=HIGH;
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == LINE1) 
address = LINE1_HEAD + x;
else 
     address = LINE2_HEAD + x;
LCD_en_command(address); 
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y ); 
LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
    LCD_set_xy( X, Y ); //set address 
    while (*s) // write character
    {
     LCDIO=*s;
        LCD_en_dat(*s);   
s ++;
    }
}
/********************************************************************/
void LCD_init(void)
{ 
CLEARSCREEN;//clear screen 
LCD_en_command(DATA_MODE);//set 8 bit data transmission mode 
LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)
LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address 
CLEARSCREEN;//clear screen
}
/********************************************************************/
/******************* OTHER PART ***********************************/
void delay_nms(unsigned int n)       
{
    unsigned int i=0,j=0;
    for (i=n;i>0;i--)
     for (j=0;j<1140;j++)
      ; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -