?? 24cxx.c
字號:
#include <REG51.H> /* special function register declarations */
#include <stdio.h> /* for the intended 8051 derivative */
#include <absacc.h>
#include <INTRINS.H>
#include "g_data.h"
#include "24cxx.h"
/***************************************************************************
*名稱:e24_start
*功能:24C32的起始位
***************************************************************************/
void e24_start(void)
{
NOP;
SDA_H;
NOP;
SCL_H;
NOP;
SDA_L;
NOP;
SCL_L;
NOP;
}
/***************************************************************************
*名稱:e24_stop
*功能:24C32的終止位
**********************************************************/
void e24_stop(void)
{
NOP;
SDA_L;
NOP;
SCL_H;
NOP;
SDA_H;
NOP;
SCL_L;
NOP;
}
/***************************************************************************
*名稱:e24_clock
*功能:24C32的一個時鐘
**************************************************************/
void e24_clock(void)
{
NOP;
SCL_H;
NOP;
NOP;
SCL_L;
NOP;
}
/***************************************************************************
*名稱:e24_start
*功能:24C32的每個操作階段后的應答信號
*輸出:result-是否成功標志0為成功
***************************************************************************/
uint8 e24_ack(void)
{
uint8 data result;
NOP;
SCL_H;
SDA_I;
NOP;
result = SDA;
SCL_L;
NOP;
return (result);
}
/***************************************************************************
*名稱:e24_send
*功能:24C32的發(fā)送數(shù)據(jù)函數(shù)
*輸入:sen_data,本此需要發(fā)送的字節(jié)數(shù)據(jù)
*輸出:若操作成功返回1,失敗0
***************************************************************************/
uint8 e24_send(uint8 send_data)
{
uint8 data i;
for (i = 0; i < 8; i++)
{
if ((send_data & 0x80) != 0)
{
SDA_H;
}
else
{
SDA_L;
}
e24_clock();
send_data <<= 1;
}
SDA_H;
//i = e24_ack();
if (e24_ack() !=0)
{
return(0);
}
else
{
return(1);
}
}
/***************************************************************************
*名稱:e24_readclock
*功能:24C32的讀操作中的位操作
*輸入:無
*輸出:所讀到的SDA上的每一位的數(shù)據(jù)
***************************************************************************/
uint8 e24_readclock(void)
{
uint8 data ch;
NOP;
SCL_H;
NOP;
ch = SDA;
NOP;
SCL_L;
NOP;
return (ch);
}
/***************************************************************************
*名稱:e24_receive
*功能:24C32的讀操作的接收部分函數(shù)
*輸入:無
*輸出:read_data-所讀到的SDA上的一個字節(jié)的數(shù)據(jù)
***************************************************************************/
uint8 e24_receive(void)
{
uint8 data read_data,i;
SDA_I;
read_data = 0;
for (i = 0; i < 8; i++)
{
read_data*=2;
if(e24_readclock() != 0)
read_data++;
}
return(read_data);
}
/***************************************************************************
*名稱:e24_write
*功能:24C32的寫操作
*輸入:addr-所寫數(shù)據(jù)的地址,write_data-所要寫入的數(shù)據(jù)
*輸出:1-寫操作成功,0-寫操作失敗
*說明:結(jié)束前后要分別開WP和關(guān)WP
***************************************************************************/
uint8 e24_write(uint16 addr, uint8 write_data)
{
WP_L;
e24_start();
if (e24_send(0xA0))
{
if (e24_send(addr/0x100))
{
if (e24_send(addr%0x100))
{
if (e24_send(write_data))
{
e24_stop();
NOP;
return(1);
}
else
{
return(0);
}
}
else
{
return(0);
}
}
else
{
return(0);
}
}
else
{
return(0);
}
WP_H;
}
/***************************************************************************
*名稱:e24_pagewrite
*功能:24C32的按頁寫操作
*輸入:pro_num-所寫入的協(xié)議對應的序號,data_array-寫入數(shù)據(jù)的指針。
*輸出:1-操作成功,0-操作失敗
*說明:同單個字節(jié)寫。
***************************************************************************/
uint8 e24_pagewrite(uint8 pro_num, uint8 * data_array)
{
uint8 data i, addr_H, addr_L; //--地址分高低字節(jié)存放--
WP_L;
//----確定相應的協(xié)議對應的存放地址-----
addr_H = pro_num/8 + 1;
addr_L = pro_num * 0x20;
e24_start();
if (e24_send(0xA0))
{
if (e24_send(addr_H))
{
if (e24_send(addr_L))
{
for (i = 0; i < 32; i++)
{
if (e24_send(*(data_array+i)) == 0)
{
return(0);
}
}
e24_stop();
NOP;
return(1);
}
else
{
return(0);
}
}
else
{
return(0);
}
}
else
{
return(0);
}
WP_H;
}
/***************************************************************************
*名稱:e24_read
*功能:24C32的讀操作
*輸入:addr-所讀數(shù)據(jù)的地址
*輸出:read_data,讀失敗,一律為0xff
***************************************************************************/
uint8 e24_read(uint16 addr)
{
uint8 read_data;
WP_H;
e24_start();
if (e24_send(0xA0))
{
if (e24_send(addr/0x100))
{
if (e24_send(addr%0x100))
{
e24_start();
if (e24_send(0xA1)) //--讀操作的控制碼--
{
read_data = e24_receive();
e24_clock();
e24_stop();
return(read_data);
}
else
{
e24_stop();
}
}
else
{
e24_stop();
}
}
else
{
e24_stop();
}
}
else
{
e24_stop();
}
return(0xff);
}
/***************************************************************************
*名稱:check_flash
*功能:檢查并載入flash對于的數(shù)據(jù)是否有效
***************************************************************************/
void check_flash(void)
{
uint8 data i, j, k, temp, add_bit, check_sum;
uint16 data addr;
for (i = 0; i < 4; i++)
{
ptl_status[i] = 0;
add_bit = 1;
for (j = 0; j < 8; j++)
{
addr = 0x100 + 0x100 * i + 0x20 * j;
temp = e24_read(addr);
if ((temp > 0) && (temp <= 16))
{
check_sum = temp;
for (k = 2; k < temp + 2; k++)
{
check_sum += e24_read(addr + k);
}
if (check_sum == e24_read(addr + 1))
{
ptl_status[i] += add_bit;
//find_buf[i*8 + j] = e24_read(addr + 2);
}
}
add_bit *= 2;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -