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

📄 main.c

📁 at91rm9200控制at2410这种基于i2c的eeprom器件
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : main.c
//* Object              : Basic twi example. Write a byte into EEPROM and read it
//* Creation            : NL   25/11/02
//* 1.1 31/03/03 JPP	: Add DBGU message 
//*----------------------------------------------------------------------------
#include "main.h"
#include "twi.h"

//*=========================================================
//*		INIT
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_SetTwiClock
//* \brief Initialization
//*----------------------------------------------------------------------------
void AT91F_SetTwiClock(const AT91PS_TWI pTwi)
{
	int sclock;

	/* Here, CKDIV = 1 and CHDIV=CLDIV  ==> CLDIV = CHDIV = 1/4*((Fmclk/FTWI) -6)*/

	sclock = (10*AT91C_MASTER_CLOCK /AT91C_TWI_CLOCK);
	if (sclock % 10 >= 5)
		sclock = (sclock /10) - 5;
	else
		sclock = (sclock /10)- 6;
	sclock = (sclock + (4 - sclock %4)) >> 2;	// div 4

    pTwi->TWI_CWGR	= 0x00010000 | sclock | (sclock << 8);
}


int main()
{
	int loop;
	
	//unsigned short write, read;
	char write[16], read[16];
	
	AT91F_DBGU_Printk("\n\n\r======================================");
	AT91F_DBGU_Printk("\n\rAT91RM9200 TWI EEPREOM Test\n\r");
	AT91F_DBGU_Printk("======================================\n\r");
	
	// Configure TWI PIOs
	AT91F_TWI_CfgPIO ();
	AT91F_PIO_CfgOpendrain(AT91C_BASE_PIOA, (unsigned int) AT91C_PA25_TWD);

	// Configure PMC by enabling TWI clock
	AT91F_TWI_CfgPMC ();
	
	// Configure TWI in master mode
	AT91F_TWI_Configure (AT91C_BASE_TWI);
		
	// Set TWI Clock Waveform Generator Register	
	AT91F_SetTwiClock(AT91C_BASE_TWI);

	AT91F_DBGU_Printk("TWI ready\n\r");
	for(loop=0;loop<16;loop++)
	{write[loop] = loop+5;
	 read[loop] = loop;
	 }
	// Write a byte and read it 
	//AT91F_TWI_Write(AT91C_BASE_TWI, 0x0, (char *)&write, 2);
	AT91F_TWI_Write(AT91C_BASE_TWI, 0x00,write, 16);
	
	// Wait 10 ms before data is written into EEPROM
	AT91F_DBGU_Printk("Wait at least 10 ms before value is written into EEPROM\n\r");
	for (loop=0; loop<1000000; loop++);
	
	//AT91F_TWI_Read(AT91C_BASE_TWI, 0x0, (char *)&read, 2);
	AT91F_TWI_Read(AT91C_BASE_TWI, 0x00,read, 16);

	// Wait 10 ms before data is written into EEPROM
	AT91F_DBGU_Printk("Compare Write and Read\n\r");
	if ((read[0] == write[0])&(read[1] == write[1])&(read[2] == write[2])&(read[3] == write[3])
			&(read[4] == write[4])&(read[5] == write[5])&(read[6] == write[6])&(read[7] == write[7])
			&(read[8] == write[8])&(read[9] == write[9])&(read[10] == write[10])&(read[11] == write[11])
			&(read[12] == write[12])&(read[13] == write[13])&(read[14] == write[14]))
	{
		AT91F_DBGU_Printk("Test TWI OK\n\r");
	} else {
		AT91F_DBGU_Printk("Error during test\n\r");
	}

	while (1);	// Set sequential part here
}
//* EOF

⌨️ 快捷键说明

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