Page 1 of 1

Read Voltage from Analog In

Posted: Tue 5. Dec 2023, 12:51
by tibeilf
Hi,

I want to read a Voltage from the Analog In 1 Pin and output that Voltage in a CAN message.
How do I do this using the PCAN Router Pro FD?

Best regards,
Tim

Re: Read Voltage from Analog In

Posted: Wed 6. Dec 2023, 13:52
by PEAK-Support
if you take a look into the Sample 08_HW_IO and check in the main.c / line 120 , you will find this code

Code: Select all

// clear EOC flag by read the ADC2 register
TempVar = ADC2->DR & 0xFFF;
ADC2->SQR3 = 6;
ADC2->CR2 |= 1 << 30;
// wait EOC flag
while ( !( ADC2->SR & ( 1 << 1)))
 {}
ResultData = ADC2->DR & 0xFFF;
The AD have 12Bit, and the Result is 8.13mV per Digit.
We will extend this in the next Version to an own Function like the DIn.

Re: Read Voltage from Analog In

Posted: Wed 10. Jan 2024, 10:01
by PEAK-Support
We have add the HW_GetAIN to the code - so now you simply call the function to get the Analogue value.
Here a sample - CAN ID 0x55A / STD Msg, trigger the function and send the result on 0x55B in the first 16 Bits (Byte 0 and 1)

Code: Select all

uint32_t  tmp32;
// catch ID 55Ah to send analog value from AIN
if ( RxMsg.id == 0x55A  &&  RxMsg.msgtype == CAN_MSGTYPE_STANDARD)
{
  HW_GetAIN ( &tmp32);
  RxMsg.id = RxMsg.id + 1;
  RxMsg.data16[0] = tmp32;
  CAN_Write ( CAN_BUS1, &RxMsg);
}
Here the new Block in "hardware.h"

Code: Select all

//! @brief      get AIN from I/O pins
//!
//! @param      buffer  result buffer (single uint32_t)
//!
//! @return             one error of HW_ERR_...
HWResult_t  HW_GetAIN ( uint32_t  *buffer);


//! @brief
//! Read digital inputs. Each bit will represent a digital pin.
//!
//! @param		buffer		Buffer for DIN-value
//!
//! @return		one error of HW_ERR_...
HWResult_t  HW_GetDIN ( uint32_t  *buffer);


//! @brief
//! Set digital outputs. Each bit will represent a digital pin.
//! 
//! @param		buffer		Buffer for DOUT-value
//!
//! @return		one error of HW_ERR_...
HWResult_t  HW_SetDOUT ( uint32_t  *buffer);