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.

Do help me analyze this program in PIC16F84A,,

Status
Not open for further replies.

janethnx

New Member
-I ask for help in making a 5-minute delay in PIC.. someone on this site (Gayan) had posted it.. I don't really understand most of the codes and on how it runs..



list p=16F84A
#include <p16F84A.inc>

__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC

d1 equ 0x0D
d2 equ 0x0E
d3 equ 0x0F
d4 equ 0x10

org 0X000
goto Init

org 0X004
retfie


Init

bsf STATUS,RP0
clrf TRISB
clrf TRISA
bcf STATUS,RP0

clrf PORTA
clrf PORTB

Main

bsf PORTB,0
call Del5M
bcf PORTB,0
call Del5M
goto Main

Del5M

movlw 0x54
movwf d1
movlw 0xA1
movwf d2
movlw 0xFD
movwf d3
movlw 0x02
movwf d4

Delay_0

decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto $+2
decfsz d4, f
goto Delay_0

;5 cycles
goto $+1
goto $+1
nop
return

end
 
If you put the code into MPLAB and use the simulator you can step through it and see what it does. When the simulation gets to the delay routine put your cursor on the nop at the end, right click and select "run to cursor" to skip the delay code.

Mike.
 
Mplab

-I try what you said, but it only stops for a while, then continues again.. Did I do the wrong move?

-I used animation,, was it?

If you put the code into MPLAB and use the simulator you can step through it and see what it does. When the simulation gets to the delay routine put your cursor on the nop at the end, right click and select "run to cursor" to skip the delay code.

Mike.
 
You can step through your code using F7 but the delay routines will take too long to step through. A simple way to skip the delay routine would be to change the numbers that go into d1 to d4 to 1.

IE,
Code:
Del5M			 
		movlw	[COLOR="Red"]0x01[/COLOR]
		movwf	d1
		movlw	[COLOR="red"]0x01[/COLOR]
		movwf	d2
		movlw	[COLOR="red"]0x01[/COLOR]
		movwf	d3
		movlw	[COLOR="red"]0x01[/COLOR]
		movwf	d4
Delay_0
		decfsz	d1, f
		goto	$+2
		decfsz	d2, f
		goto	$+2
		decfsz	d3, f
		goto	$+2
		decfsz	d4, f
		goto	Delay_0
;5 cycles
		goto	$+1
		goto	$+1
		nop
		return

To view the port value select menu View->Special Function Registers.

Mike.
 
You can step through your code using F7 but the delay routines will take too long to step through. A simple way to skip the delay routine would be to change the numbers that go into d1 to d4 to 1.
Or just press F8 instead of F7 when in the "Main" loop. This will step over the subroutines automatically. The sub is still executed and simmed, but you just miss the joy of many nested delay loops. But the code may take long to execute even using this method because you are trying for a 5 minute delay, so Pommies temporary mod has it's uses.
 
Last edited:
RE: delay program

-I did try what you've said,, thanks. But the matter is I don't the matter with me is that I don't really understand the codes.. especially the $+1 and those RPO.. whats that for?

-The codes were just given in this site. I don't know how it flows.. I did try to go over with it but I have a hard time.

Or just press F8 instead of F7 when in the "Main" loop. This will step over the subroutines automatically. The sub is still executed and simmed, but you just miss the joy of many nested delay loops. But the code may take long to execute even using this method because you are trying for a 5 minute delay, so Pommies temporary mod has it's uses.
 
Last edited:
If you look in the sticky at the top of the microcontroller section there are lots of links to tutorials on the web that you should read.

Mike.
 
-I did try what you've said,, thanks. But the matter is I don't the matter with me is that I don't really understand the codes.. especially the $+1 and those RPO.
$ means PC or program counter and is an address value. The $ changes depending on where it is located in your code. $+1 is a cheat of sorts that allows you to avoid using an address label in your program. Personally, I don't like it as it can make code harder to understand especially when people do stuff like goto $+12 which makes you count instructions to find where it jumps to. They are using goto $+1 to waste time. It wastes more time in a single instruction than a simple nop.
I see no reference to RPO in this thread so i don't know what questions you have about it. :confused:
 
Last edited:
RPO is the STATUS bit to change banks.
Used for chips who have bank select.

Certain commands need to be done in certain banks. The TRIS register is in bank 1, and the PORT is in bank 0. RPO is the command to change banks.

Read the data sheet of your PIC available at the Microchip site.

(Here I am giving advice now... :) ...)
 
Last edited:
$+1 and RP0 in delay programs

-Thanks,, that means that $+1 and $+2 or anything with $ help you move through codes which create delay coz' goto has two clock cycles?

-I have this codes using that one it is 100 % running, but with put to circuit in will only light til there is a supply.. What's the matter with it?

-By the way the codes was given to me only. The one who sent those codes said that it is the same in the Code Generating Calculator in the piclist...

$ means PC or program counter and is an address value. The $ changes depending on where it is located in your code. $+1 is a cheat of sorts that allows you to avoid using an address label in your program. Personally, I don't like it as it can make code harder to understand especially when people do stuff like goto $+12 which makes you count instructions to find where it jumps to. They are using goto $+1 to waste time. It wastes more time in a single instruction than a simple nop.
I see no reference to RPO in this thread so i don't know what questions you have about it. :confused:
 
RP0 not RPO

-Now I understand,, I thought we are declaring some variables here.. Its a zero not an O..

-But why is it that these codes don't run correctly when in fact it 100% running when tested in MPLAB?

-Can you check this codes?

__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC

d1 equ 0x0D
d2 equ 0x0E
d3 equ 0x0F
d4 equ 0x10

org 0X000
goto Init

org 0X004
retfie


Init bsf STATUS,RP0
clrf TRISB
clrf TRISA
bcf STATUS,RP0

clrf PORTA
clrf PORTB

Main bsf PORTB,0
call Del5M
bcf PORTB,0
call Del5M
goto Main

Del5M movlw 0x54
movwf d1
movlw 0xA1
movwf d2
movlw 0xFD
movwf d3
movlw 0x02
movwf d4
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto $+2
decfsz d4, f
goto Delay_0

;5 cycles
goto $+1
goto $+1
nop
return

end


RPO is the STATUS bit to change banks.
Used for chips who have bank select.

Certain commands need to be done in certain banks. The TRIS register is in bank 1, and the PORT is in bank 0. RPO is the command to change banks.

Read the data sheet of your PIC available at the Microchip site.

(Here I am giving advice now... :) ...)
 
You code looks fine except for the missing include file.

If it is working in the simulator and not in hardware then the problem could be your external oscillator. Have you got the correct resistor/capacitor combination to get a 4MHz oscillator. I'm not even sure if an RC oscillator can go that fast.

The other thing that may be wrong is you may not be waiting long enough. Isn't your delay routine 5 minutes long? I would change this to 1 second until you get something working.

Mike.
 
RE:quote for delay program

-I have that include file I just forgot to include in the codes I sent..

-Yes it is working in the simulator and I have waited long enough- more than the supposed 5 minutes.

-Maybe the problem is the external clock.. I don't have one. Should I need it? Do you have the circuit? I am playing so dumb here, sorry.

You code looks fine except for the missing include file.

If it is working in the simulator and not in hardware then the problem could be your external oscillator. Have you got the correct resistor/capacitor combination to get a 4MHz oscillator. I'm not even sure if an RC oscillator can go that fast.

The other thing that may be wrong is you may not be waiting long enough. Isn't your delay routine 5 minutes long? I would change this to 1 second until you get something working.

Mike.
 
-Maybe the problem is the external clock.. I don't have one. Should I need it? Do you have the circuit? I am playing so dumb here, sorry.

Yes, it will not work without a clock. Check the datasheet for how to connect the resistor and capacitor or, better still, use a crystal instead.

Mike.
 
crystal oscillator

-I have not tried to connect a clock oscillator,, I have an crystal oscillator here. Is clock oscillator the same as crystal? In the handbook the clock oscillator has 4 pins but the crystal has only two.. Do I need other things for it to work?

Yes, it will not work without a clock. Check the datasheet for how to connect the resistor and capacitor or, better still, use a crystal instead.

Mike.
 
I have an crystal oscillator here. Is clock oscillator the same as crystal? In the handbook the clock oscillator has 4 pins but the crystal has only two.. Do I need other things for it to work?
You need to supply power (5V) and ground to your oscillator. Then feed the output of the oscillator into pin 16 of the 16F84A. Do you have a link to the datasheet for it? You'll need to change the config word to this:
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC
How fast is the oscillator that you have? The max speed that the PIC can be clocked at would be 20Mhz so as long as your don't exceed that you should be fine.
 
RE:crystal oscillator

-I have here a handbook,, Am I right if I say that the other pin of the crystal is to be supplied with 5V, connect it with the +of the 0.1uf capacitor. Then to the 14th pin of the PIC, then ground the leg of the capacitor? And the other pin of the crystal is to pin 16 of the PIC.

-Was that all? Also changing the config..

-Mine is 4 Mhz crystal oscillator.

You need to supply power (5V) and ground to your oscillator. Then feed the output of the oscillator into pin 16 of the 16F84A. Do you have a link to the datasheet for it? You'll need to change the config word to this:
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC
How fast is the oscillator that you have? The max speed that the PIC can be clocked at would be 20Mhz so as long as your don't exceed that you should be fine.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top