合泰HT32外部中断控制多路舵机
本次分享的是外部中断进行多路舵机的控制,舵机的使用和外部中断的介绍在前面都讲过,需要的小伙伴看前面几篇文章
按键基本操作外部中断控制LED
实验效果
这里想在说的是HT32中只有三个中断源
但是每个引脚都可以配置成外部中断个中断源有15个中断通道
void EXTI0_1_IRQHandler(void)
{
}
void EXTI2_3_IRQHandler(void)
{
}
void EXTI2_3_IRQHandler(void)
{
}
#define EXTI0_IRQn EXTI0_1_IRQn
#define EXTI1_IRQn EXTI0_1_IRQn
#define EXTI2_IRQn EXTI2_3_IRQn
#define EXTI3_IRQn EXTI2_3_IRQn
#define EXTI4_IRQn EXTI4_15_IRQn
#define EXTI5_IRQn EXTI4_15_IRQn
#define EXTI6_IRQn EXTI4_15_IRQn
#define EXTI7_IRQn EXTI4_15_IRQn
#define EXTI8_IRQn EXTI4_15_IRQn
#define EXTI9_IRQn EXTI4_15_IRQn
#define EXTI10_IRQn EXTI4_15_IRQn
#define EXTI11_IRQn EXTI4_15_IRQn
#define EXTI12_IRQn EXTI4_15_IRQn
#define EXTI13_IRQn EXTI4_15_IRQn
#define EXTI14_IRQn EXTI4_15_IRQn
#define EXTI15_IRQn EXTI4_15_IRQn
typedef enum
{
EXTI_CHANNEL_0 = 0,
EXTI_CHANNEL_1,
EXTI_CHANNEL_2,
EXTI_CHANNEL_3,
EXTI_CHANNEL_4,
EXTI_CHANNEL_5,
EXTI_CHANNEL_6,
EXTI_CHANNEL_7,
EXTI_CHANNEL_8,
EXTI_CHANNEL_9,
EXTI_CHANNEL_10,
EXTI_CHANNEL_11,
EXTI_CHANNEL_12,
EXTI_CHANNEL_13,
EXTI_CHANNEL_14,
EXTI_CHANNEL_15
} EXTI_Channel_TypeDef;
key.h
#ifndef _KEY_H_
#define _KEY_H_
#include "ht32f5xxxx_ckcu.h"
#include "ht32f5xxxx_gpio.h"
#include "ht32f5xxxx_01.h"
#define HT_GPIO_GROUP (HT_GPIOB)
#define GPIO_PIN (GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_0|GPIO_PIN_1)
#define GPIO_PR (GPIO_PR_DOWN)
#define GPIO_FIFO_MODE (AFIO_FUN_GPIO)
void Key_Init(void);
#endif
key.c
#include "key.h"
#include "delay.h"
void Key_Init(void)
{
EXTI_InitTypeDef EXTI_InitStruct;
CKCU_PeripClockConfig_TypeDef CKCUClock= {{0}};
CKCUClock.Bit.PB = 1;
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.EXTI = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
GPIO_InputConfig(HT_GPIO_GROUP, GPIO_PIN, ENABLE);
GPIO_PullResistorConfig(HT_GPIO_GROUP, GPIO_PIN, GPIO_PR);
AFIO_GPxConfig( GPIO_PB, AFIO_PIN_2, AFIO_FUN_GPIO);
AFIO_EXTISourceConfig(AFIO_EXTI_CH_2, AFIO_ESS_PB);
EXTI_InitStruct.EXTI_Channel=EXTI_CHANNEL_2;
EXTI_InitStruct.EXTI_Debounce=EXTI_DEBOUNCE_ENABLE;
EXTI_InitStruct.EXTI_DebounceCnt = 65535;
EXTI_InitStruct.EXTI_IntType=EXTI_POSITIVE_EDGE;
EXTI_Init(&EXTI_InitStruct);
NVIC_EnableIRQ(EXTI2_3_IRQn);
EXTI_IntConfig(EXTI_CHANNEL_2, ENABLE);
AFIO_GPxConfig( GPIO_PB, AFIO_PIN_0, AFIO_FUN_GPIO);
AFIO_EXTISourceConfig(AFIO_EXTI_CH_0, AFIO_ESS_PB);
EXTI_InitStruct.EXTI_Channel=EXTI_CHANNEL_0;
EXTI_InitStruct.EXTI_Debounce=EXTI_DEBOUNCE_ENABLE;
EXTI_InitStruct.EXTI_DebounceCnt = 65535;
EXTI_InitStruct.EXTI_IntType=EXTI_POSITIVE_EDGE;
EXTI_Init(&EXTI_InitStruct);
NVIC_EnableIRQ(EXTI0_1_IRQn);
EXTI_IntConfig(EXTI_CHANNEL_0, ENABLE);
AFIO_GPxConfig( GPIO_PB, AFIO_PIN_4, AFIO_FUN_GPIO);
AFIO_EXTISourceConfig(AFIO_EXTI_CH_4, AFIO_ESS_PB);
EXTI_InitStruct.EXTI_Channel=EXTI_CHANNEL_4;
EXTI_InitStruct.EXTI_Debounce=EXTI_DEBOUNCE_ENABLE;
EXTI_InitStruct.EXTI_DebounceCnt = 65535;
EXTI_InitStruct.EXTI_IntType=EXTI_POSITIVE_EDGE;
EXTI_Init(&EXTI_InitStruct);
NVIC_EnableIRQ(EXTI4_15_IRQn);
EXTI_IntConfig(EXTI_CHANNEL_4, ENABLE);
}
main.c
#include "ht32.h"
#include "ht32_board.h"
#include "led.h"
#include "delay.h"
#include "uart.h"
#include "key.h"
#include "time.h"
int main()
{
uint8_t keynum = 0;
Led_Init();
USARTx_Init();
GPTM_PWM_init();
Key_Init();
Servo_Run(45);
Servo_Run2(45);
Servo_Run3(45);
Servo_Run4(45);
printf("------key test------\n");
while(1)
{
}
}
void EXTI4_15_IRQHandler(void)
{
static uint8_t key_stat = 0;
if(key_stat == 0)
{
key_stat = 1;
LED1_ON();
Servo_Run(0);
}
else if(key_stat == 1)
{
key_stat = 0;
LED1_OFF();
Servo_Run(45);
}
EXTI_ClearEdgeFlag(EXTI_CHANNEL_4);
}
void EXTI2_3_IRQHandler(void)
{
static uint8_t key_stat = 0;
if(key_stat == 0)
{
key_stat = 1;
LED2_ON();
Servo_Run2(0);
}
else if(key_stat == 1)
{
key_stat = 0;
LED2_OFF();
Servo_Run2(45);
}
EXTI_ClearEdgeFlag(EXTI_CHANNEL_2);
}
void EXTI0_1_IRQHandler(void)
{
static uint8_t key_stat = 0;
if(key_stat == 0)
{
key_stat = 1;
LED2_ON();
Servo_Run3(0);
}
else if(key_stat == 1)
{
key_stat = 0;
LED2_OFF();
Servo_Run3(45);
}
EXTI_ClearEdgeFlag(EXTI_CHANNEL_0);
}
加油耶
|