pia-468x60

pia-728x90

Monday, January 31, 2022

Atmega16 external interrupt programming example

Atmega16 has up to 21 interrupt sources. Here I will discuss only the external interrupt of this controller. External interrupt consist of three source sources:

  1. INT0 - External interrupt request 0
  2. INT1 - External interrupt request 1, and
  3. INT2 - External interrupt request 2.

First I will show how to use INT0 source. It has four sense that determine whether the interrupt is triggering. It is the Interrupt Sense Control register bits of the MCU Control Register (MCUCR).

Atmega16 external interrupt programming example
MCU Control Register (MCUCR)

Atmega16 external interrupt programming example
 Interrupt Sense Control register bits

The General Interrupt Control Register (GICR) turn on and off these external interrupt source, including the INT0 source.

Atmega16 external interrupt programming example
General Interrupt Control Register – GICR

They also have their corresponding interrupt flag in the General Interrupt Flag Control (GIFR).

Atmega16 external interrupt programming example
General Interrupt Flag Control (GIFR)

There are three bit corresponding to INTF1, INTF0, and INTF2. These bits will be cleared after the Interrupt Service Routine (ISR) executed. Optionally the programmer can clear any flag by setting it. However it is uncommon from most of microcontrollers.

In C programming the interrupt is very easy to program that I will show it in this programming example.

In this example I use only the INT0 source. The interrupt sense is the falling edge of INT0, as I will select it in program. Each time the INT0 occurs, and output LED connects to PD7 will toggle.

Atmega16 external interrupt programming example
Schematic Diagram

The source code is very simple. It contains a little lines of code.

  1. /*
  2.  * int0Example1.c
  3.  *
  4.  * Created: 5/27/2022 6:45:11 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #include <avr/interrupt.h>
  11.  
  12. int main(void)
  13. {
  14. //PortD output
  15. DDRD=0xFF;
  16. //RD2 input
  17. DDRD&=~(1<<2);
  18. //Turn on RD2
  19. PORTD|=(1<<2);
  20. //Select falling edge of INT0
  21. MCUCR|=(1<<ISC01);
  22. //Enable INT0 request
  23. GICR|=(1<<INT0);
  24. //Enable interrupt
  25. sei();
  26. //Clear Flag
  27. GIFR|=(1<<INTF0);
  28. while (1)
  29. {
  30. }
  31. }
  32.  
  33. //Interrupt Service Routine - ISR
  34. ISR(INT0_vect){
  35. //Toggle PD7
  36. PORTD^=0x80;
  37. }
  38.  

Click here to download it source file.

Saturday, December 25, 2021

A DIY USB Charger with LM1117MP Voltage Regulator

USB charging requires only a constant voltage of +5V DC, with constant current. The LM1117MP-5,0 linear voltage regulator is very popular due to its simplicity of circuit wiring. It outputs a +5V voltage, and 1A current. It has low drop down voltage.

A DIY USB Charger with LM1117MP Voltage Regulator
A finished circuit soldering

It's constructed with about a dozen of components I have. The LM1117MP-5,0 is an SMD device, but soldering it on PCB is not difficult.

A DIY USB Charger with LM1117MP Voltage Regulator
Circuit diagram


A DIY USB Charger with LM1117MP Voltage Regulator
PCB Drawing

I used Proteus 8 to draw the schematic and PCB. 

A DIY USB Charger with LM1117MP Voltage Regulator
Bottom copper pattern with non-mirror

A DIY USB Charger with LM1117MP Voltage Regulator
Top silk mirror


A DIY USB Charger with LM1117MP Voltage Regulator
Top copper mirror

A DIY USB Charger with LM1117MP Voltage Regulator
Three-D view of this PCB design

A DIY USB Charger with LM1117MP Voltage Regulator

A DIY USB Charger with LM1117MP Voltage Regulator

A DIY USB Charger with LM1117MP Voltage Regulator

Click here to download source file.

Saturday, November 27, 2021

PWM Pulse Steering in PIC16F887

CCP1 can generate PWM signal up to 4 pins at the same time, by selecting CCP1M<3:2> = 11 and P1M<1:0> = 00. The Pulse Steering Control Register (PSTRCON) allow us to select any streering pin between P1A, P1B, P1C, and P1D.

PWM Pulse Steering in PIC16F887
 PSTRCON: PULSE STEERING CONTROL REGISTER
PWM Pulse Steering in PIC16F887
SIMPLIFIED STEERING BLOCK DIAGRAM

In this example, I use all PWM steering pins to generate the signal of 25% duty cycle.

void main() {
     TRISC=0x01;
     TRISD=0x00;
     OSCCON=0x70;
     CCP1CON=0x3C;
     PSTRCON=0x0F;             // steering output to P1<D:A>
     PR2=99;                   // Period is 50us
     CCPR1L=22;               // duty cycle is 25%
     T2CON=0x00;             // Timer2 Prescaler is 1:1
     TMR2=0;                   // clear timer 2
     T2CON.TMR2ON=1;          // Enable timer2
     PIR1.TMR2IF=0;             // clear flag
     while(PIR1.TMR2IF==0);     // wait until new cycles
}

I use simulator to test this program.

PWM Pulse Steering in PIC16F887
Program simulation

This system uses a 8MHz internal RC oscillator of PIC16F887.

Labels