Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 7th August 2008, 06:49 PM   (permalink)
Default How to program 18f PICs

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 by asp1987; 7th August 2008 at 06:50 PM.
asp1987 is offline  
Old 7th August 2008, 07:01 PM   (permalink)
Default

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.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now  
Old 7th August 2008, 07:16 PM   (permalink)
Default

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
asp1987 is offline  
Old 7th August 2008, 07:30 PM   (permalink)
Default

The problem could be in the circuit if not code. If you can post the schematic then we can see.
__________________
Its not the Practice that makes a man perfect. Its the Man who makes the practice perfect
----- Pradeep K. Shima -----
pkshima is offline  
Old 7th August 2008, 07:38 PM   (permalink)
Default

Also the variable RAM starts at 0x0000

Try running with the MPLAB simulator, and why the bankselect stuff?
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now  
Old 7th August 2008, 08:36 PM   (permalink)
Default

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?
asp1987 is offline  
Old 7th August 2008, 09:05 PM   (permalink)
Default

bsf PORTD, 1 = bit set ,file D, pin one. You do not need bsf PORTD,1,1
__________________
All those who believe in psycho kinesis, raise my hand.

AllVol is offline  
Old 7th August 2008, 09:16 PM   (permalink)
Default

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
richard.c is offline  
Old 7th August 2008, 11:43 PM   (permalink)
Default

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.
Diver300 is offline  
Old 8th August 2008, 06:12 PM   (permalink)
Default

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 by asp1987; 8th August 2008 at 06:22 PM.
asp1987 is offline  
Old 11th August 2008, 07:07 PM   (permalink)
Default 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.
asp1987 is offline  
Old 11th August 2008, 07:15 PM   (permalink)
Default

Quote:
Originally Posted by asp1987 View Post
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.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 11th August 2008 at 07:15 PM.
futz is offline  
Old 11th August 2008, 07:22 PM   (permalink)
Default

Thank you.
asp1987 is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Is there any reason to program PICs in C++? speakerguy79 Micro Controllers 6 24th April 2008 11:28 AM
Program PICs without a PC! Isn't is so cool? Funny NYPD Micro Controllers 22 19th April 2008 03:56 AM
How to program DIP PICs with Breadboard Funny NYPD Electronic Books 9 15th April 2008 05:15 PM
Odd problem, My program kills pics 2camjohn Micro Controllers 6 27th November 2007 06:30 PM
Anybody program PICs? ab0tj Electronic Projects Design/Ideas/Reviews 1 13th April 2004 07:51 PM



All times are GMT. The time now is 05:35 PM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker