//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(); }
/** * 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 }
/** * 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 ; }
// Configure SPI module in 16-bit master mode void initMPUSPI_master16(uint16_t priPre, uint16_t secPre) { uint16_t SPICON1Value; // holds the information about SPI configuration uint16_t SPICON2Value; uint16_t SPISTATValue; // holds the information about SPI Enable/Disable MPU_SS = 1; // deassert MPU SS MPU_SS_TRIS = 0; // make MPU SS an output CloseSPI(); // turn off SPI module ConfigIntSPI(SPI_INT_DIS & SPI_INT_PRI_6); #if defined(__dsPIC33E__) SPICON1Value = ENABLE_SDO_PIN & SPI_MODE16_ON & ENABLE_SCK_PIN & SPI_SMP_OFF & SPI_CKE_OFF & SLAVE_ENABLE_OFF & CLK_POL_ACTIVE_LOW & MASTER_ENABLE_ON & secPre & priPre; SPICON2Value = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT; // & FIFO_BUFFER_DISABLE; SPISTATValue = SPI_ENABLE & SPI_IDLE_CON & SPI_RX_OVFLOW_CLR & BUF_INT_SEL_5; // BUF_INT_SEL_5 == Interrupt when the last bit is shifted out of SPIxSR, and the transmit is complete #else SPICON1Value = ENABLE_SDO_PIN & SPI_MODE16_ON & ENABLE_SCK_PIN & SPI_SMP_ON & SPI_CKE_OFF & SLAVE_ENABLE_OFF & CLK_POL_ACTIVE_LOW & MASTER_ENABLE_ON & secPre & priPre; SPICON2Value = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT; SPISTATValue = SPI_ENABLE & SPI_IDLE_CON & SPI_RX_OVFLOW_CLR; #endif #ifdef __PIC32MX__ // * Example: OpenSPI1(SPI_MODE32_ON|SPI_SMP_ON|MASTER_ENABLE_ON|SEC_PRESCAL_1_1|PRI_PRESCAL_1_1, SPI_ENABLE); OpenSPI(SPICON1Value, SPISTATValue); #else OpenSPI(SPICON1Value, SPICON2Value, SPISTATValue); // printf("SPI1STAT %04X, SPI1CON1 %04X, SPI1CON2 %04X\r\n", SPI1STAT, SPI1CON1, SPI1CON2); #endif _SPIROV = 0; // clear SPI receive overflow _SPIIF = 0; // clear any pending interrupts _SPIIP = INT_PRI_MPUSPI; // set interrupt priority // _SPIIE = 1; // turn on SPI interrupts }
/** * 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 }
int main(void) { initApp(); for(;;){ unsigned char i; unsigned char chipId0=0; unsigned char chipId1=0; CHIP_SELECT=1; // OpenSPI(SPI_FOSC_64&,MODE_01,SMPEND); // OpenSP for(i=0;i<NUMBER_OF_ATA_CHIPS;i++){ if(i<8){ chipId0=1<<(i); chipId1=0; } else { chipId0=0; chipId1=1<<(i-8); } //control je 1111110 askAtaForAllData(chipId1,chipId0,0xfe,BatteryData); } CloseSPI(); CHIP_SELECT=0; // tu kli?i funkcije za can } for(;;);//nikoli ne smemo priti na konec return 0; }
/*--------------------------------------------------------\ | RestartSPI | | | | Restarts, and effectively resets, the SPI module. | \---------------------------------------------------------*/ void RestartSPI(void) { CloseSPI(); OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); }
/** * Configure SPI in PIC18 as master * * clock = FOSC_64 * Mode (0,1) * Input data sampled at middle of data out */ void spiConfigureMaster(void) { CloseSPI(); OpenSPI(SPI_FOSC_64,MODE_01,SMPMID); }
void initSpi(void){ CloseSPI(); //close if was on OpenSPI(SPI_FOSC_64,MODE_01,SMPMID); // WriteSPI() }