1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

1ピン入力、十字キーで遊ぶ。(2) (構想編(2)) 比較づくり STM32C0116-DK

Last updated at Posted at 2025-08-15

目的

アナログ1入力のキーの電圧からキー番号を求める

プログラム



#include <iostream>
using namespace std;

#define N_SELECT 5
#define N_LEFT   4
#define N_DOWN   3
#define N_UP     2
#define N_RIGHT  1
#define N_NO_KEY 0

#define SELECT_t_LEFT  3
#define LEFT_t_DOWN    10
#define DOWN_t_UP      16
#define UP_t_RIGHT     23
#define RIGHT_t_NO_KEY 30

//inupt(入力)mV 比較ルーチン
int i_key(int Voltage)
{

  //output キー番号

  Voltage = Voltage / 100;

  if ( Voltage < SELECT_t_LEFT ) { //3
    
    return (N_SELECT);//戻り値(5)
    
  } else if  ( Voltage < LEFT_t_DOWN ) { //3-10
    
    return (N_LEFT);//戻り値(4)
    
  } else if ( Voltage < DOWN_t_UP ) { //10-16
    
    return (N_DOWN);//戻り値(3)
    
  } else if ( Voltage < UP_t_RIGHT  ) { //16-23
    
    return (N_UP);//戻り値(2)
 
  } else if ( Voltage < RIGHT_t_NO_KEY ) { //23-30
    
    return (N_RIGHT);//戻り値(1)
   
  } else { //30-
    
    return (N_NO_KEY);//戻り値(0)
    
  }//end if

}//i_key


int main(void){
    // Your code here!
    
    //               0        1       2    3      4      5  
    char *str1[] = {"NO_KEY","RIGHT","UP","DOWN","LEFT","SELECT"};
    
    //printf("%s ",str1[5]);

    printf("START\n\n"); 
    
    for(int i = 0;i<(33+1);i++){
        int mv = i * 100;
        printf("[%4d]mV -> ",mv);

        printf("%s",str1[i_key(mv)]);

        printf("[%d]",i_key(mv));
        
        printf("\n");
    }
    
}






START

[   0]mV -> SELECT[5]
[ 100]mV -> SELECT[5]
[ 200]mV -> SELECT[5]
[ 300]mV -> LEFT[4]
[ 400]mV -> LEFT[4]
[ 500]mV -> LEFT[4]
[ 600]mV -> LEFT[4]
[ 700]mV -> LEFT[4]
[ 800]mV -> LEFT[4]
[ 900]mV -> LEFT[4]
[1000]mV -> DOWN[3]
[1100]mV -> DOWN[3]
[1200]mV -> DOWN[3]
[1300]mV -> DOWN[3]
[1400]mV -> DOWN[3]
[1500]mV -> DOWN[3]
[1600]mV -> UP[2]
[1700]mV -> UP[2]
[1800]mV -> UP[2]
[1900]mV -> UP[2]
[2000]mV -> UP[2]
[2100]mV -> UP[2]
[2200]mV -> UP[2]
[2300]mV -> RIGHT[1]
[2400]mV -> RIGHT[1]
[2500]mV -> RIGHT[1]
[2600]mV -> RIGHT[1]
[2700]mV -> RIGHT[1]
[2800]mV -> RIGHT[1]
[2900]mV -> RIGHT[1]
[3000]mV -> NO_KEY[0]
[3100]mV -> NO_KEY[0]
[3200]mV -> NO_KEY[0]
[3300]mV -> NO_KEY[0]


おまけ


P_SELECT 0
P_LEFT   7
P_DOWN   13
P_UP     20
P_RIGHT  27
P_NO_KEY 33

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?