i2c_sample_master.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 431 行 · 第 1/2 页
C
431 行
/**************************************************************************/
/* */
/* Copyright (C) 2005 Oki Electric Industry Co., LTD. */
/* */
/* System Name : ML67Q4051/ML67Q4061 series */
/* Module Name : ML67Q4051/ML67Q4061 i2c sample program (Master) */
/* File Name : i2c_sample_master.c */
/* Revision : 01.00 */
/* Date : 2005/02/15 */
/* */
/**************************************************************************/
#include "ml674061.h"
#include "common.h"
#ifdef __IAR__
#include "intrinsics.h"
#endif
/* constants */
#define I2C_LED_NORMAL LED_NORMAL_END_PATTERN /* OK pattern */
#define I2C_END_PATTERN 0xFF /* data-end pattern */
#define BUFF_SIZE 16
#define WRITE_MODE 0x0
#define READ_MODE 0x1
#ifdef __AME_51__
#define SLAVE_ADDRESS 0x48 // For TI's TMP100 I2C temp. sensor.
#define I2C_TEMP_INDEX 0x00 // Index for temp reading
#define I2C_TEMP_SIZE 0x02 // The size of the temp reading.
#else
#define SLAVE_ADDRESS 0x30
#endif
#define I2CCTL_400K 0x1
#define I2CBC_100K32 (0x28) /* HCLK=32Mhz,I2CMD=400khz */
#define I2CBC_400K32 (0x0A) /* HCLK=32MHz,I2CMD=400kHz */
#define PORTSEL1_I2C 0x05000000
/* functions */
int main(void); /* Main routine */
void init_i2c(void); /* Initialize I2C */
void i2c_send_data(void); /* I2C send process */
void i2c_receive_data(void); /* I2C receive process */
void send_addr_data(int); /* I2C send addrress data process */
void send_byte_data(UBYTE send_data); /* I2C send data process */
void wait_for_send_receive(void); /* Waiting for Sending data or Receiving data */
int cmp_data_fn(UBYTE*, UBYTE*, int size); /* Data compare process */
#ifdef __AME_51__
void AME_51_Temp_Test(void);
#endif
/* global variables */
unsigned char I2C_SendData[BUFF_SIZE]; /* Send buffer */
unsigned char I2C_ReceivData[BUFF_SIZE]; /* Receive buffer */
UBYTE led_pt; /* LED pattern */
int err_status; /* Error status */
enum err_num { /* Error number */
NO_ERR, /* No error */
DATA_ACK_ERR, /* Data ACK non-receiving error */
ADDR_ACK_ERR, /* Address ACK non-receiving error */
BUS_ERR, /* Arbitration lost error */
DATA_CMP_ERR /* Data comparison error */
};
unsigned char cmp_data[BUFF_SIZE] /* compare data */
= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
/******************************************************************/
/* Entry point */
/* Function : main (Master) */
/* Parameters */
/* Input : Nothing */
/* Output : 0 */
/******************************************************************/
int main(void)
{
init_i2c(); /* initialize i2c */
init_led(); /* initialize led */
led_on(LED_START_PATTERN); /* light led start pattern */
led_pt = LED_NORMAL_END_PATTERN;
#ifdef __AME_51__
AME_51_Temp_Test();
#else
i2c_send_data(); /* data send proc */
if(err_status==NO_ERR)
{
i2c_receive_data(); /* data receive proc */
if(err_status==NO_ERR)
{ /* received data compare */
if(cmp_data_fn(I2C_ReceivData, cmp_data, BUFF_SIZE)==NG)
{
err_status = DATA_CMP_ERR;
led_pt = LED_4; /* turn on LED error */
}
}
}
#endif
led_on(led_pt); /* turn on lower square LED */
return 0;
}
/******************************************************************/
/* Initialize I2C */
/* Function : init_i2c (Master) */
/* Parameters */
/* Input : Nothing */
/* Output : Nothing */
/******************************************************************/
void init_i2c(void)
{
unsigned char i;
/* get a clean start */
put_hvalue(I2CCTL, 0);
put_hvalue(I2CSR, 0);
/***********************************************************
Setup of I2C
I2CCTL_I2CMEN I2C module is enabled.
I2CCTL_I2CMD corresponds to Fast-mode(400kHz)
I2CBC_I2CBC corresponds to HCLK=33MHz,I2CMD=400kHz
***********************************************************/
set_hbit(I2CCTL, I2CCTL_I2CMEN);
set_hbit(I2CCTL, I2CCTL_400K); /* set bit 0: 1 to 400k, 0 to 100k */
put_hvalue(I2CBC, I2CBC_400K32);
set_wbit(PORTSEL1, PORTSEL1_I2C); /* selection of a secondary function */
set_wbit(LSICNT, LSICNT_I2CNFON); /* I2C noise filter available */
/***********************************************************
Clear send_receive_buffer and err_status
***********************************************************/
for(i=0; i<BUFF_SIZE; i++)
{
I2C_SendData[i] = i;
I2C_ReceivData[i] = I2C_END_PATTERN;
}
err_status = NO_ERR;
return;
}
#ifdef __AME_51__
/******************************************************************/
/* I2C Test for the AME_51 on-board temp sensor testing. */
/* Function : AME_51_Temp_Test (Master) */
/* Parameters */
/* Input : Nothing */
/* Output : Nothing */
/******************************************************************/
void AME_51_Temp_Test(void)
{
int i;
set_hbit(I2CCTL, I2CCTL_I2CMTX); /* Master sends data */
while((get_hvalue(I2CSR)&I2CSR_I2CMBB) == I2CSR_I2CMBB); /* Waiting for bus free */
/* Slave address and W are set */
put_hvalue(I2CDR, ((SLAVE_ADDRESS<<1)|WRITE_MODE)&0xFF);
set_hbit(I2CCTL, I2CCTL_I2CMSTA); /* start sequence */
if((get_hvalue(I2CSR)&I2CSR_I2CMAL) == I2CSR_I2CMAL)
{ /* Master received failure */
err_status = BUS_ERR;
led_pt = LED_3; /* turn on LED error */
put_hvalue(I2CSR, 0); /* I2CSR register clear */
return;
}
else
{
wait_for_send_receive(); /* waiting for sending data */
}
if((get_hvalue(I2CSR)&I2CSR_I2CRXAK) == I2CSR_I2CRXAK)
{ /* Master transmitting failure */
err_status = ADDR_ACK_ERR; /* addres ACK error is set */
led_pt = LED_2; /* turn on LED error */
clr_hbit(I2CCTL, I2CCTL_I2CMSTA); /* stop sequence */
put_hvalue(I2CSR, 0); /* I2CSR register clear */
return;
}
put_hvalue(I2CSR, 0); /* I2CSR register clear */
put_hvalue(I2CDR, I2C_TEMP_INDEX); /* send data is set */
wait_for_send_receive(); /* waiting for sending data */
if((get_hvalue(I2CSR)&I2CSR_I2CRXAK) == I2CSR_I2CRXAK)
{ /* Master transmitting failure */
err_status = ADDR_ACK_ERR; /* addres ACK error is set */
led_pt = LED_2; /* turn on LED error */
clr_hbit(I2CCTL, I2CCTL_I2CMSTA); /* stop sequence */
put_hvalue(I2CSR, 0); /* I2CSR register clear */
return;
}
put_hvalue(I2CSR, 0); /* I2CSR register clear */
/* Slave address and W are set */
put_hvalue(I2CDR, ((SLAVE_ADDRESS<<1)|READ_MODE)&0xFF);
set_hbit(I2CCTL, I2CCTL_I2CRSTA); /* restart sequence */
clr_hbit(I2CCTL, I2CCTL_I2CMTX); /* Master receives data */
clr_hbit(I2CCTL, I2CCTL_I2CCS); /* stop clock output upon completion of next transfer */
// while((get_hvalue(I2CSR)&I2CSR_I2CMBB) == I2CSR_I2CMBB); /* Waiting for bus free */
wait_for_send_receive(); /* waiting for sending data */
if((get_hvalue(I2CSR)&I2CSR_I2CRXAK) == I2CSR_I2CRXAK)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?