MSP430F6638按下key1控制led的亮灭,并且记录按键次数,显示在lcd上,按下key2用flash保存当前按键次数,断电再开机,再一次按下key2,在lcd上显示之前保存的次数
时间: 2023-10-23 09:11:44 浏览: 175
以下是MSP430F6638的按键控制LED亮灭并记录按键次数并显示在LCD上的示例代码:
```c
#include <msp430.h>
#include <stdio.h>
#include <string.h>
#define LCD_RS BIT0
#define LCD_RW BIT1
#define LCD_E BIT2
#define LCD_D4 BIT3
#define LCD_D5 BIT4
#define LCD_D6 BIT5
#define LCD_D7 BIT6
#define LED BIT0
#define KEY1 BIT1
#define KEY2 BIT2
#define DELAY_MS 1000
unsigned int count = 0;
void lcd_init(void);
void lcd_cmd(char cmd);
void lcd_data(char data);
void lcd_string(char *str);
void lcd_clear(void);
void lcd_goto(char row, char col);
void gpio_init(void);
void timer_init(void);
void flash_write(unsigned int addr, unsigned int data);
unsigned int flash_read(unsigned int addr);
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
gpio_init();
timer_init();
lcd_init();
while(1)
{
if((P1IN & KEY1) == 0) // KEY1 is pressed
{
P1OUT |= LED; // turn on LED
count++; // increment count
lcd_goto(1, 0); // go to first row, first column
char str[16];
sprintf(str, "Count: %d", count);
lcd_string(str); // display count on LCD
__delay_cycles(DELAY_MS * 1000);
}
else
{
P1OUT &= ~LED; // turn off LED
}
if((P1IN & KEY2) == 0) // KEY2 is pressed
{
flash_write(0x1000, count); // save count to flash memory
lcd_goto(2, 0); // go to second row, first column
lcd_string("Count saved!"); // display message on LCD
__delay_cycles(DELAY_MS * 1000);
}
}
return 0;
}
void gpio_init(void)
{
P1DIR |= LED; // set LED as output
P1REN |= KEY1 | KEY2; // enable pull-up/pull-down resistors for keys
P1OUT |= KEY1 | KEY2; // set pull-up resistors for keys
}
void timer_init(void)
{
TA0CCR0 = 65535; // set timer period (maximum value)
TA0CTL = TASSEL_2 | MC_1; // select SMCLK as clock source, up mode
}
void flash_write(unsigned int addr, unsigned int data)
{
FCTL3 = FWKEY; // unlock flash memory
FCTL1 = FWKEY | WRT; // enable flash write
*(unsigned int *)addr = data; // write data to flash memory
FCTL1 = FWKEY; // disable flash write
FCTL3 = FWKEY | LOCK; // lock flash memory
}
unsigned int flash_read(unsigned int addr)
{
return *(unsigned int *)addr; // read data from flash memory
}
void lcd_init(void)
{
P6DIR |= LCD_RS | LCD_RW | LCD_E | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7; // set pins as output
P6OUT &= ~(LCD_RS | LCD_RW | LCD_E | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7); // clear pins
lcd_cmd(0x02); // send command: return home
lcd_cmd(0x28); // send command: 4-bit mode, 2-line, 5x8 font
lcd_cmd(0x0C); // send command: display on, cursor off, blink off
lcd_cmd(0x06); // send command: entry mode set, increment cursor
}
void lcd_cmd(char cmd)
{
P6OUT &= ~(LCD_RS | LCD_RW); // RS = 0, RW = 0
P6OUT |= LCD_E; // E = 1
P6OUT &= ~(LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7); // clear data pins
P6OUT |= ((cmd >> 4) & 0x0F) << 3; // send high nibble
P6OUT &= ~LCD_E; // E = 0
__delay_cycles(100); // short delay
P6OUT |= LCD_E; // E = 1
P6OUT &= ~(LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7); // clear data pins
P6OUT |= (cmd & 0x0F) << 3; // send low nibble
P6OUT &= ~LCD_E; // E = 0
__delay_cycles(100); // short delay
}
void lcd_data(char data)
{
P6OUT |= LCD_RS; // RS = 1
P6OUT &= ~LCD_RW; // RW = 0
P6OUT |= LCD_E; // E = 1
P6OUT &= ~(LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7); // clear data pins
P6OUT |= ((data >> 4) & 0x0F) << 3; // send high nibble
P6OUT &= ~LCD_E; // E = 0
__delay_cycles(100); // short delay
P6OUT |= LCD_E; // E = 1
P6OUT &= ~(LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7); // clear data pins
P6OUT |= (data & 0x0F) << 3; // send low nibble
P6OUT &= ~LCD_E; // E = 0
__delay_cycles(100); // short delay
}
void lcd_string(char *str)
{
while(*str != '\0')
{
lcd_data(*str);
str++;
}
}
void lcd_clear(void)
{
lcd_cmd(0x01); // send command: clear display
}
void lcd_goto(char row, char col)
{
char address = 0;
switch(row)
{
case 1:
address = 0x80;
break;
case 2:
address = 0xC0;
break;
case 3:
address = 0x94;
break;
case 4:
address = 0xD4;
break;
}
address += col - 1;
lcd_cmd(address);
}
```
在上面的示例代码中,我们使用了MSP430F6638的GPIO和定时器模块来控制LED的亮灭和记录按键次数,并使用了LCD模块来显示按键次数和保存状态。我们在程序的主循环中不断地检测KEY1和KEY2按键的状态,如果KEY1被按下,我们会将LED点亮并将计数器加一,并将计数器的值显示在LCD上;如果KEY2被按下,我们会将当前计数器的值保存到Flash存储器中,并在LCD上显示保存成功的消息。在下次开机时,我们可以再次按下KEY2来读取之前保存的计数器值,并在LCD上显示它。
阅读全文