Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* analog_mux_get_latest_val
- * Created on: Jul 4, 2019
- * Author: Samuel Martel
- * Description: Gets the latest values from the 3 ADCs if the conversion on
- * all of them is completed. If not, the function returns -1.
- * After reading each value, it is shifted 4 bits to the left
- * to convert it to an unsigned char (ADC values are 12bits).
- * Then, each ADC is stopped, the is_ready flag is clear to allow
- * a new conversion to be started.
- * Finally, the index of the first value is calculated by
- * converting its 2D ((x, y), ([0..19],[0..7])) to a 1D index that
- * ranges from 0 to 160, the p_row and p_column pointers are set,
- * and then the columns are shifted once.
- * The index is then returned.
- * Params: * unsigned char* f0 : pointer to the variable of the 1st value.
- * * unsigned char* f1 : pointer to the variable of the 2nd value.
- * * unsigned char* f2 : pointer to the variable of the 3rd value.
- * * unsigned int* p_row : pointer to the variable of the current row.
- * * unsigned int* p_column : pointer to the variable of the current
- * column.
- * Return: int : if error, -1. If successful, returns the current index.
- */
- int analog_mux_get_latest_val(unsigned char* f0, unsigned char* f1,
- unsigned char* f2, unsigned int* p_row, unsigned int* p_column) {
- // If not ADCs are not ready to be read:
- if (!is_ready)
- // Return -1.
- return -1;
- // Read value from ADC 1.
- unsigned long val1 = HAL_ADC_GetValue(&hadc1);
- // Cast it to unsigned char.
- *f0 = (unsigned char) (val1 >> 4);
- // Read value from ADC 2.
- unsigned long val2 = HAL_ADC_GetValue(&hadc2);
- // Cast it to unsigned char.
- *f1 = (unsigned char) (val2 >> 4);
- // Read value from ADC 3.
- unsigned long val3 = HAL_ADC_GetValue(&hadc3);
- // Cast it to unsigned char.
- *f2 = (unsigned char) (val3 >> 4);
- // Stop the 3 ADCs.
- HAL_ADC_Stop_IT(&hadc1);
- HAL_ADC_Stop_IT(&hadc2);
- HAL_ADC_Stop_IT(&hadc3);
- // Clear flag to allow a new conversion to begin.
- is_ready = NOT_READY;
- // Calculate the index of the first value in the buffer.
- int idx = column + (row * 20);
- *p_row = row;
- *p_column = column;
- // Next set of rows.
- row++;
- // If every rows of the current column have been read:
- if (row >= 8) {
- // Reset the current row to 0.
- row = 0;
- // Shift to the next column.
- column = shift_shift_registers();
- }
- // Return the current index.
- return idx;
- }
Advertisement
Add Comment
Please, Sign In to add comment