www.pudn.com > 9200-DS1307.rar > 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 int tick;	 
	//unsigned short write, read; 
	char write[8], read[7]; 
	 
	uprintf("\n\n\r");	 
	uprintf("======================================================\n\r"); 
	uprintf("==           欢迎使用英贝德公司的产品               ==\n\r"); 
	uprintf("==  下面开始AT91RM9200板实时时钟DS1307的测试(I2C) ==\n\r");	 
	uprintf("==       Begin to Start DS1307 test,OK? (Y/N)       ==\n\r");	 
	uprintf("======================================================\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("DS1307 ready\n\r");	 
	for(loop=0;loop<7;loop++) 
	{ 
	 read[loop] = 0; 
	} 
	 
	AT91F_TWI_Read(AT91C_BASE_TWI, 0x00,read, 8); 
	if((read[6]!=0x05)){		 
	//set time 
	write[0] = 0x05;//second 
	write[1] = 0x53;//minutes 
	write[2] = 0x8;//hours 
	write[3] = 0x2;//day 
	write[4] = 0x6;//date 
	write[5] = 0x09;//month 
	write[6] = 0x05;//year 
	 
	write[7] = 0x80;//contrl	 
	// Write a byte and read it  
	//AT91F_TWI_Write(AT91C_BASE_TWI, 0x0, (char *)&write, 2); 
	AT91F_TWI_Write(AT91C_BASE_TWI, 0x00,write, 7); 
	 
	// Wait 10 ms before data is written into DS1307 
	uprintf("Wait at least before value is written into DS1307\n\r"); 
	for (loop=0; loop<10000; loop++);	 
	} 
 
	while (1)	// Set sequential part here 
	{ 
			for(loop=0;loop<7;loop++) 
			{ 
	 			read[loop] = 0; 
	 		} 
	 		 
	 		 
		tick = *(AT91C_ST_CRTR);		 
		while (tick == *(AT91C_ST_CRTR)); //每秒显示一次		 
	 		 
	 		 
		AT91F_TWI_Read(AT91C_BASE_TWI, 0x00,read, 8); 
 
		uprintf("\n\r200%x年  %x月 %x日  星期%x  %x时  %x分 %x秒\n\r", 
					read[6],read[5],read[4],read[3],read[2],read[1],read[0]); 
	} 
} 
//* EOF