📄 twi.c
字号:
/*********************************************************************************************
* File: twi.c
* Author: Embest z.j.zheng
* Desc: write the 0-f to the at24c02,then read out.
* History:
*********************************************************************************************/
/*------------------------------------------------------------------------------------------*/
/* include files */
/*------------------------------------------------------------------------------------------*/
#include "Board.h"
/*------------------------------------------------------------------------------------------*/
/* Global variable */
/*------------------------------------------------------------------------------------------*/
char data_out[16];
AT91PS_USART COM0;
/*------------------------------------------------------------------------------------------*/
/* constent definition */
/*------------------------------------------------------------------------------------------*/
#define USART_INTERRUPT_LEVEL 7
#define USART_BAUD_RATE 115200
/*********************************************************************************************
* name: time_dly
* func: display code
* para: dly --in, delay value
* ret: none
* modify:
* comment:
*********************************************************************************************/
void time_dly(int dly)
{
int i;
for(; dly>0; dly--)
for(i=0; i<500; i++);
}
/*********************************************************************************************
* name: init_twi
* func:
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
init_twi(void)
{
// Configure PIO controllers to periph mode, in open drain
AT91F_TWI_CfgPIO();
AT91F_PIO_CfgOpendrain(AT91C_BASE_PIOA,AT91C_PA3_TWD | AT91C_PA4_TWCK);
// AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA ) ;
AT91F_TWI_CfgPMC();
AT91F_TWI_DisableIt(AT91C_BASE_TWI,-1); // Disable TWI IT
AT91F_TWI_Configure(AT91C_BASE_TWI); // Configure TWI in master mode
}
/*********************************************************************************************
* name: Usart_init
* func: USART initialization
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void Usart_init ( void )
{
COM0= AT91C_BASE_US0;
// Define RXD and TXD as peripheral
AT91F_US0_CfgPIO() ;
// First, enable the clock of the PIOB
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<<AT91C_ID_US0 ) ;
// Usart Configure
AT91F_US_Configure (COM0, MCK,AT91C_US_ASYNC_MODE,USART_BAUD_RATE , 0);
// Enable usart
COM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
// ------ set time out US_RTOR
// Arm time out after 255 * 4 bits time
// for 115200 bit time = 1/115200 = 8.6 micro sec time out unuti= 4* 8.6 = 34 micro
//
COM0->US_RTOR = 0xFFFF;
}
/*********************************************************************************************
* name: AT91F_US_Printk
* func: This function is used to send a string through the US channel
* para:
* ret: none
* modify:
* comment:
*********************************************************************************************/
void AT91F_US_Printk( char *buffer) // pointer to a string ending by
{
while(*buffer != '\0') {
while (!AT91F_US_TxReady(COM0));
AT91F_US_PutChar(COM0, *buffer++);
}
}
/*********************************************************************************************
* name: twi_write
* func: write the data to the eeprom by the twi
* para: pointer. pointer to the array
* ret: none
* modify:
* comment:
*********************************************************************************************/
void twi_write( AT91PS_TWI pTWI,int Numn)
{
AT91C_BASE_TWI->TWI_CWGR = 0x40f0f;
pTWI->TWI_CR = AT91C_TWI_MSEN;
pTWI->TWI_MMR = (AT91C_TWI_IADRSZ_1_BYTE | (!AT91C_TWI_MREAD) | AT91C_TWI_DADR);
pTWI->TWI_IADR = 0x0 + Numn; //INTERNAL ADDRESS
pTWI->TWI_THR =Numn;
pTWI->TWI_CR = AT91C_TWI_START; //start
while(pTWI->TWI_SR&0x4==0 ){};
// must have a short delay when corresponding to the at24c02
time_dly(100);
pTWI->TWI_CR = AT91C_TWI_STOP ; //stop
while(pTWI->TWI_SR&0x1==0){};
}
/*********************************************************************************************
* name: twi_read
* func: read the data from the eeprom by the twi
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void twi_read(AT91PS_TWI pTWI,int n)
{
AT91C_BASE_TWI->TWI_CWGR = 0x40f0f;
AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN | AT91C_TWI_SVDIS;
pTWI->TWI_MMR = (AT91C_TWI_IADRSZ_1_BYTE | AT91C_TWI_MREAD | AT91C_TWI_DADR);
pTWI->TWI_IADR = 0x0+n; //INTERNAL ADDRESS
pTWI->TWI_CR |= AT91C_TWI_START; //start
while((pTWI->TWI_SR & 0x2) == 0 ){};
data_out[n] = pTWI->TWI_RHR;
// must have a short delay when corresponding to the at24c02
time_dly(100);
pTWI->TWI_CR |= AT91C_TWI_STOP; //stop
while((pTWI->TWI_SR & 0x1) == 0){};
}
/*********************************************************************************************
* name: Main
* func: main fun
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
Main( void )
{
int i;
init_twi();
Usart_init();
AT91F_US_Printk("\n\r--twi operation example--\n\r");
AT91F_US_Printk("Write char 0-f into AT24C02\n\r");
for(i = 0; i <= 15; i++)
{
twi_write(AT91C_BASE_TWI,i); //write the i number into the at24c02
twi_read(AT91C_BASE_TWI,i); //read the i number out from the at24c02
}
for(i = 0; i < 10; i++) //change into ASCII
data_out[i] += '0';
for(i = 10; i < 16; i++)
data_out[i] += 'a'-10;
data_out[16] = '\0';
AT91F_US_Printk("Read 16 bytes from AT24C02\n\r");
AT91F_US_Printk(data_out);
do
{
}while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -