Author Topic: [STM32] save TIM3->CNT to 2 variables for display?  (Read 1166 times)

0 Members and 1 Guest are viewing this topic.

Offline kgavionicsTopic starter

  • Regular Contributor
  • *
  • Posts: 195
  • Country: ca
[STM32] save TIM3->CNT to 2 variables for display?
« on: August 21, 2022, 04:38:24 pm »
Hello
I'm trying to implement a simple menu to modify 2 variables (Volume and Contrast) using a rotary encoder hooked to TIM3 (encoder mode). What I want to achieve is when I press on the rotary switch it toggles between the 2 variables and then rotating the encoder, I can modify their respective values. My problem is I can't find a way to restore the saved counter when I toggle the switch. I tried to use 2 temporary variables, but it when I restore them to the TIM3→CNT doesn't update
Please don't laugh on my code, I'm not a programmer by trade! I hope that someone cleaver in programming help me to resolve this issue!
Thanks in advance
Code: [Select]
{
void rencoder(void)
{
pb_scan();
if(pushb==31)
{
menucounter++;
if(menucounter>2) menucounter=0;
}
switch(menucounter)
case(0):

volume=TIM3->CNT;//how to save this value without affecting the contrast variable

break;


case(1):



contrast=TIM3->CNT;



break;
}

}
« Last Edit: August 21, 2022, 07:33:42 pm by kgavionics »
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: sk
Re: [STM32] save TIM3->CNT to 2 variable for display?
« Reply #1 on: August 21, 2022, 06:04:23 pm »
> when I restore them to the TIM3→CNT

Show that.

JW
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4427
  • Country: dk
Re: [STM32] save TIM3->CNT to 2 variables for display?
« Reply #2 on: August 21, 2022, 10:27:32 pm »
read the code again, look at what it does not what you think it does

you repeatedly write the counter, of course it never counts
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4427
  • Country: dk
Re: [STM32] save TIM3->CNT to 2 variables for display?
« Reply #3 on: August 21, 2022, 11:14:26 pm »
I know that it's not working, that's why I'm asking for help dude!

you write the counter and then read the counter, what could you possibly read other than what you just wrote?
 

Online cv007

  • Frequent Contributor
  • **
  • Posts: 828
Re: [STM32] save TIM3->CNT to 2 variables for display?
« Reply #4 on: August 22, 2022, 12:27:29 am »
Your button push is when you want to restore the counter-
Code: [Select]
void rencoder(void) {
    pb_scan();
    if( pushb == 31 ){
        switch( ++menucounter ){
            default: menucounter = 0; // if not in a case below, start over at 0
            //fallthrough
            case 0: TIM3->CNT = volume; break; //restore volume value
            case 1: TIM3->CNT = contrast; break; //restore contrast value
        }
    }
    switch( menucounter ){
    case(0):// changing volume value
                volume = TIM3->CNT;
break;

    case(1):// changing contrast value
                contrast = TIM3->CNT;
break;
    }
}
 
The following users thanked this post: kgavionics

Offline kgavionicsTopic starter

  • Regular Contributor
  • *
  • Posts: 195
  • Country: ca
Re: [STM32] save TIM3->CNT to 2 variables for display?
« Reply #5 on: August 22, 2022, 12:37:21 am »
Thanks cv007, your code is working like a charm!
You really made my day!
Can you please explain this
Code: [Select]
switch( ++menucounter ) why ++menucounter?

« Last Edit: August 22, 2022, 12:51:01 am by kgavionics »
 

Online cv007

  • Frequent Contributor
  • **
  • Posts: 828
Re: [STM32] save TIM3->CNT to 2 variables for display?
« Reply #6 on: August 22, 2022, 03:46:09 am »
Quote
why ++menucounter?
Well, it just replaces the equivalent-

if( pushb == 31 ){
        menucounter++;
        switch( menucounter ){

where the increment takes place before the switch statement uses its value (pre-incremented), and the default case replaces checking for overflow.

« Last Edit: August 22, 2022, 06:14:02 am by cv007 »
 
The following users thanked this post: kgavionics

Offline kgavionicsTopic starter

  • Regular Contributor
  • *
  • Posts: 195
  • Country: ca
Re: [STM32] save TIM3->CNT to 2 variables for display?
« Reply #7 on: August 22, 2022, 02:37:28 pm »
Quote
why ++menucounter?
Well, it just replaces the equivalent-

if( pushb == 31 ){
        menucounter++;
        switch( menucounter ){

where the increment takes place before the switch statement uses its value (pre-incremented), and the default case replaces checking for overflow.
All clear now, Thanks
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf