Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help for PIC 16F84 Line follower program

Status
Not open for further replies.

tomch

Newbie level 4
Newbie level 4
Joined
May 11, 2010
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Swansea
Activity points
1,328
Line follower program

Hi there,
I have recently started my own little project to keep me occupied in my spare time. I have recently been getting into writing simple programs using C and C++. I started looking into ways of implimenting written programs into electronics hardware as I have quite a lot of expirence with the design of simple electronic circuits. This is where I stumbled upon assembly language. After looking at some projects that impliment programming into electronics, I decided that a line follower looked like a simple project to start off with.
I found a simple example of a line follower that uses a microcontroller known as a 16f84. I managed to easily build the sensor circuit without any difficulty.

**broken link removed**

After looking over the datasheet for the 16f84 I am able to understand most of what is going on. The thing that is throwing me off is the use of binary code, combined with the delays used at the bottom of the code. Any help with either of these issues will be greatly appreciated. Below is the code that the person has used. Thank you for your time.

#include p16f84a.inc

start: bSf STATUS, RP0
movlw B'11110000'
movwf TRISB
bCf STATUS, RP0
clrf PORTB

repeat: movlw B'000000101'
movwf PORTB
call delay
btfss PORTB,5
call left
btfss PORTB,6
call right
goto repeat


right movlw B'000001000'
movwf PORTB
call delay
return

left movlw B'00000010'
movwf PORTB
call delay
return

delay: clrf 0x01
delay0: clrf 0x01
delay1: decfsz 0x01, 1
goto delay1
delay2: decfsz 0x11, 1
goto delay0
return
END
 

Hi,

It makes a change to see someone moving from C to Assembler - will try and help make things clearer.

First the 16F84 /A chip used in so many web based projects is almost obselete, so whenever possible try and use a 16F628A or 16F88 which should run the same code with only the smallest of changes which the complier should soon point out to you.

The use of different Radix is common in assembler, you wil find Hex, Decimal and Binary often used in the same piece of code.
Look in a book or on the web for a table showing the values in Hec, Binary, Decimal and also Ascii - it a list worth printing out as is the pic16 assembler instruction list found towards the end of the chips datasheet.

Often the opening lines of the code will specify Radix = Decimal so the system automatically assumes any value is decimal, unless its specified as hex or binary

Binary is frequently used when setting up the i/o Ports , each bit representing each of the 8 port lines, so its easy to see how you are controlling them - however you can equally use hex if you want.

The delay at the end is a common problem for first timers.

The frequency of the oscillator, in this case a R/C set up, is divided by 4 to give the system clock speed, so if it was a 4mhz R/c the system clock is 4mhz /4 = 1 mhz

Most assembler instructions take 1 system clock cycle to execute, so to create a little delay we exectute loop that decrements a counter on each pass, when it reaches 00 it ends and a delay has occured - though it is quite short.

I will let you work the exact time out, but say each loop is 6 instructions, and it decrements the counter variable 255 times before it reaches 00 again.

If you run two or more nested loops then it possible to build up larger delays -all dependant on the system clock speed and the number of delay loops.

You can spent time working out you own delays but most use the ready made delay loop calculators found on the web.

Hope that clear..
 

    tomch

    Points: 2
    Helpful Answer Positive Rating
Cheers for the help understanding the delay loop, much appreciated. The thing that is throwing me off still, is the following two lines. I am unsure why they are 9 digits long. I thought they should be 8 digits long.
repeat: movlw B'000000101'
right movlw B'000001000'

Below is the circuit diagram that I have been provided with.
**broken link removed**

**broken link removed**
 

Hi,

Sorry , missed that 9 bits - for such old code not sure why they have used that - not very helpfull.

Basically, yes you are right, you would normally only code it with 8 bits, just remove the left hand zero and make your code 8 bits for clarity.

Thing is, when using assembler with the 8 bit chips whatever you specify, when you Build it, your code, be it in binary or decimal etc, it still gets converted to the hex code, View the Disassembley listing.

If the binary or decimal value is converted and exceeds hex FF or dec 255 or b 1111 1111 then the complier will give an error

To prove it, change right movlw B'000001000' to right movlw B'100001000' then build again and you will see the compile error.

Afaid I could not get though to that site to view the jpeg - will try later.
 

    tomch

    Points: 2
    Helpful Answer Positive Rating
after looking at the circuit diagram shouldn't,
right movlw B'000001000'
be...
right movlw B'00000100'?
Thanks a lot for this help, it is really appreciated as this has been boggling my mind for a fair few days now!
 

Hi,


Have added some notation to the assembler code, hopefully that should clear things up for you.

Shout if things are still muddled - we all get stuck like that at times.
 

    tomch

    Points: 2
    Helpful Answer Positive Rating
delay: clrf TMR0
delay0: clrf TMR0

delay1: decfsz TMR0, 1 ( 1 mean decrement and place result in file - in this case TNR0 register)
goto delay1

delay2: decfsz 0x11, 1 (0x11 is RAM variable)
goto delay0
return

in hex code can de used all types of radix, but in hex format is best - like 0x00
 

Hi again, I completely understand the code now.
I understand the operation of the oscillator connected to pin 16 and if I am not mistaken, the reason pin 10 and pin 13 are connected to ground is to keep these pins low.
I am now curious about the reset input (pin 4) and the bit of circuitry thats connected to it, can you explain the operation of it?
Cheers once again!
 

Hi,

Well those four parts are drawn a little misleadingly.

First the Mclre , its function is to do a Hard Reset if it is taken Low ( to 0v ) for a few milliseconds.
Generally you do not bother with a Reset switch, indeed on some chips you can define this pin as a Digital Input instead.

For normal operation it should be pulled up to the +5v line with a resistor, this diagram shows 1K though 4k7 or 10k are equally fine.
As well as the pull up resistor it is quite common to see a 100nf cap taken from the Mclre pin to 0v to help elimainate any interference.

The resistor R3 4.7ohm is in the +5v supply line to the pic chip circuit - this is not normally used - they are probably using it to act as a filter - for most circuits its not needed.

The 100uf cap is being used as the smoothing cap, and would normally be part of your psu , unless it was a long distance away.

The 100nf acts as high frequency suppressor - this is probably the most important part, alway use one on any pic; it should be fitted as close as possible to the +5v VDD and 0v VSS pins
 

    tomch

    Points: 2
    Helpful Answer Positive Rating
Brilliant!
The last thing that is given me jip, is the association between the oscillator and the delay. Really unsure as to why the creator has chosen this speed for the oscillator.
Any more help would be really great!
Thanks in advance.
 

Hi,

Well the RC for the oscillator is the cheapest method, although 4 mhz is popular for many 16F project, either using a crystal or ,as on some chips, their own internal oscillators.

What frequency the 10k /27pf into the pic16f84 gives I am not too sure, will check up on it, but think it will be about 1 mhz.

The reason for the delay is just to let the motor run for enough time and to let the input switches contacts settle , otherwise the pic is just too fast for the real world devices.

Once the actual RC frequency is established you can work out the delay time, I would guess about 200ms.
In Mplab you can use the SIMulator and its stopwatch to check the time for you.

Just to make sure that circuit and code was good I have just run it in another full simualtor to check it all works ok - which it does.

You might also want to look at this link which gives a truth table for the l293 inputs.
If you work things out you will see it is actually only using 2 bits to control the motor / one half of the l293

https://www.8051projects.net/dc-mot...ng/l293d-interfacing-with-microcontroller.php
 

    tomch

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top