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.

code help

Status
Not open for further replies.

gastonanthony

New Member
my code doesn't do what it is supposed to do.the program is for the pic to increment for every closing of the switch(5 volts input to rb0). it just continuously count up to 9 and then back to zero(binary). can someone help me wiht this code or schematic? here's the code
Code:
PROCESSOR	16f84a
#include "P16f84a.inc"

org 0x00
COUNT	EQU 0X0c
TEMP	EQU 0X0d
goto	main

org	0x04
movwf	TEMP
incf	COUNT,1
movlw	0x0a
subwf	COUNT,0
btfss	STATUS,0
goto	carry_on
goto	clear
carry_on
bcf	INTCON,0X01
movlw	TEMP
retfie
clear
clrf	COUNT
bcf	INTCON,1
retfie

main
bsf	INTCON,7
bsf	INTCON,4
bsf	INTCON,1
bsf	STATUS,5
movlw	0X01
movwf	TRISB
movlw	0X10
movwf	TRISA
bcf	STATUS,5
loop
movf	COUNT,0
movwf	PORTA
goto	loop
end

and here's the schematic:
 

Attachments

  • 7segcount.gif
    7segcount.gif
    17.2 KB · Views: 581
gastonanthony said:
my code doesn't do what it is supposed to do.the program is for the pic to increment for every closing of the switch(5 volts input to rb0). it just continuously count up to 9 and then back to zero(binary). can someone help me wiht this code or schematic? here's the code
Code:
PROCESSOR	16f84a
#include "P16f84a.inc"

org 0x00
COUNT	EQU 0X0c
TEMP	EQU 0X0d
goto	main

org	0x04
movwf	TEMP
incf	COUNT,1
movlw	0x0a
subwf	COUNT,0
btfss	STATUS,0
goto	carry_on
goto	clear
carry_on
bcf	INTCON,0X01
movlw	TEMP
retfie
clear
clrf	COUNT
bcf	INTCON,1
retfie

main
bsf	INTCON,7
bsf	INTCON,4
bsf	INTCON,1
bsf	STATUS,5
movlw	0X01
movwf	TRISB
movlw	0X10
movwf	TRISA
bcf	STATUS,5
loop
movf	COUNT,0
movwf	PORTA
goto	loop
end

and here's the schematic:
And ER oscilator mode is not the best solution...
Don't put MCLR HIGH without resistor!
Where are LEDs current limiting resistors???
 
hehe :oops: sorry, i forgot about the current limiting resistors, i got this schematic and code from a pdf tutorial file. i already modified the clock by connecting a capacitor at pin 16 and ground(to make an RC network) but the circuit still counts upto 9 automatically and doesn't respond to the switch :?:
 
Did you disable the WDT?
Insert this into your source code:
Code:
	list	p=16F84A
	#include	<p16F84A.inc>
	__CONFIG	_CP_OFF & _WDT_OFF & _PWRTE_ON &_RC_OSC
 
why does the mclr need a resistor?for current limiting?i thought it is ok to connect it directly to 5V because the tutorial does this for all of it's circuits :oops:
 
gastonanthony said:
why does the mclr need a resistor?for current limiting?i thought it is ok to connect it directly to 5V because the tutorial does this for all of it's circuits :oops:
It's a good pracise to put it there. Microchip strongly recomend to put it there to avoid Latch-up current... :D
 
gastonanthony said:
why does the mclr need a resistor?for current limiting?i thought it is ok to connect it directly to 5V because the tutorial does this for all of it's circuits :oops:

It's fine to directly connect it to 5V, the only real reason to use a resistor is as a pull-up for a reset switch down to 0V.

One advantage of using the 16F628, rather than the obselete 84, is that you can switch MCLR to be an extra I/O pin, which then internally connects MCLR to 5V.
 
I take it as a good practice, because sometimes when I want to reset the PIC which doesn't have a MCLR button to GND, I can't do it if there is no resistor between VDD and MCLR.

And Yes, use 16F628 instead 84. 628 is cheaper and has more peripherals... (internal 4Mhz oscilator...)
 
You failed to restore the W register correctly in the interrupt service routine.

The correct code in the interrupt service routine should be as follows:

Code:
    movf   TEMP,W 
    retfie

If an interrupt in the main loop occurs between the two instruction steps:

Code:
loop:
    movf   COUNT,0 
; <--------------------interrupt here
    movwf   PORTA 
    goto   loop

The last value of the W in the interrupt sevice routine will be written to PORTA.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top