Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. Consider a push button switch SWO connected to pin INTO external interrupts o

ID: 3704380 • Letter: 8

Question

8. Consider a push button switch SWO connected to pin INTO external interrupts of an ATMegas AVR. 8 LEDS are connected to port PBO PB7 on the same AVR. These LEDS are connected such a lit LED displays that the output port is on logic level 1. On start-up, TASK1 is executed and continue indefinitely. If the push-button switch SWO is pressed, TASKI is paused and TASK2 is carried out after which TASK1 is resumed TASKI- A roll action is performed using the LEDs: The first LED is lit and roll down to the last LED then back to the first LED. This operation is done indefinitely TASK2- ALL the LEDs blink five (5) times. Write a complete C program that implements the desired tasks

Explanation / Answer

make int sw0 as extern, which will be operated from outside. 0 is used for led off and 1 is used for led on. observe the print and code. Task2 can be called using debug mode by changing sw0 value.

/*******************/

//============================================================================
// Name        : led.c
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C, Ansi-style
//============================================================================

#include <stdio.h>
#include <string.h>
#include <unistd.h>
int sw0;
struct LED{
   unsigned int pb:1;
};
LED led[8];   //array of 8 leds
int paused;
void TASK2(){
   int i,j;
   for(i=0;i<10;i++){
       sleep(1);
       if(i%2==0)
           memset(led,1,sizeof(led));   //put all led on
       else
           memset(led,0,sizeof(led));   //put all led off

   for(j=0;j<8;j++)
       printf("%d ",led[j].pb);
   printf(" ");
   paused=0;
   }
}
void TASK1(){
   int i=0,j;
   while(1){   //continuous loop
       if(sw0==1)   //check if sw0 switch is pressed
           paused=1;
       if(!paused){
           sleep(1);   //delay for 1 sec
       memset(led,0,sizeof(led));   //put all led off 0-off 1-on
       i=i%8;
       led[i].pb=1;
       i++;
       for(j=0;j<8;j++)
           printf("%d ",led[j].pb);
       printf(" ");
       }
       else{
           TASK2();
       }
   }
}

int main() {
   memset(led,0,sizeof(led));
   paused=0;
   TASK1();
   return 0;
}


/*********************/output

1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote