battery box
I connected the battery pack from my earlier post to the Tascam DR680 and all went well (it's not exactly rocket science but you never know). I was happy but then I realized that the recorder only shows the AC symbol in the display. That would leave me completely in the dark with a high risk of the recorder shutting down without saving the file.Connecting a voltmeter is not an option so something else is needed.
weekend project
Time for a weekend project. I found a suitable box that was laying around for ages and I also have some PIC 12F675. These are 8 pin processors with on-board 10 bit ADC. I never used the ADC in a PIC so that was a good opportunity.
Looking at the discharge graph of a NiMH battery I noticed that the voltage does not drop a lot until the end of the curve where it takes a steep dive. The only important thing for me is to know that I approach the cliff. The values in table are arbitrary, time will tell if they need to be changed.
The PIC has 5 pins free so a LED bar would be possible but I dislike the idea of wasting something like 30 mA. I decided to use a green and a red LED. With a fresh battery pack the green LED is on, when the voltage drops the LED starts blinking. When the voltage drops below 11.8V the red LED turns on as well.
Below 11V only the red LED blinks.
Not many parts are needed to accomplish this.
10 bit is enough resolution to use a divider to bring the battery voltage into the 5V range required by the ADC but as I had a 7V5 zener at hand I opted to use this to drop the voltage first. R2 limits the current in the 5V protection zener and acts together with R4 to further lower the voltage. This provides a bit of margin for the convertor. The resistor values are chosen to keep the input resistance to the converter below 10K as stated in the datasheet.
I salvaged a few boxes like this from the scrapyard. The key switch found finally a use after being in a drawer for 20 years. This will prevent accidentally switching off (or on when in transport)
The packs are held in place with some packaging foam.
using the PIC ADC
Using the ADC in not difficult at all. Just don't make the mistake I made: the channels start with GPIO0 but channel 4 of the ADC is NOT GPIO3 but GPIO4! I lost an hour over this wondering why it did not work :(
The registers are known to my PICC compiler.
first setup:
TRISIO = 0b011000; //this sets GPIO3 and GPIO4 as input
CMCON = 0x07; //no need for the comparators
ANSEL = 0b00010000 ; //this sets GPIO4 as analog input
ADCON0 = 0b00001100; // this select ADC channel 4
ADFM = 1; // this sets the result as right justified (this is for clarity, ADFM is part of ADCON0)
ADON =1; // enable the converter (this is for clarity, ADON is part of ADCON0)
then to start a reading:
GODONE = 1; // do not do this in the same instruction as ADON
Wait for GODONE to become zero or use the associated interrupt.
The result of this operation is in registers ADRESH and ADRESL.
In C it is easy to make that a variable, say we want this variable to be ADRES
unsigned short ADRES;
ADRES = ADRESL; // Get the 8 bit LSB result
ADRES += (ADRESH << 8); // Get the 2 bit MSB result
Now ADRES contains the converter value.
There is obvioulsy more than one way to write the software, this is how I did it.
I don't like delay loops so I usually write my programs as one big loop with nested counters to achieve delays. Here I used 3 counters, the blinking rate is achieved by playing with the counter reload values.
increment 8 bit counter 1
if counter 1 is 0 : check GODONE / do some stuff, set LED or blinking flags/ increment counter2 /start new reading
if counter 2 is 0: reload counter 2 / increment counter 3
if counter 3 is 0: check the blinking flags / toggle output when set/reload counter 3
To toggle in C:
if(led1flash){
LED1 = LED1 ^ 1;
}
Happy programming.
weekend project
Time for a weekend project. I found a suitable box that was laying around for ages and I also have some PIC 12F675. These are 8 pin processors with on-board 10 bit ADC. I never used the ADC in a PIC so that was a good opportunity.
Looking at the discharge graph of a NiMH battery I noticed that the voltage does not drop a lot until the end of the curve where it takes a steep dive. The only important thing for me is to know that I approach the cliff. The values in table are arbitrary, time will tell if they need to be changed.
> 12.2V | battery is fresh |
> 11.8V | normal |
>11V | battery needs replaced |
< 11V | battery almost empty |
The PIC has 5 pins free so a LED bar would be possible but I dislike the idea of wasting something like 30 mA. I decided to use a green and a red LED. With a fresh battery pack the green LED is on, when the voltage drops the LED starts blinking. When the voltage drops below 11.8V the red LED turns on as well.
Below 11V only the red LED blinks.
Not many parts are needed to accomplish this.
10 bit is enough resolution to use a divider to bring the battery voltage into the 5V range required by the ADC but as I had a 7V5 zener at hand I opted to use this to drop the voltage first. R2 limits the current in the 5V protection zener and acts together with R4 to further lower the voltage. This provides a bit of margin for the convertor. The resistor values are chosen to keep the input resistance to the converter below 10K as stated in the datasheet.
I salvaged a few boxes like this from the scrapyard. The key switch found finally a use after being in a drawer for 20 years. This will prevent accidentally switching off (or on when in transport)
The packs are held in place with some packaging foam.
using the PIC ADC
Using the ADC in not difficult at all. Just don't make the mistake I made: the channels start with GPIO0 but channel 4 of the ADC is NOT GPIO3 but GPIO4! I lost an hour over this wondering why it did not work :(
The registers are known to my PICC compiler.
first setup:
TRISIO = 0b011000; //this sets GPIO3 and GPIO4 as input
CMCON = 0x07; //no need for the comparators
ANSEL = 0b00010000 ; //this sets GPIO4 as analog input
ADCON0 = 0b00001100; // this select ADC channel 4
ADFM = 1; // this sets the result as right justified (this is for clarity, ADFM is part of ADCON0)
ADON =1; // enable the converter (this is for clarity, ADON is part of ADCON0)
then to start a reading:
GODONE = 1; // do not do this in the same instruction as ADON
Wait for GODONE to become zero or use the associated interrupt.
The result of this operation is in registers ADRESH and ADRESL.
In C it is easy to make that a variable, say we want this variable to be ADRES
unsigned short ADRES;
ADRES = ADRESL; // Get the 8 bit LSB result
ADRES += (ADRESH << 8); // Get the 2 bit MSB result
Now ADRES contains the converter value.
There is obvioulsy more than one way to write the software, this is how I did it.
I don't like delay loops so I usually write my programs as one big loop with nested counters to achieve delays. Here I used 3 counters, the blinking rate is achieved by playing with the counter reload values.
increment 8 bit counter 1
if counter 1 is 0 : check GODONE / do some stuff, set LED or blinking flags/ increment counter2 /start new reading
if counter 2 is 0: reload counter 2 / increment counter 3
if counter 3 is 0: check the blinking flags / toggle output when set/reload counter 3
To toggle in C:
if(led1flash){
LED1 = LED1 ^ 1;
}
Happy programming.
The Russians propably just would use this and call it a day....
Comments
Post a Comment