5985|2

473

帖子

0

TA的资源

纯净的硅(中级)

楼主
 

MCF52259读写I2C EEPROM [复制链接]

利用MCF52259片上i2c 控制器读写常用的at24c02 eeprom,用的开发板是我们做的NSCF52259-R1开发板





//filename: i2c_driver.h
//mcf52259 i2c驱动头文件
#ifndef _NICROSYSTEM_NSCF52259R1_I2C_DRIVER_H_
#define _NICROSYSTEM_NSCF52259R1_I2C_DRIVER_H_

void init_i2c();
void i2c_start();
unsigned char i2c_send_byte(unsigned char c);
unsigned char i2c_recv_byte(unsigned char ack);
void i2c_stop();
void i2c_restart();
#endif





//Filename: i2c_driver.c
//MCF52259 I2c驱动
#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;
}





//Filename: uart.h
//串口驱动头文件
#ifndef _NICROSYSTEM_NSMCF52259R1_UART_H_
#define _NICROSYSTEM_NSMCF52259R1_UART_H_
#include "support_common.h"

void uart_putstr(uint8 channel, char *str);
void uart_putchar (uint8 channel, char ch);
char uart_getchar (uint8 channel);
void set_gpio_uart(uint8 channel);
uint8 uart_init(uint8 channel,uint32 sysClockKhz,uint32 baudRate);

#endif


//filename:uart.c
//串口驱动
#include "uart.h"

uint8 uart_init(uint8 channel,uint32 sysClockKhz,uint32 baudRate)
{
uint16 ubgs;

if(channel>2)
{
  return 1;//error
}
set_gpio_uart(channel);
/*
  * Reset Transmitter
  */
MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_TX;

/*
  * Reset Receiver
  */
MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_RX;

/*
  * Reset Mode Register
  */
MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_MR;

/*
  * No parity, 8-bits per character
  */
MCF_UART_UMR(channel) = (0
  | MCF_UART_UMR_PM_NONE
  | MCF_UART_UMR_BC_8 );

/*
  * No echo or loopback, 1 stop bit
  */
MCF_UART_UMR(channel) = (0
  | MCF_UART_UMR_CM_NORMAL
  | MCF_UART_UMR_SB_STOP_BITS_1);

/*
  * Set Rx and Tx baud by SYSTEM CLOCK
  */
MCF_UART_UCSR(channel) = (0
  | MCF_UART_UCSR_RCS_SYS_CLK
  | MCF_UART_UCSR_TCS_SYS_CLK);

/*
  * Mask all UART interrupts
  */
MCF_UART_UIMR(channel) = 0;

/*
  * Calculate baud settings
  */
ubgs = (uint16)((sysClockKhz * 1000)/(baudRate * 32));

MCF_UART_UBG1(channel) = (uint8)((ubgs & 0xFF00) >> 8);
MCF_UART_UBG2(channel) = (uint8)(ubgs & 0x00FF);
/*
  * Enable receiver and transmitter
  */
MCF_UART_UCR(channel) = (0
  | MCF_UART_UCR_TX_ENABLED
  | MCF_UART_UCR_RX_ENABLED);

return 0;//success
}

void set_gpio_uart(uint8 channel)
{
switch(channel)
{
  case 0:
  MCF_GPIO_PUAPAR|=MCF_GPIO_PUAPAR_UTXD0_UTXD0 |
  MCF_GPIO_PUAPAR_URXD0_URXD0 |MCF_GPIO_PUAPAR_URTS0_URTS0 |
  MCF_GPIO_PUAPAR_UCTS0_UCTS0 ;
  break;
  
  case 1:
  MCF_GPIO_PUBPAR|=MCF_GPIO_PUBPAR_UTXD1_UTXD1  |
  MCF_GPIO_PUBPAR_URXD1_URXD1 ;
  break;
  
  case 2:
  MCF_GPIO_PUCPAR|=MCF_GPIO_PUCPAR_UTXD2_UTXD2  |
  MCF_GPIO_PUCPAR_URXD2_URXD2 ;
  break;
  
  default:
  break;
}

}

/*
* Wait for a character to be received on the specified UART
*
* Return Values:
*  the received character
*/
char uart_getchar (uint8 channel)
{
    /* Wait until character has been received */
    while (!(MCF_UART_USR(channel) & MCF_UART_USR_RXRDY))
    {
     
    };

    return (char)MCF_UART_URB(channel);
}


/********************************************************************/
/*
* Wait for space in the UART Tx FIFO and then send a character
*/
void uart_putchar (uint8 channel, char ch)
{
    /* Wait until space is available in the FIFO */
    while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY))
    {
     
    };

    /* Send the character */
    MCF_UART_UTB(channel) = (uint8)ch;
}

/*********************
* send a string using poll mode
*******************/
void uart_putstr(uint8 channel, char *str)
{
while(*str!=0)
{
  uart_putchar(channel,*str++);
}
}





//filename: at24c02.h
//at24c02驱动头文件

#ifndef _NICROSYSTEM_NSCF52259R1_AT24C02_H_
#define _NICROSYSTEM_NSCF52259R1_AT24C02_H_
#include "support_common.h"

#define EEPROM_MAX_ADDR  256
#define WR24C02 0xA0
#define RD24C02 0xA1

unsigned char at24c02_write(unsigned char *buf,unsigned char num,uint8 addr);
unsigned char  at24c02_read(unsigned char *buf, unsigned char num, uint8 addr);
#endif





//filename:at24c02.c
//at24c02驱动
#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
{
  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
{
  buf=i2c_recv_byte(0);
}
buf[i++]=i2c_recv_byte(1);
i2c_stop();
return 0;
}





//filename:main.c
//demo主程序
/*
* 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
#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++;
     
}

}

此帖出自NXP MCU论坛

最新回复

说说你现在是什么状态,需要了解些什么?  详情 回复 发表于 2009-12-23 15:02
点赞 关注
 

回复
举报

6

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
好难懂啊
此帖出自NXP MCU论坛
 
 
 

回复

2万

帖子

74

TA的资源

管理员

板凳
 

回复 沙发 beijixue521 的帖子

说说你现在是什么状态,需要了解些什么?
此帖出自NXP MCU论坛
加EE小助手好友,
入技术交流群
EE服务号
精彩活动e手掌握
EE订阅号
热门资讯e网打尽
聚焦汽车电子软硬件开发
认真关注技术本身
 
个人签名

加油!在电子行业默默贡献自己的力量!:)

 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表