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.

Help please! :( how to write a simple mplab program for a buggy with 4 sensors please

Status
Not open for further replies.
This is what i have come up with


; Program: [Stepper_Motor.asm]; Written by: Foo DF; Date: 29-Aug-2006
; This program configures PortC RC0-7 as OUTPUT to control the L293D Motor Control Board
;
; ************* HEADER **************************************************************
LIST p=16F876 ;PIC16F876 is the target processor
#include "P16F876.INC" ;Include header file
__config H'3F39' ;Osc=XT; WDT=Off;
;Power Up Timer=Off; Brown Out Detect=Off
;LVP=Disabled, Flash Program Write=Enabled,
;Data EE Read Protect=Off; Code Protect=Off
;
;************** File Register Variable use ******************************************
cblock H'20'
NUMBER
d1 ;use for Delay subroutine
d2
d3
d4
SENSOR
SENSOR_M
endc
;
; ************* Reset Vector ********************************************************
org H'00'
clrf STATUS
movlw 0x00
movwf PCLATH
goto Initial
;
; ************* Port B and C I/O Initialisation *************************************
Initial bsf STATUS,RP0 ; Select BANK 1
; banksel TRISB
movlw B'00000000' ; Set PortB to O/Ps
movwf TRISB ; PortB setting is done!
movlw B'11111111' ; Set PortC to I/Ps
movwf TRISC ; PortC setting is done!
movlw B'00000000' ; Set PortA to O/Ps
movwf TRISA ; PortB setting is done!
movlw B'00000110'
movwf ADCON1

;
; ************* Main Program *******************************************************
banksel PORTA

Enable_BS170 movlw b'00110000'
movwf PORTA

RUN call SENSING
call SENSOR_MANIPULATE
call MOTOR
goto RUN

SENSING movfw PORTC
movwf SENSOR
movwf PORTB
; goto SENSING
return

SENSOR_MANIPULATE
clrf SENSOR_M

chk_s0 btfss SENSOR,0
goto chk_s1
bsf SENSOR_M,0

chk_s1 btfss SENSOR,1
goto chk_s2
bsf SENSOR_M,0

chk_s2 btfss SENSOR,2
goto chk_s3
bsf SENSOR_M,0

chk_s3 btfss SENSOR,3
goto chk_sX
bsf SENSOR_M,0

chk_s4 btfss SENSOR,4
goto chk_s5
bsf SENSOR_M,0

chk_s5 btfss SENSOR,5
goto chk_s6
bsf SENSOR_M,0

chk_s6 btfss SENSOR,6
goto chk_s7
bsf SENSOR_M,0

chk_s7 btfss SENSOR,7
goto chk_sX
bsf SENSOR_M,0

chk_sX return

MOTOR movfw SENSOR_M
andlw b'00001111'
call Get_Code
movwf PORTA

call Delay_100us
call Delay_100us
call Delay_100us
call Delay_100us
call Delay_100us

movlw b'00110000'
movwf PORTA
call Delay_100us
call Delay_100us
return

Get_Code addwf PCL
retlw b'00110111'
retlw b'00111111'
retlw b'00110111'
retlw b'00111111'

retlw b'00110111'
retlw b'00110111'
retlw b'00110111'
retlw b'00110111'

retlw b'00110101'
retlw b'00110111'
retlw b'00110101'
retlw b'00110111'

retlw b'00111111'
retlw b'00110111'
retlw b'00110111'
retlw b'00110000'





; *************** Delays ***********************************************************

Delay_100us movlw 0x1F
movwf d1

again2 decfsz d1,f
goto again2

goto $+1
return

Delay_50us movlw 0x0F
movwf d1

again3 decfsz d1,f
goto again3

return

;
; ************* End of the program ************************************************
END ;End of the program
 
It looks to me like you copied this off of someone. It sounds like a school project, and trust me, professors don't take too kindly to plagiarism....

Do you have a specific question you need help with? We are not going to simply give you all the answers (even if we could). We need more specifications: What are the guidelines? What exactly are you trying to do? And what have YOU yourself done so far?
 
Last edited:
I need to come up with a buggy with 4 sensors which is able to move and detect objects infront. when it detect the object infront it will turn. which is basically, 1 wheel will turn right and the other will turn left, thus turning the buggy( the buggy has only 2 big wheels.) i have done the schematic and i am going to hatch the board soon. so i am trying to start on the program. I just need to have a guide line on how do i start writing the program part by part. How do i make use of the program MPlab. i do not wish for the answers to be given, i want to understand what i am writing as well. Sorry if i sound like i just want the answers
 
What kind of sensors are you talking about here? Ultrasonic? Touch? Infrared?

At least now we're starting to get somewhere :)
 
You could purchase e.g. a PICkit 2 or 3 tutorial kit and teach yourself the basics of PIC programming. I like the PICkit 2 and the Low Parts Count board demo because the hardware is very simple. However, the tutorial is all in assembly. Normally, I'm not a fan of assembly because it's not portable, but in this case it helps you learn the chip architecture and you'll probably want to use assembly for the smaller PIC chips. There's a tutorial for the PICkit 3 that uses the C compiler and it's also good (in fact, the documentation is a bit better and more polished). Either would be good for learning and go for roughly $50. Earlier this year a friend visited for a week and he bought a number of these PIC thingies and we taught ourselves enough to be dangerous. We're both senior software engineers, so the programming part was pretty easy. It might be a bit more challenging for a newbie. It was perhaps 2 or 3 days to work through the tutorials.
 
sorry was so busy trying to do my program this few days. I have done the look up table for my buggy. Both the top and the bottom of the sensor.


; Program: [Stepper_Motor.asm]; Written by: Foo DF; Date: 29-Aug-2006
; This program configures PortC RC0-7 as OUTPUT to control the L293D Motor Control Board
;
; ************* HEADER **************************************************************
LIST p=16F876 ;PIC16F876 is the target processor
#include "P16F876.INC" ;Include header file
__config H'3F39' ;Osc=XT; WDT=Off;
;Power Up Timer=Off; Brown Out Detect=Off
;LVP=Disabled, Flash Program Write=Enabled,
;Data EE Read Protect=Off; Code Protect=Off
;
;************** File Register Variable use ******************************************
cblock H'20'
NUMBER
d1 ;use for Delay subroutine
d2
d3
d4
SENSOR
TOP
BOTTOM
TOP_motion
BTM_motion

endc
;
; ************* Reset Vector ********************************************************
org H'00'
clrf STATUS
movlw 0x00
movwf PCLATH
goto Initial
;
; ************* Port B and C I/O Initialisation *************************************
Initial
bsf STATUS,RP0 ; Select BANK 1
; banksel TRISB
movlw B'00000000' ; Set PortB to O/Ps
movwf TRISB ; PortB setting is done!
movlw B'11111111' ; Set PortC to I/Ps
movwf TRISC ; PortC setting is done!
movlw B'00000000' ; Set PortA to O/Ps
movwf TRISA ; PortB setting is done!
movlw B'00000110'
movwf ADCON1

;
; ************* Main Program *******************************************************
banksel PORTA
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Start
Enable_BS170 movlw b'00110000'
movwf PORTA
call SENSE
call TOPP
call BTM
goto Start
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

SENSE movfw PORTC
movwf SENSOR
movwf PORTB
return



TOPP
movfw SENSOR
andlw b'11110000'
movwf TOP
swapf TOP,w
call Top_Table
movwf PORTA
return


BTM
movfw SENSOR
andlw b'00001111'
movwf BOTTOM
swapf BOTTOM,w
call BTM_Table
movwf PORTA
return
; *************** Delays ***********************************************************

Delay_100us movlw 0x1F
movwf d1

again2 decfsz d1,f
goto again2

goto $+1
return

Delay_50us movlw 0x0F
movwf d1

again3 decfsz d1,f
goto again3

return


Top_Table
addwf PCL
retlw b'00110000' ;stop (Stop,stop)(0000)
retlw b'00110001' ;Left Turn (stop,forward)(0001)
retlw b'00110010' ;right turn (move,stop)(0010)
retlw b'00110011' ;reverse (reverse, reverse)(0011)
retlw b'00110100' ;turn left (stop,move)(0100)
retlw b'00110101' ;forward (move,move)(0101)
retlw b'00110110' ;turn right(move,stop)(0110)
retlw b'00110111' ;turn left(stop,move)(0111)
retlw b'00111000' ;turn right(move stop)(1000)
retlw b'00111001' ;turn right(move stop)!1001)
retlw b'00111010' ;turn right(move stop)(1010)
retlw b'00111011' ;turn right(move stop)(1011)
retlw b'00111100' ;forward (move move)(1100)
retlw b'00111101' ;turn left (stop move)(1101)
retlw b'00111110' ;turn right (move stop)(1110)
retlw b'00111111' ;stop (stop stop)(1111)





BTM_Table

retlw b'00110000' ;stop (Stop,stop)(0000)
retlw b'00110001' ;Left Turn (stop,forward)(0001)
retlw b'00110010' ;right turn (move,stop)(0010)
retlw b'00110011' ;forward (move, move)(0011)
retlw b'00110100' ;forward (move,move)(0100)
retlw b'00110101' ;turn left (stop,move)(0101)
retlw b'00110110' ;turn right(move,stop)(0110)
retlw b'00110111' ;forward(move,move)(0111)
retlw b'00111000' ;forward(move move)(1000)
retlw b'00111001' ;turn left(stop move)!1001)
retlw b'00111010' ;turn right(move stop)(1010)
retlw b'00111011' ;forward(move move)(1011)
retlw b'00111100' ;forward (move move)(1100)
retlw b'00111101' ;turn left (stop move)(1101)
retlw b'00111110' ;turn right (move stop)(1110)
retlw b'00111111' ;move (move move)(1111)



; ************* End of the program ************************************************
END ;End of the program



This is what i have come up with so far. But i am having trouble connecting the look up table to the motor. My buggy has 2 motors as well. The problem i have is the sub routine for the motor. i know i need to declare a new sub routine which is motor. but how do i continue up from there
 
oh i have gone through the pickit the school have given me, Stepper motor, ultrasonic and IO tests. However the programming i have done for those arent the same for my buggy.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top