按键控制灯 STM32单片机外接扩展板D2、D3接开关S2和S3,D9、D10、D11接RGB三色LED灯,要求每次按下S2开关一次,三色灯按红、绿、蓝颜色变化,每次按下S3开关一次,三色灯按蓝、绿、红颜色变化,每次颜色变化时,通过串行口向计算机发送“按下按钮:SX,当前颜色是:X色!”
时间: 2023-06-24 22:08:41 浏览: 160
好的,这是一个比较典型的STM32单片机外接扩展板的控制程序。下面是示例代码:
```c
#include <stdio.h>
#include "stm32f10x.h"
#define LED_R GPIO_Pin_9
#define LED_G GPIO_Pin_10
#define LED_B GPIO_Pin_11
#define SW_S2 GPIO_Pin_2
#define SW_S3 GPIO_Pin_3
void GPIO_Config(void);
void USART_Config(void);
void USART_SendString(char* str);
int main(void)
{
GPIO_Config();
USART_Config();
uint8_t red = 255, green = 0, blue = 0;
uint8_t color_idx = 0;
while (1)
{
if (GPIO_ReadInputDataBit(GPIOA, SW_S2) == 0) // S2 pressed
{
color_idx = (color_idx + 1) % 3;
switch (color_idx)
{
case 0:
red = 255;
green = 0;
blue = 0;
break;
case 1:
red = 0;
green = 255;
blue = 0;
break;
case 2:
red = 0;
green = 0;
blue = 255;
break;
}
char msg[50];
sprintf(msg, "按下按钮:S2,当前颜色是:%d色!\n", color_idx);
USART_SendString(msg);
}
if (GPIO_ReadInputDataBit(GPIOA, SW_S3) == 0) // S3 pressed
{
color_idx = (color_idx + 1) % 3;
switch (color_idx)
{
case 0:
red = 0;
green = 0;
blue = 255;
break;
case 1:
red = 0;
green = 255;
blue = 0;
break;
case 2:
red = 255;
green = 0;
blue = 0;
break;
}
char msg[50];
sprintf(msg, "按下按钮:S3,当前颜色是:%d色!\n", color_idx);
USART_SendString(msg);
}
GPIO_SetBits(GPIOB, LED_R);
GPIO_ResetBits(GPIOB, LED_G);
GPIO_ResetBits(GPIOB, LED_B);
for (int i = 0; i < red; i++);
GPIO_ResetBits(GPIOB, LED_R);
GPIO_SetBits(GPIOB, LED_G);
GPIO_ResetBits(GPIOB, LED_B);
for (int i = 0; i < green; i++);
GPIO_ResetBits(GPIOB, LED_R);
GPIO_ResetBits(GPIOB, LED_G);
GPIO_SetBits(GPIOB, LED_B);
for (int i = 0; i < blue; i++);
}
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// Configure S2 and S3 as input pull-up
GPIO_InitStructure.GPIO_Pin = SW_S2 | SW_S3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure LED_R, LED_G, LED_B as output
GPIO_InitStructure.GPIO_Pin = LED_R | LED_G | LED_B;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
// Configure USART1 Tx as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1 Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
// Enable USART1
USART_Cmd(USART1, ENABLE);
}
void USART_SendString(char* str)
{
while (*str)
{
USART_SendData(USART1, *str++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
```
需要注意的是,上面的代码中使用了`RCC_APB2PeriphClockCmd`函数来使能GPIOA、GPIOB和USART1的时钟。如果使用的是其它型号的单片机,请根据它的时钟树结构来修改这部分代码。此外,还有一个`while (1)`循环,其中实现了按键和LED的控制。这个循环必须是无限循环,否则程序将在执行完一次后退出。
在使用这段代码之前,需要先在Keil或其它IDE中,选择正确的单片机型号,以及将它连接到计算机上。然后将上面的代码复制到工程中,并编译、下载到单片机中即可。下载后,按下S2和S3按键,就可以看到RGB三色灯按照预期的颜色变化,同时在串行口上也会输出相应的信息。
阅读全文