void rfm22_write_reg(unsigned char i, unsigned char x) { rfm22_csn_low(); rfm22_csn_wait(); WriteSPI(i | 0x80); WriteSPI(x); rfm22_csn_high(); }
/****************************************************************************** * R_EEPROM subroutine * * This routine will read the last set of data from EEPROM and save that data * into holding registers in RAM for tranmission across USART. * * Inputs: addr = EEPROM memory address for pulling data from * mem = RAM location to write data to * Outputs: None ******************************************************************************/ void R_EEPROM(unsigned short long addr, unsigned char * mem) { unsigned char temp; unsigned char i; unsigned char high; unsigned char med; unsigned char low; high = addr>>16; //High byte of EEPROM memory location med = addr>>8; //Medium byte of EEPROM memory location low = addr; //Low byte of EEPROM memory location EEPROMEnable(); //Pull CS low to write WriteSPI(ReadEEPROM); //Set READ on EEPROM WriteSPI(high); //Send high address to EEPROM WriteSPI(med); //Send medium address to EEPROM WriteSPI(low); //Send low address to EEPROM for (i = 0; i < 30; i++) { temp = WriteSPI(0x00); //Save the data to holding location in RAM mem[i] = temp; } EEPROMDisable(); //Push CS high to deselect EEPROM Delay1KTCYx(1); //Wait for read to finish just in case }
void ADXL345_multiByteWrite(unsigned char startAddress, char* buffer, unsigned char size) { #if I2C unsigned char i; StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(startAddress); for (i = 0; i < size; i++) { WriteI2C(buffer[i]); } StopI2C(); #elif SPI unsigned char tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); unsigned char i; SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns WriteSPI(tx); //Send starting write address. for (i = 0; i < size; i++) { WriteSPI(buffer[i]); } SPI_CS_PIN = 1; //CS pin high, ie disable chip #endif }
void CAN2510SPIBitMod( unsigned char addr, unsigned char mask, unsigned char value ) { while( WriteSPI(CAN2510_CMD_BITMOD) ); while( WriteSPI(addr) ); while( WriteSPI(mask) ); while( WriteSPI(value) ); }
void driver_write(BYTE addr, BYTE data) { // first select the driver CS = 0; // send out address byte then the data byte WriteSPI(addr); WriteSPI(data); // deselect the device CS = 1; return; }
//zapisanie komendy do sterownika void WriteCmd(unsigned char cmd) { ClrBit(DC);//rejestr komend ClrBit(SCE); WriteSPI(cmd); SetBit(SCE); }
//zapisanie danej do sterownika void WriteData(unsigned char data) { SetBit(DC);//rejestr danych ClrBit(SCE); WriteSPI(data); SetBit(SCE); }
void pcd8544_update() { uint16_t i; uint8_t x, y; // Reset address pointer pcd8544_resetxy(); // Device select LATCbits.LC1 = 0; // Setting data mode -> D/C = 1 LATCbits.LC2 = 1; for(y=0;y<6;y++){ for(x=0;x<84;x++){ WriteSPI(pcd8544_buffer[(y * 84) + x]); //ReadSPI(); } } // Device deselect LATCbits.LC1 = 1; }
void main(void) { unsigned char data = 0; signed char err0 = 0, err1 = 0, err2 = 0; Delay100TCYx(10); //let the device startup //initialize uart usart_init(); spi_init(); printf("Var is %i\r\n", data); printf("Starting\r\n"); printf("SSPCON1 is x%x\r\n", SSPCON1); printf("SSPADD is x%x\r\n", SSPADD); printf("SSPSTAT is x%x\r\n", SSPSTAT); PORTCbits.RC2 = 1; // pin high while(1){ PORTCbits.RC2 = 0; // CS low Delay1TCY(); // delay at least 5 ns WriteSPI(0x80); // b'10000000, read single byte 000000 //WriteSPI(0xB2); // b'10101100, read single byte data = ReadSPI(); PORTCbits.RC2 = 1; // CS high Delay1TCY(); // delay at least 5 ns //data = data>>1; printf("Data is 0x%02X \r\n", data); } }
void CAN2510SequentialRead( unsigned char *DataArray, unsigned char CAN2510addr, unsigned char numbytes ) { unsigned char i; CAN2510Enable( ); // Enable SPI Communication to MCP2510 while( WriteSPI(CAN2510_CMD_READ) ); while( WriteSPI(CAN2510addr) ); i = 0; while ( numbytes != 0 ) { DataArray[i] = ReadSPI(); i++; numbytes--; } CAN2510Disable( ); // Disable SPI Communication to MCP2510 }
void CAN2510SPISend( unsigned char bufferNumber ) { static unsigned char sendCommand; sendCommand = CAN2510_CMD_RTS & 0xf8; sendCommand |= (bufferNumber & 0x07); while( WriteSPI(sendCommand) ); }
void ADXL345_oneByteWrite(unsigned char address, unsigned char data) { #if I2C StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); // control byte WriteI2C(address); // word address WriteI2C(data); // data StopI2C(); #elif SPI SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns address = address | ADXL345_SPI_WRITE; WriteSPI(address); // write bit, multibyte bit, A5-A0 address WriteSPI(data); SPI_CS_PIN = 1; //CS pin high, ie disable chip Delay1TCY(); // delay at least 5 ns #endif }
unsigned char rfm22_read_reg(unsigned char i) { unsigned char x; rfm22_csn_low(); rfm22_csn_wait(); WriteSPI(i); x = ReadSPI(); rfm22_csn_high(); return x; }
unsigned char CAN2510ReadStatus( void ) { unsigned char readValue; CAN2510Enable( ); // Enable SPI Communication to MCP2510 WriteSPI(CAN2510_CMD_STATUS); // Send code for Read Status command readValue = ReadSPI(); // Read the status code, this is a Dummy read CAN2510Disable( ); // Disable SPI Communication to MCP2510 return ( readValue ); // Return the status code (same as previous read) }
//There are two memory banks in the LCD, data/RAM and commands. This //function sets the DC pin high or low depending, and then sends //the data byte void LCDWrite(char data_or_command, char data) { _LCDData = data_or_command; //Tell the LCD that we are writing either to data or a command (data has this line high, command: low) //Send the data OpenSPI(SLV_SSOFF & SPI_FOSC_4, MODE_00, SMPEND); //Ensure the SPI port is open _LCDSelect=0; WriteSPI(data); //shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data); _LCDSelect=1; CloseSPI(); }
void pcd8544_sendbyte(uint8_t data) { // Device select LATCbits.LC1 = 0; WriteSPI(data); //ReadSPI(); // Device deselect LATCbits.LC1 = 1; }
/* void askAtaForDoubleData(unsigned char chipId1,unsigned char chipId0,unsigned char bat,unsigned char control, unsigned char* batData){//posamezni dostop 2 bayta //IDENTIFICATION WriteSPI(chipId1); while(DataRdySPI()==0); WriteSPI(chipId0); while(DataRdySPI()==0); //CONTROL //WriteSPI();//ugotovi kaj je treba nastaviti ata ?ipu while(DataRdySPI()==0); //DATA char r; for(r=0;r<2;r++)//size of data je lahko 1 2 ali 14 batData[r]=ReadSPI();//odvisno koliko je podatkov, najverjetneje sta potem potrebna minimalno 2 klica while(DataRdySPI()==0); } */ void askAtaForAllData(unsigned char chipId1,unsigned char chipId0,unsigned char control, unsigned char* batData){//burst //IDENTIFICATION WriteSPI(chipId1); while(DataRdySPI()==0); WriteSPI(chipId0); while(DataRdySPI()==0); //CONTROL WriteSPI(control);//ugotovi kaj je treba nastaviti ata ?ipu while(DataRdySPI()==0); //DATA char r; for(r=0;r<14;r++)//size of data je lahko 1 2 ali 14 { batData[r]=ReadSPI();//odvisno koliko je podatkov, najverjetneje sta potem potrebna minimalno 2 klica ; } while(DataRdySPI()==0); }
/****************************************************************************** * Initialize SPI subroutine * * This routine will initialize the SPI for future reading and writing. It also * tells the EEPROM that we are allowing writing to all of the memory space. * * Inputs: None * Outputs: None ******************************************************************************/ void InitSPI1(void) { unsigned char i; SSP1STAT = 0b10000000; //Data sampled at end, transition from idle SSP1CON1 = 0b00110000; //Enable MSSP, idle state high, FOSC/4 PIE1bits.SSP1IE = 0; //Disable MSSP1 interrupts IPR1bits.SSP1IP = 0; //Set low priority to MSSP1 interrupts if enabled EEPROMEnable(); WriteSPI(ReadSTATUS); i = WriteSPI(0x00); EEPROMDisable(); //This little block allows writing to the whole EEPROM chip EEPROMEnable(); WriteSPI(WriteENABLE); EEPROMDisable(); EEPROMEnable(); WriteSPI(WriteSTATUS); WriteSPI(0x00); //Ensure that BP0, BP1, and BP2 are EEPROMDisable(); // 0 to disable write protection Delay1KTCYx(1); EEPROMEnable(); WriteSPI(ReadSTATUS); i = WriteSPI(0x00); EEPROMDisable(); Delay1KTCYx(1); //Erase all of the EEPROM EEPROMEnable(); WriteSPI(WriteENABLE); EEPROMDisable(); EEPROMEnable(); WriteSPI(EraseMEMORY); EEPROMDisable(); Delay10KTCYx(20); //Wait for 100 ms for erase to finish }
void pcd8544_sendbyte_data(uint8_t *data, uint8_t count) { uint8_t i; // Device select LATCbits.LC1 = 0; // Setting data mode -> D/C = 1 LATCbits.LC2 = 1; for(i=0;i<count;i++){ WriteSPI(data[i]); } //ReadSPI(); // Setting command mode -> D/C = 0 //LATCbits.LC2 = 0; // Device deselect LATCbits.LC1 = 1; }
void ADXL345_multiByteRead(unsigned char startAddress, char* buffer, unsigned char size) { #if I2C unsigned char i; StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(startAddress); RestartI2C(); WriteI2C(ADXL343_ADDR_READ); for (i = 0; i < size; i++) { buffer[i] = ReadI2C(); //keep the clock pulsing // if not last byte, send ack // if last byte, send nack if(i < size-1) { AckI2C(); } else { NotAckI2C(); } } StopI2C(); #elif SPI unsigned char tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); // the &0x3F restricts reading from only the XYZ data registers unsigned char i; SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns WriteSPI(tx); //Send address to start reading from. for (i = 0; i < size; i++) { buffer[i] = ReadSPI(); //keep the clock pulsing } SPI_CS_PIN = 1; //CS pin high, ie disable chip #endif }
unsigned char ADXL345_oneByteRead(unsigned char address) { unsigned char data = 0; #if I2C StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(address); RestartI2C(); WriteI2C(ADXL343_ADDR_READ); data = ReadI2C(); NotAckI2C(); StopI2C(); return data; #elif SPI SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns address = address | ADXL345_SPI_READ; WriteSPI(address); // read bit, multibyte bit, A5-A0 address data = ReadSPI(); SPI_CS_PIN = 1; //CS pin high, ie disable chip Delay1TCY(); // delay at least 5 ns return data; #endif }
void CAN2510SPIReset( void ) { while( WriteSPI(CAN2510_CMD_RESET) ); }
void write7Segment(char data) { WriteSPI(sevenSegPattern[data]); }
/** * Send a byte of data through spi and delay for 20 instruction cycles * just in case the slave is not quick enough to react * * Input : data is the data going to be sent out * */ void spiSendByte(uint8 data) { WriteSPI(data) ; Delay10TCYx(2); }
void CAN2510SPIWrite( unsigned char addr, unsigned char value ) { while( WriteSPI(CAN2510_CMD_WRITE) ); while( WriteSPI(addr) ); while( WriteSPI(value) ); }
unsigned char CAN2510SPIRead( unsigned char addr ) { while( WriteSPI(CAN2510_CMD_READ) ); while( WriteSPI(addr) ); return ReadSPI(); }
/****************************************************************************** * W_EEPROM subroutine * * This routine will store the latest 30 bytes of temperature into EEPROM memory. * * NOTE: When writing in 30 byte chunks, the AAI mode must be set to save time. * This follows a specific procedure as outlined in the SST25VF080B * data sheet - pp. 13-15. * * Inputs: addr = EEPROM memory address for storing data * mem = RAM location to pull data from to send to EEPROM * Outputs: None ******************************************************************************/ void W_EEPROM(unsigned short long addr, unsigned char * mem) { unsigned char i; unsigned char high; unsigned char med; unsigned char low; high = addr>>16; //High byte of EEPROM memory location med = addr>>8; //Medium byte of EEPROM memory location low = addr; //Low byte of EEPROM memory location EEPROMEnable(); //Select EEPROM WriteSPI(WriteENABLE); //Set Write enable Latch (WREN) EEPROMDisable(); //Push CS high according to data sheet EEPROMEnable(); //Pull CS low to write WriteSPI(AutoWRITE); //Begin AAI mode WriteSPI(high); //Send high address to EEPROM WriteSPI(med); //Send medium address to EEPROM WriteSPI(low); //Send low address to EEPROM WriteSPI(mem[0]); //Send first of two bytes WriteSPI(mem[1]); //Send second of two bytes EEPROMDisable(); //Push CS high according to data sheet Delay1KTCYx(1); //Wait for write to finish for (i = 2; i < 29; i+=2) { EEPROMEnable(); //Pull CS low to write WriteSPI(AutoWRITE); //Continue AAI mode WriteSPI(mem[i]); //Send first of two bytes WriteSPI(mem[i+1]); //Send second of two bytes EEPROMDisable(); //Push CS high according to data sheet EEPROMEnable(); WriteSPI(ReadSTATUS); while(1) { if( WriteSPI(0x00) != 0x42 ) { continue; } else { break; } } EEPROMDisable(); } EEPROMEnable(); WriteSPI(WriteDISABLE); //Write disable to exit AAI mode EEPROMDisable(); EEPROMEnable(); WriteSPI(ReadSTATUS); i = WriteSPI(0x00); EEPROMDisable(); Delay10KTCYx(2); //Wait for 6 ms for write to finish }