📄 i2c_demo.c
字号:
/**************************************************************************/
/**MITSUBISHI**MITSUBISHI**MITSUBISHI**MITSUBISHI**MITSUBISHI**MITSUBISHI**/
/**************************************************************************/
/**************************************************************************/
/* DISCLAIMER: */
/* We (MITSUBISHI ELECTRIC B.V.) do not warrant that the Software is */
/* free from claims by a third party of copyright, patent, trademark, */
/* trade secret or any other intellectual property infringement. */
/* */
/* Under no circumstances are we liable for any of the following: */
/* */
/* 1. third-party claims against you for losses or damages; */
/* 2. loss of, or damage to, your records or data; or */
/* 3. economic consequential damages (including lost profits or */
/* savings) or incidental damages, even if we are informed of */
/* their possibility. */
/* */
/* We do not warrant uninterrupted or error free operation of the */
/* Software. We have no obligation to provide service, defect */
/* correction, or any maintenance for the Software. We have no */
/* obligation to supply any Software updates or enhancements to you */
/* even if such are or later become available. */
/* */
/* IF YOU DOWNLOAD OR USE THIS SOFTWARE YOU AGREE TO THESE TERMS. */
/* */
/* THERE ARE NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
/* PARTICULAR PURPOSE. */
/**************************************************************************/
/**************************************************************************/
/* */
/* Module : I2C_DEMO.C */
/* Purpose : Demo I2C Bus Program */
/* */
/* Author : Tim Wendland */
/* Mitsubishi Electric Europe B.V. */
/* EDEC */
/* Date : July 2000 */
/* */
/* This program demonstrates the use the UART2 in the clock- */
/* synchronous mode to implement a I2C bus, which communicates */
/* with a Microchip 24C01 EEPROM */
/* */
/**************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "sfr62.h"
#include "alias.h"
#include "i2c_demo.h"
#include "i2c.h"
/**************************************************************************
* Prototypes of Functions
**************************************************************************/
void init_sys(void);
void init_i2c_bus(void);
void read_eeprom_cmd(void);
void write_eeprom_cmd(void);
/**************************************************************************
* Global Variables
**************************************************************************/
unsigned char i2c_ram[256]; // RAM region used in I2C slave transmission and reception.
extern unsigned char status;
/***************************************************************************/
void far main(void)
{
init_sys(); /* Initalize System Clock */
init_i2c_bus(); /* Initalize i2c Bus */
asm("fset i");
while (1)
{
asm("nop");
write_eeprom_cmd();
asm("nop");
read_eeprom_cmd();
asm("nop");
}
}
/***************************************************************************/
void read_eeprom_cmd(void)
{
while(check_i2c_bus_status(Set_flag))
{
// Check if i2c bus is not busy and set the bus flag.
}
i2c_ram[0] = 0x00; // Start address
if(iic_mw_start(0x01,&i2c_ram[0],0x50) != 0)// iic_mw_start(0x01,&i2c_ram[0],0x50);
// 1st Argument: Designate data length sent by master
// 2nd Argument: Designate leading address of where data are stored
// 3rd Argument: Designate the address of the slave device
{
// For DEMO we do not provide any error checking.
if(Get_error_mass() != 0)
asm("nop");
}
while(check_i2c_bus_status(0))
{
// wait for read to be completed.
}
// For DEMO we do not provide any error checking.
if(Get_error_mass() != 0)
asm("nop");
else
{
/* read data */
while(check_i2c_bus_status(Set_flag))
{
// Check if i2c bus is not busy and set the busy flag
}
if(iic_mr_start(0x02,&i2c_ram[0],0x50)!= 0)
{
// For DEMO we do not provide any error checking.
if(Get_error_mass() != 0)
asm("nop");
}
while(check_i2c_bus_status(0))
{
// wait for read to be completed.
}
// For DEMO we do not provide any error checking.
if(Get_error_mass() != 0) // Post process error checking
asm("nop");
else
{
asm("nop");
}
}
}
/***************************************************************************/
void write_eeprom_cmd(void)
{
i2c_ram[0] = 0x00;
i2c_ram[1] = 0x56;
i2c_ram[2] = 0x78;
while(check_i2c_bus_status(Set_flag))
{
// repeat until bus is finished
}
iic_mw_start(0x03,&i2c_ram[0],0x50); // 1st Argument: Designate data length sent by master
// 2nd Argument: Designate leading address of where data are stored
// 3rd Argument: Designate the address of the slave device
while(check_i2c_bus_status(0))
{
// wait for read to be completed.
}
}
/***************************************************************************/
void init_sys(void)
{
/* System clock initialization */
prc0 = 0x01; // Write enable sys clock
cm0 = 0x00; // Set system clock
cm1 = 0x00; // divide main clock by 16
prc0 = 0x00; // disable protect reg
}
/***************************************************************************/
void init_i2c_bus(void) // Initizes I2C slave functions
{
unsigned char *MEM;
int i;
MEM = &i2c_ram[0];
if( iic_stop() == 0){ /* Check fpr current use. kill bus intialize for start of new message */
init_i2c_mas_func();
iic_ini(0x54, i2c_ram); // Intialize I2C device driver.
for (i=0;i<128;i++){
*MEM++ = 0xff; /* zero out ram buffer. */
}
}
}
/***************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -