unable to blink 3 leds in a sequence

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

unable to blink 3 leds in a sequence

Postby vinod » Tue Oct 14, 2014 9:55 pm

Hi All,
I have written the assembly program using MPLAB IDE 8 for microchip '12F675' to blink 3 LEDs in a sequence. Upon compiling and running the program on a circuit, only one LED (Green LED) blinks consistently as expected however the rest of the two LEDs (Yellow and Red) does not glow. In the circuit all the anodes of the LEDs are connected together to a 330 ohms resistor which in turn is connected to +5 volts supply. The cathodes are individually connected to the PIC microcontroller.
Please find attached the program that I have written: (LED.asm). Kindly request to review and let me know if there is any changes that needs to be done, to make this program work.

Need help, the attached program LED.jpg to glow(blink) 3 LEDs in sequence is not working for some reason.
Kindly request to review and advise.

ERRORLEVEL 2 ; SUPPRESS MESSAGES AND WARNINGS
#INCLUDE P12F675.INC ; PROCESSOR SPECIFIC VARIABLE


__CONFIG _FOSC_INTRCIO & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOREN_OFF & _CP_OFF & _CPD_OFF

CBLOCK 0x20
DEL
DEL1
ENDC

; START POINT FOR THE PIC MICROCONTROLLER
ORG 0x0000 ; PROCESSOR RESET VECTOR
NOP
CALL INIHW
GOTO START

; INTERRUPT POINT FOR THE PIC MICROCONTROLLER
ORG 0x0004
RETURN

; START POINT FOR THE USER APPLICATION
ORG 0x0010
START CALL YLED ; GLOW YELLOW LED
CALL DELAY ; DELAY
CALL GLED ; GLOW GREEN LED
CALL DELAY ; DELAY
CALL RLED ; GLOW RED LED
CALL DELAY ; DELAY
GOTO START ; REDO

; DELAY PROGRAM
DELAY MOVLW 0xFF ;
MOVWF DEL1 ;
WAIT1 MOVLW 0xFF ;
MOVWF DEL ;
WAIT DECFSZ DEL, F ;
GOTO WAIT ;
DECFSZ DEL1, F ;
GOTO WAIT1 ;
RETURN ;

; GLOW YELLOW LED
YLED BCF GPIO, 4 ; SWITCH ON YELLOW LED
BSF GPIO, 2 ; SWITCH OFF GREEN LED
BSF GPIO, 5 ; SWITCH OFF RED LED
RETURN
; GLOW GREEN LED
GLED BSF GPIO, 4 ; SWITCH OFF YELLOW LED
BCF GPIO, 2 ; SWITCH ON GREEN LED
BSF GPIO, 5 ; SWITCH OFF RED LED
RETURN
; GLOW RED LED
RLED BSF GPIO, 4 ; SWITCH OFF YELLOW LED
BSF GPIO, 2 ; SWITCH OFF GREEN LED
BCF GPIO, 5 ; SWITCH ON RED LED
RETURN

; INTIALIZE PERIPHERIALS
INIHW BCF STATUS, RP0 ; SWITCH TO BANK 00
CLRF GPIO ; CLEAR ALL PORT LATCHES
CLRF INTCON ; DISABLE ALL INTERRUPTS
CLRF PIR1 ; CLEAR ALL PERIPHERAL INTERRUPT FLAGS
MOVLW 0x07
MOVWF CMCON ; DISABLE COMPARATOR
BSF STATUS, RP0 ; SWITCH TO BANK 01
CLRF PIE1 ; DISABLE ALL PERIPHERAL INTERRUPTS
CLRF ANSEL ; DISABLE ADC
MOVLW 0x0B
MOVWF TRISIO ; SET 0,1,3 GPIO AS I/P 2,4,5 GPIO AS O/P
CALL 3FFh ; RETREIVE FACTORY OSCILATOR CALIBRATION BYTE
MOVWF OSCCAL ; LOAD IT TO OSCCAL
BCF STATUS, RP0 ; SWITCH TO BANK 00
RETURN

END
Attachments
LED Circuit1.JPG
LED Circuit1.JPG (16.14 KiB) Viewed 1722 times
vinod
 
Posts: 2
Joined: Tue Oct 14, 2014 9:15 pm

Re: unable to blink 3 leds in a sequence

Postby AussieSusan » Wed Oct 15, 2014 2:45 am

I must admit I really don't know assembler but a few general debugging techniques might help you localise the problem.
Try running a program that simply turns on one of the LEDs. If that works for all LEDs then you know that aspect works at least.
If any single LED fails to light, check to make sure it is in the right way around - this in one of my common mistakes!
Next try turning on all 3 LEDs at the same time. I'm guessing that the current draw from all 3 LEDs might be a bit much for the single resistor and you are getting an excessive voltage drop across it. If each LED works individually but not all 3 LEDs together, then you should use 3 resistors.
If all that works then you have a coding error and I'll leave it to others to help you there.
Susan
AussieSusan
Verified identity
 
Posts: 93
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: unable to blink 3 leds in a sequence

Postby Joseph Watson » Wed Oct 15, 2014 3:29 am

My take on things is that even if all three LEDs will not light at the same time when tested as AussieSusan suggests, the cause can easily be that they have different forward voltage drops, especially when different colors of LEDs are involved. Whichever LED has the lowest drop will get the most current and may even prevent the other two from lighting at all. Since the intent is to light only one at a time, a single resistor should work OK as long as the code sets up the three output pins properly. Still the suggestion that three resistors be used instead of just one is probably a good idea until you get things working correctly. Then you can return to using just one.

I see your CALL to 3FFh to retrieve the factory oscillator calibration byte. When calibrated at the factory, a RETLW instruction is stored at 3FFh to return the calibration value. However, some PIC programmers erase the entire Flash memory during a program cycle, including this special location at the top of memory. If this happens, your calibration value has been erased as well as the RETLW instruction intended to return it to your program. As a result, control will never return from the CALL to 3FFh. As a simple check, I suggest that you try commenting out the CALL 3FFh instruction and see if your program begins to run, even though its timing might be off a bit.

Good luck with your project.
NCR once refused to hire me because I was too short. I'm still waiting on my growth spurt.
User avatar
Joseph Watson
 
Posts: 49
Joined: Sat May 31, 2014 8:06 pm
Location: Ohio, USA
PIC experience: Experienced Hobbyist

Re: unable to blink 3 leds in a sequence

Postby jtemples » Wed Oct 15, 2014 5:41 am

I don't know if it's the problem, but you have serious read-modify-write issues in your code.
jtemples
Verified identity
 
Posts: 173
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Re: unable to blink 3 leds in a sequence

Postby vinod » Wed Oct 15, 2014 10:53 am

Hi All,

The issue is resolved by ric

This is a classic example of the "Read-modify-write" problem.
Have a read of http://www.xargs.com/pic/c-faq.html#rmw and http://www.microchip.com/forums/m478014.aspx

In short, the problems happens when you use a string of BSF or BCF instructions on an output port register.
e.g.

; GLOW YELLOW LED
YLED BCF GPIO, 4 ; SWITCH ON YELLOW LED
BSF GPIO, 2 ; SWITCH OFF GREEN LED
BSF GPIO, 5 ; SWITCH OFF RED LED
RETURN

If GPIO,4 is high, the first instruction sets it low, but the second instruction is executed while the external pin is still switching from high to low, it reads the port first, sees bit 4 high, so sets it high again.
You MIGHT be able to avoid this by just adding some NOPs. e.g.

; GLOW YELLOW LED
YLED BCF GPIO, 4 ; SWITCH ON YELLOW LED
NOP
BSF GPIO, 2 ; SWITCH OFF GREEN LED
NOP
BSF GPIO, 5 ; SWITCH OFF RED LED
RETURN

Otherwise you will need to rearrange your code to write the whole port in one instruction.

I thank everybody for their concern shown to resolve the problem
vinod
 
Posts: 2
Joined: Tue Oct 14, 2014 9:15 pm

Re: unable to blink 3 leds in a sequence

Postby ric » Thu Oct 16, 2014 3:22 am

That was over on the Microchip board, at: http://www.microchip.com/forums/FindPost/826386
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 453
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 1 guest