Charging portable electronics devices via USB port is a common way. Computer USB ports could powers, and charges multiple USB devices. Only one AC to USB adapter could enough for most users.
However an electronic hobbyists requires more than one USB power adapter. I have some unused electronics components. Hence I tried to design and assemble them to make a USB power port for my own use.
Charging the LG G3 phone using my USB power board
I have some SMD AMS1117-5.0, and AMS1117-3.3 chips. I designed this board to powers some USB devices and circuit prototyping.
Schematic Diagram
The picture below is a finished PCB design.
PCB Design View
My full working and test on this prototype listed below.
Finished PCB etching with DIY legend printtng
Finished PCB etching
Finished DIY PCB Assembling
Finished DIY PCB Assembling
Click here to download Proteus design file of this DIY PCB.
We have driven a long journey on Timer/Counter0 module of ATMega16.
Finally, we will discuss about this module to operate in counter mode.
Here we will talk briefly about a counter with some different techniques
of making it up.
Standard Logic Counter IC
Typically a digital counter IC made up of flip-flops, accepting a
digital pulse input. Its internal flip-flips continuous to change
whenever it has incoming input pulse. This IC has many kind of output – a
binary output, a Binary Coded Decimal (BCD) output, etc. For example a
7490 decade counter.
Counter in Microcontroller
A digital logic IC counter-like system can be created using an 8-bit
microcontroller. The easiest way to make it is coding a microcontroller
in Assembly or C language to count an external pulse straightly. This
method is not accurate nor responsive, since it has some problems of
noisy input created by bounced switching.
Another way is to use Timer/Counter module inside a microcontroller
since it can count in two ways – internal and external. An external
counting refers to counting an outside world input pulse fed to its
corresponding input counting pin, as an example of the Timer/Counter0
module of ATMega16.
Using this module to make a digital counter is very effective in
coding since it requires a few lines of code that configure the
microcontroller counting peripheral. It’s fast and responsive. The input
pulse frequency is more than 50MHz.
AVR GCC to Program Timer/Counter0 in Counter Mode
We have shown a lot about the details of Timer/Counter0 peripheral of
ATMega16 that we will not show it more here. However putting the
controller to operate in counter mode is done with a few lines of C code
with some optional port setting.
Register Setting
Making this counter to run require only one register – the
Timer/Counter Control Register (TCNT0). Its external input clock (pulse)
have two transition – rising and falling edge. These two transition can
be selected in Clock Select bit of TCNT0.
Clock Select bit of Timer/Counter Control Register
Setting CS02:00 to 0x06, the counter increase its content whenever
the external clock source on T0 (PB0) change from high to low (falling
edge). For a falling edge (low to high transition) we must set CS02:00
to 0x07 to increase the content of counter.
T0 is Timer/Counter0 counter input pin locates at PB0 of Port B. In
counter mode we no need to configure this pin to input, as it’s
automatically set.
Similarly to timer mode, Timer/Counter Register (TCNT0) store the
counting result. This 8-bit register overflows after it reaches 0xFF and
rolls back to 0x00. The TOV0 flag is set at this point.
A Program to Count a Rising Edge Pulse
Conventionally an external clock source is active at its rising edge
(low to high). So here we will use this microcontroller internal counter
to work in this way.
This introductory example, counts a low-to-high external pulse. A
binary representation output of the 8-bit TCNT0 will display on Port C.
Clock Selection bit CS02:00 set to 0x07 in hexadecimal. T0 (PB0)
external clock input pin is no need to set to a digital input direction
in counter mode.
There are no appropriate port pin selection on my AVR development
board. So I decide to test this program in software simulator – Proteus.
Proteus simulator program testing
The core coding of counter consists of a few lines of code.
/*
* timerCounter0_cntMode.c
*
* Created: 12/15/2020 6:45:34 PM
* Author : admin
*/
#include <avr/io.h>
int main(void)
{
/*Select Counter Mode and Rising Edge*/
TCCR0=(1<<CS02)|(1<<CS01)|(1<<CS00);
/*Output Counter Result On Port C*/
DDRC=0xFF;
/*Clear Timer/Counter0 Register*/
TCNT0=0;
while(1)
{
/*Shows The Result*/
PORTC=TCNT0;
}
}
We can detect the rising edge of input pulse requires two external resistors.
A Program to Count a Falling Edge Pulse
Optionally we can detecting the present of input pulse on its falling
edge. AVR GCC source code remain the same but it excepts the transition
setting in Clock Select bit of TCCR0. Again the schematic diagram and
program testing is done in Proteus simulator.
Another feature of ATMega16 AVR microcontroller Timer/Counter0
peripheral is its Clear Timer on Compare Match (CTC) Mode. In this case
the controller can only generate a square wave output on its OC0 output
pin.
Its free running Timer/Counter0 Register (TCNT0) starts count until it
reaches the value of the Output Compare Register (OCR0). At comparison
matching the TOP, the TOV0 flag is set. Output logic change at OC0 pin
is selected using software – set, clear, toggle, and disconnect.
Timing Diagram for CTC mode of Timer/Counter0
Assigning WGM01 of Waveform Generation Mode Bit of TCCR0 register to 1,
put Timer/Counter0 to operate in Clear Timer on Compare Match (CTC) mode.
CTC is Mode 2 by setting WGM01 of TCCR0
To generate square wave output at OC0, the software must set the Output
Compare Mode bit in non-PWM mode to toggle OC0 pin at compare match.
Output Compare Mode bit in non-PWM mode
Output Compare Register (OCR0) is a reference value for TCNT0. It’s
changeable during run-time to change the output square wave’s frequency.
Device’s datasheet states the formula of finding the frequency of
square wave output as below,
N is prescaler factor 1, 8, 64, 256, or 1024
When the OCR0 register is set to 0 the maximum frequency of output waveform is haft of microcontroller clock.
ATMega16 Coding Using AVR GCC
Embedded controller’s program generates an output waveform at OC0 pin
as square wave with a determined frequency. Software setting puts
Timer/Counter0 module of this AVR device to work in CTC mode as we have
described above.
We select CTC mode with its clock prescaler of 256, and OCR0 = 50. Then the output frequency of waveform at OC0 pin is,
Calculation for output frequency
Without sophisticated laboratory equipment – oscilloscope, I test this microcontroller program in Proteus simulator.
Simulation in Proteus – The output frequency is 613Hz in software
AVR GCC coding is done in Atmel Studio 7 as a preferred IDE.
/*
* m16_timer_0_ctc.c
*
* Created: 12/15/2020 11:00:35 AM
* Author : admin
*/
#include <avr/io.h>
int main(void)
{
/*CTC Mode, With 1:256 Prescaler*/
TCCR0=(1<<WGM01)|(1<<COM00)|(1<<CS02);
/*Set a preferred value of OCR0*/
OCR0=50;
/*OC0/PB3 Square Wave Output*/
DDRB=(1<<3);
/*Main Program Loop Stays Here*/
while(1)
{
}
}
Click here to download zip file of this AVR tutorial.
A Program to Create a 50Hz Frequency
Since Output Compare Register (OCR0) used for adjusting the frequency
of output waveform in this mode, we can find its actual value to create
a preferred frequency.
To get a lower 50Hz frequency we need to scale timer input to 1:1024 ( N=1024). With an equation stated by device’s manufacturer, we can find the value of OCR0 as follow,
Equation to find a 50Hz frequency
So in software embedded C we must set CS02=1 and CS00=1, and OCR0=155.
/*
* m16_ctc_50Hz.c
*
* Created: 12/15/2020 5:48:40 PM
* Author : admin
*/
#include <avr/io.h>
int main(void)
{
/*CTC Mode, With 1:1024 Prescaler*/
TCCR0=(1<<WGM01)|(1<<COM00)|(1<<CS02)|(1<<CS00);
/*Set a preferred value of OCR0 to get
a 50Hz waveform*/
OCR0=155;
/*OC0/PB3 Square Wave Output*/
DDRB=(1<<3);
/*Main Program Loop Stays Here*/
while(1)
{
}
}
This embedded program is simulated in Proteus simulator.