P IC 16f877A read and write 24c02 program
#include
#include
#define uchar unsigned char
#define uint unsigned int
static volatile unsigned char a CC @ 0x20; //transmit and receive data register
static volatile bit acc0 @ (unsigned)&acc*8+0; //receive
static volatile bit acc7 @ (unsigned)&acc*8+7; //send
static volatile unsigned char err @ 0x21; //
static volatile bit err0 @ (unsigned)&err*8+0;
static volatile bit err3 @ (unsigned)&err*8+3;
static volatile bit err6 @ (unsigned)&err*8+6;
static volatile bit err7 @ (unsigned)&err*8+7; //response bit temporary storage
void Cack(); //response bit check
void Mnack(); //send non-response bit
void Mack(); //send response bit
void Stop(); //stop IC bus
void Sta(); //start IC bus
void Wrbyt (uchar x); //Write one byte
uchar Rdbyt() ); //Read one byte
void Rdnbyt(); //Read n data
void Wrnbyt(uchar SLAwtemp ); //Write n data
void initial(); //I/O initialization subroutine
void LED (); //
void delay(uint a);
void delay1();
#define SCL RB4 //4 ;24C01-6
#define SDA RB5 //5 ;24C01-5
#define SCLIO TRISB4 //4
#define SDAIO TRISB5 //5
#define SLAW 0x0A0 //Addressing byte to write
#define SLAR 0x0A1 //Addressing byte to read
uchar numer=1; // Digital tube data temporary storage area
unsigned char sun[10] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//////////////24C01//////////////////
//--------Answer bit check---------
void Cack()
{
SCL=1;
SDAIO=1;//Input 1
SCLIO=0;
delay1();
err7=SDA;//c=1 error ERR
SCL=0;
}
///--------Send non-acknowledge bit----------
void Mnack()
{
SDA=1;
SCLIO=0;
SDAIO=0;//Output
SCL=1;
delay1();
SCL=0;
SDA=0;
}
//-------Send acknowledge bit---------------
void Mack()
{
SDA=0;
SCLIO=0;
SDAIO=0;//output
SCL=1;
delay1();
SCL=0;
SDA=1;
}
//--------Stop IC bus---------------
void Stop()
{
SDAIO=0;//output
SCLIO=0;
SDA=0;
delay1();
SCL=1;
delay1();
SDA=1;
delay1();
}
//--------;Start IC bus-------------
void Sta()
{
SDAIO=0;//output
SCLIO=0;
SDA=1;
delay1();
SCL=1;
delay1();
SDA=0;
delay1();
SCL=0;
}
//--------Write one byte------------------------
void Wrbyt(uchar x)
{
uchar i;
acc=x;
SCLIO=0;
SDAIO=0;
for(i=8;i>0;i--)
{
SDA=acc7;
SCL=1;
delay1();
SCL=0;
SDA=0;
acc=acc<<1;
}
}
//---------Read one byte-----------------
uchar Rdbyt()
{
uchar i;
SCLIO=0;
SDAIO=1;//;Inputfor
(i=8;i>0;i--)
{
acc=acc<<1;//Receive data left shift register
SCL=1;
delay1();
acc0=SDA;
SCL=0;
delay1();
}
return(acc);
}
////---------Read n data----------------
void Rdnbyt() //Transmit data
{
Sta(); //Start IC bus
delay1();
numer = Rdbyt();///Read one byte
delay1();
Stop(); //Stop IC bus
}
//---------Write n data---------------
void Wrnbyt(uchar SLAwtemp )
{
err7=1;
if(err7==1)///c=1E0,ERR
{
Sta(); //Start IC bus
delay1();
Wrbyt(SLAwtemp); //Write one byte
delay1();
Cack();//Check the response bit-
}
delay1();
while(err7==1);
Stop(); //Stop IC bus
}
//*************************************//
//System Each I/O initialization subroutine
void initial()
{
TRISC=0;
TR ISA =0;
TRISB1=1;
TRISB2=1;
}
//LED
void LED ()
{
PORTA=0X1f;
PORTC=sun[numer];
}
void delay(uint a)
{
uint b=100;
while(a--)
{
while(b--);
}
}
void delay1()
{
NOP();
NOP();
NOP();
NOP();
}
void main()
{
initial();//System I/O initialization subroutines
LED();
delay(100);
Wrnbyt (SLAW);//Address byte write #0A0
delay1();
Wrnbyt (0x05);//Write address 0x05
delay1();
Wrnbyt (0x06);//Write data 0x06
delay1();
Wrnbyt (SLAR);//Address byte read #0A1
delay1();
Wrnbyt (0x05);//Read address 0x05
delay1();
Rdnbyt (); //Read data
delay(5);
while(1)
{LED();}
}
Previous article:PIC single digital tube cyclically displays 0-9
Next article:pic16f877 dual machine communication lcd1602 liquid crystal display
Recommended ReadingLatest update time:2024-11-16 15:19
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Battery Pack BMS Coulomb Counter Chip Solution
- How to distinguish between pads and vias_Differences between vias and pads
- Could you please tell me what circuit is generally used to implement the 0/4-20mA drive circuit?
- Principle and application of diaphragm pressure gauge
- Showing off your products (6) - silicon lab
- EEWORLD University - How to draw a high-end PCB ruler with Altium20
- Agitek case sharing - Metrology and testing demonstration case of automotive electronic modules
- How to turn off MPLAB XIDE's code optimization function
- IoT Smart Parking Solutions
- EPWM1 A and B pulses are not complementary