Guest User

ADS1148 w/ PIC32 Code

a guest
May 8th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.35 KB | None | 0 0
  1. #include <p32xxxx.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>  
  4. #include <xc.h>  
  5. #include <math.h>
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9.  
  10. /* Adds support for PIC32 Peripheral library functions and macros */
  11. #define _SUPPRESS_PLIB_WARNING
  12. #define SYS_FREQ        (80000000)
  13.  
  14. #include <plib.h>
  15. #include "adc.h"
  16. #include "can.h"
  17.  
  18. // Configuration Bits
  19. #pragma config FPLLIDIV = DIV_2         // 2
  20. #pragma config FPLLMUL  = MUL_20        // 20
  21. #pragma config FPLLODIV = DIV_1
  22. #pragma config FWDTEN   = OFF
  23. #pragma config POSCMOD  = XT
  24. #pragma config FNOSC    = FRCPLL
  25. #pragma config FPBDIV   = DIV_1
  26. #pragma config FCKSM    = CSECME
  27.  
  28.  
  29. void main(){
  30.     SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
  31.     DelayMs(20);
  32.     mPORTBSetPinsDigitalOut(BIT_7);
  33.     mPORTBSetBits(BIT_7);
  34.     mPORTBSetPinsDigitalOut(BIT_3);
  35.     mPORTBSetBits(BIT_3);
  36.     adc_init();
  37.     DelayMs(200);
  38.     can_init();
  39. //    bool Front1Rear0 = 1;
  40. //    unsigned int CAN_ID_RF;
  41. //    unsigned int CAN_ID_RS;
  42. //    unsigned int CAN_ID_LF;
  43. //    unsigned int CAN_ID_LS;
  44. //    if(Front1Rear0){
  45. //        CAN_ID_RF = 0x200;
  46. //        CAN_ID_RS = 0x201;
  47. //        CAN_ID_LF = 0x202;
  48. //        CAN_ID_LS = 0x203;
  49. //    }  
  50. //    else {
  51. //        CAN_ID_RF = 0x210;
  52. //        CAN_ID_RS = 0x211;
  53. //        CAN_ID_LF = 0x212;
  54. //        CAN_ID_LS = 0x213;
  55. //    }
  56.     while(1){
  57.             while(PORTBbits.RB4);
  58.                SpiChnPutC(SPI_CHN_R, 0);
  59.                     CANToSend[0] = SpiChnGetC(SPI_CHN_R);      //Caliper Temp 1
  60.             while(PORTBbits.RB4);
  61.                 SpiChnPutC(SPI_CHN_R, 0);
  62.                     CANToSend[1] = SpiChnGetC(SPI_CHN_R);   //Caliper Temp 2
  63.             while(PORTBbits.RB4);
  64.                 SpiChnPutC(SPI_CHN_R, 0);
  65.                     CANToSend[2] = SpiChnGetC(SPI_CHN_R);      //Rotor Temp 1
  66.             while(PORTBbits.RB4);
  67.                 SpiChnPutC(SPI_CHN_R, 0);
  68.                     CANToSend[3] = SpiChnGetC(SPI_CHN_R);   //Rotor Temp 2
  69.             while(PORTBbits.RB4);
  70.                 SpiChnPutC(SPI_CHN_R, 0);
  71.                     CANToSend[4] = SpiChnGetC(SPI_CHN_R);      //Brake Pad Temp 1
  72.             while(PORTBbits.RB4);
  73.                 SpiChnPutC(SPI_CHN_R, 0);
  74.                     CANToSend[5] = SpiChnGetC(SPI_CHN_R);   //Brake Pad Temp 2
  75.             while(PORTBbits.RB4);
  76.                 SpiChnPutC(SPI_CHN_R, 0);
  77.                     CANToSend[6] = SpiChnGetC(SPI_CHN_R);    
  78.            while(PORTBbits.RB4);
  79.                 SpiChnPutC(SPI_CHN_R, 0);
  80.                     CANToSend[7] = SpiChnGetC(SPI_CHN_R);
  81.                        
  82.         CAN1TxSendMsg(0x201);  
  83.     }
  84. }
  85.  
  86. #define LED_SPI_SPEED 1000000 //1000000
  87. #define SPI_CHN_L SPI_CHANNEL2
  88. #define SPI_CHN_R SPI_CHANNEL3
  89.  
  90. static uint16_t adc_data = 0;
  91.  
  92. /**
  93.  * Initialize the LED driver.
  94.  */
  95. void adc_init()
  96. {
  97.     // set up SPI
  98.     SpiChnOpen(
  99.         SPI_CHN_R,
  100.         SPI_OPEN_MSTEN | SPI_OPEN_MODE8 | SPI_OPEN_CKE_REV,
  101.         80000000/LED_SPI_SPEED
  102.     );
  103.    
  104.     SpiChnOpen(
  105.         SPI_CHN_L,
  106.         SPI_OPEN_MSTEN | SPI_OPEN_MODE8 | SPI_OPEN_CKE_REV,
  107.         80000000/LED_SPI_SPEED
  108.     );
  109.    
  110.     //Sets and keeps SS pins low for both left and right SPIs
  111.     AD1PCFGbits.PCFG8 = 1;
  112.     mPORTBSetPinsDigitalIn(BIT_8);  //DRDY in
  113.     mPORTDSetPinsDigitalOut(BIT_9); //CS out
  114.     mPORTBSetPinsDigitalOut(BIT_7); //Start out
  115.     mPORTBSetPinsDigitalOut(BIT_9); //Reset out
  116.     mPORTDSetBits(BIT_9);           //CS High (off))
  117.     mPORTBSetBits(BIT_7);           //Start High
  118.     mPORTBSetBits(BIT_9);           //Reset High
  119.    
  120.    
  121.     SpiChnPutC(SPI_CHN_R, 0x06); //Resets ADC to reliably set pins next
  122.     SpiChnGetC(SPI_CHN_R);
  123. //    SpiChnPutC(SPI_CHN_L, 0x06); //Resets ADC to reliably set pins next
  124. //    SpiChnGetC(SPI_CHN_L);
  125.     DelayMs(1);
  126.     SpiChnPutC(SPI_CHN_R, 0x16); //Disables ADC Data reading
  127.     SpiChnGetC(SPI_CHN_R);
  128. //    SpiChnPutC(SPI_CHN_L, 0x16); //Disables ADC Data reading
  129. //    SpiChnGetC(SPI_CHN_L);
  130.     DelayMs(1);
  131.     SpiChnPutC(SPI_CHN_R, 0x04); //Syncs ADC conversion
  132.     SpiChnGetC(SPI_CHN_R);
  133. //    SpiChnPutC(SPI_CHN_L, 0x04); //Syncs ADC conversion
  134. //    SpiChnGetC(SPI_CHN_L);
  135.         DelayMs(1);
  136.     SpiChnPutC(SPI_CHN_R, 0x16); //Disables Continuous ADC Data reading
  137.     SpiChnGetC(SPI_CHN_R);
  138. //    SpiChnPutC(SPI_CHN_L, 0x16); //Disables Continuous ADC Data reading
  139. //    SpiChnGetC(SPI_CHN_L);
  140.     mPORTDSetBits(BIT_9); //Resets the Serial Interface
  141.     mPORTGSetBits(BIT_9);
  142.    
  143.    
  144.     //This sets all initial properties for reliable use
  145.     SpiChnPutC(SPI_CHN_R, 0x40); //Starts writing to ADC registers starting with Mux0
  146.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  147.     SpiChnPutC(SPI_CHN_R, 0x03); //Sets number of bytes to write plus 1
  148.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  149.     SpiChnPutC(SPI_CHN_R, 0x08); //Mux0 is set to 00001000, AKA no Burnout detect, positive input is pin 1, negative input is pin 0
  150.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  151.     SpiChnPutC(SPI_CHN_R, 0x00); //Sets voltage biases all to 0
  152.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  153.     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
  154.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  155.     SpiChnPutC(SPI_CHN_R, 0x00); //SYS0 is set to 00000000, AKA Gain setting of 1 and a data output rate of 2000 samples/second
  156.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  157.    
  158.     //Simple Attempt Below
  159. //        SpiChnPutC(SPI_CHN_R, 0x42); //Starts writing to ADC registers starting with Mux0
  160. //    SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  161. //    SpiChnPutC(SPI_CHN_R, 0x01); //Sets number of bytes to write minus 1
  162. //    SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  163. //    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
  164. //    SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  165. //    SpiChnPutC(SPI_CHN_R, 0x09); //SYS0 is set to 00001001, AKA Gain setting of 1 and a data output rate of 2000 samples/second
  166. //    SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  167. //    
  168.    
  169.     DelayMs(10);
  170. //    
  171. //    SpiChnPutC(SPI_CHN_L, 0x40); //Starts writing to ADC registers starting with Mux0
  172. //    SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  173. //    SpiChnPutC(SPI_CHN_L, 0x05); //Sets number of bytes to write minus 1
  174. //    SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  175. //    SpiChnPutC(SPI_CHN_L, 0x08); //Mux0 is set to 00001000, AKA no Burnout detect, positive input is pin 1, negative input is pin 0
  176. //    SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  177. //    SpiChnPutC(SPI_CHN_L, 0x00); //Sets voltage biases all to 0
  178. //    SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  179. //    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
  180. //    SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  181. //    SpiChnPutC(SPI_CHN_L, 0x08); //SYS0 is set to 00001000, AKA Gain setting of 1 and a data output rate of 1000 samples/second
  182. //    SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  183.    
  184.      mPORTBClearBits(BIT_7);
  185. }
  186.  
  187. uint16_t adc_read_R(unsigned int ANALOG_PIN)
  188. {
  189.     unsigned int PinToHex;
  190.     if(ANALOG_PIN == 1){
  191.         PinToHex = 0x08;
  192.     } else if(ANALOG_PIN == 2){
  193.         PinToHex = 0x10;
  194.     }  else if(ANALOG_PIN == 3){
  195.         PinToHex = 0x18;
  196.     } else if(ANALOG_PIN == 4){
  197.         PinToHex = 0x20;
  198.     } else if(ANALOG_PIN == 5){
  199.         PinToHex = 0x28;
  200.     } else if(ANALOG_PIN == 6){
  201.         PinToHex = 0x30;
  202.     } else if(ANALOG_PIN == 7){
  203.         PinToHex = 0x38;
  204.     }
  205.     mPORTDClearBits(BIT_9);
  206. //    SpiChnPutC(SPI_CHN_R, 0x40);
  207. //    SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  208. //    SpiChnPutC(SPI_CHN_R, 0x00);
  209. //    SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  210. //    SpiChnPutC(SPI_CHN_R, PinToHex);
  211. //    SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  212.     mPORTDSetBits(BIT_9);
  213.      mPORTBSetBits(BIT_7);
  214.      Nops(5);
  215.      mPORTBClearBits(BIT_7);
  216.     while(PORTBbits.RB8); //wait for DRDY pin to go low B8 for right, b4 for left
  217.     mPORTDClearBits(BIT_9);
  218.     SpiChnPutC(SPI_CHN_R, 0x12);
  219.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  220.     SpiChnPutC(SPI_CHN_R, 0xFF);
  221.     uint8_t msb = SpiChnGetC(SPI_CHN_R);
  222.     SpiChnPutC(SPI_CHN_R, 0xFF);
  223.     uint8_t lsb = SpiChnGetC(SPI_CHN_R);
  224.     adc_data = msb << 8 | lsb;
  225.     Nops(10);
  226.     return adc_data;
  227. }
  228.  
  229. uint16_t adc_read_L(unsigned int ANALOG_PIN)
  230. {
  231.     unsigned int PinToHex;
  232.     if(ANALOG_PIN == 1){
  233.         PinToHex = 0x08;
  234.     } else if(ANALOG_PIN == 2){
  235.         PinToHex = 0x10;
  236.     }  else if(ANALOG_PIN == 3){
  237.         PinToHex = 0x18;
  238.     } else if(ANALOG_PIN == 4){
  239.         PinToHex = 0x20;
  240.     } else if(ANALOG_PIN == 5){
  241.         PinToHex = 0x28;
  242.     } else if(ANALOG_PIN == 6){
  243.         PinToHex = 0x30;
  244.     } else if(ANALOG_PIN == 7){
  245.         PinToHex = 0x38;
  246.     }
  247.      mPORTGClearBits(BIT_9);
  248.     SpiChnPutC(SPI_CHN_L, 0x40); //Tells ADC we are going to change Mux0
  249.     SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  250.     SpiChnPutC(SPI_CHN_L, 0x02); //Tells ADC we are only changing 1 register
  251.     SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  252.     SpiChnPutC(SPI_CHN_L, PinToHex); //Tells Mux0 that we want whatever pin is referenced in PinToHex
  253.     SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  254.     while(PORTBbits.RB4); //wait for DRDY pin to go low B8 for right, b4 for left
  255.     SpiChnPutC(SPI_CHN_L, 0x12); //Tells ADC we want to read data
  256.     SpiChnGetC(SPI_CHN_L);  // ignore, zeros
  257.     SpiChnPutC(SPI_CHN_L, 0xFF); //Nops sent to ADC to pull out data
  258.     uint8_t msb = SpiChnGetC(SPI_CHN_L);
  259.     SpiChnPutC(SPI_CHN_L, 0xFF);
  260.     uint8_t lsb = SpiChnGetC(SPI_CHN_L);
  261.     adc_data = msb << 8 | lsb;
  262.     Nops(10);
  263.     return adc_data;
  264. }
  265.  
  266. void Right_Fast()
  267. {
  268.     CANToSend[0] = adc_read_R(1) >> 8;      //Wheel Speed 1
  269.     CANToSend[1] = adc_read_R(1) && 0xFF;   //Wheel Speed 2
  270.     CANToSend[2] = adc_read_R(2) >> 8;      //Load Cell 1
  271.     CANToSend[3] = adc_read_R(2) && 0xFF;   //Load Cell 2
  272.     CANToSend[4] = adc_read_R(3) >> 8;      //Damper Pot 1
  273.     CANToSend[5] = adc_read_R(3) && 0xFF;   //Damper Pot 2
  274.     CANToSend[6] = adc_read_R(4) >> 8;      //Tire Temp 1
  275.     CANToSend[7] = adc_read_R(4) && 0xFF;   //Tire Temp 2
  276. }
  277.  
  278. void Left_Fast()
  279. {
  280.     CANToSend[0] = adc_read_L(1) >> 8;      //Wheel Speed 1
  281.     CANToSend[1] = adc_read_L(1) && 0xFF;   //Wheel SPeed 2
  282.     CANToSend[2] = adc_read_L(2) >> 8;      //Load Cell 1
  283.     CANToSend[3] = adc_read_L(2) && 0xFF;   //Load Cell 2
  284.     CANToSend[4] = adc_read_L(3) >> 8;      //Damper Pot 1
  285.     CANToSend[5] = adc_read_L(3) && 0xFF;   //Damper Pot 2
  286.     CANToSend[6] = 0x00;      
  287.     CANToSend[7] = 0x00;
  288. }
  289.  
  290. void Right_Slow()
  291. {
  292.     CANToSend[0] = adc_read_R(5) >> 8;      //Caliper Temp 1
  293.     CANToSend[1] = adc_read_R(5) & 0xFF;   //Caliper Temp 2
  294.     CANToSend[2] = adc_read_R(6) >> 8;      //Rotor Temp 1
  295.     CANToSend[3] = adc_read_R(6) & 0xFF;   //Rotor Temp 2
  296.     CANToSend[4] = adc_read_R(7) >> 8;      //Brake Pad Temp 1
  297.     CANToSend[5] = adc_read_R(7) & 0xFF;   //Brake Pad Temp 2
  298.     CANToSend[6] = 0x00;      
  299.     CANToSend[7] = 0x00;
  300. }
  301.  
  302. void Left_Slow()
  303. {
  304.     CANToSend[0] = adc_read_L(4) >> 8;      //Caliper Temp 1
  305.     CANToSend[1] = adc_read_L(4) & 0xFF;   //Caliper Temp 2
  306.     CANToSend[2] = adc_read_L(5) >> 8;      //Rotor Temp 1
  307.     CANToSend[3] = adc_read_L(5) & 0xFF;   //Rotor Temp 2
  308.     CANToSend[4] = adc_read_L(6) >> 8;      //Brake Pad Temp 1
  309.     CANToSend[5] = adc_read_L(6) & 0xFF;   //Brake Pad Temp 2
  310.     CANToSend[6] = 0x00;      
  311.     CANToSend[7] = 0x00;
  312. }
  313.  
  314.  
  315. void testing_adc_init()
  316. {
  317.     // set up SPI
  318.     SpiChnOpen(
  319.         SPI_CHN_R,
  320.         SPI_OPEN_MSTEN | SPI_OPEN_MODE8 | SPI_OPEN_CKE_REV,
  321.         80000000/LED_SPI_SPEED
  322.     );
  323.    
  324.     //Sets and keeps SS pins low for both left and right SPIs
  325.     AD1PCFGbits.PCFG8 = 1;
  326.     mPORTBSetPinsDigitalIn(BIT_8);  //DRDY in
  327.     mPORTDSetPinsDigitalOut(BIT_9); //CS out
  328.     mPORTBSetPinsDigitalOut(BIT_7); //Start out
  329.     mPORTBSetPinsDigitalOut(BIT_9); //Reset out
  330.     mPORTDSetBits(BIT_9);           //CS High (off))
  331.     mPORTBSetBits(BIT_7);           //Start High
  332.     mPORTBSetBits(BIT_9);           //Reset High
  333. }
  334.  
  335. void testing_RREG(){
  336.     mPORTDClearBits(BIT_9);         //CS Low (on))
  337.         while(1){
  338.         DelayMs(100);
  339.      SpiChnPutC(SPI_CHN_R, 0x20);
  340.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  341.     SpiChnPutC(SPI_CHN_R, 0x03);
  342.     SpiChnGetC(SPI_CHN_R);  // ignore, zeros
  343.     int i;
  344.     for(i = 0; i < 4; i++){
  345.         mPORTDClearBits(BIT_9);
  346.         SpiChnPutC(SPI_CHN_R, 0xFF);
  347.         CANToSend[i] = SpiChnGetC(SPI_CHN_R);
  348.        
  349.         mPORTDSetBits(BIT_9);
  350.     }
  351.      CANToSend[4] = 0;
  352.           CANToSend[5] = 0;
  353.                CANToSend[6] = 0;
  354.                     CANToSend[7] = 0;
  355.         CAN1TxSendMsg(0x201);   //Send Left Fast from buffer
  356.         DelayMs(10);
  357.     }
  358. }
  359.  
  360. //void holder_main(){
  361. //        while(1){
  362. //    uint32_t i;
  363. //    for (i = 0; i < 20; i++) {
  364. //        Right_Fast();          //Load Right Fast info into CAN buffer, takes 175microseconds to run
  365. //        CAN1TxSendMsg(CAN_ID_RF);   //Send Right Fast from buffer
  366. ////        Left_Fast();            //Load Left Fast info into CAN buffer
  367. ////        CAN1TxSendMsg(CAN_ID_LF);   //Send Left Fast from buffer
  368. //        DelayMs(4);
  369. //    }
  370. //    Right_Slow();
  371. //  CAN1TxSendMsg(CAN_ID_RS);   //Send Right Fast from buffer
  372. ////    Left_Slow();
  373. ////    CAN1TxSendMsg(CAN_ID_LS);   //Send Right Fast from buffer
  374. //    DelayMs(4);
  375. //    }
  376. //}
  377.  
  378. #define CAN_BUS_SPEED 1000000
  379. #define BAUD_RATE 38400
  380.  
  381. BYTE CAN1MessageFifoArea[4 * 8 * 16]; // a bit oversized, 128 bits/msg instead of 108
  382. UINT8   dataBuffer[1024];
  383. CAN_RX_MSG_SID msgSID;
  384. int canData[8];
  385. int CANToSend[8] = {0};
  386.    
  387. void can_init(void)
  388. {
  389.     CAN_BIT_CONFIG canBitConfig;
  390.     //UINT baudPrescalar;
  391.  
  392.     CANEnableModule(CAN1,TRUE);
  393.  
  394.     CANSetOperatingMode(CAN1, CAN_CONFIGURATION);
  395.     while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION);         
  396.    
  397.     canBitConfig.phaseSeg2Tq            = CAN_BIT_3TQ;
  398.     canBitConfig.phaseSeg1Tq            = CAN_BIT_3TQ;
  399.     canBitConfig.propagationSegTq       = CAN_BIT_3TQ;
  400.     canBitConfig.phaseSeg2TimeSelect    = TRUE;
  401.     canBitConfig.sample3Time            = TRUE;
  402.     canBitConfig.syncJumpWidth          = CAN_BIT_2TQ;
  403.  
  404.     CANSetSpeed(CAN1,&canBitConfig,SYS_FREQ,CAN_BUS_SPEED);
  405.  
  406.     // sets aside memory for CAN buffer
  407.     CANAssignMemoryBuffer(CAN1,CAN1MessageFifoArea,(4 * 8 * 16)); // oversized
  408.    
  409.     // configures the transmit channel
  410.     CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY);
  411.  
  412.     CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION);
  413.     while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION);
  414. }
  415.  
  416. void CAN1TxSendMsg(int CAN_CHANNEL_VAR) //
  417. {
  418.     // Find a place to store the message until it is sent
  419.     CANTxMessageBuffer *msgBuffer;
  420.     msgBuffer = CANGetTxMessageBuffer(CAN1,CAN_CHANNEL0);
  421.    
  422.     if(msgBuffer != NULL)
  423.     {
  424. //        msgBuffer->messageWord[0] = 0;
  425. //        msgBuffer->messageWord[1] = 0;
  426. //        msgBuffer->messageWord[2] = 0;
  427.         msgBuffer->messageWord[3] = 0;
  428.  
  429.         msgBuffer->msgSID.SID = CAN_CHANNEL_VAR;   
  430.         msgBuffer->msgEID.IDE = 0;         
  431.         msgBuffer->msgEID.DLC = 8;         
  432.         msgBuffer->data[0]    = CANToSend[0];  // Voltage Cell 1 (unsigned integer)
  433.         msgBuffer->data[1]    = CANToSend[1];  // Voltage Cell 2 (unsigned integer)
  434.         msgBuffer->data[2]    = CANToSend[2];  // Voltage Cell 3 (unsigned integer)
  435.         msgBuffer->data[3]    = CANToSend[3];  // Voltage Cell 4 (unsigned integer)
  436.         msgBuffer->data[4]    = CANToSend[4];  // Voltage Cell 5 (unsigned integer)
  437.         msgBuffer->data[5]    = CANToSend[5];  // Voltage Cell 6
  438.         msgBuffer->data[6]    = CANToSend[6];  // Voltage Cell 6
  439.         msgBuffer->data[7]    = CANToSend[7];  // Temp too high flag (boolean)
  440.        
  441.         CANUpdateChannel(CAN1,CAN_CHANNEL0);
  442.         CANFlushTxChannel(CAN1,CAN_CHANNEL0);
  443.     }
  444. }
  445.  
  446. void Nops(uint32_t n) {
  447.     uint32_t i;
  448.     for (i = 0; i < n; i++) {
  449.         Nop();
  450.     }
  451. }
  452.  
  453. void DelayMs(unsigned int msec)
  454. {
  455.     unsigned int tWait, tStart;
  456.        
  457.     tWait=(SYS_FREQ/2000)*msec;
  458.     tStart=ReadCoreTimer();
  459.     while((ReadCoreTimer()-tStart)<tWait);      // wait for the time to pass
  460.  
  461. }
Advertisement
Add Comment
Please, Sign In to add comment