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.

How to program 18f PICs

Status
Not open for further replies.

asp1987

New Member
Hi..
Could anyone provide some links on 18 series PIC programming? I wrote a simple LED flashing program. But it doesn't work. Pls help.
Code:
	MOVLW 0x0F
	MOVWF BSR,1   ; bankselect
	BCF TRISD,1,1   ; rd1 output
ASP
	BSF PORTD,1,1  ; led on
	CALL DELAY
	BCF PORTD,1,1  ; led off
	CALL DELAY
	GOTO ASP

DELAY
LOOP1
	DECFSZ COUNT1,F,1
	GOTO LOOP1
	DECFSZ COUNT2,F,1
	GOTO LOOP1
	RETURN

COUNT1 EQU 0xFF00  ; variable dec.
COUNT2 EQU 0xFF01
 
Last edited:
It's not complete to say the least. You need a CONFIG statement, an ORG 0x000 and include file would be nice. An END is a must for MPASM if that's what you're using.
 
Sorry for not putting it up before.
Code:
LIST P=18F4620
#include <P18F4620.INC>
CONFIG OSC=XT

COUNT1 EQU 0xFF00 ; var dec.
COUNT2 EQU 0xFF01

RESET_VECTOR	CODE	0x0000
goto	Main
Main:
	MOVLW 0x0F
	MOVWF BSR,1 ; bankselect
	BCF TRISD,1,1
ASP
	BSF PORTD,1,1 ; led on
	CALL DELAY
	BCF PORTD,1,1 ; led off
	CALL DELAY
	GOTO ASP

DELAY
LOOP1
	DECFSZ COUNT1,F,1
	GOTO LOOP1
	DECFSZ COUNT2,F,1
	GOTO LOOP1
	RETURN
        END
 
The problem could be in the circuit if not code. If you can post the schematic then we can see.
 
Hi thanks.. It worked. But got another problem with bcf, bsf instructions.
Code:
 LIST P=18F4620
 #include <P18F4620.INC>

 CONFIG WDT=OFF
 CONFIG MCLRE=ON
 CONFIG DEBUG=OFF
 CONFIG LVP=OFF
 CONFIG OSC=XT

RESET_VECTOR	CODE	0x0000
 goto	Main
Main:
	CLRF PORTD
	CLRF LATD
	CLRF TRISD
	MOVLW 0x00
	MOVWF TRISD

ASP
	MOVLW 0xFF
	MOVWF PORTD   ; all portd pins on
;	BSF PORTD,1,1
	CALL DELAY
	MOVLW 0x00
	MOVWF PORTD   ; all portd pins off
;	BCF PORTD,1,1
	CALL DELAY
	GOTO ASP

DELAY
LOOP1
	DECFSZ 0x00,F,1  ; variables 00 and 01 as pointed out
	GOTO LOOP1
	DECFSZ 0x01,F,1
	GOTO LOOP1
	RETURN
        END

Here replacing bsf, bcf with movlw byte instruction did the trick. but not the other way around. Circuit just as before.
Could you tell me why it is so?
 
bsf PORTD, 1 = bit set ,file D, pin one. You do not need bsf PORTD,1,1
 
Hi,

You won't find any tutorials in 18F, but do look at and follow the 16F tutorials from people like Nigel - the ideas are still useable on the 18F.
Should still stick at the 18F as they are easier to use in the long run.

Don't use just numbers for your variables - plus you need to specify them before your ' main' code.

Like the BSF just mentioned you do not need the extra paramenters on the DECFSZ,d1,F.




; Variable definitions
; ====================


cblock 0x010

d1 ; delay routine work files
d2

endc
 
I think that you need to understand the banking on 18F processors better.

There are 16 banks of registers allowed for, 0 - 15. Each is 256 bytes.
The special function registers are in the top half of bank 15
All operations that involve registers (including special function registers) will access whichever bank the bank select register (BSR) is set to.

So to read or write to the file registers in bank 0 you need to set the BSR to 0
To set a bit in a port (which is a special function register) you need to set the BSR to 15

However, there is also another way of getting to some of the file registers and all of the special function registers. Each command has a flag that selects either the bank that the BSR is pointing to or the access registers. The access registers are the bottom half of bank 0 and the top half of bank 15, which is where the special function registers are.

For example:-

Code:
addwf  0x10, f, 1
will:-
add w to 0x010 if the BSR is 0,
add w to 0x110 if the BSR is 1,
add w to 0x210 if the BSR is 2
etc

Code:
addwf  0x10, f, 0
will always:-
add w to 0x010, whatever is in the BSR


MPLAB makes this flag select the access registers for all commands that use the access registers, if you simply miss out the last argument (0 or 1)

Where you wrote:-

Code:
BSF PORTD,1,1

the final "1" made the command use whatever bank the BSR was set to. That is wrong for PORTD, because PORTD is within the access registers.

If you had written:-
Code:
BSF PORTD,1
then MPLAB would have used the access registers and it would have worked, as it did when you used:-

Code:
MOVWF  PORTD

Had you written:-
Code:
MOVWF  PORTD, 1
that would also have failed because it would have needed the BSR set correctly.
 
Wow.. All of u have been of very great help. Thank you.

By the way the earlier project-SMS based device controller/ security system about which i had posted some doubts- it worked. :)
 
Last edited:
Hardware PWM using L293D

Hi,
Is it possible to control two motors via a L293D motor driver using a PICs hardware PWM? For L293d, 01/10 means motor forward/reverse. 00/11 means slow stop/fast stop, right? So basically we need the pic pwm pins for any one motor to have complementary outputs under normal operation. How can we achieve this?

Read something similar to this in 18f4620's datasheet. But couldn't understand it properly.
 
Is it possible to control two motors via a L293D motor driver using a PICs hardware PWM? For L293d, 01/10 means motor forward/reverse. 00/11 means slow stop/fast stop, right? So basically we need the pic pwm pins for any one motor to have complementary outputs under normal operation. How can we achieve this?
On the 293, pins 1 and 9 are the enable pins. PWM those for speed control. Each pair of drivers controls one motor, so a 293 can control two motors with forward & reverse.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top