?? 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 IRQ_Handler() __irq;
#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
IRQEN = 0x400; // I2C0 Master Interupt
// Transmit
I2C0ADR = 0xA0; // set i2c address (LSB = 0, Master Write)
I2C0MTX = 0x55; // send i2c byte address
delay(4000);
// 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 IRQ_Handler() __irq
{
// Transmit
if(((I2C0MSTA & 0x4) == 0x4) && (i < 8)) // Master Transmit IRQ
{
i++; // Transmits numbers 1-8
I2C0MTX = i;
}
// Recieve
if((I2C0MSTA & 0x8) == 0x8) // Master Recieve IRQ
{
dat[i] = I2C0MRX;
i++;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -