/****************************************************************************** * Function: void AcquireTemperature(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: None * * Note: None *****************************************************************************/ BOOL AcquireTemperature(void) { cs_temp_sensor = 0; MSB(temperature) = ReadSPI(); LSB(temperature) = ReadSPI(); cs_temp_sensor = 1; if(temperature.LowB.b2 == 0) return FAIL; return OK; }//end AcquireTemperature
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); } }
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) }
/** * Send command to the slave and delay extra 10 instruction cycles at the end * * Examples of command * Write for sending data to the slave and the slave will perform flash erase and write * Read for reading the desired segment from the slave * ID for reading the device ID of the slave * CONFIG for sending configuration data to the slave * * Input : command is the instruction for the slave * * Output : 1 for OK * 0 for failure as the master did not receive ACK from the slave * */ uint8 spiSendCommand(uint8 command) { uint8 acknack,dummy; spiConfigureMaster(); spiSendByte(command); acknack = ReadSPI(); if(!checkACK(&acknack)) return 0 ; //return 0 for error CloseSPI(); Delay10TCYx(1); return 1 ; }
/** * Receive data from the slave through spi * * Input : data is the data going to be stored * count is the amount of data going to be stored * * Output : 1 for OK */ uint8 spiReceiveData(uint8 *data,uint8 count) { uint8 i ; spiConfigureMaster(); for ( i = 0 ; i < count ; i ++ ) { spiSendByte(0x00); data[i] = ReadSPI(); } CloseSPI(); Delay10TCYx(1); return 1 ; //return 1 for OK }
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 }
/** * Receive status of flash writing operation by the slave * * * Output : 1 for done * 0 for not done */ uint8 spiReceiveStatus(void) { uint8 status ; spiConfigureMaster(); spiSendByte(0x00); status = ReadSPI(); CloseSPI(); Delay10TCYx(1); if (status == Done) return 1 ; else return 0 ; }
/** * Send any amount of data through spi and always check for the slave ACK * Delay extra 10 instruction cycles at the end of sending. * * Input : *data is the pointer to the data array * count is the amount of data in the array * startPoint is the starting location of the data array to access * */ uint8 spiSendData(uint8 *data,uint8 count,uint8 startPoint) { uint8 acknack , i , j = startPoint; spiConfigureMaster(); for ( i = 0 ; i < count ; i ++ ) { spiSendByte(data[j]); acknack = ReadSPI(); j++; if(!checkACK(&acknack)) return 0 ; //return 0 for error } CloseSPI(); Delay10TCYx(1); return 1 ; //return 1 for OK }
/* 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); }
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 }
unsigned char CAN2510SPIRead( unsigned char addr ) { while( WriteSPI(CAN2510_CMD_READ) ); while( WriteSPI(addr) ); return ReadSPI(); }
/****************************************************************************** * Function: void AcquireTemperature(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: None * * Note: None *****************************************************************************/ BOOL AcquireTemperature(void) { #if defined(PICDEM_FS_USB) //The PICDEM FS USB Demo Board uses a TC77 (13 bit) temperature sensor and //communicates with it through the SPI interface. cs_temp_sensor = 0; temperature.v[1] = ReadSPI(); temperature.v[0] = ReadSPI(); cs_temp_sensor = 1; if(temperature.bits.b2 == 0) return FALSE; #elif defined(__C30__) || defined(__C32__) || defined __XC16__ //Create temp variables to store the conversion data float temp; //get ready to sample the A/D #if defined(__C30__) || defined __XC16__ AD1CHS = 0x4; //MUXA uses AN4 AD1PCFGLbits.PCFG4 = 0; for(temp=0;temp<1000;temp++); //Sample delay // Get an ADC sample AD1CON1bits.SAMP = 1; //Start sampling for(temp=0;temp<1000;temp++); //Sample delay, conversion start automatically AD1CON1bits.SAMP = 0; //Start sampling for(temp=0;temp<1000;temp++); //Sample delay, conversion start automatically while(!AD1CON1bits.DONE); //Wait for conversion to complete #else AD1PCFGbits.PCFG4 = 0; AD1CON1 = 0x0000; // SAMP bit = 0 ends sampling ... // and starts converting AD1CHS = 0x00040000; // Connect RB4/AN4 as CH0 input .. // in this example RB2/AN2 is the input AD1CSSL = 0; AD1CON3 = 0x0002; // Manual Sample, Tad = internal 6 TPB AD1CON2 = 0; AD1CON1SET = 0x8000; // turn ADC ON AD1CON1SET = 0x0002; // start sampling ... for(temp=0;temp<1000;temp++); //Sample delay, conversion start automatically AD1CON1CLR = 0x0002; // start Converting while (!(AD1CON1 & 0x0001));// conversion done? #endif //convert the results to a float temp = (float)ADC1BUF0; // voltage = A2D_reading * 3.3v / 1024 temp *= 3.3; temp /= 1024; // align to 0C (subtracting -.65v) temp -= .55; //convert to TC77 style output and store to temperature temp *= 12800; temperature.Val = (WORD)temp; temperature.Val |= 0x7; //#elif defined(__C32__) //#warning "TODO" #elif defined(PIC18F87J50_PIM) // Uses TC74 (8 bit) //The PIC18F87J50 FS USB Plug-In Module (PIM) does not have a temperature //sensor, but there is a TC74 (8 bit) I2C based temperature sensor on the //HPC Explorer demo board. In order for this temperature demo code to do //anything useful, the PIM should be used in conjunction with the HPC Explorer. //The TC74 comes in 5V and 3.3V optimized versions. If a 5V part is run at //3.3V (as with the PIM installed), it may have relatively large offsets. return TRUE; // Don't need to do anything in this function, temperature // polling with the PIC18F87J50 FS USB Plug-In Module is // done with the PollTempOnHPCExplorer() function. // This is done so the I2C communication can be done with // a non-blocking approach. #elif defined(LOW_PIN_COUNT_USB_DEVELOPMENT_KIT) temperature.Val = 0x0000; return TRUE; #elif defined(PIC18F46J50_PIM) //Create temp variables to store the conversion data float temp; //get ready to sample the A/D ADCON0bits.CHS = 0x07; for(temp=0;temp<1000;temp++){} ADCON0bits.GO = 1; // Start AD conversion while(ADCON0bits.NOT_DONE); // Wait for conversion //convert the results to a float temp = (float)ADRES; // voltage = A2D_reading * 3.3v / 1024 temp *= 3.3; temp /= 1024; // align to 0C (subtracting -.65v) temp -= .55; //convert to TC77 style output and store to temperature temp *= 12800; temperature.Val = (WORD)temp; temperature.Val |= 0x7; return TRUE; #else #error "Unknown temperature acquire configuration. See AcquireTemperature function in __FILE__" #endif return TRUE; }//end AcquireTemperature