smartel99

STM32 - analog_mux_get_latest_val

Jul 26th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. /* analog_mux_get_latest_val
  2.  * Created on: Jul 4, 2019
  3.  * Author: Samuel Martel
  4.  * Description: Gets the latest values from the 3 ADCs if the conversion on
  5.  *              all of them is completed. If not, the function returns -1.
  6.  *              After reading each value, it is shifted 4 bits to the left
  7.  *              to convert it to an unsigned char (ADC values are 12bits).
  8.  *              Then, each ADC is stopped, the is_ready flag is clear to allow
  9.  *              a new conversion to be started.
  10.  *              Finally, the index of the first value is calculated by
  11.  *              converting its 2D ((x, y), ([0..19],[0..7])) to a 1D index that
  12.  *              ranges from 0 to 160, the p_row and p_column pointers are set,
  13.  *              and then the columns are shifted once.
  14.  *              The index is then returned.
  15.  * Params:  * unsigned char* f0 : pointer to the variable of the 1st value.
  16.  *          * unsigned char* f1 : pointer to the variable of the 2nd value.
  17.  *          * unsigned char* f2 : pointer to the variable of the 3rd value.
  18.  *          * unsigned int* p_row : pointer to the variable of the current row.
  19.  *          * unsigned int* p_column : pointer to the variable of the current
  20.  *                                     column.
  21.  * Return:  int : if error, -1. If successful, returns the current index.
  22.  */
  23. int analog_mux_get_latest_val(unsigned char* f0, unsigned char* f1,
  24.         unsigned char* f2, unsigned int* p_row, unsigned int* p_column) {
  25.     // If not ADCs are not ready to be read:
  26.     if (!is_ready)
  27.         // Return -1.
  28.         return -1;
  29.  
  30.     // Read value from ADC 1.
  31.     unsigned long val1 = HAL_ADC_GetValue(&hadc1);
  32.     // Cast it to unsigned char.
  33.     *f0 = (unsigned char) (val1 >> 4);
  34.    
  35.     // Read value from ADC 2.
  36.     unsigned long val2 = HAL_ADC_GetValue(&hadc2);
  37.     // Cast it to unsigned char.
  38.     *f1 = (unsigned char) (val2 >> 4);
  39.    
  40.     // Read value from ADC 3.
  41.     unsigned long val3 = HAL_ADC_GetValue(&hadc3);
  42.     // Cast it to unsigned char.
  43.     *f2 = (unsigned char) (val3 >> 4);
  44.    
  45.     // Stop the 3 ADCs.
  46.     HAL_ADC_Stop_IT(&hadc1);
  47.     HAL_ADC_Stop_IT(&hadc2);
  48.     HAL_ADC_Stop_IT(&hadc3);
  49.  
  50.     // Clear flag to allow a new conversion to begin.
  51.     is_ready = NOT_READY;
  52.  
  53.     // Calculate the index of the first value in the buffer.
  54.     int idx = column + (row * 20);
  55.     *p_row = row;
  56.     *p_column = column;
  57.    
  58.     // Next set of rows.
  59.     row++;
  60.     // If every rows of the current column have been read:
  61.     if (row >= 8) {
  62.         // Reset the current row to 0.
  63.         row = 0;
  64.         // Shift to the next column.
  65.         column = shift_shift_registers();
  66.     }
  67.    
  68.     // Return the current index.
  69.     return idx;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment