Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <p32xxxx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <xc.h>
- #include <math.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <string.h>
- /* Adds support for PIC32 Peripheral library functions and macros */
- #define _SUPPRESS_PLIB_WARNING
- #define SYS_FREQ (80000000)
- #include <plib.h>
- #include "adc.h"
- #include "can.h"
- // Configuration Bits
- #pragma config FPLLIDIV = DIV_2 // 2
- #pragma config FPLLMUL = MUL_20 // 20
- #pragma config FPLLODIV = DIV_1
- #pragma config FWDTEN = OFF
- #pragma config POSCMOD = XT
- #pragma config FNOSC = FRCPLL
- #pragma config FPBDIV = DIV_1
- #pragma config FCKSM = CSECME
- void main(){
- SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
- DelayMs(20);
- mPORTBSetPinsDigitalOut(BIT_7);
- mPORTBSetBits(BIT_7);
- mPORTBSetPinsDigitalOut(BIT_3);
- mPORTBSetBits(BIT_3);
- adc_init();
- DelayMs(200);
- can_init();
- // bool Front1Rear0 = 1;
- // unsigned int CAN_ID_RF;
- // unsigned int CAN_ID_RS;
- // unsigned int CAN_ID_LF;
- // unsigned int CAN_ID_LS;
- // if(Front1Rear0){
- // CAN_ID_RF = 0x200;
- // CAN_ID_RS = 0x201;
- // CAN_ID_LF = 0x202;
- // CAN_ID_LS = 0x203;
- // }
- // else {
- // CAN_ID_RF = 0x210;
- // CAN_ID_RS = 0x211;
- // CAN_ID_LF = 0x212;
- // CAN_ID_LS = 0x213;
- // }
- while(1){
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[0] = SpiChnGetC(SPI_CHN_R); //Caliper Temp 1
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[1] = SpiChnGetC(SPI_CHN_R); //Caliper Temp 2
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[2] = SpiChnGetC(SPI_CHN_R); //Rotor Temp 1
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[3] = SpiChnGetC(SPI_CHN_R); //Rotor Temp 2
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[4] = SpiChnGetC(SPI_CHN_R); //Brake Pad Temp 1
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[5] = SpiChnGetC(SPI_CHN_R); //Brake Pad Temp 2
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[6] = SpiChnGetC(SPI_CHN_R);
- while(PORTBbits.RB4);
- SpiChnPutC(SPI_CHN_R, 0);
- CANToSend[7] = SpiChnGetC(SPI_CHN_R);
- CAN1TxSendMsg(0x201);
- }
- }
- #define LED_SPI_SPEED 1000000 //1000000
- #define SPI_CHN_L SPI_CHANNEL2
- #define SPI_CHN_R SPI_CHANNEL3
- static uint16_t adc_data = 0;
- /**
- * Initialize the LED driver.
- */
- void adc_init()
- {
- // set up SPI
- SpiChnOpen(
- SPI_CHN_R,
- SPI_OPEN_MSTEN | SPI_OPEN_MODE8 | SPI_OPEN_CKE_REV,
- 80000000/LED_SPI_SPEED
- );
- SpiChnOpen(
- SPI_CHN_L,
- SPI_OPEN_MSTEN | SPI_OPEN_MODE8 | SPI_OPEN_CKE_REV,
- 80000000/LED_SPI_SPEED
- );
- //Sets and keeps SS pins low for both left and right SPIs
- AD1PCFGbits.PCFG8 = 1;
- mPORTBSetPinsDigitalIn(BIT_8); //DRDY in
- mPORTDSetPinsDigitalOut(BIT_9); //CS out
- mPORTBSetPinsDigitalOut(BIT_7); //Start out
- mPORTBSetPinsDigitalOut(BIT_9); //Reset out
- mPORTDSetBits(BIT_9); //CS High (off))
- mPORTBSetBits(BIT_7); //Start High
- mPORTBSetBits(BIT_9); //Reset High
- SpiChnPutC(SPI_CHN_R, 0x06); //Resets ADC to reliably set pins next
- SpiChnGetC(SPI_CHN_R);
- // SpiChnPutC(SPI_CHN_L, 0x06); //Resets ADC to reliably set pins next
- // SpiChnGetC(SPI_CHN_L);
- DelayMs(1);
- SpiChnPutC(SPI_CHN_R, 0x16); //Disables ADC Data reading
- SpiChnGetC(SPI_CHN_R);
- // SpiChnPutC(SPI_CHN_L, 0x16); //Disables ADC Data reading
- // SpiChnGetC(SPI_CHN_L);
- DelayMs(1);
- SpiChnPutC(SPI_CHN_R, 0x04); //Syncs ADC conversion
- SpiChnGetC(SPI_CHN_R);
- // SpiChnPutC(SPI_CHN_L, 0x04); //Syncs ADC conversion
- // SpiChnGetC(SPI_CHN_L);
- DelayMs(1);
- SpiChnPutC(SPI_CHN_R, 0x16); //Disables Continuous ADC Data reading
- SpiChnGetC(SPI_CHN_R);
- // SpiChnPutC(SPI_CHN_L, 0x16); //Disables Continuous ADC Data reading
- // SpiChnGetC(SPI_CHN_L);
- mPORTDSetBits(BIT_9); //Resets the Serial Interface
- mPORTGSetBits(BIT_9);
- //This sets all initial properties for reliable use
- SpiChnPutC(SPI_CHN_R, 0x40); //Starts writing to ADC registers starting with Mux0
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- SpiChnPutC(SPI_CHN_R, 0x03); //Sets number of bytes to write plus 1
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- SpiChnPutC(SPI_CHN_R, 0x08); //Mux0 is set to 00001000, AKA no Burnout detect, positive input is pin 1, negative input is pin 0
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- SpiChnPutC(SPI_CHN_R, 0x00); //Sets voltage biases all to 0
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- SpiChnPutC(SPI_CHN_R, 0x30); //Mux1 is set to 00110000, AKA internal oscillator, internal voltage reference is used, internal voltage reference is selected, and the adc is in normal operation
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- SpiChnPutC(SPI_CHN_R, 0x00); //SYS0 is set to 00000000, AKA Gain setting of 1 and a data output rate of 2000 samples/second
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- //Simple Attempt Below
- // SpiChnPutC(SPI_CHN_R, 0x42); //Starts writing to ADC registers starting with Mux0
- // SpiChnGetC(SPI_CHN_R); // ignore, zeros
- // SpiChnPutC(SPI_CHN_R, 0x01); //Sets number of bytes to write minus 1
- // SpiChnGetC(SPI_CHN_R); // ignore, zeros
- // SpiChnPutC(SPI_CHN_R, 0x30); //Mux1 is set to 00110000, AKA internal oscillator, internal voltage reference is used, internal voltage reference is selected, and the adc is in normal operation
- // SpiChnGetC(SPI_CHN_R); // ignore, zeros
- // SpiChnPutC(SPI_CHN_R, 0x09); //SYS0 is set to 00001001, AKA Gain setting of 1 and a data output rate of 2000 samples/second
- // SpiChnGetC(SPI_CHN_R); // ignore, zeros
- //
- DelayMs(10);
- //
- // SpiChnPutC(SPI_CHN_L, 0x40); //Starts writing to ADC registers starting with Mux0
- // SpiChnGetC(SPI_CHN_L); // ignore, zeros
- // SpiChnPutC(SPI_CHN_L, 0x05); //Sets number of bytes to write minus 1
- // SpiChnGetC(SPI_CHN_L); // ignore, zeros
- // SpiChnPutC(SPI_CHN_L, 0x08); //Mux0 is set to 00001000, AKA no Burnout detect, positive input is pin 1, negative input is pin 0
- // SpiChnGetC(SPI_CHN_L); // ignore, zeros
- // SpiChnPutC(SPI_CHN_L, 0x00); //Sets voltage biases all to 0
- // SpiChnGetC(SPI_CHN_L); // ignore, zeros
- // SpiChnPutC(SPI_CHN_L, 0x30); //Mux1 is set to 00110000, AKA internal oscillator, internal voltage reference is used, internal voltage reference is selected, and the adc is in normal operation
- // SpiChnGetC(SPI_CHN_L); // ignore, zeros
- // SpiChnPutC(SPI_CHN_L, 0x08); //SYS0 is set to 00001000, AKA Gain setting of 1 and a data output rate of 1000 samples/second
- // SpiChnGetC(SPI_CHN_L); // ignore, zeros
- mPORTBClearBits(BIT_7);
- }
- uint16_t adc_read_R(unsigned int ANALOG_PIN)
- {
- unsigned int PinToHex;
- if(ANALOG_PIN == 1){
- PinToHex = 0x08;
- } else if(ANALOG_PIN == 2){
- PinToHex = 0x10;
- } else if(ANALOG_PIN == 3){
- PinToHex = 0x18;
- } else if(ANALOG_PIN == 4){
- PinToHex = 0x20;
- } else if(ANALOG_PIN == 5){
- PinToHex = 0x28;
- } else if(ANALOG_PIN == 6){
- PinToHex = 0x30;
- } else if(ANALOG_PIN == 7){
- PinToHex = 0x38;
- }
- mPORTDClearBits(BIT_9);
- // SpiChnPutC(SPI_CHN_R, 0x40);
- // SpiChnGetC(SPI_CHN_R); // ignore, zeros
- // SpiChnPutC(SPI_CHN_R, 0x00);
- // SpiChnGetC(SPI_CHN_R); // ignore, zeros
- // SpiChnPutC(SPI_CHN_R, PinToHex);
- // SpiChnGetC(SPI_CHN_R); // ignore, zeros
- mPORTDSetBits(BIT_9);
- mPORTBSetBits(BIT_7);
- Nops(5);
- mPORTBClearBits(BIT_7);
- while(PORTBbits.RB8); //wait for DRDY pin to go low B8 for right, b4 for left
- mPORTDClearBits(BIT_9);
- SpiChnPutC(SPI_CHN_R, 0x12);
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- SpiChnPutC(SPI_CHN_R, 0xFF);
- uint8_t msb = SpiChnGetC(SPI_CHN_R);
- SpiChnPutC(SPI_CHN_R, 0xFF);
- uint8_t lsb = SpiChnGetC(SPI_CHN_R);
- adc_data = msb << 8 | lsb;
- Nops(10);
- return adc_data;
- }
- uint16_t adc_read_L(unsigned int ANALOG_PIN)
- {
- unsigned int PinToHex;
- if(ANALOG_PIN == 1){
- PinToHex = 0x08;
- } else if(ANALOG_PIN == 2){
- PinToHex = 0x10;
- } else if(ANALOG_PIN == 3){
- PinToHex = 0x18;
- } else if(ANALOG_PIN == 4){
- PinToHex = 0x20;
- } else if(ANALOG_PIN == 5){
- PinToHex = 0x28;
- } else if(ANALOG_PIN == 6){
- PinToHex = 0x30;
- } else if(ANALOG_PIN == 7){
- PinToHex = 0x38;
- }
- mPORTGClearBits(BIT_9);
- SpiChnPutC(SPI_CHN_L, 0x40); //Tells ADC we are going to change Mux0
- SpiChnGetC(SPI_CHN_L); // ignore, zeros
- SpiChnPutC(SPI_CHN_L, 0x02); //Tells ADC we are only changing 1 register
- SpiChnGetC(SPI_CHN_L); // ignore, zeros
- SpiChnPutC(SPI_CHN_L, PinToHex); //Tells Mux0 that we want whatever pin is referenced in PinToHex
- SpiChnGetC(SPI_CHN_L); // ignore, zeros
- while(PORTBbits.RB4); //wait for DRDY pin to go low B8 for right, b4 for left
- SpiChnPutC(SPI_CHN_L, 0x12); //Tells ADC we want to read data
- SpiChnGetC(SPI_CHN_L); // ignore, zeros
- SpiChnPutC(SPI_CHN_L, 0xFF); //Nops sent to ADC to pull out data
- uint8_t msb = SpiChnGetC(SPI_CHN_L);
- SpiChnPutC(SPI_CHN_L, 0xFF);
- uint8_t lsb = SpiChnGetC(SPI_CHN_L);
- adc_data = msb << 8 | lsb;
- Nops(10);
- return adc_data;
- }
- void Right_Fast()
- {
- CANToSend[0] = adc_read_R(1) >> 8; //Wheel Speed 1
- CANToSend[1] = adc_read_R(1) && 0xFF; //Wheel Speed 2
- CANToSend[2] = adc_read_R(2) >> 8; //Load Cell 1
- CANToSend[3] = adc_read_R(2) && 0xFF; //Load Cell 2
- CANToSend[4] = adc_read_R(3) >> 8; //Damper Pot 1
- CANToSend[5] = adc_read_R(3) && 0xFF; //Damper Pot 2
- CANToSend[6] = adc_read_R(4) >> 8; //Tire Temp 1
- CANToSend[7] = adc_read_R(4) && 0xFF; //Tire Temp 2
- }
- void Left_Fast()
- {
- CANToSend[0] = adc_read_L(1) >> 8; //Wheel Speed 1
- CANToSend[1] = adc_read_L(1) && 0xFF; //Wheel SPeed 2
- CANToSend[2] = adc_read_L(2) >> 8; //Load Cell 1
- CANToSend[3] = adc_read_L(2) && 0xFF; //Load Cell 2
- CANToSend[4] = adc_read_L(3) >> 8; //Damper Pot 1
- CANToSend[5] = adc_read_L(3) && 0xFF; //Damper Pot 2
- CANToSend[6] = 0x00;
- CANToSend[7] = 0x00;
- }
- void Right_Slow()
- {
- CANToSend[0] = adc_read_R(5) >> 8; //Caliper Temp 1
- CANToSend[1] = adc_read_R(5) & 0xFF; //Caliper Temp 2
- CANToSend[2] = adc_read_R(6) >> 8; //Rotor Temp 1
- CANToSend[3] = adc_read_R(6) & 0xFF; //Rotor Temp 2
- CANToSend[4] = adc_read_R(7) >> 8; //Brake Pad Temp 1
- CANToSend[5] = adc_read_R(7) & 0xFF; //Brake Pad Temp 2
- CANToSend[6] = 0x00;
- CANToSend[7] = 0x00;
- }
- void Left_Slow()
- {
- CANToSend[0] = adc_read_L(4) >> 8; //Caliper Temp 1
- CANToSend[1] = adc_read_L(4) & 0xFF; //Caliper Temp 2
- CANToSend[2] = adc_read_L(5) >> 8; //Rotor Temp 1
- CANToSend[3] = adc_read_L(5) & 0xFF; //Rotor Temp 2
- CANToSend[4] = adc_read_L(6) >> 8; //Brake Pad Temp 1
- CANToSend[5] = adc_read_L(6) & 0xFF; //Brake Pad Temp 2
- CANToSend[6] = 0x00;
- CANToSend[7] = 0x00;
- }
- void testing_adc_init()
- {
- // set up SPI
- SpiChnOpen(
- SPI_CHN_R,
- SPI_OPEN_MSTEN | SPI_OPEN_MODE8 | SPI_OPEN_CKE_REV,
- 80000000/LED_SPI_SPEED
- );
- //Sets and keeps SS pins low for both left and right SPIs
- AD1PCFGbits.PCFG8 = 1;
- mPORTBSetPinsDigitalIn(BIT_8); //DRDY in
- mPORTDSetPinsDigitalOut(BIT_9); //CS out
- mPORTBSetPinsDigitalOut(BIT_7); //Start out
- mPORTBSetPinsDigitalOut(BIT_9); //Reset out
- mPORTDSetBits(BIT_9); //CS High (off))
- mPORTBSetBits(BIT_7); //Start High
- mPORTBSetBits(BIT_9); //Reset High
- }
- void testing_RREG(){
- mPORTDClearBits(BIT_9); //CS Low (on))
- while(1){
- DelayMs(100);
- SpiChnPutC(SPI_CHN_R, 0x20);
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- SpiChnPutC(SPI_CHN_R, 0x03);
- SpiChnGetC(SPI_CHN_R); // ignore, zeros
- int i;
- for(i = 0; i < 4; i++){
- mPORTDClearBits(BIT_9);
- SpiChnPutC(SPI_CHN_R, 0xFF);
- CANToSend[i] = SpiChnGetC(SPI_CHN_R);
- mPORTDSetBits(BIT_9);
- }
- CANToSend[4] = 0;
- CANToSend[5] = 0;
- CANToSend[6] = 0;
- CANToSend[7] = 0;
- CAN1TxSendMsg(0x201); //Send Left Fast from buffer
- DelayMs(10);
- }
- }
- //void holder_main(){
- // while(1){
- // uint32_t i;
- // for (i = 0; i < 20; i++) {
- // Right_Fast(); //Load Right Fast info into CAN buffer, takes 175microseconds to run
- // CAN1TxSendMsg(CAN_ID_RF); //Send Right Fast from buffer
- //// Left_Fast(); //Load Left Fast info into CAN buffer
- //// CAN1TxSendMsg(CAN_ID_LF); //Send Left Fast from buffer
- // DelayMs(4);
- // }
- // Right_Slow();
- // CAN1TxSendMsg(CAN_ID_RS); //Send Right Fast from buffer
- //// Left_Slow();
- //// CAN1TxSendMsg(CAN_ID_LS); //Send Right Fast from buffer
- // DelayMs(4);
- // }
- //}
- #define CAN_BUS_SPEED 1000000
- #define BAUD_RATE 38400
- BYTE CAN1MessageFifoArea[4 * 8 * 16]; // a bit oversized, 128 bits/msg instead of 108
- UINT8 dataBuffer[1024];
- CAN_RX_MSG_SID msgSID;
- int canData[8];
- int CANToSend[8] = {0};
- void can_init(void)
- {
- CAN_BIT_CONFIG canBitConfig;
- //UINT baudPrescalar;
- CANEnableModule(CAN1,TRUE);
- CANSetOperatingMode(CAN1, CAN_CONFIGURATION);
- while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION);
- canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ;
- canBitConfig.phaseSeg1Tq = CAN_BIT_3TQ;
- canBitConfig.propagationSegTq = CAN_BIT_3TQ;
- canBitConfig.phaseSeg2TimeSelect = TRUE;
- canBitConfig.sample3Time = TRUE;
- canBitConfig.syncJumpWidth = CAN_BIT_2TQ;
- CANSetSpeed(CAN1,&canBitConfig,SYS_FREQ,CAN_BUS_SPEED);
- // sets aside memory for CAN buffer
- CANAssignMemoryBuffer(CAN1,CAN1MessageFifoArea,(4 * 8 * 16)); // oversized
- // configures the transmit channel
- CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY);
- CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION);
- while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION);
- }
- void CAN1TxSendMsg(int CAN_CHANNEL_VAR) //
- {
- // Find a place to store the message until it is sent
- CANTxMessageBuffer *msgBuffer;
- msgBuffer = CANGetTxMessageBuffer(CAN1,CAN_CHANNEL0);
- if(msgBuffer != NULL)
- {
- // msgBuffer->messageWord[0] = 0;
- // msgBuffer->messageWord[1] = 0;
- // msgBuffer->messageWord[2] = 0;
- msgBuffer->messageWord[3] = 0;
- msgBuffer->msgSID.SID = CAN_CHANNEL_VAR;
- msgBuffer->msgEID.IDE = 0;
- msgBuffer->msgEID.DLC = 8;
- msgBuffer->data[0] = CANToSend[0]; // Voltage Cell 1 (unsigned integer)
- msgBuffer->data[1] = CANToSend[1]; // Voltage Cell 2 (unsigned integer)
- msgBuffer->data[2] = CANToSend[2]; // Voltage Cell 3 (unsigned integer)
- msgBuffer->data[3] = CANToSend[3]; // Voltage Cell 4 (unsigned integer)
- msgBuffer->data[4] = CANToSend[4]; // Voltage Cell 5 (unsigned integer)
- msgBuffer->data[5] = CANToSend[5]; // Voltage Cell 6
- msgBuffer->data[6] = CANToSend[6]; // Voltage Cell 6
- msgBuffer->data[7] = CANToSend[7]; // Temp too high flag (boolean)
- CANUpdateChannel(CAN1,CAN_CHANNEL0);
- CANFlushTxChannel(CAN1,CAN_CHANNEL0);
- }
- }
- void Nops(uint32_t n) {
- uint32_t i;
- for (i = 0; i < n; i++) {
- Nop();
- }
- }
- void DelayMs(unsigned int msec)
- {
- unsigned int tWait, tStart;
- tWait=(SYS_FREQ/2000)*msec;
- tStart=ReadCoreTimer();
- while((ReadCoreTimer()-tStart)<tWait); // wait for the time to pass
- }
Advertisement
Add Comment
Please, Sign In to add comment