Advertisement
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_usart.h"
- #include "stm32f0_discovery.h"
- void GPIO_Config(void);
- void RCC_Config(void);
- void NVIC_Config(void);
- void TIM_Config(void);
- void initUSART();
- int wypelnienie = 0;
- int czestotliwosc = 0;
- char BuforRX[64]; // Bufor odczytu
- int brRead = 0; // pierwszy wolny indeks do odczytu
- int brWrite = 0; // ostatni zajety indeks odczytu
- char BuforTX[64]; // Bufor wyslania
- int btRead = 0; // pierwszy wolny indeks do wyslania
- int btWrite = 0; // ostatni zajety indeks wyslania
- char BuforTmp[64];
- uint8_t BuforIndex = 0;
- int main(void)
- {
- volatile unsigned long int i;
- RCC_Config();
- GPIO_Config();
- NVIC_Config();
- TIM_Config();
- USART2_Init();
- USART_print("Wlaczono STM32\r\n");
- while (1) {
- USART_received();
- //zapalanie diody niebieskiej
- if (!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)){
- if (TIM3->CCR3<=(0xFFFF-250)){
- TIM3->CCR3 = TIM3->CCR3-250;
- }
- }
- //przygaszenie diody zielonej
- if (!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)){
- if (TIM3->CCR4>=250){
- TIM3->CCR4 = TIM3->CCR4-250;
- }
- }
- for (i=0;i<400000;i++);
- };
- return 0;
- }
- void RCC_Config(void)
- //konfigurowanie sygnalow taktujacych
- {
- RCC_DeInit();
- //wlacz taktowanie portu GPIOA oraz GPIOC
- RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOCEN, ENABLE);
- //wlacz taktowanie licznika TIM3
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- }
- void NVIC_Config(void) {
- //Konfigurowanie kontrolera przerwan NVIC
- NVIC_InitTypeDef NVIC_InitStructure;
- //wybor odpowiedniego kanalu
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void GPIO_Config(void) {
- //konfigurowanie portow GPIO
- GPIO_InitTypeDef gpio;
- //Pin_0 - przycisk
- gpio.GPIO_Pin = GPIO_Pin_0;
- gpio.GPIO_Mode = GPIO_Mode_IN;
- gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
- gpio.GPIO_OType = GPIO_OType_PP;
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &gpio);
- //Pin_8 - led niebieski
- //Pin_9 - led zielony
- gpio.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9;
- gpio.GPIO_Mode = GPIO_Mode_AF;
- gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
- gpio.GPIO_OType = GPIO_OType_PP;
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &gpio);
- }
- void TIM_Config(void) {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- // Konfiguracja TIM3
- // Ustawienia ukladu podstawy czasu
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //bez prescelaera (prescaler=1)
- TIM_TimeBaseStructure.TIM_Period = 49999ul; //czestotliwosc PWM = 1440 Hz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- // Konfiguracja kanalu 3 - uzywamy kanalu 3 poniewaz jego wyjscie jest na GPIOC8 - gdzie jest LED NIEBIESKI
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 50ul; //wypelnienie poczatkowe = 50000/100=0.2%
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC3Init(TIM3, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- // Konfiguracja kanalu 4 - uzywamy kanalu 4 poniewaz jego wyjscie jest na GPIOC9 - gdzie jest LED ZIELONY
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 25000ul; //wypelnienie = 50000/25000=50%
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC4Init(TIM3, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM3, ENABLE);
- // Wlaczenie timera
- TIM_Cmd(TIM3, ENABLE);
- }
- void TIM3_IRQHandler(void) {
- RCC_ClocksTypeDef RCC_Clocks;
- RCC_GetClocksFreq(&RCC_Clocks);
- TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
- //pobranie wartosci wejscia
- int timer2 = TIM_GetCapture2(TIM3);
- if (timer2 != 0) {
- //obliczenia cyklu roboczego
- int timer1 = TIM_GetCapture1(TIM3);
- //obliczenia czestotliwosci
- //TIM3 counter clock = 800;
- wypelnienie = timer1 * 100 / timer2;
- czestotliwosc = RCC_Clocks.HCLK_Frequency / timer2;
- } else {
- wypelnienie = 0;
- czestotliwosc = 0;
- }
- }
- void USART_print(char tmp[]) {
- volatile int index = btWrite; // volatile - zmienna zmieniana w przerwaniach (żeby nie została usunięta)
- for (int i = 0; i < strlen(tmp); i++) {
- BuforTX[index] = tmp[i];
- index++;
- if (index >= 64)
- index = 0;
- }
- __disable_irq(); // wylaczenie przerwania
- if ((btRead == btWrite) && (USART_GetFlagStatus(USART2, USART_FLAG_TXE))== SET) {
- btWrite = index;
- USART_SendData(USART2, BuforTX[btRead]); //pierwszy znak
- btRead++;
- if (btRead >= 64)
- btRead = 0;
- USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //załącza przerwanie
- } else {
- btWrite = index;
- }
- __enable_irq(); // wlaczenie przerwania
- }
- void USART2_Init(void) {
- USART_InitTypeDef usart;
- GPIO_InitTypeDef gpio;
- NVIC_InitTypeDef nvic;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
- gpio.GPIO_Pin = GPIO_Pin_8; //LED
- gpio.GPIO_OType = GPIO_OType_PP;
- gpio.GPIO_Mode = GPIO_Mode_OUT;
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &gpio);
- gpio.GPIO_Pin = GPIO_Pin_9; //LED
- gpio.GPIO_OType = GPIO_OType_PP;
- gpio.GPIO_Mode = GPIO_Mode_OUT;
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &gpio);
- gpio.GPIO_Pin = GPIO_Pin_2; //TX
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- gpio.GPIO_Mode = GPIO_Mode_AF;
- gpio.GPIO_OType = GPIO_OType_PP;
- gpio.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &gpio);
- gpio.GPIO_Pin = GPIO_Pin_3; //RD
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- gpio.GPIO_Mode = GPIO_Mode_AF;
- gpio.GPIO_OType = GPIO_OType_PP;
- gpio.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &gpio);
- nvic.NVIC_IRQChannel = USART2_IRQn;
- nvic.NVIC_IRQChannelPriority = 0;
- nvic.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&nvic);
- usart.USART_BaudRate = 9600;
- usart.USART_WordLength = USART_WordLength_8b;
- usart.USART_StopBits = USART_StopBits_1;
- usart.USART_Parity = USART_Parity_No;
- usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_Init(USART2, &usart);
- USART_Cmd(USART2, ENABLE);
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//zezwolenie na przerwanie odbioru
- }
- void USART2_IRQHandler(void) { //obsługa przerwań
- if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { //obsługa przerwania odebrania
- BuforRX[brWrite] = USART_ReceiveData(USART2); //przepisanie odebranego bajtu z rejestru do buforu odbioru
- brWrite++;
- if (brWrite >= 64)
- brWrite = 0;
- }
- if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET) { //obsługa przerwania wysłania
- if (btRead != btWrite) {
- USART_SendData(USART2, BuforTX[btRead]);
- btRead++;
- if (btRead >= 64)
- btRead = 0;
- } else
- USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //zablokowanie przerwania transmisji - koniec transmisji
- }
- }
- void parse(char *inputText) {
- if (strncmp(inputText, "name", 4) == 0) {
- USART_print("STM32\r\n");
- } else if (strncmp(inputText, "led on b", 8) == 0) {
- //zapalanie diody niebieskiej
- GPIO_SetBits(GPIOC, GPIO_Pin_8);
- USART_print("Wlaczono diode niebieska\r\n");
- } else if (strncmp(inputText, "led off b", 8) == 0) {
- GPIO_ResetBits(GPIOC, GPIO_Pin_8); //zresetowanie diody
- USART_print("Wylaczono diode niebieska\r\n");
- } else if (strncmp(inputText, "led on g", 8) == 0) {
- //zapalanie diody zielonej
- GPIO_SetBits(GPIOC, GPIO_Pin_9);
- USART_print("Wylaczono diode zielona\r\n");
- } else {
- USART_print("Zla komenda\r\n");
- }
- memset(inputText, 0, strlen(inputText)); // wyczyszczenie zawartosci tablicy
- }
- void USART_received() {
- if (brRead != brWrite) {
- while (brRead != brWrite) {
- if (BuforRX[brRead] != '\n'&& BuforRX[brRead] != '\r') {
- if (BuforRX[brRead] != ';') {
- BuforTmp[BuforIndex] = BuforRX[brRead];
- BuforIndex++;
- if (BuforIndex >= 64)
- BuforIndex = 0;
- } else {
- parse(BuforTmp);
- BuforIndex = 0;
- }
- }
- brRead++;
- if (brRead >= 64)
- brRead = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement