中断控制呼吸灯
PWM的代码就不给出了 之前有
countsensor.c:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "PWM.h"
#include "PWM2.h"
//uint16_t CountSensor_Count;
uint8_t i;
uint8_t j;
int a;
void CountSensor_Init(void)
{
PWM_Init();
PWM_Init2();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitTypeDef GPIO_B_IS;
GPIO_B_IS.GPIO_Mode = GPIO_Mode_IPU;
GPIO_B_IS.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
GPIO_B_IS.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_B_IS);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource11);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line1 | EXTI_Line11;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 0-3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 0-3
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
//uint16_t CountSensor_Get(void)
//{
// return CountSensor_Count;
//}
void EXTI1_IRQHandler()
{
for (a = 0; a <= 100; a++);
if (EXTI_GetITStatus(EXTI_Line1) == SET)
{
/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
// CountSensor_Count ++;
for (i = 0; i <= 100; i++)
{
PWM_SetCompare2(i);
for (a = 0; a <= 10000; a++);
}
for (i = 0; i <= 100; i++)
{
PWM_SetCompare2(100 - i);
for (a = 0; a <= 10000; a++);
}
PWM_SetCompare2(100);
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
void EXTI15_10_IRQHandler(void)
{
for (a = 0; a <= 100; a++);
if (EXTI_GetITStatus(EXTI_Line11) == SET)
{
/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
{
// CountSensor_Count --;
for (j = 0; j <= 100; j++)
{
PWM_SetCompare3(j);
for (a = 0; a <= 10000; a++);
}
for (j = 0; j <= 100; j++)
{
PWM_SetCompare3(100 - j);
for (a = 0; a <= 10000; a++);
}
PWM_SetCompare3(100);
}
EXTI_ClearITPendingBit(EXTI_Line11);
}
EXTI_ClearITPendingBit(EXTI_Line11);
}
代码中的计时和防抖我都改为了for循环,这里最好不要用delay,delay是用的同一个寄存器,中断之后会更改数据,会导致中断出问题,所以要用其他计时方式
main.c:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "CountSensor.h"
#include "LED.h"
#include "PWM.h"
#include "PWM2.h"
int main(void)
{
OLED_Init();
CountSensor_Init();
LED_Init();
PWM_Init();
PWM_Init2();
LED1_OFF();
LED0_OFF();
PWM_SetCompare2(100);
PWM_SetCompare3(100);
while (1)
{
// OLED_ShowNum(1, 7, CountSensor_Get(), 5);
}
}
main基本没变化 而且加上计数器也可以用了,不过代码没加,之前的中断的问题也已经优化了
接线图:
就比更好的按钮那期少了个按钮