#include
#include
#define uchar unsigned char
#define uint unsigned int
static volatile unsigned char a CC @ 0x20; //Send 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; //acknowledgement bit temporary storage
void Cack(); //acknowledgement bit check
void Mnack(); //send non-acknowledgement bit
void Mack(); //send acknowledgement 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 write
#define SLAR 0x0A1 //Addressing byte 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/////////////////
//--------Check the acknowledge bit---------
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 the 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 a byte
delay1();
Cack();//Check the acknowledge 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 each I/O initialization subroutine
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:Introduction to PIC12F675 DC Motor Control
Next article:Digital potentiometer X9241 and PIC microcontroller interface program
Recommended ReadingLatest update time:2024-11-16 17:35
- Popular Resources
- Popular amplifiers
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
- Does anyone know about the FDS9435 chip?
- Button control water flow light: press key1, LED1-8 will flow in sequence, press key2, LED1/3/5/7 (4 intervals)...
- Tips: Quickly locate components in Altium Designer
- MSP430+DMA
- Can vias be placed on pads?
- AM26LV32E Low Voltage High Speed Quadruple Differential Line Receiver
- Is a 12 bit oscilloscope really necessary?
- 5G Science (Comic Version, So Easy!)
- Design of PCIJMC2000 Computer Data Encryption Card Based on DSP
- Review Weekly: New arrivals, Qinheng wireless charging kit, Beineng cost-effective ATSAMD51 board are waiting for you~20221107