Code: Select all
#include "AT91SAM7S256.h" // Definitions of the ARM chip and on-chip peripherals.
#include "SAM7-P256.h" // Definitions of peripherals on the Olimex dev board.
#include "Board.h"
#define SPEED_I2C 100000 // operating speed TWI bus in Hz.
#define CLKDIV (MCK/(2*SPEED_I2C)-3)
void i2c_write_byte(char value)
{
int Status;
*AT91C_TWI_MMR &= ~AT91C_TWI_MREAD; //Set the recording mode
*AT91C_TWI_THR = value; // Writes data to the transmit register, start transmission
*AT91C_TWI_CR = AT91C_TWI_START;
Status = *AT91C_TWI_SR; // Read the status register
while (!(Status & AT91C_TWI_TXCOMP))
Status = *AT91C_TWI_SR; // wait for the completion of transfer
*AT91C_TWI_CR = AT91C_TWI_STOP;
}
void AT91F_TWI_CfgPIO (void)
{
*AT91C_PIOA_ASR = 0x18; //assign to PA3, PA4 channel A
*AT91C_PIOA_BSR = 0x0;
*AT91C_PIOA_PDR = (0x18 | 0x0); //controlled by peripheral device
}
void AT91F_PMC_EnablePeriphClock (
AT91PS_PMC pPMC, // \arg pointer to PMC controller
unsigned int periphIds) // \arg IDs of peripherals
{
pPMC->PMC_PCER = periphIds;
}
void AT91F_TWI_CfgPMC (void)
{
AT91F_PMC_EnablePeriphClock(
AT91C_BASE_PMC, // PIO controller base address
((unsigned int) 1 << AT91C_ID_TWI));
}
void AT91F_TWI_Configure ( AT91PS_TWI pTWI ) // \arg pointer to a TWI controller
{
//* Disable interrupts
pTWI->TWI_IDR = (unsigned int) -1;
//* Reset peripheral
pTWI->TWI_CR = AT91C_TWI_SWRST;
//* Set Master mode
pTWI->TWI_CR = AT91C_TWI_MSEN;
}
int main()
{
char i;
AT91F_TWI_CfgPIO(); // Connecting TWI to outputs PA3 and PA4
*AT91C_PIOA_PPUDR = (AT91C_PA4_TWCK | AT91C_PA3_TWD); // Turning off the internal pull-up resistors
*AT91C_PIOA_MDER = (AT91C_PA4_TWCK | AT91C_PA3_TWD); // Turn control mode - open collector
AT91F_TWI_CfgPMC(); // allow timing TWI
AT91F_TWI_Configure(AT91C_BASE_TWI); // Disable all interrupts, do TWI reset and set in the master mode
*AT91C_TWI_CWGR = ((0x00 << 16) | (CLKDIV << 8) | (CLKDIV));
*AT91C_TWI_MMR = 0x0;//0 Set the address of the Slave; recording mode; not address internal registers use slave device
//This part is taken from the official Olimex site, there is an example for arduino
// It is assumed that these lines initialize LCD and light up all segments
i2c_write_byte(0x70 | 0x00);
i2c_write_byte(0xC8); // mode register
i2c_write_byte(0xF0); // blink register
i2c_write_byte(0xE0); // device select register
i2c_write_byte(0x00); // pointer register
// light up all the segments, initialize the local display buffer as well
for(i = 0; i < 20; i++)
{
i2c_write_byte(0xFF);
}
while(1);
return 0;
}