Making header file for button
so now you have got some basic idea about header files and library they are nothing but frequently used code a series of code that are systematically defined in a separate page for efficient and fastest use of the functions and codes.
In this chapter we will design a simple library for the button press that might be helpful for future use , by using this header file you will save your time a little bit more .
So now , to begin creating the header file go to your ATMEL studio (or simply go to note pad I prefer using atmel studio )>> Go to file >> New >> file >> click on include file >>and now write the following code in there
Or simply copy and paste the code below in note pad and save as ButtonPress.h at your desired location
After writing the code save the file as ButtonPress.h
.h means header file .. save this file to desktop for future reference
Now to check this file is working or not go to file>>new>>project>>GCC C Executable Project ;type name of your project >> select chip ATMEGA16>>
Now minimize your atmel studio ,copy your ButtonPress.h file (saved in notepad) paste it into libraries which is just right side of the opened window , to clear this following look at the following figure
In this figure there is GccApplication31 right click on it and paste the file While including these type of external header file you should write like this #include ”ButtonPress.h” more explanation is given below after the code
or simply copy and paste the previously saved ButtonPressed.h file to your currently using libraries .
To define a ButtonPress header file we use our code as below .If not defined define ButtonPress library
After including the standard avr input output header file then define a function that take four parameters
char ButtonPressed(int buttonNumber, unsigned char pinOfButton, int portBit, int confidenceLevel);
this is ButtonPressed function inside this function pass the variable like button number pin of the microcontroller used for button port name and the confidence level for the debouncing the button ; while calling the function inside the main routine .
after this define a variable that take the number of buttons array to determine the particular button is pressed or not pressed.
After that declare two variable which are used to count high and low pulse of the button while it is bouncing so that count of high and low pulse is reached in certain level and using this we can proceed the further taking this reference .
int Pressed_Confidence_Level[numberOfButtons]; //Measure button press cofidence
int Released_Confidence_Level[numberOfButtons]; //Measure button release confidence
now inside the declaration of the ButtonPressed function we should count the high pulse and low pulse for particular button and using this reference if the confidence level is reached up to certain level the function return ‘1’ where it is called . and we can do the job by if the returned value is ‘1’ or not .
statement and at last end the header file using #endif statement.
Now let us modify the previous example of led blinking pressing button .
Connect the hardware as shown in circuit below. And write the code including the button press library that you have created as follows.
here is an application of button press library and example code