⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 st ARM board I2C test code
💻 C
字号:
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 16/05/2003
* Description        : example using the I2C library for transmission of buffers
*                      between the two I2C modules
********************************************************************************
* History:
*  16/05/03 : Created.
*******************************************************************************/
#include "71x_lib.h"
#include "LED.h"

void DelayTWR(void)
{
	int i;
	for(i = 0; i < 0x800; i++);
}

void WriteByte(int addr, u8 data)
{
	//Enable Start generation
	I2C_STARTGenerate(I2C0, ENABLE);
	while(!(I2C0->SR1 & I2C_SB));

	//Send the slave address
	I2C0->DR = 0xA0 | addr >> 7 & 0x0E;	// Tx
	while(!(I2C0->SR2 & 0x20));			// ENDAD
	I2C0->CR |= 0x20;					// Set PE

	//Send Data to the slave
	while(!(I2C0->SR1 & I2C_BTF));
	I2C0->DR = addr & 255;
	while(!(I2C0->SR1 & I2C_BTF));
	I2C0->DR = data;
	while(!(I2C0->SR1 & I2C_BTF));

	//Stop the communication
	I2C_STOPGenerate(I2C0, ENABLE);
	while(I2C0->CR & I2C_STOP_Mask);
}

u8 ReadByte(int addr)
{
	u8 c;

	//Enable Start generation
	I2C_STARTGenerate(I2C0, ENABLE);
	while(!(I2C0->SR1 & I2C_SB));

	//Send the slave address
	I2C0->DR = 0xA0 | addr >> 7 & 0x0E;	// Tx
	while(!(I2C0->SR2 & 0x20));			// ENDAD
	I2C0->CR |= 0x20;					// Set PE

	//Send Data to the slave
	while(!(I2C0->SR1 & I2C_BTF));
	I2C0->DR = addr & 255;
	while(!(I2C0->SR1 & I2C_BTF));

	//Stop the communication
	I2C_STOPGenerate(I2C0, ENABLE);
	while(I2C0->CR & I2C_STOP_Mask);

	//Disable I2C acknowledge feature
	I2C_AcknowledgeConfig(I2C0, DISABLE);

	//Enable Start generation
	I2C_STARTGenerate(I2C0, ENABLE);
	while(!(I2C0->SR1 & I2C_SB));

	//Send the slave address
	I2C0->DR = 0xA1 | addr >> 7 & 0x0E;	// Rx
	while(!(I2C0->SR2 & 0x20));			// ENDAD
	I2C0->CR |= 0x20;					// Set PE

	//Read Data from the slave & stop the communication
	while(!(I2C0->SR1 & I2C_BTF));
	I2C_STOPGenerate(I2C0, ENABLE);
	c = I2C0->DR;
	while(I2C0->CR & I2C_STOP_Mask);

	return c;
}

void __main(void)
{
#ifdef DEBUG
	debug();
#endif

	LED_Init();

	//Configure SDA & SCL pin of I2C0 to alternate function Open Drain
	GPIO_Config(GPIO1, 0x6000, GPIO_AF_OD);

	//Pull down P1.15 to ground ( EEPROM's A2 = 0 )
	GPIO_Config(GPIO1, 0x8000, GPIO_OUT_PP);
	GPIO_WordWrite(GPIO1, GPIO_WordRead(GPIO1) & ~0x8000);

	//Configure I2C0 module
	I2C_Init(I2C0);
	I2C_FCLKConfig(I2C0);
	I2C_OnOffConfig(I2C0, ENABLE);
	I2C_SpeedConfig(I2C0, 100000);

	//Test R/W
	WriteByte(0, 0x55);
	DelayTWR();					// Insert wait cycle for TWR
	if(ReadByte(0) == 0x55)
		LED_Set(0, LED_ON);
	WriteByte(0, 0xAA);
	DelayTWR();
	if(ReadByte(0) == 0xAA)
		LED_Set(1, LED_ON);
	WriteByte(0, 0xFF);
	DelayTWR();
	if(ReadByte(0) == 0xFF)
		LED_Set(2, LED_ON);

	//Disable I2C0
	I2C_OnOffConfig(I2C0, DISABLE);

	while(1);
}

/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -