📄 testi2c_1.c
字号:
//字节写,字节读(随机读)
//不成功,只显示"161"
#include "2440addr.h"
#include "2440lib.h"
void TestWriteByte(void);
void TestReadByte(void);
void xmain(void)
{
ChangeClockDivider(3,1);
ChangeMPllValue(127,2,1); //405MHZ
Isr_Init();
Uart_Init(0, 115200);
Uart_Select(0);
Uart_Printf("\nIIC test is beginning\n");
//设置功能管脚,GPE14,GPE15
//GPE14 -> SCL , GPE15 -> SDA
rGPECON = (rGPECON | (0x0f<<14) ) & ( ~(0x05<<14) );
rGPEUP |= (2<<14);//GPE14/GPE15 Pull-up disable
TestWriteByte();
TestReadByte();
while(1);
}
void TestWriteByte(void)
{
U8 i;
Uart_Printf("Test IIc Write\n");
//I2C总线控制寄存器//0xaf
rIICCON |= (1<<7)//I2C-bus acknowledge enable bit
&( ~(1<<6) )//0 = I2CCLK = fpclk/16
|(1<<5)//Tx/Rxinterrupt enable
&( ~(1<<4) )//Clear pending & Resume the operation
|(0x0f<<0);//Tx clock = IICCLK/(x+1)
//I2C状态寄存器
//Master Transmit mode
rIICSTAT |= (3<<6)//11:Master transmit mode
&( ~(1<<5) )//0 = Stop signal
|(1<<4);//Enable serial output
//?
rIICADD = 0x10;//2410 slave address = [7:1]//I don't know?
//IIC移位寄存器
//0xa0, write Device address
rIICDS |= (0x0a<<4)//1010:EEPROM
&( ~(7<<1) )//片选(Device address)
&( ~(1<<0) );//Write
//I2C状态寄存器
// START
rIICSTAT |= (1<<5);//1 = Start signal
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<4) );//Clear pending & Resume the operation
//write word address
rIICDS = 1;//at 0x00
for(i=0;i<10;i++);//for setup time until rising edge of IICSCL
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<4) );//Clear pending & Resume the operation
//write data
rIICDS = 1;//write '2'
for(i=0;i<10;i++);//for setup time until rising edge of IICSCL
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<4) );//Clear pending & Resume the operation
//I2C状态寄存器
// STOP
rIICSTAT &= ( ~(1<<5) );//0 = Stop signal
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<4) );//Clear pending & Resume the operation
Delay(1);//wait until stop condtion is in effect.
}
void TestReadByte(void)
{
U8 i;
static U8 buf=0;
Uart_Printf("Test IIc Read\n");
//I2C总线控制寄存器//0xaf
rIICCON |= (1<<7)//I2C-bus acknowledge enable bit
&( ~(1<<6) )//0 = I2CCLK = fpclk/16
|(1<<5)//Tx/Rxinterrupt enable
&( ~(1<<4) )//Clear pending & Resume the operation
|(0x0f<<0);//Tx clock = IICCLK/(x+1)
//I2C状态寄存器
//Master Transmit mode
rIICSTAT |= (3<<6)//11:Master transmit mode
&( ~(1<<5) )//0 = Stop signal
|(1<<4);//Enable serial output
//?
rIICADD = 0x10;//2410 slave address = [7:1]//I don't know?
//IIC移位寄存器
//0xa0, write Device address
rIICDS |= (0x0a<<4)//1010:EEPROM
&( ~(7<<1) )//片选(Device address)
|(1<<0);//1 = Read
//I2C状态寄存器
// START
rIICSTAT |= (1<<5);//1 = Start signal
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<4) );//Clear pending & Resume the operation
//write word address
rIICDS = 1;//at 0x00
for(i=0;i<10;i++);//for setup time until rising edge of IICSCL
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<4) );//Clear pending & Resume the operation
//IIC移位寄存器
//0xa0, write Device address
rIICDS |= (0x0a<<4)//1010:EEPROM
&( ~(7<<1) )//片选(Device address)
|(1<<0);//1 = Read
//I2C状态寄存器
// START
rIICSTAT |= (1<<5);//1 = Start signal
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<7) )//disable acknowledge bit
&( ~(1<<4) );//Clear pending & Resume the operation
//Read data
buf = rIICDS;
for(i=0;i<10;i++);//for setup time until rising edge of IICSCL
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON |= (1<<7)
&( ~(1<<4) );//Clear pending & Resume the operation
//I2C状态寄存器
// STOP
rIICSTAT &= ( ~(1<<5) );//0 = Stop signal
//I2C总线控制寄存器
//clear pending & Resume the opration
rIICCON &= ( ~(1<<4) );//Clear pending & Resume the operation
Delay(1);//wait until stop condtion is in effect.
Uart_Printf("%2d\n ",buf);//show out
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -