/******************************************************************************
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
#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
}
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Bluetooth module interfaces
- Lost, lost, lost
- How to draw a circuit diagram according to the program
- How to terminate a program
- I want to buy a mobile hard drive, please recommend!
- Simple performance optimization under DSP
- Help: How to use two power supplies to drive the same load
- How are the positive and negative bias voltages generated in this circuit?
- EEWORLD University Hall----Live Replay: Maxim-E-Loomeng explains wearable health monitoring in simple terms
- BQ40Z50-R2 security mode introduction and password modification method