|
/**************************************************************
* main.c
* rev 1.0 22-Apr-2024 dell
* ISR_task3
* ***********************************************************/
#include "pico/stdlib.h"
#include <stdbool.h>
#include "hardware/pwm.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#include <string.h> // For strncmp
#include <stdlib.h> // For atoi
#include <stdio.h>
void setup_led(uint LED_PIN);
void setup_led1(uint LED_PIN);
void setup_button(uint BUTTON_PIN_ALL);
void button_red_isr();
void button_green_isr();
void button_blue_isr();
#define BUTTON_PIN0 5
#define BUTTON_PIN1 10
#define BUTTON_PIN2 20
//const uint BUTTON_PIN[] = {5, 6, 7}; // Button GPIO pin
const uint LED_PIN[] = {2, 3, 4};
bool increasing = true; // 增减状态的标志变量
uint16_t brightness = 0;
uint BUTTON_RED_ISR=5;
uint BUTTON_GREEN_ISR=6;
uint BUTTON_BLUE_ISR=7;
uint BUTTON_RED_PRESS=0;
uint BUTTON_BLUE_PRESS=0;
uint BUTTON_GREEN_PRESS=0;
// Define pins as in Task 1
// Function to adjust color intensity - To be completed by student
void adjust_color_intensity(uint color_pin) {
// Implement color intensity adjustment logic
uint slice_num = pwm_gpio_to_slice_num(color_pin);
brightness=0;
int i=1;
int j=1;
while(i==1)
{
if (increasing) {
// Increase brightness
brightness++;
if (brightness >= 255) {
increasing = false; // 如果到最高
}
} else {
// Decrease brightness
brightness--;
if (brightness <= 0) {
increasing = true; // 如果最低,切换状态
i=0;
}
}
// Set PWM level to control LED brightness
pwm_set_gpio_level(color_pin, brightness);
while(j<=30000)
{
j++;
if(j>30000)
{
j=0;
break;
}
}
}
}
// ISRs for each button - To be filled in by students
void button_red_isr() {
//pwm_set_gpio_level(LED_PIN[0], 1);
//if(gpio_get(BUTTON_PIN0))
adjust_color_intensity(LED_PIN[0]);
}
void button_green_isr() {
//pwm_set_gpio_level(LED_PIN[1], 0);
//if(gpio_get(BUTTON_PIN1))
adjust_color_intensity(LED_PIN[1]);
}
void button_blue_isr() {
//pwm_set_gpio_level(LED_PIN[2], 0);
//if(gpio_get(BUTTON_PIN2))
adjust_color_intensity(LED_PIN[2]);
}
int main() {
// Initialize stdlib and configure GPIOs
//stdio_init_all();
setup_led(LED_PIN[0]);
setup_led1(LED_PIN[1]);
setup_led(LED_PIN[2]);
setup_button(BUTTON_PIN0);
setup_button(BUTTON_PIN1);
setup_button(BUTTON_PIN2);
uart_init(uart0, 9600);
gpio_set_function(0, GPIO_FUNC_UART);
gpio_set_function(1, GPIO_FUNC_UART);
// TODO: Configure interrupts for the three buttons
irq_set_exclusive_handler(PIO0_IRQ_0, button_red_isr);
irq_set_priority(PIO0_IRQ_0, PICO_HIGHEST_IRQ_PRIORITY); // 最高优先级
gpio_set_irq_enabled_with_callback(BUTTON_PIN0, GPIO_IRQ_EDGE_FALL, true, &button_red_isr);
irq_is_enabled(PIO0_IRQ_0);
//irq_set_enabled(PIO0_IRQ_0, true);
irq_set_exclusive_handler(PIO0_IRQ_1, button_green_isr);
irq_set_priority(PIO0_IRQ_1, PICO_DEFAULT_IRQ_PRIORITY); // 次高优先级
gpio_set_irq_enabled_with_callback(BUTTON_PIN1, GPIO_IRQ_EDGE_FALL, true, &button_green_isr);
irq_is_enabled(PIO0_IRQ_1);
//irq_set_enabled(PIO0_IRQ_1, true);
irq_set_exclusive_handler(PIO1_IRQ_0, button_blue_isr);
irq_set_priority(PIO1_IRQ_0, PICO_LOWEST_IRQ_PRIORITY); // 次次高优先级
gpio_set_irq_enabled_with_callback(BUTTON_PIN2, GPIO_IRQ_EDGE_FALL, true, &button_blue_isr);
irq_is_enabled(PIO1_IRQ_0);
while (true) {
if(gpio_get(BUTTON_PIN0))//只有if触发之后才能把所有的中断都配置上
{
gpio_set_irq_enabled_with_callback(BUTTON_PIN0, GPIO_IRQ_EDGE_FALL, true, &button_red_isr);
}
if(gpio_get(BUTTON_PIN1))
{
gpio_set_irq_enabled_with_callback(BUTTON_PIN1, GPIO_IRQ_EDGE_FALL, true, &button_green_isr);
}
if(gpio_get(BUTTON_PIN2))
{
gpio_set_irq_enabled_with_callback(BUTTON_PIN2, GPIO_IRQ_EDGE_FALL, true, &button_blue_isr);
}
tight_loop_contents();
}
return 0;
}
void setup_led(uint LED_PINS) {
gpio_init(LED_PINS);
gpio_set_dir(LED_PINS, GPIO_OUT);
//gpio_put(LED_PIN, 1);
gpio_set_function(LED_PINS, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(LED_PINS);
pwm_set_wrap(slice_num, 255);
pwm_set_chan_level(slice_num, PWM_CHAN_A, 255);
pwm_set_enabled(slice_num, true);
}
void setup_led1(uint LED_PINS) {
gpio_init(LED_PINS);
gpio_set_dir(LED_PINS, GPIO_OUT);
//gpio_put(LED_PIN, 1);
gpio_set_function(LED_PINS, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(LED_PINS);
pwm_set_wrap(slice_num, 255);
pwm_set_chan_level(slice_num, PWM_CHAN_B, 255);
pwm_set_enabled(slice_num, true);
}
void setup_button(uint BUTTON_PIN_ALL) {
gpio_init(BUTTON_PIN_ALL);
gpio_set_dir(BUTTON_PIN_ALL, GPIO_IN);
gpio_pull_up(BUTTON_PIN_ALL);
}
|
|