/****************************************************************************** * @fn halJoystickGetDir * * @brief * This function utilizes the ADC to give an indication of the current * position of the joystick. Current support is for 90 degrees * positioning only. * * The joystick control is encoded as an analog voltage. Keep on reading * the ADC until two consecutive key decisions are the same. * * Meassured values from the ADC: * ------------------------------------------- * |Direction | Voltage | Value | Range | * ------------------------------------------- * |UP | 0.31 V | 0x0D | 0x00-0x27 | * |DOWN | 1.16 V | 0x31 | 0x28-0x3B | * |LEFT | 1.62 V | 0x45 | 0x3C-0x49 | * |RIGHT | 1.81 V | 0x4D | 0x4A-0x53 | * |CENTER | 2.12 V | 0x5A | 0x54- | * ------------------------------------------- * * Parameters: * * @param void * * @return uint8 * DOWN: Joystick direction is down (270 degrees) * LEFT: Joystick direction is left (180 degrees) * RIGHT: Joystick direction is right (0 degrees) * UP: Joystick direction is up (90 degrees) * CENTER: Joystick direction is centred (passive position) * ******************************************************************************/ uint8 halJoystickGetDir(void) { uint16 adcValue; uint8 direction, directionOld; do { directionOld = direction; adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_9_BIT, \ HAL_BOARD_IO_JOYSTICK_ADC_CH); // Only use 7 out of the 9 bits adcValue = (adcValue & 0x7FC0) >> 8; if (adcValue < 0x28) { direction = HAL_JOYSTICK_EVT_UP; } else if (adcValue < 0x3C) { direction = HAL_JOYSTICK_EVT_DOWN; } else if (adcValue < 0x4A) { direction = HAL_JOYSTICK_EVT_LEFT; } else if (adcValue < 0x54) { direction = HAL_JOYSTICK_EVT_RIGHT; } else { direction = HAL_JOYSTICK_EVT_CENTER; } } while(direction != directionOld); return direction; }
/*********************************************************************************** * @fn getSamples * * @brief Get ADC Samples of Specified Channel and Average * * @param none * * @return none */ unsigned int getSamples(unsigned int chan) { unsigned char loop; unsigned int avg = 0; // Clear average variable for (loop=0;loop<NUMSAMPLES;loop++) { // Loop avg = avg + adcSampleSingle(ADC_REF_AVDD,ADC_12_BIT,chan); // Accumulate samples } avg = avg >> SAMPLESHIFT; // Calculate average return avg; // Return average }
/************************************************************************************ * @fn halJoystickPushed * * @brief * This function detects if the joystick is being pushed. The function * implements software debounce. Return true only if previuosly called * with joystick not pushed. Return true only once each time the joystick * is pressed. * * Parameters: * * @param void * * @return uint8 * 1: Button is being pushed * 0: Button is not being pushed * ******************************************************************************/ uint8 halJoystickPushed(void) { uint8 value, active; uint8 i; static uint8 prevValue = 0; uint16 adcValue; // Criterion for button pushed: // 3 times joystick active and in center position value = 1; for (i=0; i<3; i++) { active = MCU_IO_GET(HAL_BOARD_IO_JOY_MOVE_PORT, HAL_BOARD_IO_JOY_MOVE_PIN); adcValue = adcSampleSingle(ADC_REF_AVDD, ADC_9_BIT, \ HAL_BOARD_IO_JOYSTICK_ADC_CH); // Only use 7 out of the 9 bits adcValue = (adcValue & 0x7FC0) >> 8; if (! active || adcValue < 0x54) { // Joystick not active or not in center position value = 0; break; } halMcuWaitUs(3); } if (value){ if (!prevValue){ value = prevValue = 1; halMcuWaitMs(100); } else { value = 0; } } else{ prevValue = 0; } return value; }