static void key_hw_int_isr_0(uint32_t const *event_pins_low_to_high, uint32_t const *event_pins_high_to_low) { key_hw_int_isr(&g_key_driver_mgr.key_instance[KEY_DRIVER_INDEX_0], event_pins_low_to_high, event_pins_high_to_low); } static void key_hw_int_isr_1(uint32_t const *event_pins_low_to_high, uint32_t const *event_pins_high_to_low) { key_hw_int_isr(&g_key_driver_mgr.key_instance[KEY_DRIVER_INDEX_1], event_pins_low_to_high, event_pins_high_to_low); }
时间: 2023-08-22 21:10:56 浏览: 111
这段代码看起来是针对硬件中断处理的,其中定义了两个函数,分别对应不同的硬件中断。在这两个函数中,都调用了一个名为key_hw_int_isr的函数,并传入了不同的参数。可以猜测这个key_hw_int_isr函数是用来处理硬件中断的通用函数,而这里的代码是在不同的硬件中断触发时,调用这个通用函数并传入不同的参数,以区分不同的硬件中断。
相关问题
static void key_hw_int_isr(key_driver_instance_t *p_key_instance, uint32_t const * event_pins_low_to_high, uint32_t const * event_pins_high_to_low) { do { if((*event_pins_high_to_low & p_key_instance->pin_mask) != 0) { key_hw_int_send_event(p_key_instance, true); //按键按下 } else if((*event_pins_low_to_high & p_key_instance->pin_mask) != 0) { key_hw_int_send_event(p_key_instance, false); //按键弹起 } else { break; } key_hw_int_disable(p_key_instance); //禁止检测 }while(0); }
这是一个关于按键中断处理的函数,它接收一个 key_driver_instance_t 类型的指针参数和两个 uint32_t 类型的指针参数。其中,event_pins_low_to_high 表示从低电平到高电平的事件标志,event_pins_high_to_low 表示从高电平到低电平的事件标志。该函数的作用是检测哪个按键被按下或弹起,并调用 key_hw_int_send_event 函数发送事件。在检测到按键事件后,该函数会禁用按键中断检测,以避免重复触发事件。
MSP430F6638按下key1控制led灯的亮灭并记录按键次数显示在lcd屏上,按照key2用flash存储当前的按键次数,断电再开机,再次按下key2在lcd上显示断电前保存的次数
好的,这是一个比较典型的嵌入式系统应用,需要结合MSP430F6638的GPIO、LCD和Flash模块进行编程实现。下面是一个大致的代码框架:
```c
#include <msp430.h>
#include <stdint.h>
#define KEY1 BIT0
#define KEY2 BIT1
#define LED BIT2
#define LCD_RS BIT3
#define LCD_E BIT4
#define LCD_DB4 BIT5
#define LCD_DB5 BIT6
#define LCD_DB6 BIT7
#define LCD_DB7 BIT8
#define CMD_CLEAR_LCD 0x01
#define CMD_RETURN_HOME 0x02
#define CMD_ENTRY_MODE_SET 0x04
#define CMD_DISPLAY_CONTROL 0x08
#define CMD_FUNCTION_SET 0x20
#define CMD_SET_CGRAM_ADDR 0x40
#define CMD_SET_DDRAM_ADDR 0x80
#define ENTRY_MODE_INCREMENT 0x02
#define ENTRY_MODE_SHIFT_CURSOR 0x00
#define DISPLAY_CONTROL_DISPLAY_ON 0x04
#define DISPLAY_CONTROL_CURSOR_ON 0x02
#define DISPLAY_CONTROL_BLINK_ON 0x01
#define FUNCTION_SET_8BIT_MODE 0x10
#define FUNCTION_SET_2LINE_MODE 0x08
#define FUNCTION_SET_FONT_5X8 0x00
#define FLASH_START_ADDR 0x1800
void lcd_write_nibble(uint8_t nibble, uint8_t rs);
void lcd_write_byte(uint8_t byte, uint8_t rs);
void lcd_init();
void lcd_clear();
void lcd_return_home();
void lcd_set_cursor(uint8_t row, uint8_t col);
void lcd_write_char(char c);
void lcd_write_string(const char* str);
void delay_ms(unsigned int ms);
void flash_write(uint16_t addr, uint8_t data);
uint8_t flash_read(uint16_t addr);
volatile uint8_t key1_pressed = 0;
volatile uint8_t key2_pressed = 0;
volatile uint8_t key2_released = 0;
volatile uint16_t key1_count = 0;
volatile uint16_t key2_count = 0;
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
PM5CTL0 &= ~LOCKLPM5; // disable high-impedance mode
P1DIR |= LED; // set LED pin as output
P1OUT &= ~LED; // turn off LED
P1REN |= KEY1 | KEY2; // enable pull-up resistors on key pins
P1IE |= KEY1 | KEY2; // enable interrupts on key pins
P1IES |= KEY1 | KEY2; // trigger interrupt on falling edge
P1IFG &= ~(KEY1 | KEY2); // clear interrupt flags on key pins
lcd_init(); // initialize LCD
lcd_clear(); // clear LCD screen
delay_ms(10); // wait for LCD to stabilize
lcd_write_string("Press Key1."); // display initial message
__enable_interrupt(); // enable global interrupts
while (1) {
if (key1_pressed) { // handle Key1 press
key1_pressed = 0;
P1OUT ^= LED; // toggle LED
key1_count++; // increment count
lcd_set_cursor(1, 0); // set cursor to second line
lcd_write_string("Key1: ");
lcd_write_char(key1_count / 100 + '0'); // display count
lcd_write_char((key1_count / 10) % 10 + '0');
lcd_write_char(key1_count % 10 + '0');
}
if (key2_pressed) { // handle Key2 press
key2_pressed = 0;
key2_count = key1_count; // save count to flash
flash_write(FLASH_START_ADDR, key2_count >> 8);
flash_write(FLASH_START_ADDR + 1, key2_count & 0xFF);
lcd_set_cursor(1, 0); // set cursor to second line
lcd_write_string("Key2: Saved.");
delay_ms(500);
lcd_clear(); // clear LCD screen
lcd_write_string("Press Key1."); // display initial message
}
if (key2_released) { // handle Key2 release
key2_released = 0;
uint16_t saved_count = (flash_read(FLASH_START_ADDR) << 8)
| flash_read(FLASH_START_ADDR + 1); // read count from flash
lcd_set_cursor(1, 0); // set cursor to second line
lcd_write_string("Key2: ");
lcd_write_char(saved_count / 100 + '0'); // display count
lcd_write_char((saved_count / 10) % 10 + '0');
lcd_write_char(saved_count % 10 + '0');
}
}
}
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR() {
if (P1IFG & KEY1) { // handle Key1 interrupt
key1_pressed = 1;
P1IFG &= ~KEY1; // clear interrupt flag
}
if (P1IFG & KEY2) { // handle Key2 interrupt
if (P1IN & KEY2) { // handle Key2 release
key2_released = 1;
} else { // handle Key2 press
key2_pressed = 1;
}
P1IFG &= ~KEY2; // clear interrupt flag
}
}
void lcd_write_nibble(uint8_t nibble, uint8_t rs) {
if (rs) {
P1OUT |= LCD_RS;
} else {
P1OUT &= ~LCD_RS;
}
P1OUT &= ~(LCD_DB4 | LCD_DB5 | LCD_DB6 | LCD_DB7);
P1OUT |= (nibble << 4) & (LCD_DB4 | LCD_DB5 | LCD_DB6 | LCD_DB7);
P1OUT |= LCD_E;
delay_ms(1);
P1OUT &= ~LCD_E;
delay_ms(1);
}
void lcd_write_byte(uint8_t byte, uint8_t rs) {
lcd_write_nibble(byte >> 4, rs);
lcd_write_nibble(byte & 0x0F, rs);
}
void lcd_init() {
P1DIR |= LCD_RS | LCD_E | LCD_DB4 | LCD_DB5 | LCD_DB6 | LCD_DB7;
delay_ms(50);
lcd_write_nibble(0x03, 0);
delay_ms(5);
lcd_write_nibble(0x03, 0);
delay_us(100);
lcd_write_nibble(0x03, 0);
delay_us(100);
lcd_write_nibble(0x02, 0);
lcd_write_byte(CMD_FUNCTION_SET | FUNCTION_SET_8BIT_MODE, 0);
lcd_write_byte(CMD_DISPLAY_CONTROL | DISPLAY_CONTROL_DISPLAY_ON, 0);
lcd_write_byte(CMD_ENTRY_MODE_SET | ENTRY_MODE_INCREMENT, 0);
lcd_write_byte(CMD_CLEAR_LCD, 0);
}
void lcd_clear() {
lcd_write_byte(CMD_CLEAR_LCD, 0);
delay_ms(2);
}
void lcd_return_home() {
lcd_write_byte(CMD_RETURN_HOME, 0);
delay_ms(2);
}
void lcd_set_cursor(uint8_t row, uint8_t col) {
uint8_t addr = col;
if (row == 1) {
addr += 0x40;
}
lcd_write_byte(CMD_SET_DDRAM_ADDR | addr, 0);
}
void lcd_write_char(char c) {
lcd_write_byte(c, 1);
}
void lcd_write_string(const char* str) {
while (*str) {
lcd_write_char(*str++);
}
}
void delay_ms(unsigned int ms) {
__delay_cycles(1000 * ms);
}
void flash_write(uint16_t addr, uint8_t data) {
FCTL3 = FWKEY; // unlock flash
FCTL1 = FWKEY | WRT; // enable write mode
*((uint8_t*)addr) = data; // write byte to flash
FCTL1 = FWKEY; // disable write mode
FCTL3 = FWKEY | LOCK; // lock flash
}
uint8_t flash_read(uint16_t addr) {
return *((uint8_t*)addr); // read byte from flash
}
```
在这个代码框架中,我们首先定义了一些常量,例如按键、LED、LCD和Flash的引脚和指令,以及保存计数值的Flash地址。然后定义了一些函数,如LCD的基本操作函数、延时函数和Flash的读写函数。在`main()`函数中初始化各个模块,并进入主循环。主循环中,通过轮询的方式检查是否有按键按下或释放,并根据按键采取相应的操作。具体来说:
- 当检测到Key1按下时,翻转LED并增加计数值,然后在LCD上显示计数值。
- 当检测到Key2按下时,将当前计数值保存到Flash中,并在LCD上显示“Saved.”的提示信息。
- 当检测到Key2释放时,从Flash中读取之前保存的计数值,并在LCD上显示该值。
在中断服务函数中,我们通过检查P1IFG寄存器来确定是哪个按键触发了中断,然后设置相应的标志位,在主循环中处理。
需要注意的是,LCD的初始化过程较为复杂,需要通过发送一系列指令来设置其工作模式和显示内容。在实际应用中,可能需要根据具体的LCD型号和接口来进行相应的修改。同时,Flash的写操作可能需要进行一些额外的保护,例如在写之前先擦除整个Flash扇区。
阅读全文