PIC16F72-AT24C02 Read and Write

Publisher:hxcp18Latest update time:2016-09-05 Source: eefocusKeywords:PIC16F72 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Using PIC microcontroller to control EEPROM AT24C02 reading and writing operation program

/*************PIC16F72 MCU program******************************/
/************************************************************************/
/*****File Function : PIC read and write AT24C02 *****/
/*****Program Author : ZhengWen(ClimberWin) *****/ /
*****MCU : PIC16F72 external crystal oscillator 12MHZ *****/
/*****Compile Date : 2011/04/15 *****/
/*****Edition Info : V1.0 *****/
/*********************************************************************/
#include
#include"WIN24C02.H"
__CONFIG(11111110111010);//bit13-bit7=1;bit6 undervoltage enable (1 enable);bit5=1;bit4 code protection (0 protection);
//bit3 power-on delay (0 enable); bit2 watchdog (1 enable); bit1-bit0 clock selection (11 RC 10 HS 01 XT OO LP)

#define uchar unsigned char
#define uint unsigned int

void Init(void); //Initialization subroutine
void delayms(unsigned int count);

#define KEY RB0 //External interrupt


#define LED16                   RC0
#define LED15                   RC1
#define LED14                   RC2
#define LED13                   RC3
#define LED12                   RC4
#define LED11                   RC5
#define LED10                   RC6
#define LED9                    RC7


/*********************************************/  
void delayms(unsigned int count)
{
 uint i,j;
 for(i=0;i  for(j=0;j<120;j++);
}
/*********************************************/  
void Init(void)
 { 
   
     PORTB = 0B00000000;
     PORTC = 0B00000000;    

     TRISB = 0B00000001; //Set RB0 as input, as a key port
     TRISC = 0B00000000; //Set RC output

     RBPU=0; //PORTB pull-up enable
      
}


/////////////Main program////////////////////////////
void main (void)
{
uint win;
uint i;

  Init(); // Initialization program 
  delayms(100);
  PORTC=0XFF;
  PORTB=0XFF;
  PORTA=0XFF;


 WIN24C02_write(0x01,0x81);//Store data in EEPROM
 delayms(50);    
 WIN24C02_write(0x02,0x18);//Store data in EEPROM
 delayms(50); 

  while(1)
  {
  delayms(1000); //delay
  PORTC=~WIN24C02_read(0x01); //read the data in 24C02 address 00H
  delayms(1000); //delay
  PORTC=~WIN24C02_read(0x02); //read the data in 24C02 address 00H
  }

}

 

head File:

WIN24C02.h

 

 

/**********************Chinese version***********************************/
/*****Function description: PIC microcontroller 24C02 read and write program *****/
/*****Author: ClimberWin *****/
/*****Written date: April 14, 2011 *****/
/*****Version information: V1.0 *****/
/*****Modified date: *****/
/****************************************************************/
#ifndef __WIN24C02_H__
#define __WIN24C02_H__

#include

#define uchar unsigned char
#define uint  unsigned int


#define SCL RB2 //SCL pin definition
#define SDA RB1 //SDA pin definition

uchar WIN24C02_read(uchar address); //Read a byte of data from the address address of 24c02

void WIN24C02_write(uchar address,uchar info); //Write one byte of data info to the address of 24c02

void WIN24C02_init(); //24C02 initialization subroutine

void delay1(volatile x);

void start();

void stop();

void writex(uchar j);

fly readx();

void clock();

void delay1(uchar x)
{
   uint i;
   for(i=0;i }


void WIN24C02_init()
{
 TRISB = 0B00000001; //Set RB1 SDA to output
   SCL=1;
     asm("NOP"); 
   SDA=1;
     asm("NOP"); 
}

void start()
{
 TRISB = 0B00000001;//设置RB1 SDA为输出
   SDA=1;
    asm("NOP"); 
   SCL=1;
     asm("NOP"); 
   SDA=0;
    asm("NOP"); 
   SCL=0;
     asm("NOP"); 
}

void stop()

{
 TRISB = 0B00000001; //Set RB1 SDA to output
   SDA=0;
     asm("NOP"); 
   SCL=1;
     asm("NOP"); 
   SDA=1;
    asm("NOP"); 
}

void writex(uchar j)

{
   uchar i,temp;
 TRISB = 0B00000001; //Set RB1 SDA to output
   temp=j;

   for (i=0;i<8;i++)
   {
     
      SCL=0;
        asm("NOP");

  if (temp & 0x80) //读取
     {
      SDA=1;
     }
    else
     {
      SDA=0;
     }
    temp=temp<<1;
      //SDA=CY;


       asm("NOP"); 
      SCL=1;
        asm("NOP"); 
   }

   SCL=0;
     asm("NOP"); 
   SDA=1;
    asm("NOP");

}

uchar readx()
{
   uchar i,j,k=0;
   TRISB = 0B00000001; //Set RB1 SDA to output

   SCL=0;  
   asm("NOP");
   
   SDA=1;

   TRISB = 0B00000011; //Set RB1 SDA to input
   for (i=0;i<8;i++)
   {
       asm("NOP"); 
       
      SCL=1;
       asm("NOP"); 
       
      if (SDA==1) j=1;
      else j=0;
      k=(k<<1)|j;
      SCL=0;
     
   }
   TRISB = 0B00000001; //Set RB1 SDA to output

    asm("NOP"); 
   return(k);

}

void clock()

{
   uchar i=0;
 TRISB = 0B00000011; //Set RB1 SDA as input
   SCL=1;
     
    asm("NOP");
   while ((SDA==1)&&(i<255))i++;
   SCL=0;
    asm("NOP"); 
   TRISB = 0B00000001; //Set RB1 SDA as output

}

uchar WIN24C02_read(uchar address)

{
   uchar i;
   start();
   writex(0xa0);
   clock();
   writex(address);
   clock();
   start();
   writex(0xa1);
   clock();
   i=readx();
   stop();
   delay1(10);
   return(i);

}

void WIN24C02_write(uchar address,uchar info)

{

  // GIE=0;//disable interruptsstart
   ();
   writex(0xa0);
   clock();
   writex(address);
   clock();
   writex(info);
   clock();
   stop();
  // GIE=1;//enable interruptsdelay1
   (50);

}

#endif

Keywords:PIC16F72 Reference address:PIC16F72-AT24C02 Read and Write

Previous article:PIC16F72-AT24C64 Read and Write
Next article:PIC16F72-74HC595 Control Program

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号