Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "stm32f4_discovery.h"
- int acounter = 0;
- int bcounter = 0;
- int ccounter = 0;
- int a = 0;
- int b = 0;
- int c = 0;
- void TIM5_IRQHandler(void) {
- if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) {
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 1 && a== 0) {
- a = 1;
- }
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 1 && a == 1)
- {
- a = 0;
- }
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 1 && b == 0) {
- b = 1;
- }
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 1 && b == 1)
- {
- b = 0;
- }
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1 && c== 0) {
- c = 1;
- }
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1 && c == 1)
- {
- c = 0;
- }
- if(a==1 || b==1 || c==1)
- {
- TIM_Cmd(TIM3, ENABLE);
- }
- else{
- TIM_Cmd(TIM3, DISABLE);
- }
- TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
- }
- }
- void TIM3_IRQHandler(void) {
- if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
- if(a == 1)
- {
- GPIO_ToggleBits(GPIOC, GPIO_Pin_6);
- for(int i = 0;i<10000000;i++);
- }
- if(b == 1)
- {
- GPIO_ToggleBits(GPIOC, GPIO_Pin_8);
- for(int i = 0;i<10000000;i++);
- }
- if(c == 1)
- {
- GPIO_ToggleBits(GPIOC, GPIO_Pin_9);
- for(int i = 0;i<10000000;i++);
- }
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
- }
- }
- int main(void) {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // RGB
- GPIO_InitTypeDef GPIO_InitDef;
- GPIO_InitDef.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_9; // 6-R 8-G 9-B
- GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
- GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOC, &GPIO_InitDef);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // buttons
- GPIO_InitTypeDef GPIO_BUTTONS;
- GPIO_BUTTONS.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_BUTTONS.GPIO_Mode = GPIO_Mode_IN;
- GPIO_BUTTONS.GPIO_OType = GPIO_OType_PP;
- GPIO_BUTTONS.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_BUTTONS.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(GPIOA, &GPIO_BUTTONS);
- GPIO_ResetBits(GPIOD, GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_9);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // timer 3
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Period = 9999;
- TIM_TimeBaseStructure.TIM_Prescaler = 2099;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- TIM_Cmd(TIM3, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); // timer 5
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure5;
- TIM_TimeBaseStructure5.TIM_Period = 99;
- TIM_TimeBaseStructure5.TIM_Prescaler = 84;
- TIM_TimeBaseStructure5.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure5.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure5);
- TIM_Cmd(TIM5, ENABLE);
- //przerwania tim3
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitTypeDef NVIC_InitStructure3;
- NVIC_InitStructure3.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure3.NVIC_IRQChannelPreemptionPriority = 0x00;
- NVIC_InitStructure3.NVIC_IRQChannelSubPriority = 0x00;
- NVIC_InitStructure3.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure3);
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
- //przerwania tim5
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
- TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
- for(;;) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement