×
嵌入式 > 嵌入式开发 > 详情

PIC16F72-AT24C02读写

发布时间:2020-09-03 发布时间:
|
用PIC单片机控制EEPROM AT24C02读写操作程序

/*************PIC16F72单片机程序******************************/
/*********************************************************************/
/*****File Function : PIC 读写AT24C02 *****/
/*****Program Author : ZhengWen(ClimberWin)*****/
/*****MCU : PIC16F72 外部晶振12MHZ*****/
/*****Compile Date : 2011/04/15 *****/
/*****Edition Info : V1.0 *****/
/*************************************************************/
#include
#include"WIN24C02.H"
__CONFIG(11111110111010);//bit13-bit7=1;bit6 欠压使能(1 enable);bit5=1;bit4 代码保护(0保护);
//bit3 上电延时(0 enable);bit2 看门狗(1 enable);bit1-bit0 时钟选择 (11 RC 10 HS 01 XT OO LP)

 

#define uchar unsigned char
#define uint unsigned int

void Init(void); //初始化子程序
void delayms(unsigned int count);

#define KEY RB0//外部中断


#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;ifor(j=0;j<120;j++);
}
/*********************************************/
void Init(void)
{

PORTB = 0B00000000;
PORTC = 0B00000000;

TRISB = 0B00000001;//设置RB0为输入,作为按键口
TRISC = 0B00000000;//设置RC输出

RBPU=0;//PORTB上拉使能

}


////////////主程序/////////////////////////
void main (void)
{
uint win;
uint i;

Init();//初始化程序
delayms(100);
PORTC=0XFF;
PORTB=0XFF;
PORTA=0XFF;


WIN24C02_write(0x01,0x81);//存数据到EEPROM
delayms(50);
WIN24C02_write(0x02,0x18);//存数据到EEPROM
delayms(50);

while(1)
{
delayms(1000); //延时
PORTC=~WIN24C02_read(0x01);//读取24C02地址00H中的数据
delayms(1000); //延时
PORTC=~WIN24C02_read(0x02);//读取24C02地址00H中的数据
}

}

头文件:

WIN24C02.h

/**********************中文版本*******************************/
/*****功能描述 : PIC单片机 24C02 读写程序*****/
/*****作 者 : ClimberWin *****/
/*****编写日期 : 2011年4月14日*****/
/*****版本信息 : V1.0 *****/
/*****修改日期 : *****/
/*************************************************************/
#ifndef __WIN24C02_H__
#define __WIN24C02_H__

#include

#define uchar unsigned char
#define uint unsigned int


#define SCL RB2 //SCL 引脚定义
#define SDA RB1 //SDA 引脚定义

uchar WIN24C02_read(uchar address); //从24c02的地址address中读取一个字节数据

void WIN24C02_write(uchar address,uchar info); //向24c02的address地址中写入一字节数据info

void WIN24C02_init(); //24C02初始化子程序

void delay1(uchar x);

void start();

void stop();

void writex(uchar j);

uchar readx();

void clock();

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


void WIN24C02_init()
{
TRISB = 0B00000001;//设置RB1 SDA为输出
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;//设置RB1 SDA为输出
SDA=0;
asm("NOP");
SCL=1;
asm("NOP");
SDA=1;
asm("NOP");
}

void writex(uchar j)

{
uchar i,temp;
TRISB = 0B00000001;//设置RB1 SDA为输出
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;//设置RB1 SDA为输出

SCL=0;
asm("NOP");

SDA=1;

TRISB = 0B00000011;//设置RB1 SDA为输入
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;//设置RB1 SDA为输出

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

}

void clock()

{
uchar i=0;
TRISB = 0B00000011;//设置RB1 SDA为输入
SCL=1;

asm("NOP");
while ((SDA==1)&&(i<255))i++;
SCL=0;
asm("NOP");
TRISB = 0B00000001;//设置RB1 SDA为输出

}

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;//关闭中断
start();
writex(0xa0);
clock();
writex(address);
clock();
writex(info);
clock();
stop();
// GIE=1;//打开中断
delay1(50);

}

#endif



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
寻找半导体产业未来10年的驱动力