STM8 8-bit Timer Configuration TIM4

The STM8 series of microcontroller’s are extremely budget friendly and provide great features for the money. If you are using SDCC as the compiler for your projects. You have probably found examples for peripherals like timers quite scarce. Here you can see an example of the STM 8-Bit Timer Configuration

Recently I wanted to understand how the basic 8-bit timer TIM4 on the STM8S103F3P6 worked. It took me some time to fully get to grips with, partly for not paying enough attention to the reference manual and datasheet, but also by looking at a bad example I found on Github.

The example I saw for configuring the prescaler of the timer advised to configure the prescaler with a value from one of 128, 64, 32, 16, 6, 4, 0. Using a fx2lafw based logic Analyzer and Pulseview I was seeing some odd results. The timer would overflow before I expected it to. It turns out configuring the prescaler with these values is incorrect.

Correct Prescaler Configuration

According to the datasheet the TIM4_PSCR register only has 3 configurable bits. They range from bit 0 to bit 2

STM8 TIM4 Prescaler

If only 3 bits can be configured, you can not write a value of 128 as this would need an 8 bit wide register. Instead the maximum value we can write is 7 which would set bits from 0 to 2.

STM8 TIM4_PSCR Bits

This means the only values we can set for this timer are 1-7. After some trial and error I figured out that these values represent the division of the input clock to the timer. The examples below are based on a input frequency of 16mhz.

 0x07 = /128 8us      per tick, 2.048ms per overflow
 0x06 = /64  4us      per tick, 1.024ms per overflow
 0x05 = /32  2us      per tick, 512us   per overflow
 0x04 = /16  1us      per tick, 256us   per overflow
 0x03 = /8   0.5us    per tick, 128us   per overflow
 0x02 = /4   0.25us   per tick, 64us    per overflow
 0x01 = /2   0.125us  per tick, 32us    per overflow
 0x00 = /0   0.0625us per tick, 16us    per overflow

I hope this helps when trying to configure the timer, as you can see from my example the maximum delay with a single over flow and a 16mhz clock is 2.048ms. You can however count multiple overflows in the ISR to make a bigger delay.

If you have found this useful or would like me to explain this in greater detail please leave a comment.

Lezyne Deca Drive 1500xxl Battery Upgrade

November 2015 I bought a new bike light, I was getting into cycling at the time and wanted to be able to commute home from work in the dark. My choice of light in the end was the Lezyne Deca Drive 1500xxl. For those of you that know its an all in one light capable of delivering 1500 lumen’s for over an hour.

Now, almost 2 years later I am still using this quality bike light. However just like laptop batteries, the factory batteries in this light lost some capacity meaning less run time which has become a problem having let me down a couple of times.

Having some electronics experience. I decided to see if I could replace the batteries myself.

Getting Started

The cells inside the Lezyne Deca Drive 1500xxl are 2 x 2800mah 18650. You can get these from eBay or Aliexpress. The ones I am using are Panasonic NCR18650B with tabs. Tabbed cells make assembling the new pack much easier. Not only are 18650 cells hard to solder due to sinking heat rapidly from the soldering iron. The cell can also become damaged from excess heat.

Before building the new battery pack. I charged the cells to ensure they were balanced. This is not absolutely necessary, but if you have a 18650 charger give it a go.

Dismantling

  1. Remove the small T6 screw on the bottom of the light.
  2. Remove the Allen Key bolt holding the mount to the light body.
  3. Using a finger nail, lift up the rubber on off switch button.
  4. Push on the light lens, so the inside of the light slides out the rear of the body.

The battery pack is in the bottom of the light. In this case it was wrapped in a blue heatshrink layer protecting the cells.

Building a new battery pack

Before building the new pack salvage the protection circuit PCB from the old pack. Do this by using a hobby knife to cut the heat shrink wrap from the existing battery pack. Once inside you will see the PCB attached between the two parallel 18650 cells. Using wire cutters, carefully cut the metal tabs which are attached to the PCB. Cut them as close to the top of the cell as possible. Otherwise you will need to solder new metal tabs to the PCB when building the new pack.

Using a flat surface, glue the two cells together with super glue and solder their tabs together. You MUST ensure the cells are soldered together with the correct polarity. Solder positive to positive and negative to negative. It’s really important you get this right otherwise it is likely the batteries may catch fire or explode. Put some electrical insulation tape length wise between the positive and negative terminals. This helps prevent any short circuit between the PCB and cells.

Now solder the PCB tabs to the positive and negative terminals of the pack while ensuring the correct polarity. You should cover the tabs with heatshrink prior to soldering. Finally wrap the battery in electrical insulation tape. Start with one layer across the top and bottom terminals. Be careful to not make the wrap too thick otherwise you will struggle to fit the new pack in the light upon reassembly.

Testing the new battery pack.

Before putting the light back together, test the battery pack to make sure it works. If you accidentally shorted the batteries when building the pack, the protection circuit should have kicked in. You will need to charge the pack before you can draw power from it. This is a safety feature.

lezyne decca drive 1500xxl testing

Reassemble

Slide the new battery pack into the light. Tuck the connector into the recess at the back. Make sure not to trap any of the wires. You may note the plastic casing looks ever so slightly bowed. I think these cells might be slightly wider than the originals, it does not cause an issue though.

lezyne deca drive 1500xxl upgraded battery

Reassembly is the reverse of disassembly, make sure none of the o’rings or seals are snagged, otherwise water may get into the housing.

lezyne deca drive 1500xxl

Test the light again and feel awesome in the knowledge the battery will last longer.

lezyne deca drive 1500xxl