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

PIC16F72-AT24C64读写

发布时间:2020-09-03 发布时间:
|
AT24C64和AT24C02程序上的主要是在读写时序上有些差别,具体看数据手册。这里列举一个字节写的时序差别。

 

/*************PIC16F72单片机程序******************************/
/*********************************************************************/
/*****File Function : PIC 读写AT24C64 *****/
/*****Program Author :ClimberWin *****/
/*****MCU : PIC16F72 外部晶振6MHZ *****/
/*****Compile Date : 2011/04/15 *****/
/*****Edition Info : V1.0 *****/
/*************************************************************/
#include
#include"WIN24C64.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;
uchar i;

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


WIN24C64_write(0x0000,0x55);//存数据到EEPROM
delayms(20);
WIN24C64_write(0x0001,0xaa);//存数据到EEPROM
delayms(20);

while(1)
{
delayms(1000); //延时
PORTC=~WIN24C64_read(0x0000);//读取24C64地址00H中的数据
delayms(1000); //延时
PORTC=~WIN24C64_read(0x0001);//读取24C64地址00H中的数据
}

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

WIN24C64.H 头文件

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

#include

#define uchar unsigned char
#define uint unsigned int


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

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

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

void WIN24C64_init(); //24C64初始化子程序

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 WIN24C64_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 WIN24C64_read(uint address)

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

}

void WIN24C64_write(uint address,uchar info)

{

start();
writex(0xa0);
clock();
writex(address/256);
clock();
delay1(10);
writex(address%256);
clock();
writex(info);
clock();
stop();
delay1(200);

}

#endif



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

热门文章 更多
django是什么_django能做什么