51 MCU IIC & EEPROM driver

Publisher:素心轻语Latest update time:2019-04-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


#include

#include "./delay/delay.h"

sbit SCL = P2^0;

sbit SDA = P2^1;

bit ack = 0;

 

unsigned char flag = 1;

#define LCDPORT P0

#define LCD_WRITE_DATA 1

#define LCD_WRITE_COM 0

sbit RS = P2^4;

sbit RW = P2^5;

sbit E = P2^6;

 

#define SUCC 0

#define ERR 1

 

void iic_start()

{

SDA = 1; //Operate SDA first, then SCL

  SCL = 1;

  delay_us(1);

  SDA = 0;

delay_us(1);

  SCL = 0; //Clamp the bus

}

 

void iic_stop()

{

SDA = 0;

SCL = 1;

delay_us(1);

SDA = 1;

delay_us(1);

SCL = 0; //Clamp the bus

}

 

bit iic_send_byte(unsigned char byte) //Send data, send high bit first, then send low bit. Only send one byte

{

unsigned char i;

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

{

SDA = byte & 0x80; //When the AND bit is non-zero, SDA = 1; When the AND bit is zero, SDA = 0;

SCL = 1;

delay_us(1); //delay 1us

  SCL = 0;

  byte <<= 1;

}

SCL = 1;

SDA = 1;

delay_us(2);

if(0 == SDA)

{

ack = 1;

}

else

{

ack = 0;

}

SCL = 0;


return ack;

}

unsigned char iic_rcv_byte() //Receive data

{

unsigned char i;

unsigned char temp = 0;

unsigned char a;

SDA = 1;

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

{

SCL = 0;

delay_us(1);

SCL = 1;

if(SDA)

{

a = 0x01;

}

else

{

a = 0;

}

temp |= (a << (7 - i));

delay_us(1);

}

SCL = 0;

return temp;

}

void iic_ack() //response signal

{

SDA = 0; //Pull the data bus low

SCL = 1; // Clock bus pull high

delay_us(1);


SCL = 0; //Clamp the bus

}

void iic_noack() //non-response signal

{

SDA = 1; //Release data bus

SCL = 1; //Pull up the clock bus

delay_us(1);


SCL = 0; //Clamp the bus

}

unsigned char AT24c02_send_byte(unsigned char devaddr, unsigned char romaddr, unsigned char *s, unsigned char num)     //发送字符串

{

unsigned char i;


iic_start(); //Start signal

iic_send_byte(devaddr); //Write hardware address

if(0 == ack) //wait for response

{

return ERR;

}

iic_send_byte(romaddr); //write physical address

if(0 == ack) //wait for response

{

return ERR;

}

for(i = 0; i < num; i++) //Send num characters

{

iic_send_byte(*s); //Send byte by byte

if(0 == ack)

{

return ERR;

}

s++;

}

iic_stop();


return SUCC;

}

unsigned char AT24c02_rcv_byte(unsigned char devaddr, unsigned char romaddr, unsigned char *s, unsigned char num)

{

unsigned char i;

iic_start();

delay_us(10);

iic_send_byte(devaddr); //Write hardware address

if(0 == ack) //wait for response

{

return ERR;

}

iic_send_byte(romaddr); //write physical address

if(0 == ack) //wait for response

{

return ERR;

}

iic_start();

iic_send_byte(devaddr + 1);

if(0 == ack) //wait for response

{

return ERR;

}

for(i = 0; i < num -1; i++) //Start reading data

{

*s = iic_rcv_byte();

iic_ack();

s++;

}

*s = iic_rcv_byte();

iic_noack();

  delay_us(10);

iic_stop();

return SUCC;

}

void lcd1602_write(unsigned char byte, unsigned char flag)

{

if(flag)

{

RS = 1; 

}

else

{

RS = 0;     

}

RW = 0;     

E = 1;      

LCDPORT = byte;

delay_ms(5);

E = 0;   


}

void lcd_init()

{

delay_ms(15);

  lcd1602_write(0x38,LCD_WRITE_COM);

  delay_ms(5);

  lcd1602_write(0x38,LCD_WRITE_COM);

  delay_ms(5);

  lcd1602_write(0x38,LCD_WRITE_COM);

  delay_ms(5);

  lcd1602_write(0x38,LCD_WRITE_COM);

  delay_ms(5);

  lcd1602_write(0x08,LCD_WRITE_COM);

  delay_ms(5);

  lcd1602_write(0x01,LCD_WRITE_COM);

  delay_ms(5);

  lcd1602_write(0x06,LCD_WRITE_COM);

  delay_ms(5);

  lcd1602_write(0x0c,LCD_WRITE_COM);

  delay_ms(5);

}

void lcd_dis_char(unsigned char x, unsigned char y, unsigned char *str)

{

if((x > 15) || (y > 1))

{

return ;

 

}

if(0 == y)

{

lcd1602_write(0x80 + x, LCD_WRITE_COM);

}

else 

{

lcd1602_write(x + 0xc0,LCD_WRITE_COM);

}

while(*str != '')

{

  

lcd1602_write(*str , LCD_WRITE_DATA);

str++;

}

}

 

 

void main()

{

//unsigned char i;

unsigned char test[10] = {'0','1','2','3','4','5','6','7','8','9'};

unsigned char temp[11];

lcd_init();

AT24c02_send_byte(0xae,100,test,10);

delay_s(1);

AT24c02_rcv_byte(0xae,100,temp,10);

temp[10] = '';

lcd_dis_char(0,0,temp);

lcd_dis_char(0,1,temp);

 

while(1)

{



}

}


Reference address:51 MCU IIC & EEPROM driver

Previous article:STM32F103C8T Learning- Environment Configuration
Next article:51 core UART serial bus ring buffer driver implementation

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号