pia-468x60

pia-728x90

Showing posts with label AVR. Show all posts
Showing posts with label AVR. Show all posts

Thursday, June 16, 2022

Programming All External Interrupt Sources of Atmega16

In previous post, I show a simple programming example of INT0. As it's already stated, the Atmega16 has up to 3 external interrupt sources. In this example, I use all its external interrupt sources, the INT0, the INT1, and the INT2.

I select the low logic level of these interrupt source to trigger the ISR. Whenever any interrupt occurs, its corresponding output LEDs will toggle its logic state. The output LEDs are PB5, PB6, and PB7.

Programming All External Interrupt Sources of Atmega16
Program simulation

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * atmega16AllExtIntEx.c
 *
 * Created: 6/10/2022 7:06:23 PM
 * Author : Admin
 */ 

#include <avr/io.h>

#include <avr/interrupt.h>

int main(void)
{
	
	//PD2 and PD3 Input
	DDRD&=~(1<<2);
	DDRD&=~(1<<3);
	//Turn on PD2 and PD3 high
	PORTD|=(1<<2)|(1<<3);
	//PortB output
	DDRB=0xFF;
	//PB2 input
	DDRB&=~(1<<2);
	//Turn PB2 high
	PORTB|=(1<<2);
	//Enable INT0...2 request
	GICR|=(1<<INT0)|(1<<INT1)|(1<<INT2);
	//Enable interrupt
	sei();
	//Clear Flag
	GIFR|=(1<<INTF0);
	while (1)
	{
	}
	return 0;
}

//Interrupt Service Routine - ISR
ISR(INT0_vect){
	//Toggle RB5
	PORTB^=0x20;
}

ISR(INT1_vect){
	//Toggle RB6
	PORTB^=0x40;
}

ISR(INT2_vect){
	//Toggle RB7
	PORTB^=0x80;
}

Click here to download its zip file.

Friday, April 29, 2022

ATMega16 two-digit multiplexing display and counting example

Display multiplexing is very common for most microcontroller programming and interfacing. In usual, a controller could drive up to 8 digits before we can see its display flickering. In this programming example, I use only two digits of 7-Segment display. Because it's easy to build the circuit with a  little of source codes.

ATMega16 two-digit multiplexing display example
Program simulation in Proteus

Controller keeps track of counting an external pulse generated by SW1 switch pressing. The maximum press counting is 60 before it rolls down to 0. The display will become flicker whenever the input switch has a longer press duration, but it's not longer than 250ms.

  1. /*
  2.  * mux2Ssd.c
  3.  *
  4.  * Created: 5/25/2022 7:20:40 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 4000000UL
  11. #include <util/delay.h>
  12.  
  13. int main(void)
  14. {
  15. //common cathode
  16. unsigned char SSD[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  17. unsigned char cnt=0;
  18. //PortC output
  19. DDRC=0xFF;
  20. //PD7 input
  21. DDRD&=~(1<<7);
  22. //Turn on PD7
  23. PORTD|=(1<<7);
  24. while (1)
  25. {
  26. //Display processing
  27. PORTD&=~(1<<0);
  28. PORTD&=~(1<<1);
  29. PORTC=SSD[cnt/10];
  30. PORTD|=(1<<0);
  31. _delay_ms(3);
  32.  
  33. PORTD&=~(1<<0);
  34. PORTD&=~(1<<1);
  35. PORTC=SSD[cnt%10];
  36. PORTD|=(1<<1);
  37. _delay_ms(3);
  38.  
  39. //Input Testing
  40. if ((PIND&0x80)==0)
  41. {
  42. //while((PIND&0x80)==0);
  43. cnt++;
  44. _delay_ms(250);
  45. }
  46. if(cnt>=60) cnt=0;
  47. }
  48. }
  49.  
  50.  

Click here to download source file.

ATMega16 two-digit multiplexing display and counting example
Schematic diagram

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.

Sunday, October 10, 2021

Displaying an External Pulse Counting Using ATMega16 Timer/Counter0

 

Introduction

As mentioned in previous post, configuring Timer/Counter0 module of ATMega16 to drive in counter mode the programmer doesn’t need to write a program routine to count external input pulse. The counter is automatically increase its content with the present of input pulse change.

Since counter mode is fast, the system can show its content on a multiplexing display without flickering. In this example of using Timer/Counter0 module, the system accept a number of input pulse from external device using its peripheral hardware inside. Its result will be displayed on a three-digit multiplexing display.

Hardware and Software Preparation

This programming example is solely experimented on a personal computer. Embedded controller’s program is written using embedded C in Atmel Studio 7 while its hardware experiment will be tested in Proteus simulator.

Hardware Preparation

Proteus is a good choice for electronics circuit design and simulation. Software simulation produces a nearby result to physical hardware test.

Displaying an External Pulse Counting Using ATMega16 Timer/Counter0
Schematic diagram preparation for system simulation

The user can choose another lower frequency of its crystal oscillator, but the delay function parameter must be properly set in software.

An optional square wave generator GEN1 supplies a variable high frequency. It’s enabled/disabled by a latched switch SW2.

Software Preparation

Software setting of counter mode of Timer/Counter0 peripheral is done within a few lines of code. Another routine is written to handle multiplexing display. This three-digits display made of some little more lines of code.

C

I created a label for delay function parameter. It’s “digitOn”, and it’s set to 5 (ms). it’s useful for code modification in later hardware test.

Proteus Simulation

Without spending time in physical hardware prototyping, the programmer can draw this example circuit in simulator. It also save a lot of time and budget of components purchasing.

Displaying an External Pulse Counting Using ATMega16 Timer/Counter0
Proteus simulation of this working example

The result shown on this picture is 195 in decimal. the maximum counting of the 8-bit TCNT0 register is 0xFF or 255 in decimal. After it reached 255 it will roll back to 0 at next counting.

Creating A 16-bit Counter

We can get a 16-bit counting register from this 8-bit TCNT0 register by adding one more 8-bit register. An additional C unsigned 16-bit integer stores the total counting result up to 65536 counts.

Circuit Diagram Preparation

This optional programming example has some variation on its output display. It’s a five-digit multiplexing display that show a counting variable up to 65536 counts before it rolls back to 0.

Displaying an External Pulse Counting Using ATMega16 Timer/Counter0
Proteus VSM circuit diagram for hardware prototyping and simulation

I added a counter timer virtual instrument to verify the counting result whenever the counter is supplied from a high speed square wave generator GEN1.

AVR GCC Code Preparation

I added an unsigned 8-bit character and another unsigned 16-bit integer to get a 16-bit counting variable in AVR GCC.

Since interrupt flag created by Timer/Counter0 Overflow (TOV0) is set whenever TCNT0 overflows and rolls back to 0, we test and clear this flag to update the 16-bit counting variable.

Unlike previous example of an 8-bit counter, this 16-bit counter needs a five-digit display. The need to add more lines of code to the program that drive this multi-digit multiplexing display. We will more embedded C codes in this example.

C

We will see its simulation result at the next section.

Simulation Result

This circuit diagram composes of a lot of components placement and breadboard wiring. Using a simulator could produce a comparable result to the one’s in physical hardware.

Displaying an External Pulse Counting Using ATMega16 Timer/Counter0
Simulation result in Proteus VSM of this working example

Square wave generator GEN1 creates a frequency of 5kHz for this counter. Timer counter counts a nearby result with respect to the microcontroller counter. This microcontroller counting system reset its content whenever it reaches the maximum value of 65536 counts.


Labels

ADC (11) Analog (15) Arduino (12) Atmega16 (19) Audio (2) AVR (20) CCS PICC (3) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) DHT11 (1) Digital I/O (23) Display (37) ds18B20 (1) EEPROM (2) Environment Sensor (2) esp8266 (2) Experiment Board (12) I2C (4) Interrupt (8) LCD (3) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (32) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) pic18 microcontrollers (2) PIC18F4550 (3) PLC (35) PWM (11) Regulator (1) RTC (3) Sensor (9) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (2) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)