
Any of you knowledgeable chaps know anything about PICs?
Specifically, I want to create a PWM outout to power a light. The only catch is I need to be able to dim the light (easy part) but the hard part is I
want to introduce some random bounce to the signal so the light flickers slightly as if blown by the wind.
Any ideas?
Sure, what bit are you having problems with? (I don't know your background/knowledge level). I've actually worked on an LED candle flicker project before, though not on PIC, but most of my projects have been PIC based lol.
I can put a circuit together to take a PWM signal from the PIC and feed it into the light.
The issue is writing the code to make the PWM flicker at a set brightness level. So I can have it flickering at full power or flicker at half power
etc.
Incidentally how did you go about doing your candle flicker circuit? It might be possible to use something other than the pic for that aspect of it if
its easier. I know PWM stuff takes up a lot of capacity within the PIC.
My programming ability with PICs stops at simple(ish) logic. Mainly done with flow diagrams unless I get really stuck and have to resort to basic.
[Edited on 25/1/12 by tegwin]
Bit banging a PWM will indeed take up a lot of resources if you want to run at a reasonable frequency, but using a PIC with on board PWM's
(either the standard CCP, ECCP or the special motor PWM some have) is a matter of writing a few registers.
The on board PWM's are easy enough to change on the fly as updates are only done at the end of the cycle.
If you've got a filter network on the PWM pin then you'll get out a voltage that varies between 0v and Vdd in a percentage based on the duty
(i.e. 50% duty will give Vdd/2)
So you need to work out the voltage profile of flicker "events" and simulate that sequence of voltages, probably updating on another timer.
You could use a table (or tables) or find some simple equation(s) that approximate well enough. Preferably with some randomness being used to avoid
it looking too artificial.
I'd recommend using the free Hi-Tech C compiler even on small devices as it's much easier to use than assembler, we even use it on the 6pin
10F parts at work.
For checking the sanity of my PWM calculations I tend to use this site.
http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html
quote:
Incidentally how did you go about doing your candle flicker circuit? It might be possible to use something other than the pic for that aspect of it if its easier. I know PWM stuff takes up a lot of capacity within the PIC.
Have a look here for some sample code: http://mondo-technology.com/
It's in assembler, but is quite easy to follow.
quote:
; Candle Simulation
; 6/04 Luhan Monat
;
; Simulate flicker of candle using incandescent lamp
;
device PIC12F675,intrc_osc,pwrte_on,wdt_off
org 20h
del1 ds 1
del2 ds 1
pcnt ds 1
temp ds 1
lev1 ds 1
lev2 ds 1
level ds 1
rbuf ds 5
LAMP = gp.5
org 0
goto start
org 4
reti
start bsf RP0
movlw 0
movwf GP
movlw 127
movwf OSCCAL
bcf RP0
bsf rbuf,0 ;seed random number
movlw 127
movwf lev1 ;initial light level
movwf lev2
; Main Loop
; Create hi and low power levels
; Switch between levels
candle movf lev1,w
movwf level
call power ;do lev1 power
call rando
andlw 7
btfss z
goto :run ;skip 7 out of 8
call rmid ;generate new hi and low levels
movwf lev1
sublw 0
movwf lev2
:run movf lev2,w ;do lev2 power
movwf level
call power
goto candle
; PWM power control
power movlw 100 ;set flicker rate: higher=slower
movwf pcnt ;set loop count
:p1 movf level,w ;get target level
movwf del1 ;set 1st delay
sublw 0
movwf del2 ;set 2nd delay
bsf LAMP ;power on
:p3 nop
decfsz del1 ;do 1st delay
goto :p3
bcf LAMP ;power off
:p4 nop
decfsz del2 ;second delay
goto :p4
decfsz pcnt
goto :p1
; find sum of 4 random numbers
; skews results around 127
rmid call rando
andlw 3fh
movwf temp
call rando
andlw 3fh
addwf temp
call rando
andlw 3fh
addwf temp
call rando
andlw 3fh
addwf temp,w
ret
; Pseudo Rando Number
; "Chop Suey Machine"
rando movf rbuf,w
addwf rbuf+1,w
movwf rbuf+1
addwf rbuf+2,w
movwf rbuf+2
addwf rbuf+3,w
movwf rbuf+3
addwf rbuf+4,w
movwf rbuf+4
bcf c
rlf rbuf+4
btfsc c
bsf rbuf+4,0
movf rbuf+4,w
addwf rbuf
ret
end
quote:
Originally posted by coyoteboy
Indeed the free hitec compiler works a treat, even if it does create MASSIVELY bloated code in non-optimised (free) mode - if you're planning to do a lot of other stuff on a small package you might want to consider getting the one with the biggest program space possible.
Hmm, might have to get some PICs and give it a go...
It seems a bit "overkill" to go for a fully blown arduino microprocessor for what should be quite a simple task.
Need to do some more research and see if PIC is the best option for the flicker or if I can find a more analogue approach. (It does not have to be
PWM, I could use a voltage to control brightness. So a V of 1.2v with a random change of a few 10ths of a volt occasionally. Electronics are not my
strong point. But always happy to learn 
I'd be sure it's possible to simulate with analog, but that's less in my comfort range so I'll bow out of offering help there.
Arduino is overkill for most of the projects it's employed on but it does the job and it's easy to get into. That said, a nice pic
programmer and the software for it, when comfy with it will open many many doors for new ideas.
[Edited on 26/1/12 by coyoteboy]
quote:
Originally posted by tegwin
Hmm, might have to get some PICs and give it a go...
It seems a bit "overkill" to go for a fully blown arduino microprocessor for what should be quite a simple task.