/* * @ VS1003 write register */ void VS1003_write_reg(uint16 address, uint16 value){ VS_WAIT_DREQ(); SPI_enable(GPIOR_VS1003_CMDCH); VSPI_WRITE(GPIOR_VS1003_CMDCH, address & 0x00ff | 0x0200); VSPI_WRITE(GPIOR_VS1003_CMDCH, value); SPI_disable(GPIOR_VS1003_CMDCH); }
/* * @ VS1003 write data */ void VS1003_write_data(char *buf, int len) { VS_WAIT_DREQ(); SPI_enable(GPIOR_VS1003_DATCH); while(len --) { SPI_write(GPIOR_VS1003_DATCH, *(buf ++)); } SPI_disable(GPIOR_VS1003_DATCH); }
void MCP2515_reset(void) { SPI_enable(); SPI_transmit(MCP_RESET); SPI_disable(); //Allow mcp2515 time to complete command _delay_us(1); }
char MCP2515_read_status(void) { SPI_enable(); SPI_transmit(MCP_READ_STATUS); uint8_t status = SPI_receive(); SPI_disable(); return status; }
void MCP2515_write(uint8_t data, uint8_t address) { SPI_enable(); SPI_transmit(MCP_WRITE); SPI_transmit(address); SPI_transmit(data); SPI_disable(); }
uint8_t MCP2515_read(uint8_t address) { SPI_enable(); SPI_transmit(MCP_READ); SPI_transmit(address); uint8_t data = SPI_receive(); SPI_disable(); return data; }
void MCP2515_bit_modify(uint8_t address, uint8_t mask, uint8_t data) { SPI_enable(); SPI_transmit(MCP_BITMOD); SPI_transmit(address); SPI_transmit(mask); SPI_transmit(data); SPI_disable(); }
/* * @ VS1003 read register */ unsigned short VS1003_read_reg(uint16 address){ uint16 cache; VS_WAIT_DREQ(); SPI_enable(GPIOR_VS1003_CMDCH); VSPI_WRITE(GPIOR_VS1003_CMDCH, address & 0x00ff | 0x0300); cache = VSPI_WRITE(GPIOR_VS1003_CMDCH, 0xffff); SPI_disable(GPIOR_VS1003_CMDCH); return cache; }
// main function; program starts here void main(void) { unsigned char data = 0; unsigned int battery_voltage = 0; // ADC reading of battery voltage ports_initialize(); // set up ports ADC_initialize(); // configure ADC SPI_initialize(); // set up SPI module sleep_initialize(); // set up sleep mode ISR_initialize(); // set up ISR while(1) { SLEEP(); // sleep PIC until WDT expires time++; // increment time (+16s) battery_voltage = ADC_read(0x09); // read battery voltage if ( time >= 1 ) // 1*16s=16s { time = 0; // reset time if(battery_voltage >= 690) //690 = 11v { LATCbits.LATC4 = 1; // turn relay on SPI_enable(); // turn on SPI module while( data != 0xDA ) // wait for shutdown command to be received { while(!SSP1STATbits.BF) // wait till data is in the buffer { CLRWDT(); } data = SSP1BUF; // read data outside } data = 0x00; SPI_disable(); // turn off SPI module __delay_ms(10000); // wait for R Pi to shutdown LATCbits.LATC4 = 0; // turn relay off } } } }
void MCP2515_rts(uint8_t rts_port) { SPI_enable(); switch (rts_port){ case 0: SPI_transmit(MCP_RTS_TX0); break; case 1: SPI_transmit(MCP_RTS_TX1); break; case 2: SPI_transmit(MCP_RTS_TX2); break; } SPI_disable(); }
void main (void) { //Stop watchdog timer WDT_hold(__MSP430_BASEADDRESS_WDT_A__); //If clock signal from master stays low, it is not yet in SPI mode while ( GPIO_INPUT_PIN_LOW == GPIO_getInputPinValue(__MSP430_BASEADDRESS_PORT3_R__, GPIO_PORT_P3, GPIO_PIN0 )) ; //P3.5,4,0 option select GPIO_setAsPeripheralModuleFunctionInputPin(__MSP430_BASEADDRESS_PORT3_R__, GPIO_PORT_P3, GPIO_PIN0 + GPIO_PIN4 + GPIO_PIN5 ); //Initialize slave to MSB first, inactive high clock polarity and 3 wire SPI returnValue = SPI_slaveInit(__MSP430_BASEADDRESS_USCI_A0__, SPI_MSB_FIRST, SPI_PHASE_DATA_CAPTURED_ONFIRST_CHANGED_ON_NEXT, SPI_CLOCKPOLARITY_INACTIVITY_HIGH ); if (STATUS_FAIL == returnValue){ return; } //Enable SPI Module SPI_enable(__MSP430_BASEADDRESS_USCI_A0__); //Enable Receive interrupt SPI_enableInterrupt(__MSP430_BASEADDRESS_USCI_A0__, SPI_RECEIVE_INTERRUPT ); //Enter LPM4, enable interrupts __bis_SR_register(LPM4_bits + GIE); }
void SPI_init(SPIDef * port, GPIOPin sck, GPIOPin miso, GPIOPin mosi, GPIOPin ncs) { SPI_enable(port); SPI_IOconfig(port, sck, miso, mosi, ncs); SPI_start(port); }