原创 MCF52211读写24c256的程序

2009-6-15 08:42 2676 5 5 分类: MCU/ 嵌入式

/********************************


** File:i2c_driver.h


** the MCF52211 microcontroller's i2c module driver header file


********************************/


#ifndef _LZP_MCF52211_I2C_DRIVER_H_
#define _LZP_MCF52211_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






/**************************************


** File:i2c_driver.c


** The MCF52211 Microcontroller's i2c module driver


*************************************/


#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(0x38);//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;
}





/*******************************


**File: at24c256.h


** AT24c256 eeprom driver's header file


*******************************/


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


#define EEPROM_MAX_ADDR  32768
#define WR24C256 0xA0
#define RD24C256 0xA1


unsigned char at24c256_write(unsigned char *buf,unsigned char num,uint16 addr);
unsigned char  at24c256_read(unsigned char *buf, unsigned char num, uint16 addr);
#endif





/**************************************


** File: at24c256.c


**at24c256's driver


*************************************/


#include "i2c_driver.h"
#include "at24c256.h"
#include "delay.h"


/* write 24xxxx I2C EEPROM
* 参数:
* buf---包含待写入数据的缓冲区
* addr---eeprom起始地址
* num--数据长度
* 返回:
* 0---success
* 1---failed
* 2---invalid address
*/
unsigned char at24c256_write(unsigned char *buf,unsigned char num,uint16 addr)
{
 unsigned char i,ret;
 
 if(addr+num>EEPROM_MAX_ADDR)
  return 2;
 
 i2c_start();
 ret=i2c_send_byte(WR24C256);
 if(ret)
 {
  i2c_stop();
  return 1;
 }
 i=(uint8)((addr&0xff00)>>8);
 ret=i2c_send_byte(i);
 if(ret)
 {
  i2c_stop();
  return 1;
 }
 i=(uint8)(addr&0x00ff);
 ret=i2c_send_byte(i);
 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(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  at24c256_read(unsigned char *buf, unsigned char num, uint16 addr)
{
 unsigned char i,ret;
 
 if(addr+num>EEPROM_MAX_ADDR)
  return 2;
 
 i2c_start();
 if(i2c_send_byte(WR24C256))
 {
  i2c_stop();
  return 1;
 }
 i=(uint8)((addr&0xff00)>>8);
 ret=i2c_send_byte(i);
 if(ret)
 {
  i2c_stop();
  return 1;
 }
 i=(uint8)(addr&0x00ff);
 ret=i2c_send_byte(i);
 if(ret)
 {
  i2c_stop();
  return 1;
 }
 i2c_restart();
 if(i2c_send_byte(RD24C256))
 {
  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;
}





/*****************************


** File: test.c


** test code for at24c256 driver


***************************/


#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 "uart0.h"
#include "i2c_driver.h"
#include "at24c256.h"


int main(void)
{
    uint16 counter = 0;
 char buf[50];
 unsigned char c;
 
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
 printf("Hello World in C from MCF52211 derivative on MCF52211 board\n\r");
 fflush(stdout);
#endif
    init_i2c();
 uart0_init(9600);
 uart0_putstr((unsigned char*)("MCF52211 At24C256 Test\n"));
/* while(1)
 {
  i2c_start();
 }*/
 for(;;) {  
  c=uart0_getchar();
  if(at24c256_write(&c,1,counter))
  {
   sprintf(buf,"write to #%d failed\n",counter); 
  }
  else
  {
   sprintf(buf,"write to #%d success\n",counter);
  }
   
     uart0_putstr((unsigned char*)buf);
     c=0;
     if(at24c256_read(&c,1,counter))
     {
      sprintf(buf,"read #%d failed\n",counter);
     }
     else
     {
      sprintf(buf,"read #%d =%d\n",counter,c);
     }
     uart0_putstr((unsigned char*)buf);
     
     counter++;
 }
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
5
关闭 站长推荐上一条 /3 下一条