My problems with P16F877A

Status
Not open for further replies.

talhak

New Member
Hello to everyone,

I'm new here and happy to see an active forum about micro-controllers.. I'm working on PIC16F877A.. I've some problems with it.. I'll be happy if someone helps me.. Thanx..

First of all this is my code for "Look-up Table".. It takes the value from 1-9 and show them on LEDs.. I really tried to find my mistake but I couldn't..

Code:
list p=16f877a
#include<p16f877a.inc>
__CONFIG H'3FF0'
NUM EQU h'21'
ADDON EQU h'22'
ORG h'00'
GOTO START

INIT:
	BSF STATUS,5
	CLRF TRISB
	BCF STATUS,5
	CLRF PORTB
	MOVLW h'09'
	MOVWF NUM  ; it loops 9 times when it is zero, end program.
	MOVLW h'00'
	MOVWF ADDON ; looking the table; first take 1 and 2, 3, ... ,9
	BCF STATUS,2
	BCF STATUS,0
	RETURN


LOOKTABLE:
	MOVF NUM,0
	ADDWF PCL,1
	RETLW h'01'
	RETLW h'02'
	RETLW h'03'
	RETLW h'04'
	RETLW h'05'
	RETLW h'06'
	RETLW h'07'
	RETLW h'08'
	RETLW h'09'

SEND:
	MOVWF PORTB ; to leds
	RETURN

START:
	CALL INIT
LOOP:
	CALL LOOKTABLE
	CALL SEND
	INCF ADDON,1
	MOVLW h'00'
	SUBWF NUM,1
	BTFSS STATUS,0
	GOTO LOOP
	END
 
Last edited:
xxxxxxxxxxxxxxxxxxxxxxxxx

Please disregard that last part...

whats wrong with your code?
 
Last edited:
for me it ends after first run.
Code:
LOOP:
	CALL LOOKTABLE
	CALL SEND
	INCF ADDON,1
	MOVLW h'00'     ;You arent doing anything with this line (-1 maybe)
	SUBWF NUM,1    ; and this line
	BTFSS STATUS,0    ;maybe use a BTFSC
	GOTO LOOP
	END
 

I used MOVLW h'00' to update the value of W register; because it(value of W) always changes after calling LookTable unit.. So I used it.. And I used "Num" register to make a loop for 9 times.. 10th time it'll skip and will go the line "END"..
 
you dont see it tho. How are you looping if you are not subtracting anything?
Code:
MOVLW h'00'     ;You arent doing anything with this line (-1 maybe)
SUBWF NUM,1    ; and this line
You are subtracting 0 from NUM... maybe if you do this:
Code:
MOVLW h'01'     ;W = 1
SUBWF NUM,1    ;Subtract W(1) from NUM
MOVLW h'00'     ;Reset W to 0 (Not Needed)

You dont need the "MOVLW h'00'" since W changes or well... gets changed to the new value before (or in ) the lookup table.
 
Last edited:
Hey, I've been looking through your code and have spotted a few things.

Firstly, you inilize the register 'addon' with the value 0. and the register 'num' with the value 9. My understanding is that you use addon register to count up to 9 and the num register to test to see if the addon register has reached 9? This wouldn't work because binary doesnt start at 1 but at 0. eg count 9 starting at 0 and you would get 0 1 2 3 4 5 6 7 8. This can be solved easily by inizalising 'num' with the value of 10 (H'0A')

Secondly, you plan to use the register 'addon' to count but then use the value in the 'num' register in the look up table. Now as mensioned before, you inizalize this as the value 9 and it isn't changed by your code there fore will always equal 9; which brings me nicly onto the problem of the program displaying H'A7' at the start.

If you look at the look up table and run it through. You load the W register with the value in the num register, which we have discovered is and always will be nine. You add the value of the w register to the PCL. This increment the program counter by 9 which if you look below moves out of the look up table to the movwf comand.

Code:
LOOKTABLE:
	MOVF NUM,0    
	ADDWF PCL,1
	RETLW h'01'       ;0
	RETLW h'02'       ;1
	RETLW h'03'       ;2
	RETLW h'04'       ;3
	RETLW h'05'       ;4
	RETLW h'06'       ;5
	RETLW h'07'       ;6
	RETLW h'08'       ;7
	RETLW h'09'       ;8

SEND:
	MOVWF PORTB   ;9
	RETURN

This can be solved by adding a "retlw h'00'" just after 'ADDWF PCL,1'

Finaly, to test to see if 'addon' has reached 9, you are subtracting 0 from it and testing the carry bit in the status register. This does nothing as said by someone else. What you could do, (there are a few solutions to this) is to xor the value in the num register with the value in the addon register and then test the zero bit in the status register.


Code:
LOOP:
	CALL LOOKTABLE
	CALL SEND
	INCF ADDON,1
	MOVF NUM,W
	XORWF ADDON,W
	BTFSS STATUS,Z
	GOTO LOOP
	END

On a quick note, you do realise a look up table is not needed for what you are trying to do? If you send the contents of the 'addon' register to portb it would have the same results.
Also it would be a lot easier for you and other people to understand your code if it was properly commented and in a logical order.


Hope this helps
 
Last edited:
Dear Pepsiiuk,

First of all thanx a lot for your interest..

About the program,

I used "num" reg to count 9 times.. I always decrement it and when it equals 0 I finished the program.. I used "addon" register to add pcl 0 to 9 to reach all values of table.. All my controls are with "num" reg..

* ADDON: 0 1 2 3 4 5 6 7 8 => Take value from table (every time I inc addon to reach one more below line and take the value(retlw)

* NUM: 9 8 7 6 5 4 3 2 1 => for looping 9 times.. I always dec it and test the bit status,0(z).. if it's set, I finish the program(end)

------------------------------------------------------------------------
Yes I know it's a little complicated program and hard to understand, but it's our one of the subject which we have obligation to learn & do (we have final exam after 10 days)
 
This code effectively counts UP from 1-9:

Code:
	list p=16f877a
	#include <p16f877a.inc>

	__CONFIG H'3FF0'

NUM EQU h'21'
ADDON EQU h'22'

	ORG h'00'
	GOTO START

INIT:
	BSF STATUS,5
	CLRF TRISB
	BCF STATUS,5
	CLRF PORTB
	MOVLW h'09'
	MOVWF NUM  ; it loops 9 times when it is zero, end program.
	MOVLW h'00'
	MOVWF ADDON ; looking the table; first take 1 and 2, 3, ... ,9
	BCF STATUS,2
	BCF STATUS,0
	RETURN


LOOKTABLE:
	MOVF ADDON,0
	ADDWF PCL,1
	RETLW h'01'
	RETLW h'02'
	RETLW h'03'
	RETLW h'04'
	RETLW h'05'
	RETLW h'06'
	RETLW h'07'
	RETLW h'08'
	RETLW h'09'

SEND:
	MOVWF PORTB ; to leds
	RETURN

START:
	CALL INIT
LOOP:
	CALL LOOKTABLE
	CALL SEND
	INCF ADDON,1
	DECFSZ NUM,1
	GOTO LOOP
	END
 
To Count down i did:
Code:
	list p=16f877a
	#include <p16f877a.inc>

	__CONFIG H'3FF0'

NUM EQU h'21'
ADDON EQU h'22'

	ORG h'00'
	GOTO START

INIT:
	BSF STATUS,5
	CLRF TRISB
	BCF STATUS,5
	CLRF PORTB
	MOVLW h'0A'
	MOVWF NUM  ; it loops 9 times when it is zero, end program.
	MOVLW h'00'
	MOVWF ADDON ; looking the table; first take 1 and 2, 3, ... ,9
	BCF STATUS,2
	BCF STATUS,0
	RETURN


LOOKTABLE:
	MOVF NUM,0
	ADDWF PCL,1
	NOP
	RETLW h'01'
	RETLW h'02'
	RETLW h'03'
	RETLW h'04'
	RETLW h'05'
	RETLW h'06'
	RETLW h'07'
	RETLW h'08'
	RETLW h'09'

SEND:
	MOVWF PORTB ; to leds
	RETURN

START:
	CALL INIT
LOOP:
	CALL LOOKTABLE
	CALL SEND
	INCF ADDON,1
	DECFSZ NUM,1
	GOTO LOOP
	END
 

Ah, now I see what your trying to do. In that care AtomSoft's latest post will help you out.
 
Microcontroller problem or the software???

Hi there,
I'm have some problem with the control of the relay module.
I'm using port B from 16F876A to trigger the relay.
Without any loading on the relay pole, the device is switched perfectly with no problem.
The relay is rated at 10A 277VAC and i use to control some flourescent lights/incand bulb and motorised screen. When it use purely for switching lights everything working fine. But when it is use to control the motorised screen. It affect the rest of the i/o port.
For eg.
while i had light 'on' in portB.1
and i trigger screen up on portB.6
the moment i stop the screen up on portB.6
the rest of the portB.1 will all been reset to 0.
Why is this happening?
Even if i control a small motorised screen.

I have even connected the MOV on the relay contact (parallel). It does not solve the problem. I had tried to use a snubber network too, using RC network connected onto the relay contact (parallel as well). The problem remain.

I had discuss this with one of my collegue and he did mention about the microcontroller write/read issue. It is something abt when the controller write to the port and while the process of reading the port affect it to change the state of it.
I have running out of idea how to overcome this issue.

I'm using BasicMicros to write my code.
 
1. Start your own topic
2. before you write a new topic make sure you include a schematic and some code if you want some good help. (If you dont then you wont get help)
3. wait for some help and youll be happy.

4.. Do you have a diode on the relay? Answer this in the new topic you will create.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…