May 12, 2024

AVR microcontroller timing counter study notes to share

The timing counter's structure and application timer counter (Timer/Counter) is often used for counting, delay, measurement period, frequency, pulse width, and providing timing pulse signals. In practical applications, the measurement of the physical quantity such as the rotational speed, displacement, speed, flow rate, etc. is usually also converted into a pulsed electrical signal by the sensor, and the cycle or frequency is measured by using a timer counter, and then obtained through calculation processing.

AVR timer counter interface function: Through the mutual cooperation of timing counter and compare match register, generate a square wave signal with variable duty ratio, that is pulse width modulation output PWM signal, can be used for D/A, stepless speed control motor, frequency conversion Control etc. This article mainly introduces the AVR microcontroller timing/counter study notes, specific follow the small series to understand the next.

AVR microcontroller timing counter study notes to share

AVR microcontroller timing/counter study notes

The timer/counter 1 (16 bits) has a normal mode, a CTC mode, a fast PWM mode, a phase correction PWM mode, and the like.

1, normal mode (set as overflow interrupt working mode, vector number is 9)

Working principle: At the set clock frequency, the count register is incremented by 1 from the initial value. When the count reaches the maximum value (65535), it overflows and the interrupt subroutine is executed.

Registers to be configured: T/C1 control register B (TCCR1B), count registers (TCNT1H and TCNT1L), and T/C1 interrupt mask register (TIMSK).

(1) Setting the clock frequency (internal and external) by bit2, bit1, and bit0 of TCCR1B

(2) The initial count value is set by TCNT1H and TCNT1L (Note that write H and write L again)

(3) The interrupt enable is set by bit2 of TIMSK (note that the total interrupt SREG|=BIT(7) should also be turned on)

Interrupt statement during programming #pragma interrupt_handler Interrupt function name: Interrupt vector number

Note: In the interrupt subroutine, the initial value of the count should be loaded again because the initial value of the count becomes 0 when the interrupt is entered.

2, CTC mode (need to use OC1A, OC1B two pins)

Application: For generating a square wave signal with a duty cycle of 50%

AVR microcontroller timing counter study notes to share

(N is the frequency division coefficient)

Registers to be configured: T/C1 control register A (TCCR1A), T/C1 control register B (TCCR1B), and output compare register 1A (OCR1A).

(1) Bits 7 and 6 of TCCR1A are used to set the OC1A pin's compare output mode. Bits 5 and 4 are used to set the OC1B pin's compare output mode. (here we all set, OC1A and OC1B levels are reversed at the compare match)

(2) Set the waveform generation mode by combining bit 4 and bit 3 of TCCR1B with bit 1 and bit 0 of TCCR1A. (here we set mode 4 - CTC mode)

(3) The clock frequency is set in bit2, bit1, and bit0 of TCCR1B.

(4) OCR1A is used to set the upper limit of the count

Routines:

#include "iom16v.h"

#include macros.h

Void main()

{

DDRD|=0X30; //PD4, PD5 set to output

TCCR1A=0X50; //Reverse when comparing matches

TCCR1B=0X09; //CTC mode, no prescaler

OCR1A=1999; //OC1A, OC1B both generate 2KHz square wave signal (system clock is 8MHz)

}

3, fast PWM mode (need to use OC1A, OC1B two pins)

Prerequisite: The waveform generation mode is set to mode 15 (fast PWM) by the combination of bits 4 and 3 of TCCR1B and bits 1 and 0 of TCCR1A.

AVR microcontroller timing counter study notes to share

(1) Bits 7 and 6 of TCCR1A are used to set the OC1A pin's compare output mode. Bits 5 and 4 are used to set the OC1B pin's compare output mode. (Here we are set to, OC1A is inverted when compare match; OC1B is cleared when compare match, set when TOP)

(2) The clock frequency is set in bit2, bit1, and bit0 of TCCR1B.

(3) OCR1A is used to set the upper limit of the count, OCR1B is used to set the matching value of the count

Routine 1:

#include "iom16v.h"

#include macros.h

Void main()

{

DDRD|=0X30;

TCCR1A=0x63; //15 mode, clear when OC1B compare match, set 1 when TOP; negate when OC1A match

TCCR1B=0X1B; //64 prescaler

OCR1A=1249; //upper limit value, OC1B output 100Hz; OC1A output 50Hz square wave

OCR1B=250; // Matching value, OC1B output duty cycle is 20%

}

Routine 2:

#include"iom16v.h"

#include"macros.h"

// Generates a 38KHz signal with 50% duty cycle

Void main()

{

DDRD|=0X30;

TCCR1A=0x23; // Configuration register, OC1A as normal IO port; OC1B clear at compare match, set at TOP

TCCR1B=0X19; //15 mode, no prescaler

OCR1A = 209; / / upper limit value, get 38KHz PWM signal (OC1B mouth)

OCR1B=104; // Match register, one-by-one duty cycle

}

Routine 3:

#include"iom16v.h"

#include"macros.h"

// Generate a signal with a 1Hz duty cycle of 50%

Void main()

{

DDRD|=0X30;

TCCR1A=0x23; // Configuration register, OC1A as normal IO port; OC1B clear at compare match, set at TOP

TCCR1B=0X1D; //15 mode, 1024 prescaler

OCR1A=7811; //upper limit value, get 1Hz PWM signal (OC1B port)

OCR1B=3905; // Match registers, one-to-one duty cycle

Mini PC Office

Do you know the reason why Mini PC is becoming more and more welcome nowadays? You will realize that Mini PC windows 11 absolutely should be popular if keep thinking deeply a little more, since not only there are all the functions, performance warranty, various ports, but solve the weight problem people struggle when need to move computer in old time. At this shop, you can see Mini PC office, Mini Gaming PC , j4125 Mini PC, etc. Whatever you need for normal office, design drawing, gaming, education learning or teaching, Telecast alive, daily entertainment,etc. You can always find right one here.

Of course, China All In One Business Computer, Education Laptop, Budget Business Laptop, Gaming Laptop , Yoga Laptop , Android Tablet also available at this store, since what we do is customizing Quad Core Laptop, 10.1 android tablet, PC Mini Intel and Business All In One Computer.

Another reason why more and more clients return to us is that quality with competitive price, in time and lifetime after-sales service, customized and multiple service, fast delivery time.

home assistant Mini PC,custom mini pc,Windows 11 Mini PC,j4125 Mini PC

Henan Shuyi Electronics Co., Ltd. , https://www.shuyiaiopc.com