void SPI_Read(char adr, char*odb) { SPI_CS_Enable(); SPI_Transmit(0x80|adr); *odb=SPI_Transmit(0xFF); SPI_CS_Disable(); }
void SPI_Write(char adr, char msg) { SPI_CS_Enable(); SPI_Transmit(0x00|adr); SPI_Transmit(msg); SPI_CS_Disable(); }
uchar SPI_ReadEEPROM(unsigned int address) { SPI_Transmit(0xA0); SPI_Transmit(address >> 8); SPI_Transmit(address); return SPI_Transmit(0); }
uchar SPI_FlushPage(unsigned long address, uchar pollvalue) { SPI_Transmit(0x4C); SPI_Transmit(address >> 9); SPI_Transmit(address >> 1); SPI_Transmit(0); if (pollvalue == 0xFF) { clockWait(15); return 0; } else { /* polling flash */ uchar counter = 30; TIMER1 = 0; while (counter != 0) { if (SPI_ReadFlash(address) != 0xFF) { return 0; } if (TIMER1 > CLOCK_T_320us) { TIMER1 = 0; counter--; } } return 1; /* error */ } }
uchar SPI_ReadFlash(unsigned long address) { SPI_Transmit(0x20 | ((address & 1) << 3)); SPI_Transmit(address >> 9); SPI_Transmit(address >> 1); return SPI_Transmit(0); }
uchar SPI_EnterProgrammingMode() { uchar check; uchar count = 3; // Try to connect 3 times for (; count > 0; count--) { SPI_Transmit(0xAC); SPI_Transmit(0x53); check = SPI_Transmit(0); SPI_Transmit(0); if (check == 0x53) { return 0; // Success! We're connected an in programming mode! } // If the connect command failed, // the communication may be out of sync. // Pulse reset (as per the datasheet) and // try again. SET_RST(); // Device into normal operation clockWait(1); CLR_RST(); // Device back into reset mode clockWait(63); // Wait 20 ms before sending another program enable command } return 1; /* error: device dosn't answer */ }
uchar SPI_WriteEEPROM(unsigned int address, uchar data) { SPI_Transmit(0xC0); SPI_Transmit(address >> 8); SPI_Transmit(address); SPI_Transmit(data); clockWait(30); // Wait 9.6ms for write to complete return 0; }
uchar SPI_WriteFlash(unsigned long address, uchar data, uchar pollmode) { // We're not guarenteed that there was a chip erase, so we can't // skip programming of 0xFF... /* 0xFF is value after chip erase, so skip programming if (data == 0xFF) { return 0; } */ SPI_Transmit(0x40 | ((address & 1) << 3)); SPI_Transmit(address >> 9); SPI_Transmit(address >> 1); SPI_Transmit(data); if (pollmode == 0) return 0; if (data == 0x7F) { clockWait(15); // wait 4,8 ms return 0; } else { // Poll flash for (30 * 320 uS) uchar counter = 30; TIMER1 = 0; while (counter != 0) { if (SPI_ReadFlash(address) != 0x7F) { return 0; }; if (TIMER1 > CLOCK_T_320us) { TIMER1 = 0; counter--; } } return 1; /* error */ } }
void printRegisters(){ unsigned char input[5]; /* Loop through all the registers */ for (int i = 0; i < (int)(sizeof(addresses)/sizeof(addresses[0])); i++){ /* Select the device */ CSN_lo; /* Send the read command */ SPI_Transmit(addresses[i].addr); /* Read all the bytes in that register */ for(int ii = 0; ii < addresses[i].size; ii++){ input[ii] = SPI_Transmit(0xff); } /* Unselect the device */ CSN_hi; /* Print the register address, name, and content */ printf("0x%02hhX %-11s 0x",addresses[i].addr, addresses[i].name); for(char ii = 0; ii < addresses[i].size; ii++){ printf("%02hhX", input[0]); } putchar('\n'); } }
int main(void) { /* desabilita os pinos de RX e TX */ UCSR0B=0x00; /* Para uso do botão */ DDRB &= (~(1 << PB0)); PORTB |= (1 << PB0); LCD_inicia_4bits(); SPI_MasterInit(); while(1) { if(!tst_bit(PINB, PB0)) { SPI_HABILITA_SLAVE(); _delay_ms(20); SPI_Transmit('A'); SPI_DESABILITA_SLAVE(); while(!tst_bit(PINB, PB0)); } } }
int TickFct_Transmission(int state){ unsigned char dataIn = 0; unsigned char dataOut = 0; switch(state){ //Transitions case TN_SMStart: state = TN_Wait; break; case TN_Wait: //first bit is right. //second bit is left. //third bit is crouch. //fourth bit is jump. //fifth bit is reset. //sixth bit is win. if(rightFlag){ rightFlag = 0; dataOut = dataOut | 0x01; } if(leftFlag){ leftFlag = 0; dataOut = dataOut | 0x02; } if(crouchFlag){ crouchFlag = 0; dataOut = dataOut | 0x04; } if(jumpFlag){ jumpFlag = 0; dataOut = dataOut | 0x08; } if(resetFlag){ resetFlag = 0; scoreReset = 1; dataOut = dataOut | 0x10; } if(winFlag){ winFlag = 0; dataOut = dataOut | 0x20; } dataIn = SPI_Transmit(dataOut); if((dataIn&0x01)==0x01){ motorFlag = 1; } if((dataIn&0x02)==0x02){ scoreFlag = 1; motorFlag = 1; } if((dataIn&0x04)==0x04){ scoreReset = 1; } if((dataIn&0x08)==0x08){ scoreReset = 1; } break; default: state = TN_SMStart; break; } return state; }