📄 main.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;
AT91F_DBGU_Printk("\n\n\r======================================");
AT91F_DBGU_Printk("\n\rFFT-RM9200 TWI EEPREOM Test\n\r");
AT91F_DBGU_Printk("\n\rPlease Noted JP7 JUMP\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");
write = 0xAA55;
// Write a byte and read it
AT91F_TWI_Write(AT91C_BASE_TWI, 0x0, (char *)&write, 2);
// 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);
// Wait 10 ms before data is written into EEPROM
AT91F_DBGU_Printk("Compare Write and Read\n\r");
if (read == write){
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 + -