cancel
Showing results for 
Search instead for 
Did you mean: 

CAN bus calculated data is wrong, preset data is right

CAlbe.2
Associate II

Good day

I am sending a CAN message from a STM32F042. The first two bytes are the value read from an ADC split into 2 bytes. Then just for testing I set 4 bytes to "F042" which is then a fixed value that i know its sending what I ask. The problem I have is no matter how i format the ADC value its never correct on the CAN BUS even though my RxData that i pass to can shows it is correct. Any value that I put into RxData that is fixed such as the F042 is always correct.

I believe based on on the fact that I can fill all 8 bytes with fixed data like F042 and it always transmits as recieves correctly that my harware is good (My harness, a twisted pair cable with 120ohm termination, and both boards), for me that means it must be something in code of the F042 is going wrong.

This is the section of code described above

  ADC_Select_CH4();
  HAL_ADC_Start(&hadc);
  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
  HAL_ADC_Stop(&hadc);
  temp = HAL_ADC_GetValue(&hadc);
  remainder100 = temp%100;
  TxData[0] = remainder100;
  count100 = (temp-remainder100)/100;
  TxData[1] = count100;

  TxData[3] = 'F';
  TxData[4] = '0';
  TxData[5] = '4';
  TxData[6] = '2';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox);

Ive attached a screenshot of the logic analizer, the RxData from cubeide and the main.c file.

1 ACCEPTED SOLUTION

Accepted Solutions
CAlbe.2
Associate II

I am not 100% sure what solved this, butni made the variable that i am putting into the TxData a pointer, it came out wrong for obvious reasons but for the first time i had a value from a variable that was the same in TxData and is was on the logic analizer. I removed the '&' for the pointer, reuploaded and it was correct from then. THe only thing that i suspect is that I was very close to the maximum memory of the MCU, maybe by just reducing the program with one line somewhere i got a more stable program (Just a guess though). But it works now so I am happy. Got a lot of programming to do now but no problems at least.

View solution in original post

4 REPLIES 4

The RxData is not shown.

Byte 0 should have range 0..99

Byte 1 should have range 0..40

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
CAlbe.2
Associate II

Hi, it's not in loop back mode, so the rxdata from this microcontroller won't mean anything. What can be seen in the logic analizer is what the other mcu actually receives.

SofLit
ST Employee

Hello,

As you receive fixed data correctly, it's not a CAN issue. It's more related to C programming. 

You need to re-check these lines. Watch them in debug before sending and see if they are what you expected.

  temp = HAL_ADC_GetValue(&hadc);
  remainder100 = temp%100;
  TxData[0] = remainder100;
  count100 = (temp-remainder100)/100;
  TxData[1] = count100

Check if you see the same data in debug as what you see with the logic analyzer.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
CAlbe.2
Associate II

I am not 100% sure what solved this, butni made the variable that i am putting into the TxData a pointer, it came out wrong for obvious reasons but for the first time i had a value from a variable that was the same in TxData and is was on the logic analizer. I removed the '&' for the pointer, reuploaded and it was correct from then. THe only thing that i suspect is that I was very close to the maximum memory of the MCU, maybe by just reducing the program with one line somewhere i got a more stable program (Just a guess though). But it works now so I am happy. Got a lot of programming to do now but no problems at least.