STM32F051 触摸按键功能
生活随笔
收集整理的這篇文章主要介紹了
STM32F051 触摸按键功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
搞了幾天STM32F051的觸摸按鍵功能,最后發現其實很簡單。最開始下載了官方的庫,結果使用的時候發現程序居然會跑飛,后來還是自己寫吧,寫完了才發現原來很簡單,這里把代碼貼出來和大家分享。
// --------------------TSCtrl.h--------------------------- #pragma onceenum TSCStatus { TSC_RELEASE, TSC_PRESSED };class TSCtrl { public:// 構造對象。static TSCtrl &Construction(void){static TSCtrl ts_obj;return ts_obj;}// 掃描端口。 void Scan(void);// 獲取狀態。 TSCStatus GetPortStatus(int index);~TSCtrl(); private: TSCtrl(); }; #define tsc (TSCtrl::Construction()) //-----------------------TSCtrl.cpp----------------------------- #include "TSCtrl.h" #include "stm32f0xx_gpio.h" #include "stm32f0xx_rcc.h" #include <stdlib.h>#define THRESHOLD 30 // 按下時刻的門檻,根據實際情況修改此值。#define PORT_NR 3struct PortStatus { short fixed_value; short current_value; TSCStatus status; unsigned short enable_mask; }; struct ScanTrace { PortStatus port[PORT_NR]; char port_index; };ScanTrace _trace = { 935, 0, TSC_RELEASE, 1 << 12, // 935根據你的板卡而定,它是指在沒有觸碰按鈕時刻采集的數據,該值在IOGXCR寄存器中。 926, 0, TSC_RELEASE, 1 << 13, // 1<<13這些數據也要根據實際情況修改,這里使用的是GP4_IO1, GP4_IO2,GP4_IO4。 1100, 0, TSC_RELEASE, 1 << 15, 0 };#define CAP_PORT GPIOA // 根據實際情況修改。 #define SAMPCAP_PIN GPIO_Pin_11 #define CHANNEL0_PIN GPIO_Pin_9 #define CHANNEL1_PIN GPIO_Pin_10 #define CHANNEL2_PIN GPIO_Pin_12static void _init_hw(void) { // 打開時鐘。 ::RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);GPIO_InitTypeDef gpio_config; gpio_config.GPIO_Mode = GPIO_Mode_AF; gpio_config.GPIO_OType = GPIO_OType_OD; gpio_config.GPIO_Speed = GPIO_Speed_2MHz; gpio_config.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio_config.GPIO_Pin = SAMPCAP_PIN; ::GPIO_Init(CAP_PORT, &gpio_config); // 初始化CAP引腳。gpio_config.GPIO_OType = GPIO_OType_PP; gpio_config.GPIO_Pin = CHANNEL0_PIN | CHANNEL1_PIN | CHANNEL2_PIN; ::GPIO_Init(CAP_PORT, &gpio_config); // 初始化CHANNEL引腳。// 重定義引腳的功能。 ::GPIO_PinAFConfig(CAP_PORT, 9, GPIO_AF_3); ::GPIO_PinAFConfig(CAP_PORT, 10, GPIO_AF_3); ::GPIO_PinAFConfig(CAP_PORT, 11, GPIO_AF_3); ::GPIO_PinAFConfig(CAP_PORT, 12, GPIO_AF_3);::RCC_AHBPeriphClockCmd(RCC_AHBPeriph_TS, ENABLE);// CTPH = 1x tPGCLK, CTPL = 1x tPGCLK, PGPSC = 5(fHCLK/32), MCV = 6, TSCE = ENABLE TSC->CR = (0 << 28) | (0 << 24) | (5 << 12) | (6 << 5) | (1 << 0);// 關閉施密特出發。 unsigned tmp_value = 0xFFFFFFFF; tmp_value &= (unsigned)~((unsigned)1 << 12); tmp_value &= (unsigned)~((unsigned)1 << 13); tmp_value &= (unsigned)~((unsigned)1 << 14); tmp_value &= (unsigned)~((unsigned)1 << 15); TSC->IOHCR &= tmp_value;// Sampling enabled tmp_value = 0; tmp_value |= (unsigned)((unsigned)1 << 14); TSC->IOSCR |= tmp_value;// Channel enabled, G4_IO4 G4_IO2 G4_IO1 // TSC->IOCCR = (0x01 << 12);// Enable group, G4 TSC->IOGCSR |= (1 << 3); }static int _wait_eoc(void) {int ret = 0;if (TSC->ISR & 0x01){// Check MCEF flagif (TSC->ISR & 0x02){ret = 0;}else{ret = 1;}}return ret; }static void _scan_channel(int index) { TSC->IOCCR = _trace.port[index].enable_mask; TSC->ICR |= 0x03; TSC->CR |= 0x02;int delay_cnt = 0; while(_wait_eoc() == 0) { if(++delay_cnt > 10000) // 避免死循環。 { _trace.port[index].status = TSC_RELEASE; return; } } _trace.port[index].current_value = TSC->IOGXCR[3]; short diff = ::abs(_trace.port[index].current_value - _trace.port[index].fixed_value); if(diff > THRESHOLD) { _trace.port[index].status = TSC_PRESSED; } else { _trace.port[index].status = TSC_RELEASE; } }void TSCtrl::Scan(void) { _scan_channel(_trace.port_index++); _trace.port_index = (_trace.port_index >= PORT_NR) ? 0 : _trace.port_index; }TSCStatus TSCtrl::GetPortStatus(int index) { return _trace.port[index].status; }TSCtrl::TSCtrl() { _init_hw(); }TSCtrl::~TSCtrl() { }使用代碼如下:
int main(void) { TSCStatus t; while(1) { tsc.Scan(); t = tsc.GetPortStatus(1); t = tsc.GetPortStatus(2); t = tsc.GetPortStatus(3);} }
總結
以上是生活随笔為你收集整理的STM32F051 触摸按键功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GNSS测量与数据处理作业
- 下一篇: 串口通信原理