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:
And this is what I wrote:
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:
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.
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
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.