Assignment Answer sheet
Assignment Answer sheet
Q.1)
1. Blink the onboard LED (GP25) 10 times in the first minute, 20 times in the second, and
30 times in the third.
2. Allow you to press a button (connected to GP15) to toggle the blinking pattern to 30
times (first minute), 20 times (second minute), and 10 times (third minute) for subsequent
3-minute cycles.
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/time.h" // Required for to_ms_since_boot, get_absolute_time
1
ThinkClock Battery Labs (Assignment Answer sheet)
2
ThinkClock Battery Labs (Assignment Answer sheet)
while (true) {
// Check if a button press event occurred and process it
if (g_button_pressed_event) {
g_reverse_pattern = !g_reverse_pattern; // Toggle the pattern
g_button_pressed_event = false; // Reset the event flag
}
3
ThinkClock Battery Labs (Assignment Answer sheet)
/**
* @brief Blinks the LED a specified number of times over a period of one minute.
* Each blink (ON + OFF cycle) is timed so that 'num_blinks' occur in 60 seconds.
* @param num_blinks The number of times the LED should blink within one minute.
* If 0 or negative, the LED remains off for one minute.
*/
void blink_led_n_times_in_one_minute(int num_blinks) {
if (num_blinks <= 0) {
gpio_put(LED_PIN, 0); // Keep LED off
sleep_ms(60000); // Wait for 60 seconds
return;
}
4
ThinkClock Battery Labs (Assignment Answer sheet)
/**
* @brief Interrupt Service Routine (ISR) for button presses.
* Handles debouncing and sets a global flag when a valid button press (falling edge) occurs.
* @param gpio The GPIO pin number that triggered the interrupt.
* @param events The type of event(s) that occurred.
*/
void button_isr(uint gpio, uint32_t events) {
if (gpio == BUTTON_PIN) { // Check if the interrupt is from our button
uint32_t current_time = to_ms_since_boot(get_absolute_time());
// Check for falling edge and debounce
if ((events & GPIO_IRQ_EDGE_FALL) && (current_time - g_last_button_press_time >
DEBOUNCE_DELAY_MS)) {
g_last_button_press_time = current_time; // Update last press time
g_button_pressed_event = true; // Set the global event flag
}
}
}
Code Explaination :
Pin Definitions:
Global Variables:
g_reverse_pattern: A boolean that's false for the normal pattern (10, 20, 30 blinks) and
true for the reversed pattern (30, 20, 10 blinks).
g_button_pressed_event: A flag set by the interrupt service routine (ISR) when the button
is pressed. This allows the main loop to handle the button press safely.
5
ThinkClock Battery Labs (Assignment Answer sheet)
main() function:
Initializes the LED pin as an output and the button pin as an input with an internal pull-up
resistor. This means the button should connect GP15 to Ground (GND) when pressed.
Sets up an interrupt (button_isr) to detect when the button is pressed (a falling edge on
GP15).
The main while(true) loop continuously checks g_button_pressed_event. If true, it
toggles g_reverse_pattern.
Based on g_reverse_pattern, it sets the number of blinks for each of the three minutes.
It then calls blink_led_n_times_in_one_minute() for each minute in the 3-minute cycle.
This function is called automatically when the button state changes (specifically, on a
falling edge, i.e., when pressed).
It includes a debounce mechanism: it only registers a press if enough time
(DEBOUNCE_DELAY_MS) has passed since the last registered press.
If a valid press is detected, it sets g_button_pressed_event to true.
Q.2
First Circuit: Shift a 0V to 3.3V PWM signal down by 1.65V, resulting in a signal that swings
from -1.65V to +1.65V. The peak-to-peak amplitude remains 3.3V.
Second Circuit: Shift the -1.65V to +1.65V signal back up by 1.65V, restoring it to the original
0V to 3.3V range.
6
ThinkClock Battery Labs (Assignment Answer sheet)
Explanation:
U1 is an op-amp.
Resistors R1,R2,R3,R4 are all of equal value
The input PWM signal (VPWM) is applied to the non-inverting path (via R1 and R2
forming a voltage divider, with R2 to ground).
The reference voltage VREF (+1.65V) is applied to the inverting path (via R3 and R4
forming the feedback network).
The standard difference amplifier formula, when all four resistors are equal (R1=R2=R3=R4=R),
is:
VOUT1=VPWM−VREF
let's verify:
Explanation:
U2 is an op-amp.
VIN_shifted (the output of Circuit 1) is one input.
VREF (+1.65V from the buffered divider) is the second input.
Resistors R5 and R6 (e.g., 10k$\Omega$ each) connect these inputs to the non-inverting
terminal (V+) of U2. The voltage at V+ will be the average: V+=(VIN_shifted+VREF)/2.
Resistors R7 and R8 (e.g., 10k$\Omega$ each) configure U2 as a non-inverting amplifier
with a gain of (1+R8/R7). If R7=R8, the gain is 2.
7
ThinkClock Battery Labs (Assignment Answer sheet)
let's verify:
This circuit successfully shifts the signal back to the original 0V to 3.3V range.
Q3)
Both diagrams illustrate methods to assemble a 3S3P battery pack (3 cells in series to achieve a
higher voltage, and 3 such sets in parallel to achieve higher capacity), but they differ in how the
cells are grouped. the pros and cons of each:
Pros:
Cons:
8
ThinkClock Battery Labs (Assignment Answer sheet)
Pros:
Cons: