Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Basic PIC16F84A Question

Status
Not open for further replies.

komita

Newbie level 3
Joined
Jan 13, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,330
Hello everybody, after a long time I am trying to get back to my favorite hobby .. electronics.

I am trying to get familiar with PIC programming but I am having some trouble.
Attached is a picture of my circuit design in Multisim, and following is the code from the PIC.

What I am trying to accomplish is get the pic to tun on both LED's for a few seconds then turn them off for a few seconds. But what ends up happening is they blink very quickly and then stay turned off for a few seconds. I suspect a programming mistake but cannot find it.

(on a side note, I am just beginning to design circuits myself and was wondering if you could tell me what I have done wrong/right on this one, as you can see the output voltage at the LED's is around 1.055V (for the split second they are on)/ Why so low? I was looking for something close to 5V similar to the input?

Any help appreciated.

CODE:
########################################
#include "p16f84a.inc" ; This includes PIC16F84A definitions for the MPASM assembler

STATUS equ 0x03
TRISA equ 0x85
TRISB equ 0x86
PORTA equ 0x05
PORTB equ 0x06
COUNT1 equ 08h
COUNT2 equ 09h

GOTO START

START
;BCF STATUS, RP0 ;BANK 0
CLRF PORTA ;clear bth ports statuses to zero
CLRF PORTB

BSF STATUS,5 ;set the 5th bit of status register to on (this transfers us to Bank 1 to manipulate settings)
MOVLW b'0000000' ;mpve zeros to working
MOVWF PORTB ;then move them to portb statuses (mening output)
MOVLW b'111' ;set this to working
MOVWF PORTA ;then set porta to input
BCF STATUS,5 ;set the 5th bit of status register to zero (this transfers us to Bank 0 to manipulate values)


REDO

CLRF PORTB ; set all zeros to portB

movlw 0xFF ;First put the value of b255 in the W register
movwf COUNT1 ;Now move it to our 08h register.
movlw 0x2 ;First put the value of b2 in the W register for the second count (so it will be 255 x 2 = 510)
movwf COUNT2 ;Now move it to our 09h register

Loop2
decfsz COUNT1,1 ;Subtract 1 from 255
goto Loop2 ;If COUNT is zero, carry on.
decfsz COUNT2,1 ;Subtract 1 from 255
goto Loop2 ;Go back to the start of our loop.


MOVLW b'1111111'
MOVWF PORTB


movlw 0xFF ;First put the value of b255 in the W register
movwf COUNT1 ;Now move it to our 08h register.
movlw 0x2 ;First put the value of b2 in the W register for the second count (so it will be 255 x 2 = 510)
movwf COUNT2 ;Now move it to our 09h register

Loop1
decfsz COUNT1,1 ;Subtract 1 from 255
goto Loop1 ;If COUNT is zero, carry on.
decfsz COUNT2,1 ;Subtract 1 from 255
goto Loop1 ;Go back to the start of our loop.

GOTO REDO

END
#################################################

---------- Post added at 18:50 ---------- Previous post was at 18:50 ----------

CIRCUIT:
###################################################################################
**broken link removed**

Uploaded with ImageShack.us
 

Hi,
It seems your program is correct, only thing is Oscillator-clock is not countered.

First you have to find time required for an instruction-cycle (Tcy). PIC needs 4 clock-cycles for one Tcy. Hence if you use 4MHz Xtal then 1Tcy need 1us time.

Then you have to count how many Tcy will consumed within the delay-loop(loop2). Tcy need to execute each instruction(opcode) is mentioned in Data-sheet of PIC under the topic "Instruction set summery".

Then you can find blinking-delay of LEDs. According to your code and assuming 4MHz oscillator clocks the PIC; blinking delay is about 2ms. It may be not correctly visible in your simulation.

You will need 3 registers as counters to implement few-seconds delay (again assuming 4MHz oscillator)

Wish you success !
 

Hi,
The output voltage is the forward voltage of the LED. Connect a 220R resistor between PIC output and LED. Then observe output at PIC pin, you'll find 4-5v. This is of course assuming your code is correct as I've not gone through it.

Hope this helps.
Tahmid.
 
Last edited:

I found multiple errors in your source code.
#include "p16f84a.inc" ; This includes PIC16F84A definitions for the MPASM assembler

STATUS equ 0x03 ; remove this line (redundant)
TRISA equ 0x85 ; remove this line (redundant)
TRISB equ 0x86 ; remove this line (redundant)

PORTA equ 0x05 ; remove this line (redundant)

PORTB equ 0x06 ; remove this line (redundant)


COUNT1 equ 08h ;error! because 08h register is a SFR (EEDATA) cannot use as a GPR. Use 0Ch
COUNT2 equ 09h ;error! Because 09h register is a SFR (EEADR) cannot use as a GPR. Use 0Dh

GOTO START

START
;BCF STATUS, RP0 ;BANK 0
CLRF PORTA ; clear both ports statuses to zero
CLRF PORTB

BSF STATUS,5 ;set the 5th bit of status register to on (this transfers us to Bank 1 to manipulate settings)
MOVLW b'0000000' ;mpve zeros to working
MOVWF PORTB ;this line Change to MOVWF TRISB; set to PORTB output.
MOVLW b'11111111' ;set this to working
MOVWF PORTA ;this line Change to MOVWF TRISA; set to PORTA input;
BCF STATUS,5 ;set the 5th bit of status register to zero (this transfers us to Bank 0 to manipulate values)


REDO

CLRF PORTB ; set all zeros to portB

movlw 0xFF ;First put the value of b255 in the W register
movwf COUNT1 ;Now move it to our 08h register.
movlw 0x2 ;First put the value of b2 in the W register for the second count (so it will be 255 x 2 = 510)
movwf COUNT2 ;Now move it to our 09h register

Loop2
decfsz COUNT1,1 ;Subtract 1 from 255
goto Loop2 ;If COUNT is zero, carry on.
decfsz COUNT2,1 ;Subtract 1 from 255
goto Loop2 ;Go back to the start of our loop.


MOVLW b'1111111'
MOVWF PORTB


movlw 0xFF ;First put the value of b255 in the W register
movwf COUNT1 ;Now move it to our 08h register.
movlw 0x2 ;First put the value of b2 in the W register for the second count (so it will be 255 x 2 = 510)
movwf COUNT2 ;Now move it to our 09h register

Loop1
decfsz COUNT1,1 ;Subtract 1 from 255
goto Loop1 ;If COUNT is zero, carry on.
decfsz COUNT2,1 ;Subtract 1 from 255
goto Loop1 ;Go back to the start of our loop.

GOTO REDO

END
 
Last edited:

Thanks guys, I see I have done some mistakes as expected. I will work on this tomorrow change the code and post how it turned out. Now do I need to add a crystal or is it ok as it is? Also if I add the resistors to get close to 5v voltage should I add the serially to the LED's or parallel?
 

Add the resistor in series, ie, between the PIC output pin and the LED anode.

You also need to add a crystal oscillator between pins 15 and 16.

Hope this helps.
Tahmid.
 

Now do I need to add a crystal or is it ok as it is?

Yes, you will need a crystal (along with proper capacitors - see the datasheet) or ceramic resonator.

**broken link removed**
 

Hey guys, thanks for all the help so far, it is much appreciated! I made the modification and for the most part the circuit acts as expected, except the LED still just blink, instead of staying ON for the same amount of time as OFF.

I attached a oscilloscope to see whats happening (picture below) and as you can see the ON and OFF states are clearly visible, but if you look close you will notice a little spike in the voltage on the up-slope and down-slope. It is at this points that the LED's BLINK, and are off for the rest of the time (even if the voltage higher than 0).

The second and last thing that's bothering me is that even after adding the 220ohm resistors in series with the LED's I still don't get the 5V output I would think should be there. Also as you can see in the oscilloscope the ON voltage before and after the resistor is a little higher than last time (1.8V) but still low.

Does the fact that I am turning both LED's ON at the same time have something to do with it? If yes, is there a way to get 5V to each output in this case?

**broken link removed**

Uploaded with ImageShack.us
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top