Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Assembly to C converting help

Status
Not open for further replies.

Cantafford

Member
Hey,

I'm building a line follower robot using PIC18f4580. I have found an example of project but the code is written in assembly. So I'm trying to convert from assembly to C because I want it in C. This is the original code:

Code:
LIST P=18F4580, F=INHX32

#include <P18F4580.INC> 


CONFIG OSC = HS  ; Oscillator High Speed


#define SEN1 PORTC,0

#define SEN2 PORTC,1

#define SW1 PORTC,3

#define M1_EN PORTC,2

#define M1_FRWD PORTB,1

#define M1_RVSE PORTB,2

#define M2_EN PORTD,7

#define M2_FRWD PORTB,4

#define M2_RVSE PORTB,5


RESET_VECTOR  CODE 0x0000


  GOTO Main  ;go to start of main code





Main:

  BSF  TRISC,0    BSF  TRISC,1    BSF  TRISC,3 

  BCF  TRISC,2    BCF  TRISD,7 

MOVLW  B'00000000'  ;WREG= 11111111 (binary)

MOVWF   TRISB 

MOVLW  B'00000000'  ;WREG = 11111111 (binary)

  MOVWF   TRISD 

  CLRF    PORTB

  CLRF    PORTC

  CLRF    PORTD


L1  BTFSC  SW1

  BRA  L1

 

L2  BSF   M1_FRWD

BSF  M2_FRWD

BSF  M1_EN

  BSF  M2_EN 

L3  BTFSC  SEN1

  GOTO MOTOR_FRWD_2

  BCF  M2_EN

  BRA  L3

 

MOTOR_FRWD_2    BSF  M1_FRWD

  BSF  M2_FRWD

  BSF  M1_EN

  BSF  M2_EN

 

L4  BTFSC  SEN2

  GOTO L2

  BCF  M1_EN

  BRA  L4

 

  BSF  M1_FRWD

  BSF  M2_FRWD

  BSF  M1_EN

  BSF  M2_EN

 

    END

And this is what I wrote:
Code:
/*
 * File:   main.c
 * Author: Paul
 *
 * Created on December 7, 2016, 11:40 AM
 */


#include <xc.h>
#include "header.h"

void L2();
void L3();
void MOTOR_FRWD_2();
void L4();

int flag = 1;
int flag2 = 1;

#define SEN1 PORTCbits.RC0   
#define SEN2 PORTCbits.RC1   
#define SW1 PORTCbits.RC3
#define M1_EN PORTCbits.RC2
#define M1_FRWD PORTBbits.RB1
#define M1_RVSE PORTBbits.RB2
#define M2_EN PORTDbits.RD7
#define M2_FRWD PORTBbits.RB4
#define M2_RVSE PORTBbits.RB5


void main(void) 
{
 OSCCON = 0b01101111; // internal 4Mhz oscillator

 TRISCbits.RC0 = 1;
 TRISCbits.RC1 = 1;
 TRISCbits.RC3 = 1;

 TRISCbits.RC2 = 0;
 TRISDbits.RD7 = 0;

 TRISB = 0b00000000;
 TRISD = 0b00000000;
 PORTB = 0;
 PORTC = 0;
 PORTD = 0;

 while(SW1==1);
 L2();
 do
 {
     L3();
 }while(flag==1);
 flag = 1; // flag = 1 so program can execute L3 next time it runs
}

void L2()
{
    M1_FRWD = 1;
    M2_FRWD = 1;
    M1_EN = 1;
    M2_EN = 1;
}

void L3()
{
    if(SEN1==1) { MOTOR_FRWD_2(); flag=0;}
    if(flag==1) M2_EN = 0;  
}

void MOTOR_FRWD_2()
{
    M1_FRWD = 1;
    M2_FRWD = 2;
    M1_EN = 1;
    M2_EN = 1;
    L4();
}
   
void L4()
{
    if(SEN2==1) {flag2 = 0; L2();}
    if(flag2==1) {M1_EN = 0; L4();  }
    flag2 = 1;
}

It's the first time I'm trying to convert an advanced code from assembly to C...so I'm guessing what I wrote isn't correct. Also I do not understand how the last 4 instructions from the assembly code are executed:
Code:
...
L4  BTFSC  SEN2

  GOTO L2

  BCF  M1_EN

  BRA  L4

 

  BSF  M1_FRWD

  BSF  M2_FRWD

  BSF  M1_EN

  BSF  M2_EN

 

    END
It seems to me that the program will never reach this instructions...or am I missing something?

Please help me convert the code from assembly to C if possible by giving me advice on what I did wrong and how I should improve that and also help me understand how the program executes those 4 instructions at the end. Thank you have a nice day.
 
You are correct, the last 4 instructions are unreachable.

Your C code isn't far off but is lacking the main L2 loop - notice the goto L2 in L4?

Here's a rough conversion,
Code:
while(1){            //main loop equiv L1 long loop
    while(SW==1);        //wait SW high
    M1_FRWD=1;
    M2_FRWD=1;
    M1_EN=1;
    M1_EN=2;
    while(SEN1==0){
        M2_EN=0;
    }
    M2_FRWD=1;
    M1_EN=1;
    M2_EN=1;
    if(SEN2==0){
        M1_EN=0;
    }      
  }

Mike.
 
Thank you. Also does this look like a line follower project that would work to you? Is it worth buying the components to try to assemble it in hardware?
I'm skeptical because of the 4th instructions at the end it makes me belive the project was poorly made and it will not work :(
 
It looks like it will work but very badly. It's either going straight, full left or full right and so will zig zag very slowly along a line.

I'd look for a better example. Preferably with multiple sensors and using PID control.

Mike.
 
Yeah you are right. I've found some better examples of projects using atmega microcontrollers. They are already in C so all I have to do is write the program for the PIC. Should be simple enough will post if I'll encounter problems :D. Thanks.
 
Last edited:
This is how I would interpret this in C -

Code:
#include <xc.h>

#define SEN1 PORTCbits.RC0
#define SEN2 PORTCbits.RC1
#define SW1 PORTCbits.RC3
#define M1_EN PORTCbits.RC2
#define M1_FRWD PORTBbits.RB1
#define M1_RVSE PORTBbits.RB2
#define M2_EN PORTDbits.RD7
#define M2_FRWD PORTBbits.RB4
#define M2_RVSE PORTBbits.RB5


void motorForward2(void){
  M1_FRWD = 1;
  M2_FRWD = 1;
  M1_EN = 1;
  M2_EN = 1;
}

void main(void){
  /*
  ** Initialization Routine
  */
  PORTB = 0x00;  // clear PORTB output latch
  PORTC = 0x00;  // clear PORTC output latch
  PORTD = 0x00;  // clear PORTD output latch
  TRISC &= 0b01111011;  // RC2 and RC7 output
  TRISC |= 0b00001011;  // RB0,RB1 and RB3 input
  TRISB = 0x00;  // RB0-RB7 output
  TRISD = 0x00;  // RD0-RD7 output
  /*
  ** Main Function
  */

  while(SW1);  // wait for SW1 to go low

  motorForward2();

  while(!SEN1){
   M2_EN = 0;
  }

  motorForward2();

  while(!SEN2){
   M1_EN = 0;
  }

  motorForward2();
}

As was stated prior, this code wouldn't work very well at all, but this is how I'd interpret this code in C. Also, notice the absence of a forever loop. At some point it would repeatedly hit the reset vector after processing sensor 2.
 
This is how I would interpret this in C -

Code:
#include <xc.h>

#define SEN1 PORTCbits.RC0
#define SEN2 PORTCbits.RC1
#define SW1 PORTCbits.RC3
#define M1_EN PORTCbits.RC2
#define M1_FRWD PORTBbits.RB1
#define M1_RVSE PORTBbits.RB2
#define M2_EN PORTDbits.RD7
#define M2_FRWD PORTBbits.RB4
#define M2_RVSE PORTBbits.RB5


void motorForward2(void){
  M1_FRWD = 1;
  M2_FRWD = 1;
  M1_EN = 1;
  M2_EN = 1;
}

void main(void){
  /*
  ** Initialization Routine
  */
  PORTB = 0x00;  // clear PORTB output latch
  PORTC = 0x00;  // clear PORTC output latch
  PORTD = 0x00;  // clear PORTD output latch
  TRISC &= 0b01111011;  // RC2 and RC7 output
  TRISC |= 0b00001011;  // RB0,RB1 and RB3 input
  TRISB = 0x00;  // RB0-RB7 output
  TRISD = 0x00;  // RD0-RD7 output
  /*
  ** Main Function
  */

  while(SW1);  // wait for SW1 to go low

  motorForward2();

  while(!SEN1){
   M2_EN = 0;
  }

  motorForward2();

  while(!SEN2){
   M1_EN = 0;
  }

  motorForward2();
}

As was stated prior, this code wouldn't work very well at all, but this is how I'd interpret this code in C. Also, notice the absence of a forever loop. At some point it would repeatedly hit the reset vector after processing sensor 2.

That code will just go through the loop once and then exit to nowhere! How can you ever have code that returns from main on an embedded controller?

Mike.
 
You are correct Pommie. But look at the assembly code I wrote it from. It doesn't contain a forever loop either.

As I stated the assembly is poorly written that will also do the same thing.
 
You are correct Pommie. But look at the assembly code I wrote it from. It doesn't contain a forever loop either.

As I stated the assembly is poorly written that will also do the same thing.

It does contain a forever loop - check GOTO L2 in sub L4. However, totally agree - very badly written code.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top