void inline SendDataToTLC5940(struct Queue *argQueue, uint8_t argCount) { SPCR |= (1 << SPE); while(argCount) { if(!QUEUE_Empty(argQueue)) { cli(); SPDR = QUEUE_Remove(argQueue); while(!(SPSR & (1 << SPIF))); --argCount; sei(); } } SPCR &= ~(1 << SPE); }
int main() { uint8_t tmpCurrentMode, tmpPreviousMode; //init ddr SetHigh(DBP_GSCLK_DDR, DBP_GSCLK_PIN); SetHigh(DBP_BLANK_DDR, DBP_BLANK_PIN); SetHigh(DBP_XLAT_DDR, DBP_XLAT_PIN); SetHigh(DBP_VPRG_DDR, DBP_VPRG_PIN); SetHigh(DBP_DCPRG_DDR, DBP_DCPRG_PIN); SetHigh(DBP_SIN_DDR, DBP_SIN_PIN); SetHigh(DBP_SCLK_DDR, DBP_SCLK_PIN); //set initial values SetHigh(DBP_BLANK_PORT, DBP_BLANK_PIN); SetHigh(DBP_VPRG_PORT, DBP_VPRG_PIN); SetHigh(DBP_DCPRG_PORT, DBP_DCPRG_PIN); SetLow(DBP_GSCLK_PORT, DBP_GSCLK_PIN); SetLow(DBP_XLAT_PORT, DBP_XLAT_PIN); SetLow(DBP_SIN_PORT, DBP_SIN_PIN); SetLow(DBP_SCLK_PORT, DBP_SCLK_PIN); //init adc converter /* SetHigh(IRP_TRANSMITTER_DDR, IRP_TRANSMITTER_PIN); SetLow(IRP_RECEIVER_DDR, IRP_RECEIVER_PIN); ADMUX = (1 << REFS0); //voltage reference = AVCC, input channel = ADC0 ADCSRA = (1 << ADATE) | (1 << ADIE) | (1 << ADEN) | (1 << ADPS1); //f.osc / 4 ADCSRB = (1 << ADTS1) | (1 << ADTS0); //auto trigger source = timer0 compare match A */ //init GSCLK timer TCCR0A |= (1 << WGM01); TCCR0B |= (1 << CS02) | (1 << CS00); OCR0A = 0x03; //f.osc / 4096 TIMSK0 |= (1 << OCIE0A); //init spi SetHigh(SPI_SS_DDR, SPI_SS_PIN); SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1); //F_CPU / 16 ~ 365kHz //init i2c TWAR = (SLAVE_Address << 1); TWCR = (1 << TWEA) | (1 << TWEN) | (1 << TWIE); //init queue QUEUE_Init(&TWI_InputBuffer); //send dc data to driver through spi for(i = 0; i < 36; ++i) { SPDR = 0xff; while(!(SPSR & (1 << SPIF))); } SetHigh(DBP_XLAT_PORT, DBP_XLAT_PIN); SetLow(DBP_XLAT_PORT, DBP_XLAT_PIN); tmpPreviousMode = CMD_SetDC; SetLow(DBP_VPRG_PORT, DBP_VPRG_PIN); //send gs data to driver through spi for(i = 0; i < 72; ++i) { SPDR = 0xff; while(!(SPSR & (1 << SPIF))); } SPCR &= ~(1 << SPE); SetHigh(DBP_XLAT_PORT, DBP_XLAT_PIN); SetLow(DBP_XLAT_PORT, DBP_XLAT_PIN); SetLow(DBP_BLANK_PORT, DBP_BLANK_PIN); tmpPreviousMode = CMD_SetGS; //enable interrupts sei(); while(1) { if(!GLB_NeedXLAT && !QUEUE_Empty(&TWI_InputBuffer)) { tmpCurrentMode = QUEUE_Remove(&TWI_InputBuffer); switch(tmpCurrentMode) { case CMD_SetDC: { SetHigh(DBP_VPRG_PORT, DBP_VPRG_PIN); SendDataToTLC5940(&TWI_InputBuffer, 36); GLB_NeedXLAT = 1; tmpPreviousMode = tmpCurrentMode; break; } case CMD_SetGS: { if(tmpPreviousMode == CMD_SetGS) { SetHigh(DBP_SCLK_PORT, DBP_SCLK_PIN); SetLow(DBP_SCLK_PORT, DBP_SCLK_PIN); } SetLow(DBP_VPRG_PORT, DBP_VPRG_PIN); SendDataToTLC5940(&TWI_InputBuffer, 72); GLB_NeedXLAT = 1; tmpPreviousMode = tmpCurrentMode; break; } case CMD_ClearGS: { if(tmpPreviousMode == CMD_SetGS) { SetHigh(DBP_SCLK_PORT, DBP_SCLK_PIN); SetLow(DBP_SCLK_PORT, DBP_SCLK_PIN); } SetLow(DBP_VPRG_PORT, DBP_VPRG_PIN); SPCR |= (1 << SPE); cli(); for(i = 0; i < 72; ++i) { SPDR = 0x00; while(!(SPSR & (1 << SPIF))); } sei(); SPCR &= ~(1 << SPE); GLB_NeedXLAT = 1; tmpPreviousMode = CMD_SetGS; break; } } } } }
void MTC_ClearEvent(MTC_INFO *p) { QUEUE_Empty(p->pQueue); }