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.

Problem with PIC16F84A

Status
Not open for further replies.

byronyau

New Member
I use this microcontroller to create an 4 digits frequency counter.

The code can be got from below address.

**broken link removed**
-------Save the html file and use notepad to open it--------

or

**broken link removed**

In fact, the code can work well.
However, if I add some codes behind the below code, the microcontroller will always go back to "Reset" part. So the counter function is skipped.

Out_range movfw freq+0 ;Underflow (freq = 0)?
iorwf freq+1, 0
iorwf freq+2, 0
btfsc STATUS,2 ;If zero means underflow
goto underflow
btfsc freq+0,7 ;Overflow (freq > 7ffffff)?
goto overflow

-----> add code here will cause the problem mentioned before. If the register "freq" is modified, the microcontroller will skip the counter function and always go back to "Reset" region. If I want to add more codes in my program, how to solve the problem?

Config of the 16F84A:
20MHz XTAL as oscillator

PORTA is connected to 2 to 4 decoder 74LS139 to select a 7-segment display among four displays.
PORTB is connected to common anode 7-segment displays' pins.
RTCC is set to count the input pulse signal.

Thank you very much!!!
 
The problem with the program and your modification is that it doesn't take much to break it. The first thing I checked was the use of computed goto in the look up tables.

Code:
table	macro	label		;Define look up table
label	addwf	PC
	endm

First of all, a lookup table using the above code should never cross a 256 instruction word page boundary. That means the position of the "addwf PC" instruction and the last item of the table should be on the same page.

Worse, the program assumes that the value of PCLATH register is 0. It is never initialized by the program and relies on the good PIC to set it to 0 on reset. Therefore, it assumes that all of the lookup table items must be within the first 256 program words.

If you assemble the unmodified code, the lookup table sits at the very edge of this boundary. Adding just one instruction, even a lousy NOP, will break it. The solution is to move the lookup tables to the front row, ahead of the "Reset" label but behind the interrupt vector (location 04h).

Code:
	org	0x00		;Processor reset vector
	goto	Reset		;Go to beginning of program
	org	0x04		;Interrupt vector location
	retfie			;Return from interrupt
;
; <- INSERT LOOKUP TABLES HERE.
;
Reset	bsf	STATUS,5
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top