这是最近做的MCF52259开发板测试程序,利用片上I2C读写24C02
/*********************************************************
@ Nicrosystem Freescale coldfire MCF52259 Development Board
@ NSCF52259-R1
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-12-01
**********************************************************/
#include "i2c_driver.h"
#include "at24c02.h"
#include "delay.h"
/* write 24xxxx I2C EEPROM
* 参数:
* buf---包含待写入数据的缓冲区
* addr---eeprom起始地址
* num--数据长度
* 返回:
* 0---success
* 1---failed
* 2---invalid address
*/
unsigned char at24c02_write(unsigned char *buf,unsigned char num,uint8 addr)
{
unsigned char i,ret;
if(addr+num>EEPROM_MAX_ADDR)
return 2;
i2c_start();
ret=i2c_send_byte(WR24C02);
if(ret)
{
i2c_stop();
return 1;
}
ret=i2c_send_byte(addr);
if(ret)
{
i2c_stop();
return 1;
}
for(i=0;i<num;i++)
{
if(i2c_send_byte(buf))
{
i2c_stop();
return 1;
}
}
i2c_stop();
delay_ms(80000,0,5);
return 0;
}
/*read 24xxxx I2C EEPROM
* arguments:
* buf---return the data
* addr---address of cell you want to write
* num--the numner of data we want to read
* return:
* 0---success
* 1---failed
* 2---invalid address
*/
unsigned char at24c02_read(unsigned char *buf, unsigned char num, uint8 addr)
{
unsigned char i,ret;
if(addr+num>EEPROM_MAX_ADDR)
return 2;
i2c_start();
if(i2c_send_byte(WR24C02))
{
i2c_stop();
return 1;
}
ret=i2c_send_byte(addr);
if(ret)
{
i2c_stop();
return 1;
}
i2c_restart();
if(i2c_send_byte(RD24C02))
{
i2c_stop();
return 1;
}
for(i=0;i<num-1;i++)
{
buf=i2c_recv_byte(0);
}
buf[i++]=i2c_recv_byte(1);
i2c_stop();
return 0;
}
/*********************************************************
@ Nicrosystem Freescale coldfire MCF52259 Development Board
@ NSCF52259-R1
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-12-01
**********************************************************/
#include "support_common.h" /* include peripheral declarations and more */
#include "i2c_driver.h"
void init_i2c()
{
unsigned char temp;
MCF_GPIO_PASPAR |=MCF_GPIO_PASPAR_SCL0_SCL0 |MCF_GPIO_PASPAR_SDA0_SDA0 ;
MCF_I2C_I2FDR(0)=MCF_I2C_I2FDR_IC(0x3A);//about 100khz
MCF_I2C_I2ADR(0)=0xf0;
MCF_I2C_I2CR(0)=0x80;//enable i2c
// MCF_I2C1_I2CR=0;
if(MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IBB)
{
MCF_I2C_I2CR(0)=0x0;
MCF_I2C_I2CR(0)=MCF_I2C_I2CR_IEN | /* enable module */
MCF_I2C_I2CR_MSTA;
temp=MCF_I2C_I2DR(0);
MCF_I2C_I2SR(0)=0;
MCF_I2C_I2CR(0)=0x0;
MCF_I2C_I2CR(0)=0x80;//enable i2c
}
}
void i2c_start()
{
while(MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IBB);
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MTX ;
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MSTA ;
}
unsigned char i2c_send_byte(unsigned char c)
{
// MCF_I2C0_I2CR|=MCF_I2C_I2CR_MTX ;
// if(MCF_I2C0_I2SR|MCF_I2C_I2SR_IAL)
// {
//arbitration lost
// MCF_I2C0_I2SR&=~MCF_I2C_I2SR_IAL ;
// }
// i2c_start();
// MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MTX ;
// MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_MSTA ;
MCF_I2C_I2DR(0)=c;
while((MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IIF)==0)
{
}
MCF_I2C_I2SR(0)&=~MCF_I2C_I2SR_IIF;
if(MCF_I2C_I2SR(0)&MCF_I2C_I2SR_RXAK)
return 1;//failed
else
return 0;//success
}
unsigned char i2c_recv_byte(unsigned char ack)
{
unsigned char temp;
if(ack>0)
{
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_TXAK;
}
else
{
MCF_I2C_I2CR(0)&=~MCF_I2C_I2CR_TXAK;
}
MCF_I2C_I2CR(0)&=~MCF_I2C_I2CR_MTX ;
temp=MCF_I2C_I2DR(0);
while((MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IIF)==0)
{
}
MCF_I2C_I2SR(0)&=~MCF_I2C_I2SR_IIF;
temp= MCF_I2C_I2DR(0);
while((MCF_I2C_I2SR(0)&MCF_I2C_I2SR_IIF)==0)
{
}
MCF_I2C_I2SR(0)&=~MCF_I2C_I2SR_IIF;
return temp;
}
void i2c_stop()
{
MCF_I2C_I2CR(0)&=~MCF_I2C_I2CR_MSTA ;
}
void i2c_restart()
{
MCF_I2C_I2CR(0)|=MCF_I2C_I2CR_RSTA;
}
/*********************************************************
@ Nicrosystem Freescale coldfire MCF52259 Development Board
@ NSCF52259-R1
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-12-01
**********************************************************/
/*
* main implementation: use this sample to create your own application
*
*/
#include "support_common.h" /* include peripheral declarations and more */
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
/* Standard IO is only possible if Console or UART support is enabled. */
#include <stdio.h>
#endif
#include "uart.h"
#include "i2c_driver.h"
#include "at24c02.h"
#include "delay.h"
int main(void)
{
int counter = 0;
char ch="0";
uint8 ret="0";
char buf[50];
uint8 addr="0";
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
printf("Hello World in C from MCF52259 derivative on MCF52259 board\n\r");
fflush(stdout);
#endif
uart_init(0,80000,19200);
pit_init(0);
init_i2c();
uart_putstr(0,(char*)("Nicrosystem NSCF52259R1 i2cdemo start....\n"));
for(;;) {
counter++;
ch=uart_getchar(0);
sprintf(buf,"received:%d\n",ch);
uart_putstr(0,buf);
ret=at24c02_write((uint8*)(&ch),1,addr);
if(ret)
{
sprintf(buf,"Write ADDR#%d failed\n",addr);
uart_putstr(0,buf);
}
else
{
sprintf(buf,"Write ADDR#%d success\n",addr);
uart_putstr(0,buf);
}
ch=0;
ret=at24c02_read((uint8*)(&ch),1,addr);
if(ret)
{
sprintf(buf,"Read ADDR#%d failed\n",addr);
uart_putstr(0,buf);
}
else
{
sprintf(buf,"Read ADDR#%d success, get:%d\n",addr,ch);
uart_putstr(0,buf);
}
addr++;
}
}
文章评论(0条评论)
登录后参与讨论