AVR Timer Compare Interrupt

Publisher:平安幸福Latest update time:2017-11-18 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Recently I plan to make a Bluetooth receiver myself and connect it to the quadcopter, so I study how to output 4-channel PWM.

The plan is to use the timer interrupt, calculate the duty cycle during the interrupt, and then switch the specified PIN pin between high and low levels.

It took me a long time to understand the usage of this. The following code implements an interrupt once per second on the 16M ATMEGA8 chip.

void setup() {

    pinMode(13, OUTPUT);

   

   //Disable interrupts 

    cli();

    //TCNT1 is the accumulator register here, which increases once per clock

    //TCNT1 = 0;

    //TCCR1 control register

    TCCR1A = 0;

    TCCR1B = 0;

    //OCR1A is the accumulation upper limit register. Adjusting this value can control the frequency.

    // Here 15625 is calculated as follows: 16M / 1024 = 15625

    OCR1A = 15625;

    // Set the frequency division to 1024

    // Here _BV is a macro, which is the function of 1 << n. The various ways of setting bits feel quite fragmented.

    TCCR1B |= (_BV(CS12) | _BV(CS10));

    // The CTC mode controlled by WGM12 means that TCNT1 will be cleared to 0 by itself, otherwise it needs to be cleared to 0 manually

    TCCR1B |= 1 << WGM12;

    // Set up comparison interrupt

    bitSet(TIMSK, OCIE1A);

    // Enable interrupt

    be();

}

int flag = 0;

void loop() {

  // put your main code here, to run repeatedly:

}

ISR(TIMER1_COMPA_vect) {

    if (flag == 0) 

    {

        flag = 1;

        digitalWrite(13, HIGH);

    }

    else

    {

        flag = 0;

        digitalWrite(13, LOW);

    }

}


Keywords:AVR Reference address:AVR Timer Compare Interrupt

Previous article:AVR timer interrupt overflow
Next article:ATMEGA128 PWM output series routines

Recommended ReadingLatest update time:2024-11-16 20:56

Track following robot using AVR microcontroller ATmega16
In this project, we will make another gadget using AVR Atmega16 microcontroller. If you are new to AVR microcontrollers, then you can refer to the previous AVR projects and tutorials. Building a robot is a challenge for all electronics enthusiasts. This challenge can be maximized if the robot can perform certain act
[Microcontroller]
Track following robot using AVR microcontroller ATmega16
Technical knowledge sharing: AVR microcontroller introduction and feature analysis
AVR microcontroller is a RISC microcontroller launched by Atmel in 1997. RISC (Reduced Instruction Set Computer) is relative to CISC (Complex Instruction Set Computer). RISC does not simply reduce instructions, but improves the computing speed by making the computer structure simpler and more reasonable. RISC gives p
[Power Management]
PCF8563 subroutine based on AVR microcontroller
/************************************************************************          Purpose: Establish PCF8591 operation library Target system: Based on AVR microcontroller                                                  Application software: ICCAVR                                                         *************
[Microcontroller]
STM32 one timer outputs 4 PWMs with different frequencies and adjustable duty cycles
main.c /*********************************************      Title: Exercise on operating USART      Software platform: MDK-ARM Standard Version4.70      Hardware platform: stm32f4-discovery        Main frequency: 168M      Periph_Driver_version: V1.0.0            Description: Use one timer (TIM3) to implement four PW
[Microcontroller]
Precautions for AVR MCU fuse configuration operation
The configuration of AVR fuse bits is a relatively delicate task. Users often overlook its importance or find it difficult to master. The following are some key points and related matters that need to be noted when configuring AVR fuse bits. For the specific definition and function of ATmega128 fuse bits, please refer
[Microcontroller]
Proteus introductory tutorial: AVR microcontroller simulation example
Introduction: This article is an introductory tutorial for Proteus and a simulation example of AVR microcontroller. 1. Simulation example of AVR microcontroller This example is to realize AVR driving LCD1602 and use oscilloscope to monitor the data line. Before you start, you need to prepare the simulation file,
[Microcontroller]
Proteus introductory tutorial: AVR microcontroller simulation example
AVR Microcontroller Tutorial - Buzzer
Introduction The timer/counter (timer for short) is a vital part of microcontroller programming. Even the simplest microcontroller will have a timer. You may think that we have already touched upon the timer in the delay function, but it is not. It is just a software delay by "wasting time". We touched upon the time
[Microcontroller]
AVR microcontroller common library functions
AVR commonly used library functions 1. Character Type Functions 2. Standard C Input/Output Functions 3. Standard Library Functions – Standard library and memory allocation functions 4. Mathematical Functions 5. String Functions 6. BCD Conversion Functions 7. Memory Access Functions 8. Delay Functions
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号