#include "stm32f4xx.h" #include "stm32f4_discovery.h" char znak; int i=0; int p=0; void TIM4_IRQHandler(void) { if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); } } uint8_t usartGetChar(void) { // czekaj na odebranie danych while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); return USART_ReceiveData(USART3); } void USART3_IRQHandler(void) { // sprawdzenie flagi zwiazanej z odebraniem danych przez USART if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { // odebrany bajt znajduje sie w rejestrze USART3->DR if (USART3->DR == '+' ) { p++; } if (USART3->DR == '-' ) { if(p>0){ p--; } } } } int main(void) { // wlaczenie taktowania wybranego portu RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // wlaczenie taktowania wybranego układu USART RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); // konfiguracja linii Rx i Tx GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); // ustawienie funkcji alternatywnej dla pinów (USART) GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3); USART_InitTypeDef USART_InitStructure; // predkosc transmisji (mozliwe standardowe opcje: 9600, 19200, 38400, 57600, 115200, ...) USART_InitStructure.USART_BaudRate = 9600; // długość słowa (USART_WordLength_8b lub USART_WordLength_9b) USART_InitStructure.USART_WordLength = USART_WordLength_8b; // liczba bitów stopu (USART_StopBits_1, USART_StopBits_0_5, USART_StopBits_2, USART_StopBits_1_5) USART_InitStructure.USART_StopBits = USART_StopBits_1; // sprawdzanie parzystości (USART_Parity_No, USART_Parity_Even, USART_Parity_Odd) USART_InitStructure.USART_Parity = USART_Parity_No; // sprzętowa kontrola przepływu (USART_HardwareFlowControl_None, USART_HardwareFlowControl_RTS, USART_HardwareFlowControl_CTS, USART_HardwareFlowControl_RTS_CTS) USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // tryb nadawania/odbierania (USART_Mode_Rx, USART_Mode_Rx ) USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // konfiguracja USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); //struktura do konfiguracji kontrolera NVIC NVIC_InitTypeDef NVIC_InitStructure2; // wlaczenie przerwania związanego z odebraniem danych (pozostale zrodla przerwan zdefiniowane sa w pliku stm32f4xx_usart.h) USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); NVIC_InitStructure2.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE; // konfiguracja kontrolera przerwan NVIC_Init(&NVIC_InitStructure2); // wlaczenie przerwan od ukladu USART NVIC_EnableIRQ(USART3_IRQn); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 42000; TIM_TimeBaseStructure.TIM_Prescaler = 1000; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM4, ENABLE); TIM_ClearITPendingBit(TIM4, TIM_IT_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); int j=0,m; for(;;) { if(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update)) { if (j!=0){ j--; if(j==0) { i=m; } } else if(j==0 && p!=0){ j=p; if(p!=0){ m=i; i=5; } } if(i==4){ i=0; GPIO_ToggleBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15); } else if(i==3){ GPIO_ToggleBits(GPIOD, GPIO_Pin_15); i++; } else if(i==2){ GPIO_ToggleBits(GPIOD, GPIO_Pin_14); i++; } else if(i==1){ GPIO_ToggleBits(GPIOD, GPIO_Pin_13); i++; } else if(i==0){ GPIO_ToggleBits(GPIOD, GPIO_Pin_12); i++; } TIM_ClearFlag(TIM2, TIM_FLAG_Update); } } }