[an error occurred while processing this directive]
|
TWI interrupt code
#pragma vector=TWI_vect
__interrupt void TWI_ISR( void )
{
PORTB |= SET_TOUCH;SMCR &= 0xFE; /* abort sleep if the part was suppose to sleep */
i2c_com = 1; /* flag to indicate that there is an ongoing I2C com */switch (TWSR)
{
// Read cycle
case TWI_STX_ADR_ACK: /* Own SLA+R has been received; ACK has been returned */
case TWI_STX_DATA_ACK: /* Data byte in TWDR has been transmitted; NOT ACK has been received */
{
/* place action on data out *//* return the data placed into the RAM starting at address 0x200 */
TWDR = *((unsigned char *)(0x0200 | Rx));
/* incremente pointer for next read */
Rx++;
} break;case TWI_STX_DATA_NACK: /* Data byte in TWDR has been transmitted; NOT ACK has been received */
case TWI_SRX_STOP_RESTART:
i2c_com = 0; /* clear flag; current I2C finished */
break;/* Own SLA+W has been received ACK has been returned */
// case TWI_SRX_ADR_ACK: /* Code 0x60 */
// break;/* Previously addressed with own SLA+W; data has been received; ACK has been returned */
case TWI_SRX_ADR_DATA_ACK: /* Code 0x80 */
{ /* a new data was just received */
Rx = TWDR;/* place action on data in */
} break; /* end case TWI_SRX_ADR_DATA_ACK */
} /* end switch (TWSR) */
if (TWSR == TWI_BUS_ERROR)
{
TWCR = (1<(1< (1< (0< }
else if (TWSR != TWI_NO_STATE)
{
TWCR = (1<(1< (1< (0< } PORTB &= CLEAR_TOUCH;
}
Sleep code/*============================================================================
void doSlowMode(void)
------------------------------------------------------------------------------
Purpose: Send the device in sleep for a WDT period.Input : None
Output : None
Notes : -
============================================================================*/
#pragma optimize = 2
void doSlowMode(void)
{ uint8_t tempSleep;gvRunLog -= 7;
/* prepare to sleep - Standby */
SMCR = 0x04;/* Set the interrupt on SS low */
PCICR |= 0x01; /* enable interrupt on PCint7..0 (SS pin located on PCINT2) */
PCMSK0 |= 0x04; /* mask interrupt on pin Pb2 -> */if(!(PINB & 0x04))
{ SMCR &= 0xFE;
}
if(i2c_com)
{ SMCR &= 0xFE;
}if(sleepTimeIndex > 6) /* then sleep for infinite time */
{
/*
* The part will go to sleep for an indefinite period
* it will wake-up on com only.
*//* turn off WDT */
__disable_interrupt();
__watchdog_reset();
MCUSR &= ~(1 << WDRF);
WDTCSR |= (1<WDTCSR = 0x00;
__enable_interrupt();
}
else /* else sleep for at some time (16ms to 1sec) */
{ tempSleep = SLEEP_TIME[sleepTimeIndex];/*
* The part will go to sleep for a WDT period.
* On wake-up it will acquire a new set of data
*/
__disable_interrupt();
/* set the wdt in interrupt mode */
__watchdog_reset();
MCUSR &= ~(1 << WDRF);
WDTCSR |= (1<WDTCSR = 0x00; /* set the wdt prescaler */
__watchdog_reset();
WDTCSR |= (1<WDTCSR = tempSleep;
__enable_interrupt();
}
__sleep();
/* Will return here after the WDT interrupt is executed - or wake-up *//* clear the sleep enable flag */
SMCR &= 0xFE;/* clear the pin change interrupt mask */
PCMSK0 &= 0xFB;/* turn-on the whatchdog asap; the WDT fuse is off */
__disable_interrupt();/* Restore the wdt prescaler to 32ms */
EnableWDT(0x09);__enable_interrupt();
flagSleepOccured = 1; /* set the sleep flag in status */gvRunLog += 7;
}
E-mail: info@telesys.ru