コード例 #1
0
ファイル: main.c プロジェクト: NodeUSB/i2c-moisture-sensor
static inline loopSensorMode() {
    while(1) {
        if(twiDataInReceiveBuffer()) {
            uint8_t usiRx = twiReceiveByte();

            if(TWI_GET_CAPACITANCE == usiRx) {
                twiTransmitByte(currCapacitance >> 8);
                twiTransmitByte(currCapacitance &0x00FF);
                currCapacitance = getCapacitance();
            } else if(TWI_SET_ADDRESS == usiRx) {
                uint8_t newAddress = twiReceiveByte();
                if(twiIsValidAddress(newAddress)) {
                    eeprom_write_byte((uint8_t*)0x01, newAddress);
                }

            } else if(TWI_GET_ADDRESS == usiRx) {
                uint8_t newAddress = eeprom_read_byte((uint8_t*) 0x01);
                twiTransmitByte(newAddress);

            } else if(TWI_MEASURE_LIGHT == usiRx) {
                if(!lightMeasurementInProgress()) {
                    getLight();
                }

            } else if(TWI_GET_LIGHT == usiRx) {
                GIMSK &= ~_BV(PCIE0);//disable pin change interrupts
                TCCR1B = 0;          //stop timer
                
                twiTransmitByte(lightCounter >> 8);
                twiTransmitByte(lightCounter & 0x00FF);

                GIMSK |= _BV(PCIE0); 
                TCCR1B = _BV(CS10) | _BV(CS11);                 //start timer1 with prescaler clk/64

            } else if(TWI_GET_TEMPERATURE == usiRx) {
コード例 #2
0
int main(void)
{
	uint8_t data;

	LED_DDR |= (1<<LED_P);			// Set LED pin as an output.
	
	twiSlaveInit( SLAVE_ADRS );		// Initialize TWI hardware for Slave operation.
	
	sei();							// Enable interrupts.
	
	twiSlaveEnable();				// Enable the TWI interface to receive data.
	
    while(1)
    {
		if( twiDataInReceiveBuffer() )
		{
			data = twiReceiveByte();

			// If the data is 00, then turn OFF the LED. Turn it ON for any non-zero value.			
			if( data == 0)
			{
				LED_PORT &= ~(1<<LED_P);		// Turn LED OFF.
			}
			else
			{
				LED_PORT |= (1<<LED_P);			// Turn LED ON.
			}
		}
    }
}
コード例 #3
0
ファイル: access.c プロジェクト: CmdrZin/chips_avr_examples
/* Service incoming I2C message. */
void access_all()
{
	uint8_t temp;
	uint8_t index;
	bool search;
	uint8_t mod;
	uint8_t cmd;
	bool scan;
	
	void (*func)() = 0;

	/* Check for I2C message. */
	if(twiDataInReceiveBuffer())
	{
		accMsgBuff[accMsgIndex] = twiReceiveByte();
		if( ++accMsgIndex >= ACCESS_MSG_BUFF_SIZE )
		{
			accMsgIndex = 0;					// ERROR..too many bytes.
			accMsgSize = 0;
			accFuncTable = 0;
			while( twiDataInReceiveBuffer() )
			{
				// Flush input buffer.
				(void)twiReceiveByte();
			}
		}

		// Valid message being received? ~LEN.7:4 == LEN.3:0
		if ( accMsgIndex == 1)
		{
			accMsgSize = getMsgData(0);
			// NOTE: Compiler was treating as 16bit using r25:24
			temp = ~accMsgSize;
			temp >>= 4;
			temp &= 0x0F;
			accMsgSize &= 0x0F;
			if( temp != accMsgSize )
			{
				accMsgIndex = 0;		// ERROR..size check failed.
				accMsgSize = 0;
			}
			else
			{
				accMsgSize += 3;		// ALL messages are a minimum of three bytes.
			}
		}

		// Message check. ALL messages are at least three bytes long. LEN MOD CMD
		if ( accMsgIndex == 3)
		{
			// Three bytes received. Should be a LEN MOD CMD. Check for a MOD match.
			search = true;
			index = 0;
			while(search)
			{
				mod = flash_get_mod_access_id(index);

				if ( mod == 0)
				{
					// End of list. No match.
					search = false;
					accMsgIndex = 0;
					accMsgSize = 0;
					accFuncTable = 0;
				}
				else if ( mod == accMsgBuff[1] )
				{
					search = false;
					// Get the function table for this module.
					accFuncTable = flash_get_mod_function_table(index);
				}
				++index;
			}
		} // end if == 3

		// Process command now?
		if ( (accMsgIndex == accMsgSize) && (accMsgIndex != 0) && (accFuncTable != 0) )
		{
			scan = true;
			index = 0;
			while(scan)
			{
				cmd = flash_get_access_cmd(index, accFuncTable);
				func = (void (*)())flash_get_access_func(index, accFuncTable);
				
				if ( accMsgBuff[2] == cmd )
				{
					func();
					accMsgIndex = 0;
					accMsgSize = 0;
					accFuncTable = 0;
					scan = false;
				}
				++index;
			}
		}
	} // end if recv data