KeyScan Exported Functions

group KeyScan_Exported_Functions

Functions

void KeyScan_Init(KEYSCAN_TypeDef *KeyScan, KEYSCAN_InitTypeDef *KeyScan_InitStruct)

Initializes the KeyScan peripheral according to the specified parameters in the KeyScan_InitStruct.

Example usage

void driver_keyscan_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_KEYSCAN, APBPeriph_KEYSCAN_CLOCK, ENABLE);

    KEYSCAN_InitTypeDef KEYSCAN_InitStruct;
    KeyScan_StructInit(&KEYSCAN_InitStruct);

    KEYSCAN_InitStruct.rowSize  = KEYBOARD_ROW_SIZE;
    KEYSCAN_InitStruct.colSize  = KEYBOARD_COLUMN_SIZE;
    KEYSCAN_InitStruct.scanmode     = KeyScan_Manual_Scan_Mode;
    KEYSCAN_InitStruct.debounceEn   = vDebounce_En;
    KeyScan_Init(KEYSCAN, &KEYSCAN_InitStruct);
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • KeyScan_InitStruct[in] Pointer to a KEYSCAN_InitTypeDef structure that contains the configuration information for the specified KeyScan peripheral.

void KeyScan_DeInit(KEYSCAN_TypeDef *KeyScan)

Disable the KeyScan peripheral clock, and restore registers to their default values.

Example usage

void driver_keyscan_init(void)
{
    KeyScan_DeInit(KEYSCAN);
}

Parameters:

KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

void KeyScan_StructInit(KEYSCAN_InitTypeDef *KeyScan_InitStruct)

Fills each Keyscan_InitStruct member with its default value.

Example usage

void driver_keyscan_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_KEYSCAN, APBPeriph_KEYSCAN_CLOCK, ENABLE);

    KEYSCAN_InitTypeDef KEYSCAN_InitStruct;
    KeyScan_StructInit(&KEYSCAN_InitStruct);

    KEYSCAN_InitStruct.rowSize  = KEYBOARD_ROW_SIZE;
    KEYSCAN_InitStruct.colSize  = KEYBOARD_COLUMN_SIZE;
    KEYSCAN_InitStruct.scanmode     = KeyScan_Manual_Scan_Mode;
    KEYSCAN_InitStruct.debounceEn   = vDebounce_En;
    KeyScan_Init(KEYSCAN, &KEYSCAN_InitStruct);
}

Note

The default settings for the KeyScan_InitStruct member are shown in the following table:

KeyScan_InitStruct Member

Default Value

colSize

2

rowSize

2

scanmode

KeyScan_Auto_Scan_Mode

detectMode

KeyScan_Detect_Mode_Level

clockdiv

0x1f8

delayclk

0x01

fifotriggerlevel

1

fifoOvrCtrl

KeyScan_FIFO_OVR_CTRL_DIS_LAST

debounceEn

KeyScan_Debounce_Enable

scantimerEn

KeyScan_ScanInterval_Enable

detecttimerEn

KeyScan_Release_Detect_Enable

scanInterval

0x10

debouncecnt

0x10

releasecnt

0x1

keylimit

0x03

Parameters:

KeyScan_InitStruct[in] Pointer to a KEYSCAN_InitTypeDef structure which will be initialized.

void KeyScan_INTConfig(KEYSCAN_TypeDef *KeyScan, uint32_t KeyScan_IT, FunctionalState newState)

Enable or disable the specified KeyScan interrupts.

Example usage

void driver_keyscan_init(void)
{
    KeyScan_INTConfig(KEYSCAN, KEYSCAN_INT_SCAN_END | KEYSCAN_INT_ALL_RELEASE, ENABLE);
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • KeyScan_IT[in] Specifies the KeyScan interrupt sources to be enabled or disabled, refer to KeyScan Interrupt Definition. This parameter can be any combination of the following values:

    • KEYSCAN_INT_OVER_READ: KeyScan FIFO over read interrupt. When there is no data in the FIFO, reading the FIFO will trigger this interrupt to prevent over-reading.

    • KEYSCAN_INT_THRESHOLD: KeyScan FIFO threshold interrupt. when data in the FIFO reaches the threshold level, the interrupt is triggered.

    • KEYSCAN_INT_SCAN_END: KeyScan finish interrupt. Whether the key value is scanned or not, the interrupt will be triggered as long as the scanning action is completed.

    • KEYSCAN_INT_FIFO_NOT_EMPTY: KeyScan FIFO not empty interrupt. If there is data in the FIFO, the interrupt will be triggered.

    • KEYSCAN_INT_ALL_RELEASE: KeyScan all release interrupt. When the release time count reaches the set value, if no key is pressed, the interrupt is triggered.

  • newState[in] New state of the specified KeyScan interrupts. This parameter can be one of the following values:

    • ENABLE: Enable the specified KeyScan interrupts.

    • DISABLE: Disable the specified KeyScan interrupts.

void KeyScan_INTMask(KEYSCAN_TypeDef *KeyScan, uint32_t KeyScan_IT, FunctionalState newState)

Mask or unmask the specified KeyScan interrupts.

Example usage

void KeyScan_Handler(void)
{
    if (KeyScan_GetFlagState(KEYSCAN, KEYSCAN_INT_FLAG_ALL_RELEASE) == SET)
    {
        //add user code here.
        KeyScan_ClearINTPendingBit(KEYSCAN, KEYSCAN_INT_ALL_RELEASE);
        KeyScan_INTMask(KEYSCAN, KEYSCAN_INT_ALL_RELEASE, DISABLE);
    }
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • KeyScan_IT[in] Specifies the KeyScan interrupt sources, refer to KeyScan Interrupt Definition. This parameter can be any combination of the following values:

    • KEYSCAN_INT_OVER_READ: KeyScan FIFO over read interrupt. When there is no data in the FIFO, reading the FIFO will trigger this interrupt to prevent over-reading.

    • KEYSCAN_INT_THRESHOLD: KeyScan FIFO threshold interrupt. when data in the FIFO reaches the threshold level, the interrupt is triggered.

    • KEYSCAN_INT_SCAN_END: KeyScan finish interrupt. Whether the key value is scanned or not, the interrupt will be triggered as long as the scanning action is completed.

    • KEYSCAN_INT_FIFO_NOT_EMPTY: KeyScan FIFO not empty interrupt. If there is data in the FIFO, the interrupt will be triggered.

    • KEYSCAN_INT_ALL_RELEASE: KeyScan all release interrupt. When the release time count reaches the set value, if no key is pressed, the interrupt is triggered.

  • newState[in] New state of the specified KeyScan interrupt mask. This parameter can be one of the following values:

    • ENABLE: Enable the interrupt mask of KeyScan.

    • DISABLE: Disable the interrupt mask of KeyScan.

void KeyScan_Read(KEYSCAN_TypeDef *KeyScan, uint16_t *outBuf, uint16_t count)

Read data from KeyScan FIFO.

Example usage

void keyscan_demo(void)
{
    uint16_t data[3] = {0};
    KeyScan_Read(KEYSCAN, data, 3);
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • outBuf[out] Buffer to save data read from KeyScan FIFO.

  • count[in] Data length to be read.

void KeyScan_Cmd(KEYSCAN_TypeDef *KeyScan, FunctionalState NewState)

Enable or disable the KeyScan peripheral.

Example usage

void driver_keyscan_init(void)
{
    KeyScan_Cmd(KEYSCAN, ENABLE);
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • NewState[in] New state of the KeyScan peripheral. This parameter can be one of the following values:

    • ENABLE: Enable the KeyScan peripheral.

    • DISABLE: Disable the KeyScan peripheral.

void KeyScan_FilterDataConfig(KEYSCAN_TypeDef *KeyScan, uint16_t data, FunctionalState NewState)

Set filter data.

Example usage

void keyscan_demo(void)
{
    KeyScan_FilterDataConfig(KEYSCAN, 0x01, ENABLE);
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • data[in] Config the data to be filtered. This parameter should not be more than 9 bits.

  • NewState[in] New state of the KeyScan filtering. This parameter can be one of the following values:

    • ENABLE: Enable data filtering.

    • DISABLE: Disable data filtering.

void KeyScan_debounceConfig(KEYSCAN_TypeDef *KeyScan, uint8_t time, FunctionalState NewState)

Config the KeyScan debounce time.

Example usage

void keyscan_demo(void)
{
    KeyScan_debounceConfig(KEYSCAN, 10, ENABLE);

}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • time[in] KeyScan hardware debounce time. Debounce time = delay clock * time.

  • NewState[in] New state of the KeyScan debounce function. This parameter can be one of the following values:

    • ENABLE: Enable KeyScan debounce function.

    • DISABLE: Disable KeyScan debounce function.

uint16_t KeyScan_GetFifoDataNum(KEYSCAN_TypeDef *KeyScan)

Get KeyScan FIFO data number.

Example usage

void KeyScan_Handler(void)
{
    if (KeyScan_GetFlagState(KEYSCAN, KEYSCAN_INT_FLAG_SCAN_END) == SET)
    {
        KeyScan_INTMask(KEYSCAN, KEYSCAN_INT_SCAN_END, ENABLE);

        //KeyScan FIFO not empty
        if (KeyScan_GetFlagState(KEYSCAN, KEYSCAN_FLAG_EMPTY) != SET)
        {
            uint8_t data_len = KeyScan_GetFifoDataNum(KEYSCAN);
            KeyScan_Read(KEYSCAN, data, data_len);
            //add user code here.
        }
    }
}

Parameters:

KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

Returns:

Data length in FIFO.

void KeyScan_ClearINTPendingBit(KEYSCAN_TypeDef *KeyScan, uint32_t KeyScan_IT)

Clear the KeyScan interrupt pending bit.

Example usage

void KeyScan_Handler(void)
{
    if (KeyScan_GetFlagState(KEYSCAN, KEYSCAN_INT_FLAG_ALL_RELEASE) == SET)
    {
        //clear KeyScan interrupt
        KeyScan_ClearINTPendingBit(KEYSCAN, KEYSCAN_INT_ALL_RELEASE);
    }
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • KeyScan_IT[in] Specifies the interrupt pending bit to clear, refer to KeyScan Interrupt Definition. This parameter can be any combination of the following values:

    • KEYSCAN_INT_OVER_READ: KeyScan FIFO over read interrupt. When there is no data in the FIFO, reading the FIFO will trigger this interrupt to prevent over-reading.

    • KEYSCAN_INT_THRESHOLD: KeyScan FIFO threshold interrupt. when data in the FIFO reaches the threshold level, the interrupt is triggered.

    • KEYSCAN_INT_SCAN_END: KeyScan finish interrupt. Whether the key value is scanned or not, the interrupt will be triggered as long as the scanning action is completed.

    • KEYSCAN_INT_FIFO_NOT_EMPTY: KeyScan FIFO not empty interrupt. If there is data in the FIFO, the interrupt will be triggered.

    • KEYSCAN_INT_ALL_RELEASE: KeyScan all release interrupt. When the release time count reaches the set value, if no key is pressed, the interrupt is triggered.

void KeyScan_ClearFlags(KEYSCAN_TypeDef *KeyScan, uint32_t KeyScan_FLAG)

Clear the specified KeyScan flags.

Example usage

void keyscan_demo(void)
{
    KeyScan_ClearFlags(KEYSCAN, KEYSCAN_FLAG_FIFOLIMIT);
}

Note

KEYSCAN_FLAG_FULL and KEYSCAN_FLAG_EMPTY can’t be cleared manually. They are cleared by hardware automatically.

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • KeyScan_FLAG[in] Specifies the KeyScan flag to clear, refer to KeyScan Flags. This parameter can be one of the following values:

    • KEYSCAN_FLAG_FIFOLIMIT: When data filtering occurs, this bit will be set to 1.

    • KEYSCAN_FLAG_DATAFILTER: FIFO data filter status.

    • KEYSCAN_FLAG_OVR: FIFO overflow status.

FlagStatus KeyScan_GetFlagState(KEYSCAN_TypeDef *KeyScan, uint32_t KeyScan_FLAG)

Get the specified KeyScan flag status.

Example usage

void KeyScan_Handler(void)
{
    if (KeyScan_GetFlagState(KEYSCAN, KEYSCAN_INT_FLAG_ALL_RELEASE) == SET)
    {
        //add user code here.
    }
}

Parameters:
  • KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

  • KeyScan_FLAG[in] Specifies the KeyScan flag to check, refer to KeyScan Flags. This parameter can be one of the following values:

    • KEYSCAN_FLAG_FIFOLIMIT: When data filtering occurs, this bit will be set to 1.

    • KEYSCAN_INT_FLAG_THRESHOLD: FIFO threshold interrupt status.

    • KEYSCAN_INT_FLAG_OVER_READ: FIFO over read interrupt status.

    • KEYSCAN_INT_FLAG_SCAN_END: Scan finish interrupt status.

    • KEYSCAN_INT_FLAG_FIFO_NOT_EMPTY: FIFO not empty interrupt status.

    • KEYSCAN_INT_FLAG_ALL_RELEASE: All release interrupt status.

    • KEYSCAN_FLAG_DATAFILTER: FIFO data filter status.

    • KEYSCAN_FLAG_OVR: FIFO overflow status.

    • KEYSCAN_FLAG_FULL: FIFO full status.

    • KEYSCAN_FLAG_EMPTY: FIFO empty status.

Return values:
  • SET – The specified KeyScan flag is set.

  • RESET – The specified KeyScan flag is unset.

Returns:

The status of KeyScan flag.

uint16_t KeyScan_ReadFifoData(KEYSCAN_TypeDef *KeyScan)

Read KeyScan FIFO data.

Example usage

void keyscan_demo(void)
{
    uint16_t data = KeyScan_ReadFifoData(KEYSCAN);
}

Parameters:

KeyScan[in] Selected KeyScan peripheral, which can be KEYSCAN.

Returns:

KeyScan FIFO data.