#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)
{
}
}
Previous article:STM32F103C8T Learning- Environment Configuration
Next article:51 core UART serial bus ring buffer driver implementation
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- How to assign a separate segment to the bss of a C file in the link file?
- Voltage to Frequency Converter
- ON
- Why are PCBs mostly green?
- [GD32L233C-START Review] 5. UART+DMA receives variable length data
- 【TI Course】What is the resolution within 5 meters?
- Class AB amplifiers are replaced by Class D amplifiers
- Design of Internet Radio Based on RVB2601
- About MSP430 MCU serial communication data loss
- ccs5.5 printf console output cannot be displayed