STM32旋钮编码器调光器程序
时间: 2023-11-11 16:07:05 浏览: 159
下面是一个基于STM32单片机和旋钮编码器的调光器程序示例:
```c
#include "stm32f10x.h"
#define LED_PIN GPIO_Pin_13
#define LED_PORT GPIOC
#define ENCODER_PIN1 GPIO_Pin_0
#define ENCODER_PIN2 GPIO_Pin_1
#define ENCODER_PORT GPIOA
volatile uint8_t encoderState = 0;
volatile uint8_t lastEncoderState = 0;
volatile int16_t encoderCount = 0;
void Encoder_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = ENCODER_PIN1 | ENCODER_PIN2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(ENCODER_PORT, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
}
void TIM3_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
}
void EXTI0_IRQHandler(void)
{
if (GPIO_ReadInputDataBit(ENCODER_PORT, ENCODER_PIN1) == 1)
{
if (GPIO_ReadInputDataBit(ENCODER_PORT, ENCODER_PIN2) == 0)
{
encoderState = 1;
}
else
{
encoderState = 2;
}
}
else
{
if (GPIO_ReadInputDataBit(ENCODER_PORT, ENCODER_PIN2) == 1)
{
encoderState = 3;
}
else
{
encoderState = 4;
}
}
if (encoderState != lastEncoderState)
{
if ((lastEncoderState == 1 && encoderState == 2) ||
(lastEncoderState == 2 && encoderState == 4) ||
(lastEncoderState == 4 && encoderState == 3) ||
(lastEncoderState == 3 && encoderState == 1))
{
encoderCount++;
}
if ((lastEncoderState == 2 && encoderState == 1) ||
(lastEncoderState == 4 && encoderState == 2) ||
(lastEncoderState == 3 && encoderState == 4) ||
(lastEncoderState == 1 && encoderState == 3))
{
encoderCount--;
}
lastEncoderState = encoderState;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI1_IRQHandler(void)
{
if (GPIO_ReadInputDataBit(ENCODER_PORT, ENCODER_PIN2) == 1)
{
if (GPIO_ReadInputDataBit(ENCODER_PORT, ENCODER_PIN1) == 1)
{
encoderState = 3;
}
else
{
encoderState = 4;
}
}
else
{
if (GPIO_ReadInputDataBit(ENCODER_PORT, ENCODER_PIN1) == 0)
{
encoderState = 1;
}
else
{
encoderState = 2;
}
}
if (encoderState != lastEncoderState)
{
if ((lastEncoderState == 1 && encoderState == 2) ||
(lastEncoderState == 2 && encoderState == 4) ||
(lastEncoderState == 4 && encoderState == 3) ||
(lastEncoderState == 3 && encoderState == 1))
{
encoderCount++;
}
if ((lastEncoderState == 2 && encoderState == 1) ||
(lastEncoderState == 4 && encoderState == 2) ||
(lastEncoderState == 3 && encoderState == 4) ||
(lastEncoderState == 1 && encoderState == 3))
{
encoderCount--;
}
lastEncoderState = encoderState;
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
int main(void)
{
Encoder_Init();
LED_Init();
TIM3_Init();
while (1)
{
if (encoderCount > 0)
{
TIM3->CCR1 += 10;
encoderCount--;
}
if (encoderCount < 0)
{
TIM3->CCR1 -= 10;
encoderCount++;
}
if (TIM3->CCR1 > 1000)
{
TIM3->CCR1 = 1000;
}
if (TIM3->CCR1 < 0)
{
TIM3->CCR1 = 0;
}
if (encoderCount != 0)
{
GPIO_WriteBit(LED_PORT, LED_PIN, Bit_SET);
}
else
{
GPIO_WriteBit(LED_PORT, LED_PIN, Bit_RESET);
}
}
}
```
以上是一个简单的STM32旋钮编码器调光器程序示例,其主要实现了旋钮编码器的读取和LED灯的控制。需要注意的是,该程序仅供参考,具体实现应根据需要进行调整和优化。
阅读全文