PIC16f877A read and write 24c01 program

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

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


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

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

PIC16F877A-UART
#include #define unchar unsigned char #define uint unsigned intunchar RC_label; //The starting bit of the flag is 1 and the ending bit is 0 unchar caiji_label;//Acquisition frame format flag is 1 if correct and 0 if wrong unchar data RC ;//Store received data unchar data TX1 ;//Store own parameters unchar dat
[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号