stm32--Analog I2C fram driver for fm24c04i2c-Hua

Source: Internet
Author: User
Tags ack

The driver is suitable for all Ferroelectric series I2C Fram, only the capacity, level (3V and 5V), the driver is for 51MCU, but only slightly modified for other MCU can be used.

When the amount of storage data is small and the frequency of reading and writing is frequent, I2C FRAM can be selected.

This CODE is designed to demonstrate how the fm24c04/fm24c04a serial FRAM could
Be interfaced to the 8051 microcontroller. The INTERFACE USES 2 LINES
From PORT 2 (P2.7 and P2.6) to communicate.
The CODE shown demonstrates A ' RANDOM READ ' and ' BYTE WRITE '. The other *
MODES of OPERATION CAN be CREATED from expanding UPON these routines.
Shenzhen Hua Technology Co., Ltd.
The writed:2003-11-20
Email:gaoqiang@huazhoucn.com
By:tiger
//=============================
#i nclude<reg52.h>
#i nclude<intrins.h>
#define UCHAR unsigned char
#define UINT unsigned int
#define IIC_READ 0XA1//definition Read instruction
#define Iic_write 0xa0//Definition write instructions
#define NOP _nop_ ()
#define SDA INT0
#define SCL T0
//==============================
Uchar Idata buff[32]; Save the byte read out form IIC device in test operation
Uchar idata readbuff[32];//Test Array, staging write readout data
Uchar Writebuff[32];
//===============================
Sbit sda=p1^4;//p2^7; The SDA bit is PORT 2 bit 7
Sbit scl=p1^7;//p2^6; The SCL bit is PORT 2 bit 6
Sbit wp=p2^5;
Sbit test=p1^0;
//===============================
Define a bit_operation byte to use in shift operation
Use this mode can achieve high operation speed
Uchar Bdata bbyte;//defines a bit operation with an array, this method can improve the bit operation speed
Sbit a0=bbyte^0;
Sbit a1=bbyte^1;
Sbit a2=bbyte^2;
Sbit a3=bbyte^3;
Sbit a4=bbyte^4;
Sbit a5=bbyte^5;
Sbit a6=bbyte^6;
Sbit a7=bbyte^7;
//========================================
Bit ifack; The record of the SDA state to Confirn if ACK has happened
Bit no_ack; No ACK flag
Bit bus_fault; Bus fault Flag
//========================================
Function:routes to provide A START SIGNAL
void Start (void)
{
scl=0; Sda=1; scl=1; sda=0; scl=0;
}
//=======================================
Function:routes to provide A STOP SIGNAL
void Stop (void)
{
scl=0; sda=0; scl=1; Sda=1; scl=0;
}
//=====================================
Function:routes to provide ACK Singal
void ack (void)
{
scl=0; sda=0; scl=1; scl=0;
}
//=====================================
Function:routes to release the SDA to RECEIVE A ACK SIGNAL
OR to provide A no_ack SIGNAL
Type=1 Waiting for response signal
Type=0 produces no answer signal
void Nack (Uchar type)
{
scl=0; Sda=1; SCL=1;IFACK=SDA; scl=0;
if (type)
{
if (ifack)//If no answer signal, then place the flag bit No_ack, program abort
{
no_ack=1;//users can join their own exception handlers
while (1);
}
else no_ack=0;
}
}
//=======================================================
Function:the IIC DEVICE SHIFT out of A BYTE to the MASTER
Uchar Inbyte (void) {//Read data from IIC devices
Sda=1;
SCL=1;A7=SDA; scl=0;
SCL=1;A6=SDA; scl=0;
SCL=1;A5=SDA; scl=0;
SCL=1;A4=SDA; scl=0;
SCL=1;A3=SDA; scl=0;
SCL=1;A2=SDA; scl=0;
SCL=1;A1=SDA; scl=0;
SCL=1;A0=SDA; scl=0;
return (Bbyte);
}
//=======================================================
Function:the IIC DEVICE SHIFT in A BYTE from the MASTER
void Outbyte (Uchar outdata) {//write data to IIC device
Bbyte=outdata;
Sda=a7; scl=1; scl=0;
SDA=A6; scl=1; scl=0;
SDA=A5; scl=1; scl=0;
SDA=A4; scl=1; scl=0;
SDA=A3; scl=1; scl=0;
SDA=A2; scl=1; scl=0;
SDA=A1; scl=1; scl=0;
sda=a0; scl=1; scl=0;
}
//======================================================
Function:byte WRITE. ' Add ' the Write address, ' Wbyte ' the DATA WANT to write
void WriteByte (UINT Add,uchar wbyte)//add to write address, wbyte to write data
{
Uchar Temph,templ;
Temph= (Uchar) (add/256);
Templ= (Uchar) (add%256);
Start (); Start signal
Outbyte (Iic_write); Write commands
Nack (1); Waiting for an answer
Outbyte (temph); Write address
Nack (1); Waiting for an answer
Outbyte (Templ); Write address
Nack (1);
Outbyte (Wbyte); Write Data
Nack (1); Waiting for an answer
Stop (); Stop signal
}
//=====================================================
Function:random read. ' Add ' is the address WANT to read
Uchar ReadByte (UINT Add)//add for read address
{
Uchar Temp,temph,templ;
Temph= (Uchar) (add/256);
Templ= (Uchar) (add%256);
Start (); Start signal
Outbyte (Iic_write); Write commands
Nack (1); Waiting for an answer
Outbyte (temph); Write address
Nack (1); Waiting for an answer
Outbyte (Templ); Write address
Nack (1);
Start (); Start signal
Outbyte (Iic_read); Read command
Nack (1); Waiting for an answer
Temp=inbyte (); Reading data
Nack (0); No answer
Stop (); Stop signal
return (temp);
}
//=================================================
ligatures function
Add is read start address, PTR data save pointer, Writelen to write data length
void Writebyteseq (UINT Add,uchar *ptr,uint Writelen)
{
Uchar temp;
Uchar Temph,templ;
UINT I;
Temph= (Uchar) (add/256);
Templ= (Uchar) (add%256);
Start ();
Outbyte (Iic_write);
Nack (1);
Outbyte (temph);
Nack (1);
Outbyte (Templ);
Nack (1);
for (i=0;i<writelen;i++)
{
Outbyte (* (ptr+i));
Nack (1);
}
Stop ();
}
//=================================================
Link-reading function
Add for read start address, PTR data save pointer, Writelen for read data length
void Readbyteseq (UINT Add,uchar *ptr,uint Readlen)
{
Uchar Temph,templ;
UINT I;
Temph= (Uchar) (add/256);
Templ= (Uchar) (add%256);
Start ();
Outbyte (Iic_write);
Nack (1);
Outbyte (temph);
Nack (1);
Outbyte (Templ);
Nack (1);
Start ();
Outbyte (Iic_read);
Nack (1);
for (i=0;i<readlen-1;i++)
{
* (Ptr+i) =inbyte ();
ACK ();

}
* (ptr+readlen-1) =inbyte ();
Nack (0);
Stop ();
}
//=======================================================
This is A TEST OPERATION
void Main (void)
{
Uchar I;
Uchar j=0;
Wp=0;
while (1)
{
if (j++>255) {j=0;}
for (i=0;i<32;i++) readbuff[i]=j;
Writebyteseq (0, Readbuff, 32);
for (i=0;i<32;i++) readbuff[i]=0;
Readbyteseq (0,readbuff,32);
for (i=0;i<32;i++)
WriteByte (I,i);
for (i=0;i<32;i++)
Buff[i]=readbyte (i);
i=0;
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.