# HOWTO: Making your own digital driver



## PEU (Mar 14, 2008)

Since DJPark presented us with the POP driver concept (and then it was used by a myriad of other drivers) it was a mistery to me how to know when the circuit lost its power. Found many solutions to this problem, but none of them were as elegant as the original idea.

So I decided that was about time to do an extra effort.

I started by stripping a DealExtreme 5levels driver to its bare board and capture the schematics:







Its clear that the capacitor holds the power while the power is down in the ON-OFF-ON cycle that changes levels.

But then what happens? I didn't knew, so I posted a question in a Microchip Related Forum (Thanks Alain & Others!!) to see if someone else was able to help me.

The solution was found in less than a couple of hours, but it took ME a couple of days to understand it  

Here is the gist of it, to understand the following, some knowledge of PIC programming is needed.

The PIC chips (12F629 in this case) have a Brown-Out Detection feature that can be enabled at programming time by setting a bit in the configuration register. Its called the BODEN bit (Brown-out Detection ENABLE)

Then we have the capacitor that performs two functions in the circuit:
1) keep power when battery power is lost
2) by carefully selecting its value, you allow the uC to survive *n* milliseconds

Then we have the Brown-Out and Power on Reset bits at the PCON register.

In normal conditions, when the uC starts for the first time or when power was lost for a long time this bit is set at 0 (datasheet: TABLE 9-6: INITIALIZATION CONDITION FOR SPECIAL REGISTERS)

BUT, here is the KEY, if power was lost but our capacitor managed to keep the uC running in the ON-OFF-ON with voltage below vBOD (Brownout voltage, see datasheet) but above vreset (below this value uC cold resets) BOR will show that power was lost for a little while but then it was recovered.

To be able to detect changes in these bits, we must set them to 1 the line after we check their value, remember Brown-Out Reset restarts your program from the top, not from where it was running, the only difference this time is that the PCON register values aren't lost like what happens in a cold start.

The metacode would be something like this:


```
init configuration bit to enable BOD
init registers to proper values 
(depending on your program, main loop needs)

if PCON.1=1 and PCON.0=0 then this means BOD was detected
set these bits to 1
mainloop
your payload here
loop
end
```

I hope this help some of you guys to develop new drivers, I hope I can finish mine 

Another option can be used: instead of building a driver from scratch, why not reprogram already made ones to suit your need 

I must say thanks again to the people that helped me in this quest to learn new stuff, without them I would be still scratching my head.


Pablo


----------



## TranquillityBase (Mar 14, 2008)

> Another option can be used: instead of building a driver from scratch, why not reprogram already made ones to suit your need


 
+1 :twothumbs

Great thread Pablo!


----------



## PEU (Mar 14, 2008)

My prototype working with 3 levels 1-10-100%

Button presses simulate on-off cycles

http://www.veoh.com/videos/v6423799RQcCM87p



Pablo


----------



## chimo (Mar 14, 2008)

I love threads like this!! Thanks Pablo!


----------



## zipplet (Mar 16, 2008)

I've been looking into this due to my own driver design. Initially I wasn't going to use the brown out detect but a second signal line to measure the voltage before the diode (while the PIC still has power).

I think I'll use this method now as it's cleaner, thanks!


----------



## PEU (Mar 16, 2008)

With the method I described, if you only need to change levels, there is no need for a main loop, for me its just an infinite loop to keep the uC running, because its all done in the detection stage. 

[dream mode on]
My hope is that someone with better programming skills than me, help our community to develop a customizable firmware, for many of the cheap drivers available these days, most of them based in PIC12F629, something like Open Source Hardware.

I know its possible because is being done in a lot of areas, replace OEM firmware with custom ones (Sony PSP, iPOD, Linksys Routers, etc)

I suggested this years ago, but maybe I was a little ahead of time 


Pablo


----------



## WildChild (Mar 16, 2008)

PEU said:


> With the method I described, if you only need to change levels, there is no need for a main loop, for me its just an infinite loop to keep the uC running, because its all done in the detection stage.
> 
> [dream mode on]
> My hope is that someone with better programming skills than me, help our community to develop a customizable firmware, for many of the cheap drivers available these days, most of them based in PIC12F629, something like Open Source Hardware.
> ...



Is an infinite loop very power hungry with this IC? Usually, on a PC, an infinite loop tops the CPU at 100% level and is very power hungry...


----------



## zipplet (Mar 16, 2008)

Well I already made a thread elsewhere recently about my own design and I see no reason to not release the source code + hex file when it's complete for the benefit of the community  Others should do this too.

Such a low powered PIC uses negligable power but they *can be put to sleep* using a sleep instruction, waiting for an interrupt. This is probably best.


----------



## PEU (Mar 17, 2008)

Im using PIC12F683 because it has a PWM subsystem built in hardware, a nice feature, because you set it and then forget. 

While testing the software for my driver, I noticed that a couple of bits from the STATUS register must be also be checked, so if you use this uC, keep in mind this too 

The infinite loop is not power hungrier than other operations, so don't worry.


Pablo


----------



## MrAl (Mar 20, 2008)

Hello,

Hi Pablo how ya been?
What are you trying to build here, as what do you want it to do?

Yes i agree, an infinite loop in a PIC is much different than in infinite
loop in a PC, as for the PIC this is a typical operating condition, while
for the PC it means something is wrong somewhere.
There is one exception to this rule however, and that is when you want
to conserve power when the PIC is not being used (off or whatever but
power is still supplied). In this case you can 'shutdown' the PIC and
conserver battery power, then wake it up later if something has to be
done.


----------



## warlord (Mar 20, 2008)

PEU said:


> Another option can be used: instead of building a driver from scratch, why not reprogram already made ones to suit your need
> Pablo



Exactly! If you'll notice DX sku's 7612 and 6190 are exactly the same. The only difference it that one PIC is programmed with 5modes and the other is programmed with 16modes in 3 groups.

Maybe it's time I get into PIC programming...


----------



## zipplet (Mar 20, 2008)

I highly recommend getting into PIC programming. Pick up a PICkit 2 starter kit (including the demo board) which comes with everything you need to get going!

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023805


----------



## Mr Happy (Mar 20, 2008)

Maybe someone can answer a question that's been puzzling me. How do you get the program into a SMT chip, especially one that's already mounted on a board?

With a DIL package you could plug the chip into the programmer socket, load the program, and plug it back into its normal place. But SMT chips need soldering, they don't even have sockets. Does it mean there has to be an on-board programming interface on the actual driver? And where would that fit on a miniature driver board?

I'm sure there's an obvious answer here, but I'm not quite seeing it...


----------



## zipplet (Mar 20, 2008)

In circuit serial programming (ICSP).

All modern PICs support it, it requires soldering a few wires (MCLR, CLK, DAT, GND, VCC) to the target circuit or chip.

Alternatively, you can get adapter sockets for small SMT devices to program them using a socket based programmer for use before you solder the device into your circuit.


----------



## linterno (Mar 24, 2008)

Pablo:

Did you try to read the original program from PIC12F629? Where you able to do this? Did you measure the capacitor capacity? Did you try to program the device using ICSP?

Sorry for so many questions.

Juan C.

EDIT. Fixed Pablo's name. It was wrong before.


----------



## warlord (Mar 25, 2008)

:thinking:So when are we going to get the full "How-to" treatment?

You know, the details of which drivers to buy and how to pull and program our own PICs?


----------



## PEU (Mar 26, 2008)

linterno said:


> Pablo:
> 
> Did you try to read the original program from PIC12F629? Where you able to do this? Did you measure the capacitor capacity? Did you try to program the device using ICSP?
> 
> ...



No, and I don't plan to, that would be pirating their firmware. I used the schematic in the 1st post to learn and then explained what I found and what I was teached by asking in other forums. 
Regarding ICSP I need to build some kind of connector to reprogram the 12F629 without removing it from the board, as soon as I do it, I will let you know my findings. 
Worst case is having to remove the PIC, but the idea is to avoid that.



warlord said:


> :thinking:So when are we going to get the full "How-to" treatment?
> You know, the details of which drivers to buy and how to pull and program our own PICs?



As I said, my coding skills arent enough to reprogram one of DX boards, so I made my own using a slightly better microprocessor.

For anyone versed in C or ASM for PICs generating a HEX file to reprogram DX converters would be a piece of cake. I provided all the information needed to do the ON-OFF-ON detection.


Pablo


----------



## PEU (Mar 26, 2008)

MrAl said:


> Hello,
> 
> Hi Pablo how ya been?
> What are you trying to build here, as what do you want it to do?
> ...



Hi Al, sorry for not replying to your post earlier, how are you doing? hopefully fine!! 

Im designing a new flashlight and I wanted to build my own converter in the process, so these are my findings and since I was helped in other forum I wanted to do the same here.

You are right on with the differences in the infinite loops, in this case if I put the uC to sleep all its functions would be disabled till I wake it up again, so putting it to sleep is a no go, we need it running. 
The sleep function is usefull when you have the uC powered all the time and you use a soft turn on/off. In the off state, its a must to keep it consuming the smallest amount of current or else the batteries would drain. 
Consumption in some uC's in the sleep state, are in the order of uA, far less than the batteries self discharge.


Pablo


----------



## MatajumotorS (Mar 26, 2008)

PEU said:


> Since DJPark presented us with the POP driver concept (and then it was used by a myriad of other drivers) it was a mistery to me how to know when the circuit lost its power. Found many solutions to this problem, but none of them were as elegant as the original idea.
> 
> The PIC chips (12F629 in this case) have a Brown-Out Detection feature that can be enabled at programming time by setting a bit in the configuration register. Its called the BODEN bit (Brown-out Detection ENABLE)
> 
> ...


 
Great info on Brown-Out, i will try to use it :huh:


----------



## warlord (Mar 26, 2008)

PEU said:


> No, As I said, my coding skills arent enough to reprogram one of DX boards, so I made my own using a slightly better microprocessor.
> 
> For anyone versed in C or ASM for PICs generating a HEX file to reprogram DX converters would be a piece of cake. I provided all the information needed to do the ON-OFF-ON detection.
> 
> ...



Aw nuts! I was hoping that'd you or we (as a group) could develop a simple how to. You know. Instructions on what to buy. Basic primer in coding and a piecemeal coding chart where you could patch together driver settings from the code chart to make a driver to function as desired.

This is much more interesting than just swapping out drivers...


----------



## PEU (Mar 26, 2008)

I don't know how to make a configurable & stable ASM PWM routine using the PIC 12F629, besides that coding the app is a piece of cake.

Thats why I used the 12F683, because it has a PWM subsystem built in 

Besides this, maybe my lack in programming skills entices someone to learn how to program 


Pablo


----------



## MrAl (Mar 26, 2008)

Hi Pablo,

Im doing ok so far  Thanks.

I was wondering if you are doing a buck config or boost, or just
resistive PWM?


----------



## PEU (Mar 26, 2008)

MrAl said:


> I was wondering if you are doing a buck config or boost, or just
> resistive PWM?



Im using the the PWM pulse train to ENABLE-DISABLE a VLDO regulator enable pin for a li-ion based driver. 
Something like the very well know POP or Flupic drivers


Pablo


----------



## HKJ (Mar 27, 2008)

PEU said:


> I don't know how to make a configurable & stable ASM PWM routine using the PIC 12F629, besides that coding the app is a piece of cake.



Use timer interrupt, on each timeinterrupt toggle output pin and load timer with on or off time value, depending on output pin status.


----------



## MatajumotorS (Mar 27, 2008)

PEU said:


> I don't know how to make a configurable & stable ASM PWM routine using the PIC 12F629, besides that coding the app is a piece of cake.
> 
> Thats why I used the 12F683, because it has a PWM subsystem built in
> 
> ...


 
search for software PWM, or Light Brain project


----------



## RustyKnee (Mar 27, 2008)

MrAl said:


> Hi Pablo,
> 
> Im doing ok so far  Thanks.
> 
> ...


 
Do you mean PWM with a current limiting resistor? If you mean that PWM is itself resistive...it isn't. It is on then off repeated very quickly. 

Off is virtually zero current Full voltage, Power dissapated in FET---- 0Amps x Z Volts = 0 Watts.

On is virtually zero voltage across FET, Full current, Power dissapated in FET---- MAmps x 0 Volts = 0 Watts.

With out any current smoothing you need to watch the peak forward current isn't exceeded. As long as your battery voltage is low enough to not cause excessive peak forward current you shouldn't need a current limiting resistor.

Hardware PWM can be Kilohertz...so flickering shouldn't be apparent. PWM without current smoothing should also mean that the LED doesn;t change tint between levels....it will be the same tint as at the full peak forward current.

This thread has got me interested in doing my own software on one of the DX or similar boards....I wonder what is in my fenix P1D lol.

Stu


----------



## PEU (Mar 27, 2008)

MatajumotorS said:


> search for software PWM, or Light Brain project



Been there, done that. Software PWM (at least the one provided by picbasic) is not at stable as I would like. Will try other routes, yesterday I had an idea I want to test.

Rustyknee (Can I apply to the club of rustyknees too?  )

The VLDO regulator takes care of the current limiting in my board. In the DX board the AMC7135 does it.


Pablo


----------



## RustyKnee (Mar 27, 2008)

PEU said:


> Rustyknee (Can I apply to the club of rustyknees too?  )


 
If you have metal in there....absolutely . What happened to your knee?

Stu


----------



## MatajumotorS (Mar 27, 2008)

PEU said:


> Been there, done that. Software PWM (at least the one provided by picbasic) is not at stable as I would like. Will try other routes, yesterday I had an idea I want to test.
> 
> Rustyknee (Can I apply to the club of rustyknees too?  )
> 
> ...


 
Was this code unstable? for me it is stable 

"
 runbulb ; MODULATE LAMP (PWM)
movlw .255 ; loop 255 times
movwf mainloop
 run_loop
bsf lamp ; turn lamp ON
 movf lamp_power, W ; delay = (3uS x lamp_power) 
call delay2
 bcf lamp ; turn lamp OFF
 movf lamp_power, W
sublw .100 ; W = 100 - lamp_power
call delay2 ; delay = (3uS x (100 - lamp_power))
 decfsz mainloop, F ; decrement loop counter, skip if "0"
goto run_loop ; loop counter > 0 keep looping
return ; return from subroutine
"


----------



## PEU (Mar 27, 2008)

Thats not picbasic Mataju 


Pablo


----------



## MrAl (Mar 27, 2008)

RustyKnee said:


> Do you mean PWM with a current limiting resistor? If you mean that PWM is itself resistive...it isn't. It is on then off repeated very quickly.
> 
> Stu



What i mean is either buck or boost PWM which is a switching regulator,
or using a pure resistance in series with the LED and PWM'ing the series
combo which doesnt provide any power saving except as the dimming
itself would provide (and all loss at full power).

So basically, the circuit can be either:
1. Buck (input voltage is higher than LED voltage).
2. Boost (input voltage is lower than LED voltage).
3. Resistive PWM (input voltage is higher than LED voltage and there is also
some resistance in series with the LED, and no energy storage element such
as an inductor).

These are the most common topologies.


----------



## MrAl (Mar 27, 2008)

PEU said:


> Im using the the PWM pulse train to ENABLE-DISABLE a VLDO regulator enable pin for a li-ion based driver.
> Something like the very well know POP or Flupic drivers
> 
> 
> Pablo



Hi Pablo,

Oh, ok, so it's a sort of two stage PWM down converter without any
inductor. That's an interesting idea. I guess there are dim settings
too then, controlled by PIC software?

If there are instablities, it could be based on the start up of the second
stage regulator, although if i could see the schematic i would be able to
render a better opinion here.

Oh yes, the 12F683 has built in PWM which is nice


----------



## linterno (Mar 27, 2008)

Here you can find a good C or Basic compiler for PIC micro controllers. These compilers already include native functions for PWM like:

PWM_Init(freq)
PWM_Change_Duty(duty_ratio)
PWM_Start()
PWM_Stop()

There is a free version hex output limited to 2k of program words which should be more than enough for an small project like the one you (If I include myself, we) are thinking about.

Juan C.


----------



## MrAl (Mar 27, 2008)

Hi Juan,

That's nice too. I end up doing a lot of my own bit banged PWM code
anyway because i end up using assembler mostly. It's harder to do
but once you've done it you've done it.


----------



## MatajumotorS (Apr 11, 2008)

:thinking:
Hi, could someone help me with Brown-out, i have some questions.
I am using Assembler, PIC12F629.

as i understood:

1. need to enable BOD and PUT
2. check is the PCON,1 =1 and PCON,0 =0
- if Yes then do what needed after brown-out and set them both to 1
- if NO - start as usal and set them both to 1

is this ok or i am missing something.


```
#include "p12f629.inc"
     __CONFIG       _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
   errorlevel -302 ; suppress banksel warning messages
;*******************************************************************
;******************* CONSTANT DEFINITIONS **************************
;*******************************************************************
 
;*******************************************************************
;********************** PIN ASSIGNMENTS ****************************
;*******************************************************************
;#define  button GPIO, 3  ; momentary button (active low)
#define  lamp GPIO, 0  ; PWM output (FET drive)
#define  lamp2 GPIO, 1  ; PWM output (FET2 drive)
;#define  analog GPIO, 0  ; battery monitor (from voltage divider)
;*******************************************************************
;******************* VARIABLE DEFINITIONS **************************
;*******************************************************************
 cblock 0x20 
 ld_count
 hi_count
 lo_count
 
 
 endc
; Bank Select macro instructions
#define setbank0 bcf STATUS,RP0 ; Sel Bank 0
#define setbank1 bsf STATUS,RP0 ; Sel Bank 1
;*******************************************************************
 ORG     0x000             ; processor reset vector
 goto main
;*******************************************************************
;************************ Subroutines ******************************
;*******************************************************************
;******************** delay routines *******************************
wait2ms
 movlw d'2'
 goto long_delay
wait6ms
 movlw d'6'
 goto long_delay
wait100ms
 movlw d'100'
 goto long_delay
wait20ms
 movlw d'20'
 goto long_delay
wait255ms
 movlw d'255'
long_delay   ; enter here with desired ms in W (max 255ms)
 movwf ld_count ; (1ms resolution)
ld_loop
 call ms_delay ; 1ms delay
 
 decfsz ld_count, F
  goto ld_loop
 return
 
ms_delay   ; enter here for approx 1 ms delay
 movlw .10  ; (1) 10
v_delay    ; enter here for delay with desired ms*10 in W (max 25.5ms)
 movwf hi_count ; (1) (i.e. for 20 ms delay W = .200)(.1ms resolution)
loop_1 movlw  .31  ; (1) 
 movwf lo_count ; (1)
 
loop_2 decfsz lo_count, F ;  (30x3)+2 = 92
  goto loop_2  ; /
 nop   ; (1)
 decfsz hi_count, F ; (1)
  goto loop_1  ; (2)
 return
;--------------------------------------------------------
delay2    ; DELAY ROUTINE FOR LAMP PWM 
 movwf lo_count
d2loop
 decfsz lo_count, F
  goto d2loop
 return
;*******************************************************************
;********************** Initialization *****************************
;*******************************************************************
main      
 setbank1
 
 call    0x3FF            ; get factory oscillator calibration value 
 movwf   OSCCAL           ; load OSCAL register
 clrf OPTION_REG
 movlw b'00001000'       
 movwf TRISIO   ; set directions for GPIO port
 setbank0
 clrf GPIO   ; clear the GPIO port
;*******************************************************************
;*******************************************************************
[SIZE=3] [B]btfss PCON,1[/B][/SIZE]
[B][SIZE=3]  goto turnled[/SIZE][/B]
[B][SIZE=3]  btfss PCON,0[/SIZE][/B]
[B][SIZE=3]  goto brown_out[/SIZE][/B]
 
[B][SIZE=3]turnled[/SIZE][/B]
[B][SIZE=3]  bsf  lamp[/SIZE][/B]
 
[B][SIZE=3]loopl [/SIZE][/B]
[B][SIZE=3]  goto loopl[/SIZE][/B]
 
[B][SIZE=3]brown_out[/SIZE][/B]
[B][SIZE=3]  movlw b'00000011'[/SIZE][/B]
[B][SIZE=3]  movwf PCON[/SIZE][/B]
[B][SIZE=3]  bsf  lamp2[/SIZE][/B]
[B][SIZE=3]  call  wait255ms[/SIZE][/B]
[B][SIZE=3]  call  wait255ms[/SIZE][/B]
[B][SIZE=3]  bcf  lamp2[/SIZE][/B]
[B][SIZE=3]  goto turnled[/SIZE][/B]
;*******************************************************************
 end
```
 
EDIT:
OK one big mistake found - PCON is in second bank.... enything else?


----------



## zipplet (Apr 11, 2008)

With assembler there is no reason you cannot use the PWM hardware built into the PIC - I have done it. I have some sample code if anyone needs it, which I used to sound a buzzer for an alarm clock. It can be adapted for this,


----------



## schiesz (Apr 11, 2008)

zipplet said:


> I highly recommend getting into PIC programming. Pick up a PICkit 2 starter kit (including the demo board) which comes with everything you need to get going!
> 
> http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023805



What is the difference between the Starter Kit and the debug express here? I'm very curious to learn these, and wanted to see which would be better for this task.

Also, i am looking at the PIC12F683 options on that site, and there are many different ones. I cannot tell what the difference is between them, although a few (but not all) are different package type (but I do not know what those are). I was hoping to find a big one to practice with (using the starter kit) and then a small one to perhaps actually use, but I do not see sizes listed...

I will probably just order a few to try, but if anyone can help here it would be appreciated.

Thanks,

schiesz


----------



## zipplet (Apr 11, 2008)

The starter kit and debug express come with different microcontrollers. The microcontroller included with the debug express can be debugged using the PICkit 2, whereas the one included with the starter kit cannot without extra hardware (it doesn't support debugging directly).

Both kits give you an identical PICkit 2 so it doesn't matter too much which one you pick - it's just down to if you want to begin with a simple device (with the starter kit) or a more complicated device (with the debug express).

When looking for PICs, you will want to get ones in the PDIP package, not any of the others (I/P or I/SP suffix on the part name). These are the easy to handle dual-in-line packages. For this reason I also recommend the starter kit over the debug express as it comes with a PDIP PIC which can be swapped out for another one from the same series.

If you have more questions feel free to ask, I've been using PICs for a while and I'm sure some others here have been too.


----------



## schiesz (Apr 11, 2008)

Ok great, that was the impression I got from the web site, but I wanted to make sure. I will look at the PDIP packaged microcontrollers now also. Thanks for the help.

schiesz


----------



## mpf (Apr 12, 2008)

Hi 
I started from scratch making my own digital driver based on Atmel Attiny series.
It is a linear buck regulator 


If any one wants to try working with this uC I may be able to help.


Here is a brief break down of what I found useful

Programming Board AVR STK500 (+ 12V supply) http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail?name=ATSTK500-ND
http://www.avrfreaks.net/ has lotsof details on programming Atmel deivces


A PC to run the Windows software for programming and pcb design

Tektronix TDS 1001 digital storage oscilloscope. (2 channel)
Its is light and about the size of a shoe box and can trigger on a given pulse width which I found invaluable for debugging with only one output pin.
*http://www.tequipment.net/TektronixTDS1001B.asp*

Digitech QM1538 Multimeter with RS232 output and temperature measurement

Soldering Iron low wattage (say WES51-ND http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail?name=WES51-ND )

1.5A switchable voltage supply 3V 4.5V 6V 7.5V 9V 12V
http://www.jaycar.com.au/productVie...&pageNumber=&priceMin=&priceMax=&SUBCATID=381 


For PCB design I now use EagleCAD http://www.cadsoft.de/ some pcb companies such as www.sunstone.com (which I have not used) accept output from EagleCAD


To build the PCB prototypes I use Toner Transfer System http://www.pulsarprofx.com/PCB/a_Pages/1_Menu/overview.html I don't use the *[FONT=Arial, Helvetica, Geneva, Swiss, SunSans-Regular]GreenTRF[/FONT]* green film as I found it pulled the toner off the board. I touch up with a Printed Circuit Marker pen if needed otherwise I put up with the odd small hole in the copper.

For desoldering SMD devices I use Chip Quick Removal Alloy http://chipquik.com/ 

To see the SMD devices I use a head magnifying glass like http://www.jaycar.com.au/productVie...rd2=&pageNumber=&priceMin=&priceMax=&SUBCATID= 



To power the uC I use either an LM2980 5V Low drop out 3 terminal regulator if the input supply is <16V otherwise I use a MIC5233 adj which is good for up to 36V supply (also has reverse battery protection).


One of the advantages of the Attiny 24 and 25 uC is that they have the option of a x20 gain on a differential A/D converter input. This great for measuring the current in a sense resistor of a current control system.


I people are interested I can go into a bit more detail about how I code the control loop. For a basic intro to Attiny programming see http://www.avrbeginners.net/ 


matthew


----------



## MatajumotorS (Apr 12, 2008)

:bump: for post #35


----------



## zipplet (Apr 12, 2008)

MatajumotorS said:


> :bump: for post #35



I haven't looked over your code in detail, but I will if these suggestions I'm going to give don't help you. Also, please could you detail the problem you have?

Are you using a large enough capacitor on the PICs power supply? Try something like 10-100uF during testing - overly large I know, but it will let you see what's going on.

Your circuit should be wired so that the capacitor will not drain through the LED - otherwise all of the power is sucked out too quickly when power is removed from the circuit. I may be wrong here but this is the experience I've had.

About bank selecting, I tend to overuse the banksel command to be safe rather than sorry, then optimise and remove unneeded ones later on if I need to speed the code up.


----------



## PEU (Apr 12, 2008)

Since you do all the detection work before the led is even turned on, capacitor value can be very small, I use small ceramic ones to give you an idea... 
I don't understand assembler, so I can't help in that area.


Pablo


----------



## linterno (Apr 12, 2008)

PEU said:


> I don't understand assembler, so I can't help in that area.


Pablo, What programming language and what compiler do you use for PIC programming?

Saludos,

Juan C.


----------



## PEU (Apr 12, 2008)

http://www.melabs.com/products/pbp.htm


----------



## Supernam (Apr 13, 2008)

Pablo, this thread is useless to me...

...I'm not smart enough to understand it.:laughing:

Can you make us a heavy duty driver for the P7?


----------



## mpf (Apr 13, 2008)

PEU said:


> Im using the the PWM pulse train to ENABLE-DISABLE a VLDO regulator enable pin for a li-ion based driver.
> Something like the very well know POP or Flupic drivers



You could use D2Dim's (www.taskled.com/leds/*d2dim*.pdf) PWM output to drive multiple AMC7135 provide they respond fast enough. TaskLed's d2dim comes complete with the UI already programmed

matthew


----------



## Acetronics (Apr 14, 2008)

MatajumotorS said:


> :bump: for post #35


 
Hi, Mataju

This is not PbP pro, but could help 


```
#include "p12f629.inc"
 Errorlevel -302
     __CONFIG       _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
 
;*******************************************************************
;******************* CONSTANT DEFINITIONS **************************
;*******************************************************************
 
;*******************************************************************
;********************** PIN ASSIGNMENTS ****************************
;*******************************************************************
;#define  button GPIO, 3  ; momentary button (active low)
; !!! NO Pullups available for GPIO.3 !!!
; so, PULLUPS might be disabled ...
#define  lamp GPIO, 0  ; PWM output (FET drive)
#define  lamp2 GPIO, 1  ; PWM output (FET2 drive)
;#define  analog GPIO, 0  ; battery monitor (from voltage divider)
;*******************************************************************
;******************* VARIABLE DEFINITIONS **************************
;*******************************************************************
 cblock 0x20 
 ld_count
 hi_count
 lo_count
 
 
 endc
;*******************************************************************
 ORG     0x000             ; processor reset vector
 goto main
;*******************************************************************
;************************ Subroutines ******************************
;*******************************************************************
;******************** delay routines *******************************
wait2ms:
 movlw d'2'
 goto long_delay
wait6ms:
 movlw d'6'
 goto long_delay
wait100ms:
 movlw d'100'
 goto long_delay
wait20ms:
 movlw d'20'
 goto long_delay
wait255ms:
 movlw d'255'
long_delay:   ; enter here with desired ms in W (max 255ms)
 movwf ld_count ; (1ms resolution)
ld_loop:
 call ms_delay ; 1ms delay
 
 decfsz ld_count, F
  goto ld_loop
 return
 
ms_delay:   ; enter here for approx 1 ms delay
 movlw .10  ; (1) 10
v_delay    ; enter here for delay with desired ms*10 in W (max 25.5ms)
 movwf hi_count ; (1) (i.e. for 20 ms delay W = .200)(.1ms resolution)
 
loop_1:
 movlw  .31  ; (1) 
 movwf lo_count ; (1)
 
loop_2:
 decfsz lo_count, F ;  (30x3)+2 = 92
  goto loop_2  ; /
 nop   ; (1)
 decfsz hi_count, F ; (1)
  goto loop_1  ; (2)
 return
;--------------------------------------------------------
delay2:    ; DELAY ROUTINE FOR LAMP PWM 
 movwf lo_count
 
d2loop:
 decfsz lo_count, F
  goto d2loop
 return
 
;*******************************************************************
;********************** Initialization *****************************
;*******************************************************************
main:
 
 
 call    0x3FF             ; get factory oscillator calibration value 
 movwf   OSCCAL           ; load OSCAL register
 
  BANKSEL GPIO
 
 movlw  .7
 movwf  CMCON    ; comparators OFF
 
 clrf  GPIO       ; clear the GPIO port = no "glitch" ...
 
  BANKSEL OPTION_REG
 
 clrf  OPTION_REG   ; Weak Pull up ( see input defines ...)
 
 Clrf VRCON    ; Vref OFF 
 
 Movlw  b'00000000'
 movwf WPU     ;Which ports ? 
 
 movlw  b'00001000'   ; GPIO.3  as INPUT  
 movwf TRISIO      ; set directions for GPIO port
 
 BANKSEL GPIO
;*******************************************************************
; here, serious things begin ... Tadaaaaa !!!
;*******************************************************************
 btfss PCON,1    ;Test POR bit
  goto turnled    ;If 0 Then PO Reset
 
 btfss PCON,0    ; IF POR Then test BOR
  goto brown_out   ; IF 0 Then BO Reset
  goto Resetbits   ; Else set bits and wait ...
 
brown_out:     ; It's a BOR ... BOR Led ON 1/2 sec
        ; Then Power Led ON
  bsf  lamp2
  call  wait255ms
  call  wait255ms
  bcf  lamp2
 
turnled:
  bsf  lamp     ; Power Led ON
 
Resetbits: ; set both bits for next Powerdown
  Bsf PCON,0
  Bsf PCON,1
 
loopl:       ; ... Waiting for me to learn "Castejano" ??? 
  goto loopl
 
;*******************************************************************
 ORG 0x3FE
 
  goto main      ; if program pointer lost 
  end
```
 
Regards to Peu ... :wave:

Alain


----------



## PEU (Apr 14, 2008)

Welcome to the forums Alain, keep your wallet safe, flashlights can become a very expensive hobby 

Hey guys, see 1st post and my thanks to a certain Alain, well he is the same very nice guy that had patience to make me understand what I posted.


Pablo


----------



## Acetronics (Apr 14, 2008)

PEU said:


> Welcome to the forums Alain, keep your wallet safe, flashlights can become a very expensive hobby
> 
> Pablo


 
Hi, Pablo

You're right ... as I often drive my leds more-than-close to the Datasheets limits !!!
The last game was a car-tuning Stroboscope ... try to see 50µs @ 10 Hz ...

Poor LEDs ... but they survived !!!

Light be yours ...

Alain


----------



## Acetronics (Apr 15, 2008)

Hi, Pablo,

While browsing ... I think you've forgotten the MAIN thing  of the device in post #1 scheme ...:nana:

You won't believe it : 

THE MAINS SWITCH !!!

... Hey, You're still a friend ??? 

Regards 
Alain


----------



## PEU (Apr 15, 2008)

Of course friends! 

You are still new to the forums, but I think I speak for almost everyone, when we speak about flashlights that change modes by ON-OFF-ON cycles its a given that the battery will be disconnected in some way, for example the one you suggested: a switch, but there are also switchless flashlights that can be used with this circuit too, we call them: twisty/twisties

:nana: 


Pablo


----------



## kevinm (May 27, 2011)

Without reprogramming, is there a way to lock the microcontroller into one mode group? I like the Low, Medium, High set, btu really don't like the strobe/flash settings.


----------

