int main(void) { char TxData[4] = {MCP23017_IODIRA, 0x00, MCP23017_GPIOA, 0xFF}; int ctr = 0; int ctr1 =0; WDTCTL = WDTPW + WDTHOLD; TIME430_CALIBRATE_CLOCK(); i2cSetupPins(); P1DIR = BIT0; P1OUT &= ~BIT0; i2cSetupTx(BITEXPANDER); i2cTransmit(&TxData[0],2); while(1) { i2cTransmit(&TxData[2],2); for(ctr=15; ctr>=0; ctr--) { TxData[3] = (char)ctr; for(ctr1=0; ctr1<100; ctr1++) { TIME430_DELAY_MS(10); } i2cTransmit(&TxData[2],2); } } return 0; }
/* Places 'data' into register 'reg' */ void MAX7300_SetRegister(uint8_t address, uint8_t reg, uint8_t data) { i2cAddress(address, TW_WRITE); i2cTransmit(reg); i2cTransmit(data); i2cStop(); }
void srf08SetGain(uint8_t gain) { if(gain > 31) gain = 31; i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x01); // select gain register i2cTransmit(gain); i2cStop(); }
void srf08SetRange(uint16_t range) { // range is calculated: ((Range Register x 43mm) + 43mm) uint8_t actualValue = (range /= 43) & 0xff; i2cStart(); i2cTransmit(currentAddress); // select current device i2cTransmit(0x02); // select range register i2cTransmit(actualValue); // write the range value i2cStop(); }
uint8_t srf08GetLight() { i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x01); // select light register i2cStart(); i2cTransmit(currentAddress + 1); uint8_t light = i2cReceive(false); i2cStop(); return light; }
uint8_t srf08GetVersion() { i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x00); // select software version register i2cStart(); // restart to indicate read operation i2cTransmit(currentAddress + 1); // select current device's read address uint8_t version = i2cReceive(false); // get the value at version register i2cStop(); return version; }
void srf08InitiateRanging(uint8_t units) { // valid unit values if(units != SRF08_UNIT_INCHES && units != SRF08_UNIT_CENTIMETERS && units != SRF08_UNIT_MICROSECONDS) return; i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x00); // select command register i2cTransmit(units); // write appropriate unit identifier to start ranging i2cStop(); }
/* serialEE_WriteBlock: * Send 'datasize' bytes from the array pointed to by 'data' * to the block_address and block of the EEPROM */ void serialEE_WriteBlock(uint8_t* data, uint8_t datasize, EE_AddressStruct* EE_AddressData) { uint8_t pageOverflow; uint8_t overflow; uint8_t newpage = 1; uint8_t j; uint8_t i; /* If writing will overflow a page, then write data until * the end of the page. * Stop the I2C, and then resume writing onto the next page. */ /* 18/04/2007, and keep writing for to the next page until all data is written */ overflow = ((EE_AddressData->EE_Address) % EE_PAGE_SIZE) + datasize; pageOverflow = overflow / EE_PAGE_SIZE; for( j = 0, i = 0; j <= pageOverflow; j++) { /* Address the next page and write the remaining bytes*/ i2cAddress( (EE_I2C_ADDRESS) | (EE_BLOCKSELECT(EE_AddressData->EE_Block)) , TW_WRITE ); #if EE_WORD_ADDRESS == 1 i2cTransmit( (uint16_t)((EE_AddressData->EE_Address)+ i) >> 8); #endif i2cTransmit(((EE_AddressData->EE_Address)+ i)& 0xFF); for( ; ((((i + ((EE_AddressData->EE_Address) % EE_PAGE_SIZE)) % (EE_PAGE_SIZE)) != 0) || newpage == 1) && (i < datasize); i++) { newpage = 0; i2cTransmit(data[i]); } i2cStop(); newpage = 1; _delay_ms(5); } }
uint16_t srf08GetDistance(uint8_t echo) { // valid range is [0, 16] if(echo > 16) echo = 16; i2cStart(); i2cTransmit(currentAddress); i2cTransmit(2 + echo * 2); // select the correct distance register i2cStart(); i2cTransmit(currentAddress + 1); uint16_t distance = i2cReceive(true) << 8; // get the high byte distance |= i2cReceive(false); // get the low byte i2cStop(); return distance; }
/* Returns the state of the data in 'reg'*/ uint8_t MAX7300_ReadRegister(uint8_t address, uint8_t reg) { uint8_t readByte; i2cAddress(address, TW_WRITE); i2cTransmit(reg); i2cAddress(address, TW_READ); readByte = i2cRead(ACK_BIT); return readByte; }
/* getTemp: * Obtain the temperature from the DS1624 IC * and return the pointer to where it is stored */ int8_t* getTemp(void) { static int8_t temperature[2]; i2cAddress(DS1624_ADDRESS, TW_WRITE); /* Intiate Read Temperature */ i2cTransmit(0xAA); /* Receive the bytes*/ i2cAddress(DS1624_ADDRESS, TW_READ); i2cReadBlock(temperature, 2); i2cStop(); return temperature; }
/* serialEE_ReadBlock: * Read 'datasize' bytes to the array pointed to by 'data' * from the block_address and block of the EEPROM */ void serialEE_ReadBlock(uint8_t* data, uint8_t datasize, EE_AddressStruct* EE_AddressData) { /* Begin reading from the given address and block */ i2cAddress( (EE_I2C_ADDRESS) | (EE_BLOCKSELECT(EE_AddressData->EE_Block)) , TW_WRITE ); #if EE_WORD_ADDRESS == 1 i2cTransmit((uint16_t)(EE_AddressData->EE_Address) >> 8); #endif i2cTransmit( (EE_AddressData->EE_Address) & 0xFF ); /* Begin reading */ i2cAddress( (EE_I2C_ADDRESS) | (EE_BLOCKSELECT(EE_AddressData->EE_Block)) , TW_READ ); i2cReadBlock(data , datasize); i2cStop(); }
void srf08ChangeAddress(uint8_t newAddress) { if(newAddress < 0xe0 || newAddress > 0xfe) return; if(newAddress % 2 != 0) return; // address can be changed by writing following values // in sequence into command register: 0xa0 0xaa 0xa5 address i2cStart(); i2cTransmit(currentAddress); // select current device i2cTransmit(0x00); // // select command register i2cTransmit(0xa0); i2cStop(); i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x00); i2cTransmit(0xaa); i2cStop(); i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x00); i2cTransmit(0xa5); i2cStop(); i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x00); i2cTransmit(newAddress); i2cStop(); currentAddress = newAddress; }