📄 i2c_master.c
字号:
/***************************************************************************
Author : ADI - Apps www.analog.com/MicroConverter
Date : Sept. 2005
File : I2C_Master.c
Hardware : Applicable to ADuC702x rev H or I silicon
Currently targetting ADuC7026.
Description : I2C master to demonstrate with I2C_Slave.c
Operates in two modes, read & write (called recieve and
transmit here). At the begining of an I2C transmission, the
Master sends an address. The LSB of this address determines
if the Master is to read (1) or write (0).
***************************************************************************/
#include<ADuC7020.h>
void delay(int);
void My_IRQ_Function();
#define count 0x4; // Number of bytes to be recieved - 1
int i = 0, dat[5]; // Size of dat should be (count + 1)
int main()
{
GP1CON = 0x22; // I2C on P1.0 and P1.1
I2C0CFG = 0x82; // Master Enable & Enable Generation of Master Clock
// I2C-Master setup
I2C0DIV = 0xCFCF; // 0x3232 = 400kHz
// 0xCFCF = 100kHz
IRQ = My_IRQ_Function; // Specify Interrupt Service Routine
IRQEN = 0x400; // I2C0 Master Interupt
// Transmit
I2C0ADR = 0xA0; // set i2c address (LSB = 0, Master Write)
I2C0MTX = 0x55; // send i2c byte address
delay(1500);
// Recieve
i = 0;
I2C0CNT = count; // Number of bytes to be read from slave
I2C0ADR = 0xA1; // set i2c address (LSB = 1, Master Read)
while (1)
{
};
return 0;
}
void delay (int length)
{
while (length >0)
length--;
}
/*************************************************/
/*************************************************/
/************ IRQ Service Routine *************/
/*************************************************/
/*************************************************/
void My_IRQ_Function()
{
// Transmit
if(((I2C0MSTA & 0x4) == 0x4) && (i < 8)) // Master Transmit IRQ
{
i++; // Transmits numbers 1-8
I2C0MTX = i;
}
// Receive
if((I2C0MSTA & 0x8) == 0x8) // Master Recieve IRQ
{
dat[i] = I2C0MRX;
i++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -