/*********************************************************************************************
* File name: iic.c
* Author:
* Descript: iic source code.
* History:
*
*********************************************************************************************/
/*------------------------------------------------------------------------------------------*/
/* include files */
/*------------------------------------------------------------------------------------------*/
#include "2410lib.h"
#include "iic_test.h"
/*------------------------------------------------------------------------------------------*/
/* global variables */
/*------------------------------------------------------------------------------------------*/
int f_nGetACK;
/*********************************************************************************************
* name: iic_test
* func: test iic
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void iic_test(void)
{
UINT8T szData[16];
// UINT8T szBuf[40];
unsigned int i;
uart_printf("\n IIC Protocol Test Example, using AT24C04...\n");
uart_printf(" Write char 0-f into AT24C04\n");
f_nGetACK = 0;
// Enable interrupt
rINTMOD = 0x0;
ClearPending(BIT_IIC);
rINTMSK &= ~BIT_IIC;
pISR_IIC = (unsigned)iic_int_24c04;
// Initialize iic
rIICADD = 0x10; // S3C2410X slave address
rIICCON = 0xaf; // Enable ACK, interrupt, SET IICCLK=MCLK/16
rIICSTAT = 0x10; // Enable TX/RX
// Write 0 - 16 to 24C04
for(i=0; i<16; i++)
{
iic_write_24c040(0xa0, i, i);
uart_printf(" %d",i);
delay(10);
}
// Clear array
for(i=0; i<16; i++)
szData[i]=0;
// Read 16 byte from 24C04
for(i=0; i<16; i++)
iic_read_24c040(0xa0, i, &(szData[i]));
// Printf read data
uart_printf(" \nRead 16 bytes from AT24C04\n");
for(i=0; i<16; i++)
{/*
#ifdef BOARDTEST
sprintf(szBuf," %2x ",szData[i]);
if(i<8)
Lcd_DspAscII6x8(194+10*i,170,0xec,szBuf);
else
Lcd_DspAscII6x8(194+10*(i-8),180,0xec,szBuf);
#endif
*/
uart_printf(" %2x ", szData[i]);
}
rINTMSK |= BIT_IIC;
uart_printf("\n end.\n");
}
/*********************************************************************************************
* name: iic_write_24c040
* func: write data to 24C040
* para: unSlaveAddr --- input, chip slave address
* unAddr --- input, data address
* ucData --- input, data value
* ret: none
* modify:
* comment:
*********************************************************************************************/
void iic_write_24c040(UINT32T unSlaveAddr,UINT32T unAddr,UINT8T ucData)
{
f_nGetACK = 0;
// Send control byte
rIICDS = unSlaveAddr; // 0xa0
rIICSTAT = 0xf0; // Master Tx,Start
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
//Send address
rIICDS = unAddr;
rIICCON = 0xaf; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
// Send data
rIICDS = ucData;
rIICCON = 0xaf; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
// End send
rIICSTAT = 0xd0; // Stop Master Tx condition
rIICCON = 0xaf; // Resumes IIC operation.
delay(5); // Wait until stop condtion is in effect.
}
/*********************************************************************************************
* name: iic_read_24c040
* func: read data from 24C040
* para: unSlaveAddr --- input, chip slave address
* unAddr --- input, data address
* pData --- output, data pointer
* ret: none
* modify:
* comment:
*********************************************************************************************/
void iic_read_24c040(UINT32T unSlaveAddr,UINT32T unAddr,UINT8T *pData)
{
char cRecvByte;
f_nGetACK = 0;
//Send control byte
rIICDS = unSlaveAddr; // 0xa0
rIICSTAT = 0xf0; // Master Tx,Start
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
// Send address
rIICDS = unAddr;
rIICCON = 0xaf; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
//Send control byte
rIICDS = unSlaveAddr; // 0xa0
rIICSTAT = 0xb0; // Master Rx,Start
rIICCON = 0xaf; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
//Get data
cRecvByte = rIICDS;
rIICCON = 0x2f;
delay(1);
// Get data
cRecvByte = rIICDS;
// End receive
rIICSTAT = 0x90; // Stop Master Rx condition
rIICCON = 0xaf; // Resumes IIC operation.
delay(5); // Wait until stop condtion is in effect.
*pData = cRecvByte;
}
/*********************************************************************************************
* name: iic_int_24c04()
* func: IIC interrupt handler
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void __irq iic_int_24c04(void)
{
ClearPending(BIT_IIC);
f_nGetACK = 1;
}
阅读(580) | 评论(0) | 转发(0) |