主控是stm32f103c8t6,按一次按键,led1闪烁,两个io口持续6秒分别输出高低电平,循环5分钟;继续按一次按键,还是这两个io口持续6秒分别输出高低电平,循环10分钟;再继续按一次按键,仍然是这两个io口持续6秒分别输出高低电平,一直循环。一个io口作为adc电流采样通道,输入24v,采样到电流超过5A就将输出高电平的io口关掉,,
时间: 2024-03-20 17:42:42 浏览: 70
好的,根据您的要求,可以给您提供以下的代码实现:
```c
#include "stm32f10x.h"
#define LED_GPIO_PORT GPIOA
#define LED_GPIO_PIN GPIO_Pin_0
#define IO1_GPIO_PORT GPIOA
#define IO1_GPIO_PIN GPIO_Pin_1
#define IO2_GPIO_PORT GPIOA
#define IO2_GPIO_PIN GPIO_Pin_2
#define IO_ADC_GPIO_PORT GPIOA
#define IO_ADC_GPIO_PIN GPIO_Pin_3
#define IO_ADC_GPIO_CHANNEL ADC_Channel_3
#define IO_ADC_THRESHOLD 5000 // 5A
#define BUTTON_GPIO_PORT GPIOA
#define BUTTON_GPIO_PIN GPIO_Pin_4
#define BUTTON_DEBOUNCE_TIME 20 // 按键消抖时间
#define BUTTON_LONG_PRESS_TIME 2000 // 长按时间,单位ms
volatile uint32_t button_pressed_time = 0;
volatile uint8_t button_long_pressed_flag = 0;
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// LED引脚配置
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
// IO1引脚配置
GPIO_InitStructure.GPIO_Pin = IO1_GPIO_PIN;
GPIO_Init(IO1_GPIO_PORT, &GPIO_InitStructure);
// IO2引脚配置
GPIO_InitStructure.GPIO_Pin = IO2_GPIO_PIN;
GPIO_Init(IO2_GPIO_PORT, &GPIO_InitStructure);
// IO ADC引脚配置
GPIO_InitStructure.GPIO_Pin = IO_ADC_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(IO_ADC_GPIO_PORT, &GPIO_InitStructure);
// 按键引脚配置
GPIO_InitStructure.GPIO_Pin = BUTTON_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(BUTTON_GPIO_PORT, &GPIO_InitStructure);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 35999; // 1s计数
TIM_TimeBaseStructure.TIM_Prescaler = 7199; // 10ms计数
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, IO_ADC_GPIO_CHANNEL, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
uint16_t ADC_ReadValue(void)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Delay(uint32_t nCount)
{
while (nCount--);
}
void LED_Flash(uint32_t nCount)
{
while (nCount--)
{
GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN);
Delay(500000);
GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN);
Delay(500000);
}
}
void IO1_High(void)
{
GPIO_SetBits(IO1_GPIO_PORT, IO1_GPIO_PIN);
Delay(6000000);
GPIO_ResetBits(IO1_GPIO_PORT, IO1_GPIO_PIN);
}
void IO2_High(void)
{
GPIO_SetBits(IO2_GPIO_PORT, IO2_GPIO_PIN);
Delay(6000000);
GPIO_ResetBits(IO2_GPIO_PORT, IO2_GPIO_PIN);
}
void IO_ADC_Check(void)
{
if (ADC_ReadValue() > IO_ADC_THRESHOLD)
{
GPIO_ResetBits(IO1_GPIO_PORT, IO1_GPIO_PIN);
}
}
void EXTI4_IRQHandler(void)
{
if (GPIO_ReadInputDataBit(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN) == RESET)
{
button_pressed_time = TIM_GetCounter(TIM2);
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
int main(void)
{
GPIO_Configuration();
TIM2_Configuration();
ADC_Configuration();
EXTI_Configuration();
while (1)
{
// 第一种情况
while (TIM_GetCounter(TIM2) < 30000)
{
if (GPIO_ReadInputDataBit(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN) == RESET)
{
if (TIM_GetCounter(TIM2) - button_pressed_time > BUTTON_DEBOUNCE_TIME)
{
if (TIM_GetCounter(TIM2) - button_pressed_time > BUTTON_LONG_PRESS_TIME)
{
button_long_pressed_flag = 1;
}
break;
}
}
}
if (button_long_pressed_flag)
{
button_long_pressed_flag = 0;
continue;
}
LED_Flash(1);
IO1_High();
IO2_High();
for (int i = 0; i < 30; i++)
{
IO_ADC_Check();
Delay(100000);
}
// 第二种情况
while (TIM_GetCounter(TIM2) < 60000)
{
if (GPIO_ReadInputDataBit(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN) == RESET)
{
if (TIM_GetCounter(TIM2) - button_pressed_time > BUTTON_DEBOUNCE_TIME)
{
if (TIM_GetCounter(TIM2) - button_pressed_time > BUTTON_LONG_PRESS_TIME)
{
button_long_pressed_flag = 1;
}
break;
}
}
}
if (button_long_pressed_flag)
{
button_long_pressed_flag = 0;
continue;
}
IO1_High();
IO2_High();
for (int i = 0; i < 60; i++)
{
IO_ADC_Check();
Delay(100000);
}
// 第三种情况
while (1)
{
if (GPIO_ReadInputDataBit(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN) == RESET)
{
if (TIM_GetCounter(TIM2) - button_pressed_time > BUTTON_DEBOUNCE_TIME)
{
if (TIM_GetCounter(TIM2) - button_pressed_time > BUTTON_LONG_PRESS_TIME)
{
button_long_pressed_flag = 1;
}
break;
}
}
}
if (button_long_pressed_flag)
{
button_long_pressed_flag = 0;
continue;
}
IO1_High();
IO2_High();
while (1)
{
IO_ADC_Check();
Delay(100000);
}
}
}
```
需要注意的是,此代码只是提供一个基本的框架和思路,具体实现还需要根据您的硬件平台和实际需求进行适当修改。
阅读全文