pia-468x60

pia-728x90

Wednesday, October 6, 2021

DIY USB Power Using LM1117-5.0

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.

DIY USB Power Using LM1117-5.0
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.

DIY USB Power Using LM1117-5.0
Schematic Diagram
The picture below is a finished PCB design.

DIY USB Power Using LM1117-5.0
PCB Design View

My full working and test on this prototype listed below.

DIY USB Power Using LM1117-5.0
Finished PCB etching with DIY legend printtng

DIY USB Power Using LM1117-5.0
Finished PCB etching


DIY USB Power Using LM1117-5.0
Finished DIY PCB Assembling

DIY USB Power Using LM1117-5.0
Finished DIY PCB Assembling

Click here to download Proteus design file of this DIY PCB.

Wednesday, September 29, 2021

ATMega16 Timer/Counter0 in Counter Mode

 

Introduction

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.

ATMega16 Timer/Counter0 in Counter Mode

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.

ATMega16 Timer/Counter0 in Counter Mode
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.

ATMega16 Timer/Counter0 in Counter Mode
Proteus simulator program testing

The core coding of counter consists of a few lines of code.

  1. /*
  2.  * timerCounter0_cntMode.c
  3.  *
  4.  * Created: 12/15/2020 6:45:34 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. int main(void)
  11. {
  12. /*Select Counter Mode and Rising Edge*/
  13. TCCR0=(1<<CS02)|(1<<CS01)|(1<<CS00);
  14. /*Output Counter Result On Port C*/
  15. DDRC=0xFF;
  16. /*Clear Timer/Counter0 Register*/
  17. TCNT0=0;
  18. while (1)
  19. {
  20. /*Shows The Result*/
  21. PORTC=TCNT0;
  22. }
  23. }

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.

ATMega16 Timer/Counter0 in Counter Mode
Program testing in Proteus simulator
  1. /*
  2.  * timerCounter0_cntModeFalling.c
  3.  *
  4.  * Created: 12/15/2020 9:17:40 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. int main(void)
  11. {
  12. /*Select Counter Mode and Falling Edge*/
  13. TCCR0=(1<<CS02)|(1<<CS01);
  14. /*Output Counter Result On Port C*/
  15. DDRC=0xFF;
  16. /*Clear Timer/Counter0 Register*/
  17. TCNT0=0;
  18. while (1)
  19. {
  20. /*Shows The Result*/
  21. PORTC=TCNT0;
  22. }
  23. }


Monday, August 30, 2021

ATMega16 Timer/Counter0 CTC Mode


Introduction

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.

Simulation Sample

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.

ATMega16 Timer/Counter0 CTC Mode
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.


ATMega16 Timer/Counter0 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.

ATMega16 Timer/Counter0 CTC Mode
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,

ATMega16 Timer/Counter0 CTC Mode
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,

ATMega16 Timer/Counter0 CTC Mode
Calculation for output frequency

Without sophisticated laboratory equipment – oscilloscope, I test this microcontroller program in Proteus simulator.

ATMega16 Timer/Counter0 CTC Mode
Simulation in Proteus – The output frequency is 613Hz in software

 AVR GCC coding is done in Atmel Studio 7 as a preferred IDE.

  1. /*
  2.  * m16_timer_0_ctc.c
  3.  *
  4.  * Created: 12/15/2020 11:00:35 AM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10.  
  11. int main(void)
  12. {
  13. /*CTC Mode, With 1:256 Prescaler*/
  14. TCCR0=(1<<WGM01)|(1<<COM00)|(1<<CS02);
  15. /*Set a preferred value of OCR0*/
  16. OCR0=50;
  17. /*OC0/PB3 Square Wave Output*/
  18. DDRB=(1<<3);
  19. /*Main Program Loop Stays Here*/
  20. while (1)
  21. {
  22. }
  23. }

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,

ATMega16 Timer/Counter0 CTC Mode
Equation to find a 50Hz frequency

So in software embedded C we must set CS02=1 and CS00=1, and OCR0=155.

  1. /*
  2.  * m16_ctc_50Hz.c
  3.  *
  4.  * Created: 12/15/2020 5:48:40 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10.  
  11. int main(void)
  12. {
  13. /*CTC Mode, With 1:1024 Prescaler*/
  14. TCCR0=(1<<WGM01)|(1<<COM00)|(1<<CS02)|(1<<CS00);
  15. /*Set a preferred value of OCR0 to get
  16.   a 50Hz waveform*/
  17. OCR0=155;
  18. /*OC0/PB3 Square Wave Output*/
  19. DDRB=(1<<3);
  20. /*Main Program Loop Stays Here*/
  21. while (1)
  22. {
  23. }
  24. }

This embedded program is simulated in Proteus simulator. 

ATMega16 Timer/Counter0 CTC Mode
Simulation result in Proteus

Labels