Using: C Language & tinkercad.com & arduino uno r3 #include #include #include #includ
Posted: Fri May 20, 2022 12:09 pm
Using: C Language & tinkercad.com
& arduino uno r3
#include <stdint.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void uart_setup(void);
void uart_put_byte(unsigned char byte_val);
void uart_printf(const char * fmt, ...);
void setup(void) {
// (a) Initialise Timer 1 in normal
mode so that it overflows
// with a period of approximately
0.25 seconds.
// Hint: use the table you
completed in a previous exercise.
// (b) Enable timer overflow for
Timer 1.
// (c) Turn on interrupts.
// (d) Send a debugging message
to the serial port using
// the uart_printf function defined below. The
message should consist of
// your student number, "n10507621", followed
immediately by a comma,
// followed by the pre-scale factor that
corresponds to a timer overflow
// period of approximately 0.25 seconds.
Terminate the
// debugging message with a
carriage-return-linefeed pair, "\r\n".
}
// (e) Create a volatile global variable
called counter.
// The variable should be a 32-bit unsigned
integer of type uint32_t.
// Initialise the variable to 0.
// INSERT GLOBAL VARIABLE HERE
// (f) Define an interrupt service routine to
process timer overflow
// interrupts for Timer 1. Every time the
interrupt service
// routine is called, counter should increment by
1.
// INSERT ISR HERE
// (g) Define a function called
get_elapsed_time which has
// no parameters, but returns a value of type
double which contains
// the total elapsed time measured up to the time
at which it is called.
// Use the method demonstrated in the Topic 9
lecture to compute the
// elapsed time, taking into account the fact
that the timer counter has
// 16 bits rather than 8 bits.
// INSERT FUNCTION HERE
// -------------------------------------------------
// Helper functions.
// -------------------------------------------------
// Make sure this is not too big!
char buffer[100];
void uart_setup(void) {
#define BAUD (9600)
#define UBRR (F_CPU / 16 / BAUD - 1)
UBRR0H = UBRR >> 8;
UBRR0L = UBRR & 0b11111111;
UCSR0B = (1 << RXEN0) | (1 <<
TXEN0);
UCSR0C = (3 << UCSZ00);
}
void uart_printf(const char * fmt, ...) {
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
for (int i = 0; buffer; i++) {
uart_put_byte(buffer);
}
}
#ifndef __AMS__
void uart_put_byte(unsigned char data) {
while (!(UCSR0A & (1 << UDRE0))) { /* Wait
*/ }
UDR0 = data;
}
#endif
int main() {
uart_setup();
setup();
for (;;) {
double time_now =
get_elapsed_time();
uart_printf("Elapsed time =
%d.%03d\r\n", (int)time_now, (int)((time_now - (int)time_now) *
1000));
_delay_ms(1000);
}
return 0;
}
Implement and test a function called get_elapsed_time which computes the elapsed time from power-up in a ATMEGA328P microcontroller. The program will use a designated 16-bit timer in normal mode, with overflow interrupt handling. Time calculation will be accurate to the nearest timer update “tick” Your task is to adapt the sample program provided in “Lecture 9: Implementing Timer Overflow ISR” to implement a new library function called get_elapsed_time () which is capable of tracking elapsed time for a reasonably long period. Use Timer 1, and set it up in normal operational mode so that it overflows approximately once every 0.25 seconds. Create a global 32-bit unsigned integer variable called counter. Implement an interrupt service routine which increments counter by 1 every time the timer overflows. Implement a function called get_elapsed_time() which returns the elapsed time since program start, accurate to the nearest timer stick”, as a double-precision floating point value. To implement the function, follow the detailed specification laid out in comments in the program skeleton below. Notes • Use this test driver to implement and test your function in TinkerCad Circuits prior to submission.
• Do not use the static qualifier for global variables. This causes variables declared at file scope to be made private, and will prevent AMS from marking your submission.
& arduino uno r3
#include <stdint.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void uart_setup(void);
void uart_put_byte(unsigned char byte_val);
void uart_printf(const char * fmt, ...);
void setup(void) {
// (a) Initialise Timer 1 in normal
mode so that it overflows
// with a period of approximately
0.25 seconds.
// Hint: use the table you
completed in a previous exercise.
// (b) Enable timer overflow for
Timer 1.
// (c) Turn on interrupts.
// (d) Send a debugging message
to the serial port using
// the uart_printf function defined below. The
message should consist of
// your student number, "n10507621", followed
immediately by a comma,
// followed by the pre-scale factor that
corresponds to a timer overflow
// period of approximately 0.25 seconds.
Terminate the
// debugging message with a
carriage-return-linefeed pair, "\r\n".
}
// (e) Create a volatile global variable
called counter.
// The variable should be a 32-bit unsigned
integer of type uint32_t.
// Initialise the variable to 0.
// INSERT GLOBAL VARIABLE HERE
// (f) Define an interrupt service routine to
process timer overflow
// interrupts for Timer 1. Every time the
interrupt service
// routine is called, counter should increment by
1.
// INSERT ISR HERE
// (g) Define a function called
get_elapsed_time which has
// no parameters, but returns a value of type
double which contains
// the total elapsed time measured up to the time
at which it is called.
// Use the method demonstrated in the Topic 9
lecture to compute the
// elapsed time, taking into account the fact
that the timer counter has
// 16 bits rather than 8 bits.
// INSERT FUNCTION HERE
// -------------------------------------------------
// Helper functions.
// -------------------------------------------------
// Make sure this is not too big!
char buffer[100];
void uart_setup(void) {
#define BAUD (9600)
#define UBRR (F_CPU / 16 / BAUD - 1)
UBRR0H = UBRR >> 8;
UBRR0L = UBRR & 0b11111111;
UCSR0B = (1 << RXEN0) | (1 <<
TXEN0);
UCSR0C = (3 << UCSZ00);
}
void uart_printf(const char * fmt, ...) {
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
for (int i = 0; buffer; i++) {
uart_put_byte(buffer);
}
}
#ifndef __AMS__
void uart_put_byte(unsigned char data) {
while (!(UCSR0A & (1 << UDRE0))) { /* Wait
*/ }
UDR0 = data;
}
#endif
int main() {
uart_setup();
setup();
for (;;) {
double time_now =
get_elapsed_time();
uart_printf("Elapsed time =
%d.%03d\r\n", (int)time_now, (int)((time_now - (int)time_now) *
1000));
_delay_ms(1000);
}
return 0;
}
Implement and test a function called get_elapsed_time which computes the elapsed time from power-up in a ATMEGA328P microcontroller. The program will use a designated 16-bit timer in normal mode, with overflow interrupt handling. Time calculation will be accurate to the nearest timer update “tick” Your task is to adapt the sample program provided in “Lecture 9: Implementing Timer Overflow ISR” to implement a new library function called get_elapsed_time () which is capable of tracking elapsed time for a reasonably long period. Use Timer 1, and set it up in normal operational mode so that it overflows approximately once every 0.25 seconds. Create a global 32-bit unsigned integer variable called counter. Implement an interrupt service routine which increments counter by 1 every time the timer overflows. Implement a function called get_elapsed_time() which returns the elapsed time since program start, accurate to the nearest timer stick”, as a double-precision floating point value. To implement the function, follow the detailed specification laid out in comments in the program skeleton below. Notes • Use this test driver to implement and test your function in TinkerCad Circuits prior to submission.
• Do not use the static qualifier for global variables. This causes variables declared at file scope to be made private, and will prevent AMS from marking your submission.