Exp 09:
Aim: To develop skills
to code and interface key and LED to microcontrollers in embedded systems.
Flowchart:
Algorithm:
- Initialize port pin P0.0 as input.
- Initialize port pin P2.0 as output.
- Read the status of key.
- If key is closed then turn ON the LED.
- Add some delay
- Turn OFF the LED.
- Repeat from step3.
Diagram:
Program:
Step 3: ‘C’
Language Program
#include<reg51.h>
sbit LED_pin=P2^0; sbit KEY_pin=P0^0;
void delay_ms(unsigned int);
void main (void) { KEY_pin=1; LED_pin=0; LED_pin=1;
while(1) { if(KEY_pin
== 0 ) { LED_pin =
0;
delay_ms(200); LED_pin =
1; } } } void delay_ms(unsigned int k) { unsigned int
i, j;
for(i=0;i<k;i++) {
for(j=0;j<1275;j++); } } |
Comments
/* Special function register declarations */ /* for the intended 8051 derivative */ //Define LED PIN //Define key PIN
//Function prototype declaration
//Make key pin input //Make LED pin output //Make LED off initially
//Iinfinite loop
//If key pressed
//LED ON //Delay //LED OFF
//Delay function
|
Conclusion: We learnt how to
code for the interfacing of key and LED to the microcontroller in the embedded
system.