atmega8 Example: Delay of system library function

Publisher:温馨幸福Latest update time:2016-12-29 Source: eefocusKeywords:atmega8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*********************************************************** 

* Library Description: ATMEGE8 Delay Library Function 

* Version: v1.0                                                                                          

*                                                                                           

************************************************************ 

*Note: LED PC5                                             

***********************************************************/  

  

#include  

  

//Define external crystal  

#define F_CPU 6000000UL  

  

// Delay including header files  

#include  

  

// Function declaration  

void Delay_s(int ss);  

  

  

int main(void)  

{  

    //LED etc. PC5 is set as output  

    DDRC |= (1 << DDC5);  

    //Start with PC5 outputting high level, LED is off  

    PORTC |= (1 << PC5);  

      

    while(1)  

    {  

        //Return  

        PORTC ^= (1 << PC5);  

        //Delay 1s  

        Delay_s(1);  

    }      

  

    return 0;  

}  

  

/*********************************************************** 

** Name: void Delay_s(int ss) 

** Function: Accurate 1s delay 

** Input parameter: ss The number of seconds to delay 

** Export parameters: None 

** Instructions: System library function delays are limited by the size of the crystal oscillator 

***********************************************************/  

void Delay_s(int ss)  

{  

    int i = 0;  

  

    while(ss--)  

    {  

        for(i = 0; i < 25; i++)  

        {     

            _delay_ms(40);  

        }  

    }  

  

}  


//First, F_CPU (i.e., crystal frequency) is no longer defined in the library file , so define it yourself in main.c.

// And the system delay function has restrictions on the delay size due to different crystal oscillators, please note:

/** 

   \ingroup util_delay 

 

   Perform a delay of \c __us microseconds, using _delay_loop_1(). 

 

   The macro F_CPU is supposed to be defined to a 

   constant defining the CPU clock frequency (in Hertz). 

 

   The maximal possible delay is 768 us / F_CPU in MHz. 

 */  


 

/** 

   \ingroup util_delay 

 

   Perform a delay of \c __ms milliseconds, using _delay_loop_2(). 

 

   The macro F_CPU is supposed to be defined to a 

   constant defining the CPU clock frequency (in Hertz). 

 

   The maximal possible delay is 262.14 ms / F_CPU in MHz. 

 */  


Keywords:atmega8 Reference address:atmega8 Example: Delay of system library function

Previous article:atmega8 example: 5110 LCD driver
Next article:atmega8 example: USART serial communication

Recommended ReadingLatest update time:2024-11-16 16:01

Design of high-precision ultrasonic distance meter based on ATMEGA8
Because ultrasonic waves have strong directivity, slow energy consumption, and long propagation distance in the medium, ultrasonic waves are often used for distance measurement, such as rangefinders and level measuring instruments. Ultrasonic detection is relatively fast, convenient, simple to calculate, easy to achiev
[Microcontroller]
Design of high-precision ultrasonic distance meter based on ATMEGA8
ATmega88 Timer0 overflow interrupt
// Fuse bits F7 DC F9 FF //   Function: pb1 outputs high and low level with a time interval of one second #include iom88v.h #include macros.h int blink_sign = 0;  void timer0_init(void) {    TCCR0A = 0x00; //timer0 normal mode    TCCR0B = 0x03; //Prescale/64 start TIME0 normal mode    TCNT0 = 0x83; //Crystal o
[Microcontroller]
ATmega88 General Registers
The register file is optimized for the AVR enhanced RISC instruction set. To achieve the required performance and flexibility, the register file supports the following input/output schemes: · Output one 8-bit operand, input one 8-bit result · Output two 8-bit operands, input one 8-bit result · Output two 8-bit operand
[Microcontroller]
ATmega88 General Registers
ATmega8 clock system and its distribution
Clock system and its distribution Figure 10 shows the main clock system and its distribution of AVR. These clocks do not need to work at the same time. In order to reduce power consumption, different sleep modes can be used to disable the clocks of modules that do not need to work. For details, see P 30 "Power Managem
[Microcontroller]
ATmega8 clock system and its distribution
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号