Printable Version | Subscribe | Add to Favourites
New Topic New Poll New Reply
Author: Subject: Any electronics people here? PICs?
tegwin

posted on 25/1/12 at 04:21 PM Reply With Quote
Any electronics people here? PICs?

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?





------------------------------------------------------------------------------------------------------------------------
Would the last person who leaves the country please switch off the lights and close the door!

www.verticalhorizonsmedia.tv

View User's Profile Visit User's Homepage View All Posts By User U2U Member
coyoteboy

posted on 25/1/12 at 04:49 PM Reply With Quote
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.
View User's Profile E-Mail User View All Posts By User U2U Member
tegwin

posted on 25/1/12 at 04:58 PM Reply With Quote
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]





------------------------------------------------------------------------------------------------------------------------
Would the last person who leaves the country please switch off the lights and close the door!

www.verticalhorizonsmedia.tv

View User's Profile Visit User's Homepage View All Posts By User U2U Member
iank

posted on 25/1/12 at 06:18 PM Reply With Quote
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





--
Never argue with an idiot. They drag you down to their level, then beat you with experience.
Anonymous

View User's Profile View All Posts By User U2U Member
coyoteboy

posted on 25/1/12 at 06:29 PM Reply With Quote
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.


It was originally a tutorial for a student who started on arduino as their first attempt and then progressed it to a simple 8 pin PIC for smaller packaging, it drove 3 pins with 3 different intensities (RGB essentially) and flickered all in a normalised form (i.e. changed the intensity of all of them relative to their original brightness so as not to change colour). I may still have the code somewhere but that was software PWM'd (bit banging as above) as the package chosen was cheap and tiny and doing nothing else in operation.

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.

View User's Profile E-Mail User View All Posts By User U2U Member
Madinventions

posted on 25/1/12 at 09:41 PM Reply With Quote
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



Ed.





Mojo build diary: http://www.madinventions.co.uk

Solo music project: Syrrenfor http://www.reverbnation.com/syrrenfor

View my band website:
http://www.shadowlight.org.uk

http://www.eastangliankitcars.co.uk/

View User's Profile Visit User's Homepage View All Posts By User U2U Member
MikeRJ

posted on 25/1/12 at 10:19 PM Reply With Quote
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.


The older version of the free compiler (PICC-Lite) was vastly better if you can find a copy somewhere. Limited device support but full optimisation.

The Atmel (e.g. Arduino) is a much better bet if you want to develop in C on an 8 bit micro with a free compiler however.

[Edited on 25/1/12 by MikeRJ]

View User's Profile View All Posts By User U2U Member
tegwin

posted on 25/1/12 at 10:26 PM Reply With Quote
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





------------------------------------------------------------------------------------------------------------------------
Would the last person who leaves the country please switch off the lights and close the door!

www.verticalhorizonsmedia.tv

View User's Profile Visit User's Homepage View All Posts By User U2U Member
coyoteboy

posted on 26/1/12 at 02:10 AM Reply With Quote
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]

View User's Profile E-Mail User View All Posts By User U2U Member
MikeRJ

posted on 26/1/12 at 08:33 AM Reply With Quote
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.


You don't have to buy an Arduino to use an Atmel, and the Atmel in the Arduino is roughly equivalent to a upper/mid range PIC anyway i.e 18Fxxxx (though AVR is a bit faster in most cases). You can buy 8 pin AVR's that you can still program using WinAVR (The free GCC compiler for the Atmel AVR devices). The advantage of the Arduino is that it comes with a bootloader already programmed so you can simply plug it into a USB port and download you compiled code without any extra hardware. You can get very small versions of the Arduino to integrate into projects, have a look here.

That said a PIC will do the job very well, but the free compiler isn't a patch on WinAvr. You simply need to buy a PIC with a CCP module (or ECCP) which can be configured for PWM operation. The lamps brightness can be adjusted by setting a mean PWM level, and the flickering can be simulated by adding pseudo-random values to this mean PWM value. Pseudo random values can easily be generated using a Liner Feedback Shift Register (LFSR) which takes only a handful of lines of code - lots of info available on this on the web, or I can dig out an example if you get stuck. For the best realism you might want to run a couple of PN generators, one for flicker intensity (the amount to adjust PWM by) and one for flicker timing (interval between PWM updates).

Don't forget you will need some means of getting the compiled code into the PIC. The PicKit2/PicKit3 programmers (or one of the many cheap clones available) are a good way to start as they integrate into the MPLAB development environment.




[Edited on 26/1/12 by MikeRJ]

View User's Profile View All Posts By User U2U Member

New Topic New Poll New Reply


go to top






Website design and SEO by Studio Montage

All content © 2001-16 LocostBuilders. Reproduction prohibited
Opinions expressed in public posts are those of the author and do not necessarily represent
the views of other users or any member of the LocostBuilders team.
Running XMB 1.8 Partagium [© 2002 XMB Group] on Apache under CentOS Linux
Founded, built and operated by ChrisW.