Using Atmega8 to implement 8-way keyboard D-type trigger latch function

Publisher:Blissful5Latest update time:2011-11-08 Keywords:Atmega8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Achievement purpose:

When a pin is configured as an input, learn how to program a pull-up resistor to simplify the hardware.

How to use software to control sampling frequency and time to achieve anti-interference purpose.

In order to make the program run more stably and prevent it from running away, learn how to use the watchdog.

Circuit and software principle description:

To simplify the code and circuit diagram, this experiment only uses two inputs and two outputs.

(Atmega8 can be expanded to support up to 11 D flip-flops by modifying the software). Under the supervision of the watchdog, Atmega8 periodically scans the sampling levels of PB0 and PB1. If the results of ten consecutive samples are the same, it is considered a valid sample. If one or more of the ten samples are different, it is considered an interference or critical state and will not be processed. This software implements the function of a D-type flip-flop latch: that is, each time the SW is pressed, the corresponding output will flip once.

In order to increase the versatility of the program and facilitate future performance testing or adjustments, the periodic scanning sampling cycle and the effective number of samplings in this program can be easily adjusted. (Just modify the sampling_times and sampling_interval values ​​in the program. This program is defined as a valid input only when the level is the same after 20 scans. The interval between each scan is 50us).

Change the 2 in #define sapleing_way 2 to the number of ways you need, and the new number of ways will be automatically processed without modifying the code.


Question 1:

Why do we need to use sampling_times scans to sample? Only when the results of consecutive sampling_times samplings are consistent is it considered a valid input?

A: It is to increase the anti-interference ability and prevent the uncertainty caused by keyboard jitter when pressing. After the implementation is completed, you can set the sampling number to 1, and you will find that the operation of the D flip-flop will become unreliable.

Question 2:

Why use a watchdog?

A: In actual applications, many unknown situations often occur, which may cause the AVR chip to "run away", that is, the program will go wrong or even freeze. The chip must be reset to solve the problem. The watchdog actually resets the AVR chip regularly. Of course, when designing the program, it is important to place the watchdog instruction in the correct position to ensure that the program will not be reset during execution, and that the program will fall into an infinite loop and reset within the allowed time.

View the C code (the code has detailed comments):

/************************************************************
Experiment 4 (Second Edition): Use Atmega8 to implement the function of D-type trigger latch
Purpose: www.elecfans.com
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 sapleing_way 2 //Define how many 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 for each sampling, in units of us. typedef struct { unsigned int v_last; //The result of the last sampling_times sampling value unsigned int v_current; //The result of the current sampling_times sampling value unsigned int v[sampling_times]; //Store the continuous sampling values ​​of sampling_times 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]=PINB&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 8-way keyboard D-type trigger latch function

Previous article:1602b LCD display usage example
Next article:Design of multi-chip microcomputer DC power supply control board

Recommended ReadingLatest update time:2024-11-17 01:40

atmega8 routine: query mode AD acquisition
/****************************************************** **********  * Function library description: ATMEGE8 AD analog-to-digital conversion                          * Version: v1.0                                                                            *                                                            *
[Microcontroller]
AVR BOOTLOADER Example Development Notes
I wrote a Bootloader application example based on Atmega8, and encountered some problems during the process. The process and problems are described below for your reference. The code of the project example is at http://download.csdn.net/detail/knowmuch/7712209, which also includes a C# supporting burning program. Frie
[Microcontroller]
AVR BOOTLOADER Example Development Notes
ATmega88 outputs 1khz square wave
#include iom88v.h #include macros.h int blink_sign = 0;  //Time:Time =256-start num * N /clk //start num:Count = 256 - (clk/N )* time //           246 = 256 - (8000 0000 / 8) *  time //time = 0.0001 //time = 0.00001 //0.001s = 1ms //0.1ms = 0.0001 void timer0_init(void) {    TCCR0A = 0x00;     //timer0 normal mo
[Microcontroller]
ATmega88 Standby Mode
Standby mode When SM2..0 is 110 and an external crystal oscillator or ceramic resonator is selected as the clock source, the SLEEP instruction puts the MCU into Standby mode. The only difference between this mode and the Power-down mode is that the oscillator continues to work. The wake-up time only takes 6 clock cyc
[Microcontroller]
ATmega88 Standby Mode
Use of AVR MCU ATMEGA8 USART
In a recent project, the blogger encountered a function that required the use of USART serial port for communication. First of all, there is one very different thing between USART and UART: UART: universal asynchronous receiver and transmitter              TX , RX   USART: universal synchronous asynchronous receiver a
[Microcontroller]
ISD2500+ATmega8 realizes intelligent voice system
1 Introduction With the rapid development of semiconductor technology and computer technology, voice technology is also making continuous breakthroughs, especially speech synthesis technology is becoming more and more perfect, and new voice chips are constantly emerging. The ISD2500 series voice chips produced
[Microcontroller]
ISD2500+ATmega8 realizes intelligent voice system
ATmega88 simple pin settings
// Fuse bits F7 DC F9 FF // pb0 or pd7 low is a fault -》》 PD5 outputs low, PC0 PC1 PC2 outputs high level  //PD3 is high level emergency -》》PD6 30% //PD2 is low level, normal -》》PD6 DIP switch //PB6 high -- PB2 low #include iom88v.h #include macros.h unsigned char yingji_sign; unsigned char normal_sign; unsigned
[Microcontroller]
ATmega8 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 opera
[Microcontroller]
ATmega8 General Registers
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号