Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f0xx.h"
- #include "stm32f0xx_gpio.h"
- #include "stm32f0xx_rcc.h"
- #include "stm32f0xx_tim.h"
- void Init(void) {
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //Wlaczamy GPIOA dla USER buttona
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); //Wlaczamy GPIOC dla LEDow
- GPIO_InitTypeDef GP; //Tworzymy strukture GPIO
- GP.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
- GP.GPIO_Mode = GPIO_Mode_OUT; //LEDy jako wyjscie
- GP.GPIO_OType = GPIO_OType_PP;
- GP.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GP.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GP); //dodajemy do struktury
- GP.GPIO_Pin = GPIO_Pin_0;
- GP.GPIO_Mode = GPIO_Mode_IN; //Button jako wejscie
- GP.GPIO_OType = GPIO_OType_PP;
- GP.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GP.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GP); //dodajemy do struktury
- }
- int counter = 0;
- int mode = 0;
- int zmienna = 0;
- int zmienna2 = 0;
- int odstep = 0;
- void func() {
- counter++;
- if (counter >= 2000) {
- counter = 0;
- }
- if (mode == 0) {
- if (counter > 1000) {
- GPIO_SetBits(GPIOC, GPIO_Pin_6);
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_6);
- }
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_6);
- }
- if (mode == 1) {
- if (counter > 1000) {
- GPIO_SetBits(GPIOC, GPIO_Pin_7);
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_7);
- }
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_7);
- }
- if (mode == 2) {
- if (counter > 1000) {
- GPIO_SetBits(GPIOC, GPIO_Pin_8);
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_8);
- }
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_8);
- }
- if (mode == 3) {
- if (counter > 1000) {
- GPIO_SetBits(GPIOC, GPIO_Pin_9);
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_9);
- }
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_9);
- }
- if (mode == 4) {
- if (counter > 1000) {
- GPIO_SetBits(GPIOC, GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);
- } else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);
- }
- }/* else {
- GPIO_ResetBits(GPIOC, GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);
- }*/
- }
- void button() {
- if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1 && zmienna < 2000) {
- zmienna++;
- odstep=0;
- }
- else {
- if (zmienna > 1 && zmienna < 500) {
- mode++;
- zmienna=0;
- zmienna2=0;
- }
- if (zmienna >= 1000) {
- if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1 && odstep == 0)
- ;
- if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0)
- odstep++;
- if (odstep >= 1000){
- zmienna=0;
- zmienna2=0;
- odstep=0;
- }
- if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1 && odstep >= 1) {
- zmienna2++;
- } else {
- if (zmienna2 > 50 && zmienna2 < 500) {
- mode=2;
- zmienna=0;
- zmienna2=0;
- odstep=0;
- }
- if (zmienna2 > 1000) {
- mode=0;
- zmienna=0;
- zmienna2=0;
- odstep=0;
- }}
- } else {
- zmienna=0;
- odstep=0;
- }
- }
- if (mode > 4) {
- mode = 0;
- }
- }
- int main(void) {
- Init();
- SysTick_Config(SystemCoreClock / 10000);
- while (1) {
- }
- }
- void SysTick_Handler(void) //wektor przerwania
- {
- func();
- button();
- }
Add Comment
Please, Sign In to add comment