PIC16f877A read and write 24c02 program

Publisher:心有所属Latest update time:2017-12-07 Source: eefocusKeywords:PIC16f877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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();}  
}  


Keywords:PIC16f877A Reference address:PIC16f877A read and write 24c02 program

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

PIC16F877A microcontroller (how to write configuration words)
1 MPLAB X V5.0, compiler is XC8 (V2.0) Click production—set Configuration Bit ---- click generate source code to output ---- copy the generated content to the main function // PIC16F877A Configuration Bit Settings // 'C' source line config statements // CONFIG #pragma config FOSC = EXTRC     // Oscillator Select
[Microcontroller]
PIC16F877A microcontroller (how to write configuration words)
Design of a vehicle-mounted infrared night vision device based on PIC16F877A single-chip microcomputer
0 Introduction With the rapid development of the automobile market and the increasing awareness of security, people have higher and higher requirements for automobile safety protection technology. The existing automobile lighting system has the disadvantages of short visibility and poor effect in low visibility condi
[Microcontroller]
Design of a vehicle-mounted infrared night vision device based on PIC16F877A single-chip microcomputer
PWM output program for PIC16F877A and AVR microcontrollers
//In the PICC compiler master, debugging passed, PIC16F877A PWM signal output #include pic.h #define uchar unsigned char #define uint unsigned int __CONFIG(0x1832); void delay() {  uint k;  for(k=0;k 5000;k++);  } void main() {  uchar k;  T2CON=0X04; //Start timer 2  CCPR1L=0X0F; //PWM match initial val
[Microcontroller]
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号