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.

can someone tell me why this isn't working?

Status
Not open for further replies.

erospawn

New Member
Its PWM code using a lookup table.. If I run it through mplab sim, it follows the code through. However, in real life the LED just stays full on...

Code:
;PWM test using a lookup table.


list P=16f676
#include <p16f676.inc>
	__config _HS_OSC & _BODEN_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CP_OFF 
	
	cblock		20h
	BH,BL,wait,del_clk1,del_clk2,run,runn,pc,tmp,count
	endc
	t equ      0x02
RESET_VECTOR CODE	0X000
goto main	
	
main	
	

	bsf	STATUS,RP0		;bank 1
	movlw	0x00
	movwf	OSCCAL
	movlw	0x00
	movwf	TRISC		;make all PORTC outputs
	movlw	0xff
	movwf	TRISA		;make all PORTA inputs
	movlw	0x07
	movwf	OPTION_REG
	bcf		STATUS,RP0
	CLRF	TMR0
	BCF		INTCON,5
	movlw	0x07		;turn off comparitors
	movwf	CMCON
    clrf    pc	

	
start
	movlw	0xff			;adjustment for speed of fade
	movwf	runn	
l1	btfsc	PORTA,0			;call to see if low, if low make fade slower
    goto	l1
    call	loopfi
    goto    start
    
loopfi	
	call	loop20
    return
	

loopfo
	call	firstt
    return

loop20
	movf	runn,0
	movwf	run
on
	movlw	0xff
	movwf	PORTC		;turn on leds
    movf	tmp,BH		;set on time of duty cycle

loopon
	decfsz	BH,1		;count down the on time
	goto	loopon
	movlw	0x00		;turn leds off
	movwf	PORTC
	movf	tmp,BH      ;set off time of duty cycle
	
loopoff
	incfsz	BH,1		;count up to the off time
	goto	loopoff
	decfsz	run,1	;run the PWM for a moment
	goto	on
	incf	pc
	movf    pc,w
	sublw   .10
	btfsc	STATUS,2
	clrf	pc
	movf	pc,w
	call    value
	movwf   tmp
	goto    loop20
	return

value
    addwf	t
    retlw	0x08
    retlw	0x0f
    retlw	0x16
    retlw	0x1d
    retlw	0x24
    retlw	0x2b
    retlw	0x32
    retlw   0x39
    retlw	0x40
    retlw	0x47
    retlw	0x4e
    retlw	0x55
    retlw	0x5c
    retlw   0x63
    retlw	0x6a
    retlw	0x71
    retlw	0x78
    retlw	0x7f
    retlw	0x86
    retlw	0x8d
    retlw	0x94
    retlw	0x9b
    retlw	0xa2
    retlw	0xa9
    retlw   0xb0
    retlw	0xb7			
	end
 
Last edited by a moderator:
Are you saying it should be working? The light is supposed to fade-in to full brightness..its just sitting there at full bright.
 
OK, here's the circuit.. its a 16f676 with a 4Mhz crystal .. I don't have any pcb type software so I just drew it up.. It works with a different program, only difference is I consolidated it all into one loop with a lookup table.. so, Im not sure I get whats happening...
 

Attachments

  • Img_3434.jpg
    Img_3434.jpg
    15.1 KB · Views: 274
Hey guys,

Thanks.. In my haste I forgot to add the M_CLR wire tied high. I downloaded a PIC simulator program that goes through the code and shows what all the registers are doing. I just ran the code and stepped through the first and second iterations and realized that the same value was being moved back into the W register after the lookup and thusly, the light wasn't doing anything but staying on the whole time. Aside from moving some lines of code around to look better anyway.. the line that I changed was

movwf tmp for whatever reason, this line just moved the registers address into W, 0x28.

to movf tmp,w
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top