私はnucleoボードf411RE、特に汎用タイマーTIM9、TIM10、TIM11をいじっています。TIM10(16ビットタイマー)を使用して、有効にして実行するだけで時間を測定し、TIM10-> CNTに保存されている値を使用して、if条件を使用して決定(LEDをオンにするかどうか)を決定したかった.
<、>、<=、>= 演算子を使用しているときに、カウンター (TIM10->CNT) の値を他の数値と比較すると、次のようになります。
if( (TIM10->CNT) <= 10000 ){
//do something
****actually does something****
}
あるいは:
if( (TIM10->CNT) == 0 ){
/**this one with the equals sign only works when equaled to zero, any number but zero
seems to not being recognized**/
//do something
****actually does something****
}
すべてが期待どおりに機能し、関数は思いどおりに動作します。
この投稿の主人公と同じように、次のステートメントのように等号を使用してタイマー カウンター レジスタをゼロ以外の数値 (「任意の値」として表される) と比較しようとすると、常に問題が発生します。
if( (TIM10->CNT) == any value ){
//do something
****this block does not get executed****
}
上記のコメントで述べたように、等号 ( == ) をゼロ以外の数値で明示的に使用すると、その条件で定義されたブロックは実行されません。TIM10カウンターを使用して比較できる唯一の方法は、不等式演算子(>、<、=>、> =)を使用するとき、または明示的にゼロと比較するときのようです。
これが私を悩ませている唯一の部分であるため、この時点まで実際のコードを投稿していません。他のすべてのコンポーネントは魅力的に機能しています。とにかく、ここに私のコードを垣間見ることができます:
疑似コード:
--- プログラム開始 ---
--- TIM10 -> ゼロに初期化された CNT ---
---while ループ開始 ---
---TIM10→CNTレジスタを読み込み、その値を変数に格納---
---ボタンを押すだけで使えるTIM10---
---カウンターがすでに実行されている間にもう一度ボタンが押された場合、タイマーカウンターを停止し、その値を変数に保存して次の関数で使用します---
---経過時間TIM10→CNTの値に応じてLEDをON/OFFする機能---
実際の C++ コードは次のとおりです。
#include <GPIO_PORT.h> //includes the class GPIO_PORT which I created to manage the register configuration when initializing a GPIO port. With this I can easily set whether a pin is an input or an output and many other things as writing and reading to it.
int main(void){
uint32_t time_value;
GPIO_PORT led('A',5,0); //GPIO_PORT object that configures and initializes PORTA 5 pin as output pin.
GPIO_PORT boton('C',13,1);//GPIO_PORT object that configures and initializes PORTC 13 pin as an input pin for the push button
system_config(); //function that configures the clock source, pll, and all the system clock selection and configuration related registers
/*here starts the TIM10 configuration*/
RCC->APB2ENR |= (1<<17); //TIM10 clock peripheral enabled
TIM10->PSC = 160 - 1 ; //TIM10 preescaler needed accordingly to my calculations
TIM10->ARR = 65535; //value that the counter has to reach before overflow occurs. In this case the maximum possible 0xFFFF.
TIM10->CR1 |= (1<<3); //one pulse mode enable so the counter stops at update event generation (may it be overflowing or setting the UG bit)
TIM10->CNT = 0; //TIM10 counter initialized to zero
while(1){
//in the first iteration, the timer has not been enabled yet so it's value is zero, in following iterations, timer simply gets the value of the TIM10 counter so it can be used in the drive led function
time_value = TIM10->CNT;
//if time_value is zero (has not been enabled before yet) and the push button is pressed, then initializes TIM10->CNT to zero and enables start counting.
if( (time_value == 0) & (!boton.read_pin_status()) ){
TIM10->CR1 |= (1<<0); //TIM10 counter enabled
TIM10->CNT = 0; //TIM10 counter placed at a value of zero
time_value = TIM10->CNT; //need to "return" the value of the timer counter(in this case zero) in order to use it in the drive led function.
}else if( (time_value != 0) & (!boton.read_pin_status()) ){
time_value = TIM10->CNT; //"returns" the current value of timer counter by storing it in another variable.
TIM10->EGR |= (1<<0); //software generated update event in order to stop the timer as well as reseting the TIM10 counter to zero.
}
if(time_value == 0){
//this block gets executed
led.set_pin_status(0);
}else if( (time_value < 10000) & (timer != 0)){
//this block gets executed
led.set_pin_status(0);
}else if((time_value < 20000) & (timer >= 10000)){
//this block gets executed
led.set_pin_status(1);
}else if((time_value < 40000) & (timer >= 20000)){
//this block gets executed
led.set_pin_status(0);
}else if(time_value == 65535 ){
//THIS BLOCK DOES NOT GETS EXECUTED NO MATTER WHAT YOU REPLACE 65535 FOR, THE FACT OF USING "==" BUGS THE BLOCK EXECUTION
led.set_pin_status(1);
HAL_Delay(1500);
led.set_pin_status(0);
HAL_Delay(1500);
TIM10->EGR |= (1<<0); //since 65535 is the maximum possible value, when reaching this point, the timer counter shall be reseted to zero as well as being disabled. In one pulse mode, setting the UG bit does so.
}
}
}
「==」演算子をタイマーカウンターレジスタと組み合わせて使用できない理由についてのヘルプとコメントをいただければ幸いです。
pd: どんなコメントでも実際に高く評価されます。
pd2: 遅延が何らかの形で最後の if 条件をバグらせていると思うかもしれませんが、実際にはそこにどの命令が配置されているかは関係ありません。そのブロックは「==」の使用法のために実行されません。
pd3: 前に言ったように、問題は明示的に "==" の使用にあるようです。なぜなら、コードの他のすべての部分を試してみたところ、問題なく動作するからです (クロック構成、タイマー構成、GPIO 構成、および対応する各ピンへの書き込みと読み取りの方法)