• AVR Freaks

Helpful ReplyHot!Translating assembler to C, timeout control flow

Author
ndcroos
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/10/02 11:14:36
  • Location: 0
  • Status: offline
2020/06/03 12:09:21 (permalink)
0

Translating assembler to C, timeout control flow

Hi all,
 
I am in the process of translating assembler to XC8 C.
I am using a PIC18F26K22.
I have a question about translating the control flow of the following fragment.
 

    MOVLW    TIMEOUT
    SUBWF    TIMEO,0
    BTFSC    STATUS,0
    GOTO    EXIT         ; Timeout

    BTFSC    KEYBOARD,4       
    GOTO    EXIT        ;Pressed #
    BTFSC    KEYBOARD,5
    GOTO    EXIT        ;Pressed *
    ; Other assembler code in routine

 
The code does the following, I think:
A counter is started,
If the timer reaches zero, go to exit.
If # is pressed, go to exit.
If * is pressed, go to exit.
Else, continue the current function

I am translating it like this:
I have a callback function which decreases the variable timeOut each time it is called.
 

void function(){
    timeOut = TIMEOUT_TIME; //Timeout time: 30 s
    while (timeOut > 0){
        KB = I2C1_Read1ByteRegister(ADP5585,ADP5585_FIFO_1);
        if(KB == ADP5585_KEY_AST){
            exit_();
            return;
        }
        if(KB == ADP5585_KEY_HASHTAG){
            exit_();
            return;
        }
    }
    // Other code
}


I know my translation is wrong, but I have trouble fixing it.
I have trouble with implementing the parts:
"If the timer reaches zero, go to exit."
And
"Else, continue the current function."

Also, with the keypad presses, I am not sure if 'return' is the right choice.
post edited by ndcroos - 2020/06/03 13:28:03
#1
ric
Super Member
  • Total Posts : 27954
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 13:03:46 (permalink)
0
How about this?
void function(){
    timeOut = TIMEOUT_TIME; //Timeout time: 30 s
    while (timeOut > 0){
        KB = I2C1_Read1ByteRegister(ADP5585,ADP5585_FIFO_1);
        if(KB == ADP5585_KEY_AST){
            break;  //exit while loop
        }
        if(KB == ADP5585_KEY_HASHTAG){
            break;  //exit while loop
        }
    }


I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
ndcroos
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/10/02 11:14:36
  • Location: 0
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 13:09:22 (permalink)
0
Hello ric. Thanks. But if timeout is reached, or * or # are pressed, the function should finish there. Otherwise, it should continue with the rest of 'function'. So when exit is called, it shouldn't continue with the remaining part of 'function'. Sorry if this wasn't clear from my post.
#3
ric
Super Member
  • Total Posts : 27954
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 13:11:16 (permalink)
0
so just do a return then, as you were.
What is the "exit_()" function meant to do?
 
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
ndcroos
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/10/02 11:14:36
  • Location: 0
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 13:14:57 (permalink)
0
This is my exit function.
It should give a long beep.
It also resets a fase variable. This variable denotes the current state.
The original assembler code uses a state machine. In my C translation I am also using a state machine.
 
void exit_(){
    longBeep();
    fase = 0;
}
#5
ric
Super Member
  • Total Posts : 27954
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 13:19:56 (permalink) ☄ Helpfulby ndcroos 2020/06/03 13:30:06
+1 (1)
So what is wrong with the code you presented in post#1?
The only change I would make is replacing the chain of "if (KB ==" statements with a switch() statement, but it should still work.
 
and change
void exit_(){
to
void exit_(void){
 
empty brackets in a function declaration are NOT the same as (void) in C.
(they are in C++)
 
post edited by ric - 2020/06/03 13:22:51

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
1and0
Access is Denied
  • Total Posts : 10985
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 13:22:37 (permalink) ☄ Helpfulby ndcroos 2020/06/03 13:26:55
+1 (1)

if ((timeOut == 0) || (KB == ADP5585_KEY_AST) || (KB == ADP5585_KEY_HASHTAG)) {
    exit_();
    return;
}

post edited by 1and0 - 2020/06/03 13:24:16
#7
ric
Super Member
  • Total Posts : 27954
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 13:24:06 (permalink)
0
1and0

if ((timeOut == 0) || (KB == ADP5585_KEY_AST) || (KB == ADP5585_KEY_HASHTAG) {
    exit_();
    return;
}



Indeed. I assumed some extra code was to be added for each different key, but if that's the complete version, then this is a better way to do it.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
du00000001
Just Some Member
  • Total Posts : 3834
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 18:30:51 (permalink)
0
Hi all,
 
I'm somewhat missing the decrement of the timeout variable!
Or is that one decremented in some timer interrupt?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#9
ric
Super Member
  • Total Posts : 27954
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 19:15:08 (permalink)
0
Yeah, I assumed "callback function" meant it is being done by a timer interrupt.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
ndcroos
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/10/02 11:14:36
  • Location: 0
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 23:23:39 (permalink)
0
This is my callback function. It is indeed called by a timer interrupt.

void timeout_int_handler(void){
    if(timeOut > 0){
        timeOut--;
    }
}

#11
ric
Super Member
  • Total Posts : 27954
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/03 23:29:12 (permalink)
+2 (2)
... and hopefully the "timeout" variable was created with a "volatile" qualifier in the definition.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#12
jbroadwell81@gmail.com
Super Member
  • Total Posts : 1460
  • Reward points : 0
  • Joined: 2005/11/02 13:58:11
  • Location: Indianapolis, Indiana
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/09 05:59:42 (permalink)
+1 (1)
ric
... and hopefully the "timeout" variable was created with a "volatile" qualifier in the definition.

And if you're on an 18F, "timeout" needs to be an 8 bit variable.  If it's 16 bits or 32 bits, you need an accessor function to make sure you're accessing it coherently.

http://www.serialwombat.com
When someone asks you "Can't you just...", watch out.
#13
ric
Super Member
  • Total Posts : 27954
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/09 06:20:03 (permalink)
+1 (1)
jbroadwell81@gmail.com
ric
... and hopefully the "timeout" variable was created with a "volatile" qualifier in the definition.

And if you're on an 18F, "timeout" needs to be an 8 bit variable.  If it's 16 bits or 32 bits, you need an accessor function to make sure you're accessing it coherently.

Agree, although plainly it is an 8 bit variable in the original assembler version.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#14
jbroadwell81@gmail.com
Super Member
  • Total Posts : 1460
  • Reward points : 0
  • Joined: 2005/11/02 13:58:11
  • Location: Indianapolis, Indiana
  • Status: offline
Re: Translating assembler to C, timeout control flow 2020/06/09 08:10:44 (permalink)
+1 (1)
ric
 
Agree, although plainly it is an 8 bit variable in the original assembler version.
 

Agree.  I didn't see any variable declarations in the sample code.  When people use "int" or "uint" rather than <stdint.h> types, they sometimes get promotions they didn't intend.  Just thought it warranted mentioning.

http://www.serialwombat.com
When someone asks you "Can't you just...", watch out.
#15
Jump to:
© 2020 APG vNext Commercial Version 4.5