Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The code below is for the transmission NUCLEO
- Written by Riccardo Geraci
- University of Wolverhampton
- Embedded System Design: Developing a CAN BUS diagnostics module for a car
- Oct 2018 - April 2019
- Research Srcs: https://os.mbed.com/users/WiredHome/notebook/can---getting-started/
- Averaging is used to filter out the 'wiggle' from the LM35's output. When the code is first
- compiled there are no samples
- */
- #include "mbed.h" // Mbed library
- bool DEBUG_MODE = false; //Bool used to shows printf statements to the console
- Ticker ticker; //Ticker object used for looping interrupt
- CAN can(PA_11, PA_12); //rx, tx, CAN object with the rx and tx pins
- DigitalOut tx_can_stat_led(PA_10); //Led on the PCB used for showing can activity
- DigitalOut tx_power_stat_led(PA_9); //Led on the PCB used for showing the the pcb is powered
- AnalogIn analog_value(PA_0); //init pin for lm35 output
- const int calib = 28; //calibration constant to subtract from the
- //sensed temperature to match actual temperature
- float meas; //variable to hold the analogue voltage from pin PA_0
- float temp; //variable to hold the temperature in C after ADC
- float averageTemp; //variable to hold the average temperature
- float temps[8]; //Array with a length of 8 used to take the average temperature
- int position = 0; //variable used to cycle through temps array later
- float getLM35Temperature(){ //function to return the LM35's temperature
- meas = analog_value.read(); //Set the meas variable to signal coming in PA_0
- temp = ((meas*5000/10) - calib); //Convert the analogue value 0 to 1 to 0 to 5.
- //Divided by 10 to get 1 degree output, subtracting the calib constant to bring
- //the temperature to the correct range
- temps[position] = temp; //Set nth position of the temps array to the current
- //temperature after conversion
- //To avoid going outside the bounds of the array, the position variable is reset each
- //time is reaches 7
- if (position==7) {
- position =0;
- } else {
- position++;
- }
- averageTemp = 0; //Reset the averageTemp variable so the
- //last average doesn't affect the current average
- for (int i=0; i<8; i++) {
- averageTemp += temps[i] /8; //loop throught the 8 temperature readings
- //and take the average
- }
- if (DEBUG_MODE){ //Lines used for debugging, display on the console via serial
- //led = temp > 21; //Light the led when tempature passes 21^C
- printf("volts=%f\ttemperature = %.0f^C\taverage= %.0f^C\r\n", meas, temp, averageTemp);
- wait(.01);
- }
- return averageTemp; //When the getLM35Temperature() function is called
- //this is the value that will be given back, or subsituted
- // }
- }
- void sendCANMsg(){ //function to send lm35 temperature as a CAN message
- char currTemp = getLM35Temperature(); //Variable to hold the average temperature
- if(can.write(CANMessage(1337, &currTemp, 1))){ //(Random Identifier 11-bit defaultly, pointer to data, num of bits to send)
- //above returns true if msg successfully sent (network good, outbound buffer not full, etc)
- if (DEBUG_MODE){
- printf("Message sent (temperature): %d\r\n", currTemp); //Debug code for console
- }
- wait(0.2); //delay to stop bus flooding with 1337 msg, hogging, allows other msgs to have a chance
- tx_can_stat_led = !tx_can_stat_led; // Blink the CAN_LED to show activity
- }
- }
- int main() //Main entry point function required by C/C++
- {
- tx_power_stat_led = 1; //Show the power led of the PCB
- ticker.attach(&sendCANMsg, 1); //reccuring interrupt, repeating every 1 second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement