TQ210 裸机测试 AT24C02A代码
2014-02-11 13:19
309 查看
#include <bsp.h>
#include <dbgserial.h>
#include "utils.h"
#include "I2C.h"
/* 映射内存*/
#define rGPD1CON (*(volatile unsigned *)(BASE_REG_PA_GPIO+0xC0))
#define rGPD1DAT (*(volatile unsigned *)(BASE_REG_PA_GPIO+0xC4))
#define rGPD1PUD (*(volatile unsigned *)(BASE_REG_PA_GPIO+0xC8))
#define rI2CCON0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X00))
#define rI2CCON2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X00))
#define rI2CSTAT0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X04))
#define rI2CSTAT2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X04))
#define rI2CADD0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X08))
#define rI2CADD2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X08))
#define rI2CDS0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X0C))
#define rI2CDS2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X0C))
#define rI2CLC0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X10))
#define rI2CLC2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X10))
#define rAPLL_CON (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X100))
#define rMPLL_CON (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X108))
#define rCLK_SRC0 (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X200))
#define rCLK_DIV0 (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X300))
/*宏*/
#define IICTEST
#define CLEINTADDRESS (0XA0)
#define MASTERADDRESS (0X10)
#define WRDATA (1)
#define POLLACK (2)
#define RDDATA (3)
#define SETRDADDR (4)
#define IICBUFSIZE 0x20
static unsigned char iicSendData[IICBUFSIZE];
static unsigned char iiRecData[256]; //用于存储AT24C02读出的数据
static volatile int iicSendDataCount;
static volatile int iicStatus;
static volatile int iicMode;
static int iicPt;
static unsigned short ucPower = 0;
unsigned int GetPower()
{
ucPower+=10;
if(ucPower>100)
ucPower=10;
return ucPower;
}
void PowerI2C_Init(void)
{
Uart_SendByte('\r');
Uart_SendByte('\n');
Uart_SendByte('I');
Uart_SendByte('2');
Uart_SendByte('C');
Uart_SendByte('_');
Uart_SendByte('I');
Uart_SendByte('n');
Uart_SendByte('i');
Uart_SendByte('t');
Uart_SendByte('\n');
#ifdef IICTEST
rGPD1CON =
0xffff22 ; // SDA0
rGPD1PUD =
0x0; //UP_DOWN POWER DISABLE Disables Pull-up/down
rI2CCON0 =
( (1<<7) | (1<<6) | (1<<5) | (0x3)) ;
rI2CADD0 = MASTERADDRESS; //写入自己的地址
#else
rGPD1CON = 0x22ffff ; // SDA2
rGPD1PUD = 0X3ff;//UP_DOWN POWER DISABLE Disables Pull-up/down
#endif
}
void Wr24C02(unsigned int slvAddr,unsigned int addr,unsigned char data);
void Rd24C02(unsigned int slvAddr,unsigned int addr,unsigned char *data);
void IicPoll(void);
void Run_IicPoll(void);
void Test_AT(void)
{
unsigned int i,j;
Uart_SendByte('W');
Uart_SendByte('\n');
for(i=0;i<256;i++)
Wr24C02(0xa0,(unsigned char)i,i+0X1);//写入数据到AT24C02
for(i=0;i<256;i++)
//数组数据清零
iiRecData[i] = 0;
Uart_SendByte('R');
Uart_SendByte('\n');
for(i=0;i<256;i++)
Rd24C02(0xa0,(unsigned char)i,&(iiRecData[i]));//读取AT24C02的数据放入data数组中
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
{
Uart_SendDWORD(iiRecData[i*16+j]);
Uart_SendByte(',');
}
Uart_SendByte("\n");
}
}
void Wr24C02(unsigned int slvAddr,unsigned int addr,unsigned char data) // slvAddr 为从地址
{ //addr为字节地址,data为写入的数据
iicMode = WRDATA; //写数据模式
iicPt = 0;
iicSendData[0] = (unsigned char)addr;
iicSendData[1] = data;
iicSendDataCount = 2; //根据AT24C02字节写的发式,要写从地址和字节地址
rI2CDS0 = slvAddr; //把0xa0地址写入到数据移位寄存器IICDS
//Master Tx mode, Start(Write), IIC-bus data output enable
//Bus arbitration sucessful, Address as slave status flag Cleared,
//Address zero status flag cleared, Last received bit is 0
rI2CSTAT0 = 0xf0; //
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(iicSendDataCount!=-1)
Run_IicPoll();
iicMode = POLLACK;
while(1)
{
rI2CDS0 = slvAddr;
iicStatus = 0x100; //To check if _iicStatus is changed
rI2CSTAT0 = 0xf0; //Master Tx, Start, Output Enable, Sucessful, Cleared, Cleared, 0
rI2CCON0 = 0xe3; //Resumes IIC operation.
while(iicStatus==0x100)
Run_IicPoll();
if(!(iicStatus & 0x1))
break; //When ACK is received
}
rI2CSTAT0 = 0xd0; //Master Tx condition, Stop(Write), Output Enable
rI2CCON0 = 0xe3; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Write is completed.
}
//************************[ _Rd24C02 ]********************************
void Rd24C02(unsigned int slvAddr,unsigned int addr,unsigned char *data)
{
iicMode = SETRDADDR; //设置要从从机读取数据的从地址
iicPt = 0;
iicSendData[0] = (unsigned char)addr;
iicSendDataCount = 1;
//写从地址
rI2CDS0 = slvAddr;
rI2CSTAT0 = 0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(iicSendDataCount!=-1)
Run_IicPoll();
iicMode = RDDATA;
//读数据模式
iicPt = 0;
iicSendDataCount = 1;
//
rI2CDS0 = slvAddr;
rI2CSTAT0 = 0xb0; //Master Rx,Start
rI2CCON0 = 0xe3; //Resumes IIC operation.
while(iicSendDataCount!=-1)
Run_IicPoll();
*data = iicSendData[1];
}
void Run_IicPoll(void)
{
if(rI2CCON0 & 0x10) // Tx/Rx 中断使能
IicPoll();
}
void IicPoll(void)
{
unsigned int iicSt,i;
iicSt = rI2CSTAT0;
//ICC状态寄存器
if(iicSt & 0x8){} //总线仲裁失败
if(iicSt & 0x4){} //从地址与ICCADD地址匹配
if(iicSt & 0x2){} //从地址为00000000b
if(iicSt & 0x1){} //未收到ACK
switch(iicMode)
{
case POLLACK:
iicStatus = iicSt;
break;
case RDDATA:
//从从机中读取数据
if((iicSendDataCount--)==0)
{
iicSendData[iicPt++] = rI2CDS0;
rI2CSTAT0 = 0x90; //Stop MasRx condition
rI2CCON0 = 0xe3; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Too long time...
//The pending bit will not be set after issuing stop condition.
break;
}
iicSendData[iicPt++] = rI2CDS0;
//The last data has to be read with no ack.
if((iicSendDataCount)==0)
rI2CCON0 = 0x2f; //Resumes IIC operation with NOACK.
else
rI2CCON0 = 0xe3; //Resumes IIC operation with ACK
break;
case WRDATA:
//写数据到从机
if((iicSendDataCount--)==0)
{
rI2CSTAT0 = 0xd0; //stop MasTx condition
rI2CCON0 = 0xe3; //resumes IIC operation.
Delay(1); //wait until stop condtion is in effect.
//The pending bit will not be set after issuing stop condition.
break;
}
rI2CDS0 = iicSendData[iicPt++]; //iicSendData[0] has dummy.
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rI2CCON0 = 0xe3; //resumes IIC operation.
break;
case SETRDADDR:
//设置要从从机机读取数据的从机地址
if((iicSendDataCount--)==0)
{
break; //IIC operation is stopped because of IICCON[4]
}
rI2CDS0 = iicSendData[iicPt++];
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rI2CCON0 = 0xe3; //resumes IIC operation.
break;
default:
break;
}
}
#include <dbgserial.h>
#include "utils.h"
#include "I2C.h"
/* 映射内存*/
#define rGPD1CON (*(volatile unsigned *)(BASE_REG_PA_GPIO+0xC0))
#define rGPD1DAT (*(volatile unsigned *)(BASE_REG_PA_GPIO+0xC4))
#define rGPD1PUD (*(volatile unsigned *)(BASE_REG_PA_GPIO+0xC8))
#define rI2CCON0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X00))
#define rI2CCON2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X00))
#define rI2CSTAT0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X04))
#define rI2CSTAT2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X04))
#define rI2CADD0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X08))
#define rI2CADD2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X08))
#define rI2CDS0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X0C))
#define rI2CDS2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X0C))
#define rI2CLC0 (*(volatile unsigned *)(BASE_REG_PA_I2C0+0X10))
#define rI2CLC2 (*(volatile unsigned *)(BASE_REG_PA_I2C2+0X10))
#define rAPLL_CON (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X100))
#define rMPLL_CON (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X108))
#define rCLK_SRC0 (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X200))
#define rCLK_DIV0 (*(volatile unsigned *)(BASE_REG_PA_CMU_CLK+0X300))
/*宏*/
#define IICTEST
#define CLEINTADDRESS (0XA0)
#define MASTERADDRESS (0X10)
#define WRDATA (1)
#define POLLACK (2)
#define RDDATA (3)
#define SETRDADDR (4)
#define IICBUFSIZE 0x20
static unsigned char iicSendData[IICBUFSIZE];
static unsigned char iiRecData[256]; //用于存储AT24C02读出的数据
static volatile int iicSendDataCount;
static volatile int iicStatus;
static volatile int iicMode;
static int iicPt;
static unsigned short ucPower = 0;
unsigned int GetPower()
{
ucPower+=10;
if(ucPower>100)
ucPower=10;
return ucPower;
}
void PowerI2C_Init(void)
{
Uart_SendByte('\r');
Uart_SendByte('\n');
Uart_SendByte('I');
Uart_SendByte('2');
Uart_SendByte('C');
Uart_SendByte('_');
Uart_SendByte('I');
Uart_SendByte('n');
Uart_SendByte('i');
Uart_SendByte('t');
Uart_SendByte('\n');
#ifdef IICTEST
rGPD1CON =
0xffff22 ; // SDA0
rGPD1PUD =
0x0; //UP_DOWN POWER DISABLE Disables Pull-up/down
rI2CCON0 =
( (1<<7) | (1<<6) | (1<<5) | (0x3)) ;
rI2CADD0 = MASTERADDRESS; //写入自己的地址
#else
rGPD1CON = 0x22ffff ; // SDA2
rGPD1PUD = 0X3ff;//UP_DOWN POWER DISABLE Disables Pull-up/down
#endif
}
void Wr24C02(unsigned int slvAddr,unsigned int addr,unsigned char data);
void Rd24C02(unsigned int slvAddr,unsigned int addr,unsigned char *data);
void IicPoll(void);
void Run_IicPoll(void);
void Test_AT(void)
{
unsigned int i,j;
Uart_SendByte('W');
Uart_SendByte('\n');
for(i=0;i<256;i++)
Wr24C02(0xa0,(unsigned char)i,i+0X1);//写入数据到AT24C02
for(i=0;i<256;i++)
//数组数据清零
iiRecData[i] = 0;
Uart_SendByte('R');
Uart_SendByte('\n');
for(i=0;i<256;i++)
Rd24C02(0xa0,(unsigned char)i,&(iiRecData[i]));//读取AT24C02的数据放入data数组中
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
{
Uart_SendDWORD(iiRecData[i*16+j]);
Uart_SendByte(',');
}
Uart_SendByte("\n");
}
}
void Wr24C02(unsigned int slvAddr,unsigned int addr,unsigned char data) // slvAddr 为从地址
{ //addr为字节地址,data为写入的数据
iicMode = WRDATA; //写数据模式
iicPt = 0;
iicSendData[0] = (unsigned char)addr;
iicSendData[1] = data;
iicSendDataCount = 2; //根据AT24C02字节写的发式,要写从地址和字节地址
rI2CDS0 = slvAddr; //把0xa0地址写入到数据移位寄存器IICDS
//Master Tx mode, Start(Write), IIC-bus data output enable
//Bus arbitration sucessful, Address as slave status flag Cleared,
//Address zero status flag cleared, Last received bit is 0
rI2CSTAT0 = 0xf0; //
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(iicSendDataCount!=-1)
Run_IicPoll();
iicMode = POLLACK;
while(1)
{
rI2CDS0 = slvAddr;
iicStatus = 0x100; //To check if _iicStatus is changed
rI2CSTAT0 = 0xf0; //Master Tx, Start, Output Enable, Sucessful, Cleared, Cleared, 0
rI2CCON0 = 0xe3; //Resumes IIC operation.
while(iicStatus==0x100)
Run_IicPoll();
if(!(iicStatus & 0x1))
break; //When ACK is received
}
rI2CSTAT0 = 0xd0; //Master Tx condition, Stop(Write), Output Enable
rI2CCON0 = 0xe3; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Write is completed.
}
//************************[ _Rd24C02 ]********************************
void Rd24C02(unsigned int slvAddr,unsigned int addr,unsigned char *data)
{
iicMode = SETRDADDR; //设置要从从机读取数据的从地址
iicPt = 0;
iicSendData[0] = (unsigned char)addr;
iicSendDataCount = 1;
//写从地址
rI2CDS0 = slvAddr;
rI2CSTAT0 = 0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(iicSendDataCount!=-1)
Run_IicPoll();
iicMode = RDDATA;
//读数据模式
iicPt = 0;
iicSendDataCount = 1;
//
rI2CDS0 = slvAddr;
rI2CSTAT0 = 0xb0; //Master Rx,Start
rI2CCON0 = 0xe3; //Resumes IIC operation.
while(iicSendDataCount!=-1)
Run_IicPoll();
*data = iicSendData[1];
}
void Run_IicPoll(void)
{
if(rI2CCON0 & 0x10) // Tx/Rx 中断使能
IicPoll();
}
void IicPoll(void)
{
unsigned int iicSt,i;
iicSt = rI2CSTAT0;
//ICC状态寄存器
if(iicSt & 0x8){} //总线仲裁失败
if(iicSt & 0x4){} //从地址与ICCADD地址匹配
if(iicSt & 0x2){} //从地址为00000000b
if(iicSt & 0x1){} //未收到ACK
switch(iicMode)
{
case POLLACK:
iicStatus = iicSt;
break;
case RDDATA:
//从从机中读取数据
if((iicSendDataCount--)==0)
{
iicSendData[iicPt++] = rI2CDS0;
rI2CSTAT0 = 0x90; //Stop MasRx condition
rI2CCON0 = 0xe3; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Too long time...
//The pending bit will not be set after issuing stop condition.
break;
}
iicSendData[iicPt++] = rI2CDS0;
//The last data has to be read with no ack.
if((iicSendDataCount)==0)
rI2CCON0 = 0x2f; //Resumes IIC operation with NOACK.
else
rI2CCON0 = 0xe3; //Resumes IIC operation with ACK
break;
case WRDATA:
//写数据到从机
if((iicSendDataCount--)==0)
{
rI2CSTAT0 = 0xd0; //stop MasTx condition
rI2CCON0 = 0xe3; //resumes IIC operation.
Delay(1); //wait until stop condtion is in effect.
//The pending bit will not be set after issuing stop condition.
break;
}
rI2CDS0 = iicSendData[iicPt++]; //iicSendData[0] has dummy.
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rI2CCON0 = 0xe3; //resumes IIC operation.
break;
case SETRDADDR:
//设置要从从机机读取数据的从机地址
if((iicSendDataCount--)==0)
{
break; //IIC operation is stopped because of IICCON[4]
}
rI2CDS0 = iicSendData[iicPt++];
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rI2CCON0 = 0xe3; //resumes IIC operation.
break;
default:
break;
}
}
相关文章推荐
- Delphi 7 ADO使用
- Qt 中sax方式 解析 xml文件
- c++的类型转换
- Qt多线程概述
- [遇到的问题]java正则表达式
- C#中params的使用
- C++编程基础之函数指针
- python优化
- Java虚拟机参数 -XX等相关参数应用
- 稀疏表达:向量、矩阵与张量(上)
- asp.net页面传值方法汇总
- Java命令行运行参数说明大全(偷来的)
- uva - 11292 - Dragon of Loowater(贪心)
- eclipse生成Hibernate映射文件
- C#编程总结--总目录
- c#笔记
- netperf安装以及使用方法
- 整理之c++笔试2
- java慎用String.substring(int start, int end)
- 整理之c++笔试