Using Atmega8 to implement the function of D trigger latch

Publisher:PositiveVibesLatest update time:2017-12-20 Source: eefocusKeywords:Atmega8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/******************************************************************************
Experiment 4 (Second Edition): Use Atmega8 to implement the function of D-type trigger latch.
Implementation purpose:
1. When the pin is set as input, understand how to program the pull-up resistor .
2. How to use software to control the sampling frequency and time to achieve a certain anti-interference purpose
. 3. In order to make the program run more stable and prevent it from running away, how to use the watchdog?

 

By armok (2004-09-18) a13809260240@126.com 
************************************ ***************************************/


#include //This experiment uses Atmega8
#include

#define sampling_way 2 //Define the number of sampling channels. The maximum value is 8. PB is input and PD is output.
#define sampling_times 20 //Define the number of sampling times. The same sampling values ​​for consecutive times are considered valid sampling.
#define sampling_interval 50 //Define the time interval between each sampling, in us.


typedef struct
{ unsigned int v_last; //The result of the last sampling_times sampling values
  ​​unsigned int v_current; //The result of the current sampling_times sampling values
  ​​unsigned int v[sampling_times]; //Store the continuous sampling values ​​of sampling_times
  unsigned int v_temp; //Store the temporary value for comparison, valid when 1, invalid when 0
} inputStruct;

void delay_nus(unsigned int n); //Delay function, unit is us.
void watchdog_init(void); //Initialize watchdog function
void port_init(void); //Port initialization function
void main(void) //Main function
{  
  unsigned int i;
  unsigned int j;
  inputStruct pb_input[sapleing_way];
     
  port_init(); //Initialize port
  watchdog_init(); //Initialize watchdog
  
  while (1)
  {  
 //The following for loop stores the sampling results of consecutive sampling_times times in the corresponding array
    for (i=0;i {
     delay_nus(sampling_interval); //Sampling once every sampling_interval
  for(j=0;j  {
   pb_input[j].v[i]= PIN B&BIT(j);
  }  
 }


 //The following for loop determines whether the sampling results of consecutive sampling_times times are valid
    for(j=0;j    {
    for (i=1;i    {
     if (pb_input[j].v[i-1]==pb_input[j].v[i]) //If the sampling results of sampling_times times are the same, it is considered valid
    pb_input[j].v_temp=1; //The flag that sampling_times times are valid
     else //Otherwise, it is discarded and no processing is performed.
       {
     pb_input[j].v_temp=0; //sampling_times times sampling is invalid, no processing
     break;
    }
     }
 
    //The following if judges the level of PB input , compares it with the result of the previous sampling calculation, and judges whether to flip the corresponding PD
        if (pb_input[j].v_temp==1) //sampling_times times sampling is valid, make the following judgment 
     {
       if (pb_input[j].v[0]==0) //The input is low level
         pb_input[j].v_current=0;       
       else
         pb_input[j].v_current=1; //The input is high level 
       if (pb_input[j].v_last==1 && pb_input[j].v_current==0) //If the first ten samples are high level and the current ten are low level, it is regarded as a valid action and the output is executed
        PORTD^=BIT(j); //Flip the corresponding PD bit 
       pb_input[j].v_last=pb_input[j].v_current; //Pass the current result to the previous result and prepare for the next processing  
     } 
    } //end for
 
 WDR(); //Clear the watchdog count
  } //end while  
} // end main()


void delay_nus(unsigned int n)//n microsecond delay function
{
unsigned int i;
for (i=0;i  {
      asm("nop");
  }
}

void port_init(void)
{
  DDRB=0x00; //Set PB0-7 as input
  PORTB=0xFF; //Works with the next sentence
  SFIOR&=~BIT(2); //Set the PDU pull-up resistor of SFIOR to be valid. Works with the previous sentence.
  DDRD=0xFF; //PD0-7 is output

void watchdog_init(void)
{
 WDR(); //Watchdog count cleared
 WDTCR=0x0F; //Enable watchdog, and use 2048K frequency division , typical overflow time 2.1S at 5V
}


Keywords:Atmega8 Reference address:Using Atmega8 to implement the function of D trigger latch

Previous article:Simple 24-hour clock with 1/100 second
Next article:ICCAVR Learning Mini Program

Recommended ReadingLatest update time:2024-11-16 14:54

ATmega8 EEPROM Data Memory
EEPROM Data Memory ATmega8 contains 512 bytes of EEPROM data memory. It exists as an independent data space and can be read and written byte by byte. The life of EEPROM is at least 100,000 erase cycles. EEPROM access is determined by the address register, data register and control register. P 209 “Memory Programming
[Microcontroller]
ATmega8 EEPROM Data Memory
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号