/*************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
}
/*********************************************/
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
Previous article:PIC16F72-AT24C64 Read and Write
Next article:PIC16F72-74HC595 Control Program
- Popular Resources
- Popular amplifiers
- DIY design of battery management system (schematic diagram + PCB + BOM + part of source code)
- DIY design of battery management system (schematic diagram + PCB + BOM + part of source code)
- MCU Principles and Interface Technology C51 Programming (Edited by Zhang Yigang)
- Example interpretation of 51 single chip microcomputer complete learning and application
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
- Circuit parameters of real op amp
- [Urgent recruitment in Shanghai] RFIC senior design engineer
- Hamburg Records
- About the problem that KEIL MDK can't simulate debugging ARM program
- Flash (STM32) "Big Explanation"
- 【Live FAQ】Develop AI intelligent robots based on TI's newly released Robotics SDK
- ISA bus interface.pdf
- MCU Programming Ideas - State Machine
- Touch screen interface definition
- Overview of 5G System Standard Development