In: Computer Science
Make a C program to blink/toggle a LED every second with the
Atmega128 chip.
Hint: Because the delay function has a maximum delay limit, you
will need an internal counter to accumulate delays to one
second.
a) Write code that uses the delay function for implementation
b) Write code that uses a stopwatch on the third toggle
a) Write code that uses the delay function for implementation
// C progam to impliment delay function
// you can use it for time delay
#include <stdio.h>
#include <time.h> // To use time library of C
void delay(int number_of_seconds)
{
   // Converting time into milli_seconds 1000 milli
seconds=1 seconds
   int milli_seconds = 1000 * number_of_seconds;
   // Storing start time
   clock_t start_time = clock();
   // looping till required time is not achieved
   while (clock() < start_time + milli_seconds)
       ;
}
// Driver code to test the function
int main()
{
   int i;
   for (i = 0; i < 10; i++) {
       // delay of one second
       delay(1);
       printf("%d seconds have passed\n",
i + 1);
   }
   return 0;
}
b) Write code that uses a stopwatch on the third toggle
# Python program to impliment a stop watch
#importing the required libraries
import tkinter as Tkinter
from datetime import datetime
counter = 66600
running = False
def counter_label(label):
   def count():
       if running:
           global
counter
  
           # To manage the
intial delay.
           if
counter==66600:          
  
          
    display="Starting..."
           else:
          
    tt = datetime.fromtimestamp(counter)
          
    string = tt.strftime("%H:%M:%S")
          
    display=string
  
          
label['text']=display # Or label.config(text=display)
  
           #
label.after(arg1, arg2) delays by
           # first argument
given in milliseconds
           # and then calls
the function given as second argument.
           # Generally like
here we need to call the
           # function in
which it is present repeatedly.
           # Delays by
1000ms=1 seconds and call count again.
          
label.after(1000, count)
           counter +=
1
  
   # Triggering the start of the counter.
   count()     
  
# start function of the stopwatch
def Start(label):
   global running
   running=True
   counter_label(label)
   start['state']='disabled'
   stop['state']='normal'
   reset['state']='normal'
  
# Stop function of the stopwatch
def Stop():
   global running
   start['state']='normal'
   stop['state']='disabled'
   reset['state']='normal'
   running = False
  
# Reset function of the stopwatch
def Reset(label):
   global counter
   counter=66600
  
   # If rest is pressed after pressing stop.
   if running==False:     
       reset['state']='disabled'
       label['text']='Welcome!'
  
   # If reset is pressed while the stopwatch is
running.
   else:      
          
       label['text']='Starting...'
  
root = Tkinter.Tk()
root.title("Stopwatch")
  
# Fixing the window size.
root.minsize(width=250, height=70)
label = Tkinter.Label(root, text="Welcome!", fg="black",
font="Verdana 30 bold")
label.pack()
f = Tkinter.Frame(root)
start = Tkinter.Button(f, text='Start', width=6,
command=lambda:Start(label))
stop = Tkinter.Button(f, text='Stop',width=6,state='disabled',
command=Stop)
reset = Tkinter.Button(f, text='Reset',width=6, state='disabled',
command=lambda:Reset(label))
f.pack(anchor = 'center',pady=5)
start.pack(side="left")
stop.pack(side ="left")
reset.pack(side="left")
root.mainloop()