Advertisement
TheVideoVolcano

Tx (Oil Temp) - STM32 NUCLEO32-303K8

Jul 15th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. /*
  2. The code below is for the transmission NUCLEO
  3. Written by Riccardo Geraci
  4. University of Wolverhampton
  5. Embedded System Design: Developing a CAN BUS diagnostics module for a car
  6. Oct 2018 - April 2019
  7. Research Srcs: https://os.mbed.com/users/WiredHome/notebook/can---getting-started/
  8.  
  9. Averaging is used to filter out the 'wiggle' from the LM35's output. When the code is first
  10. compiled there are no samples
  11. */
  12.  
  13. #include "mbed.h" // Mbed library
  14.  
  15. bool DEBUG_MODE = false; //Bool used to shows printf statements to the console
  16.  
  17. Ticker ticker; //Ticker object used for looping interrupt
  18. CAN can(PA_11, PA_12); //rx, tx, CAN object with the rx and tx pins
  19.  
  20. DigitalOut tx_can_stat_led(PA_10); //Led on the PCB used for showing can activity
  21. DigitalOut tx_power_stat_led(PA_9); //Led on the PCB used for showing the the pcb is powered
  22.  
  23. AnalogIn analog_value(PA_0); //init pin for lm35 output
  24.  
  25. const int calib = 28; //calibration constant to subtract from the
  26. //sensed temperature to match actual temperature
  27.  
  28. float meas; //variable to hold the analogue voltage from pin PA_0
  29. float temp; //variable to hold the temperature in C after ADC
  30. float averageTemp; //variable to hold the average temperature
  31. float temps[8]; //Array with a length of 8 used to take the average temperature
  32. int position = 0; //variable used to cycle through temps array later
  33.    
  34. float getLM35Temperature(){ //function to return the LM35's temperature
  35.  
  36.         meas = analog_value.read(); //Set the meas variable to signal coming in PA_0
  37.         temp = ((meas*5000/10) - calib); //Convert the analogue value 0 to 1 to 0 to 5.
  38.         //Divided by 10 to get 1 degree output, subtracting the calib constant to bring
  39.         //the temperature to the correct range
  40.  
  41.         temps[position] = temp; //Set nth position of the temps array to the current
  42.         //temperature after conversion
  43.  
  44.         //To avoid going outside the bounds of the array, the position variable is reset each
  45.         //time is reaches 7
  46.         if (position==7) {
  47.             position =0;
  48.         } else {
  49.             position++;
  50.         }
  51.  
  52.         averageTemp = 0; //Reset the averageTemp variable so the
  53.         //last average doesn't affect the current average
  54.        
  55.  
  56.         for (int i=0; i<8; i++) {
  57.             averageTemp += temps[i] /8; //loop throught the 8 temperature readings
  58.             //and take the average
  59.         }
  60.    
  61.     if (DEBUG_MODE){ //Lines used for debugging, display on the console via serial
  62.          //led = temp > 21; //Light the led when tempature passes 21^C
  63.          printf("volts=%f\ttemperature = %.0f^C\taverage= %.0f^C\r\n", meas, temp, averageTemp);
  64.          wait(.01);
  65.         }
  66.        
  67.         return averageTemp; //When the getLM35Temperature() function is called
  68.         //this is the value that will be given back, or subsituted
  69.  
  70.    // }
  71. }
  72.  
  73. void sendCANMsg(){ //function to send lm35 temperature as a CAN message
  74.    char currTemp = getLM35Temperature(); //Variable to hold the average temperature
  75.     if(can.write(CANMessage(1337, &currTemp, 1))){  //(Random Identifier 11-bit defaultly, pointer to data, num of bits to send)
  76.     //above returns true if msg successfully sent (network good, outbound buffer not full, etc)
  77.     if (DEBUG_MODE){
  78.         printf("Message sent (temperature): %d\r\n", currTemp); //Debug code for console
  79.         }
  80.         wait(0.2); //delay to stop bus flooding with 1337 msg, hogging, allows other msgs to have a chance
  81.  
  82.     tx_can_stat_led = !tx_can_stat_led; // Blink the CAN_LED to show activity
  83.     }
  84. }
  85.  
  86. int main() //Main entry point function required by C/C++
  87. {
  88.     tx_power_stat_led = 1; //Show the power led of the PCB
  89.     ticker.attach(&sendCANMsg, 1); //reccuring interrupt, repeating every 1 second
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement