Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx_conf.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_tim.h"
- #include "stm32f4xx_usart.h"
- #include "misc.h"
- uint16_t lewa, znak, prawa, wynik;
- unsigned int n = 0;
- int main(void)
- {
- SystemInit();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- 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);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
- //GPIO_InitTypeDef GPIO_InitStructure;
- 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);
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Period = 9999;
- TIM_TimeBaseStructure.TIM_Prescaler = 8399;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART3, &USART_InitStructure);
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitTypeDef NVIC_InitStructure2;
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- NVIC_InitStructure2.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 0x01;
- NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 0x01;
- NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure2);
- NVIC_EnableIRQ(USART3_IRQn);
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
- TIM_Cmd(TIM3, ENABLE);
- USART_Cmd(USART3, ENABLE);
- void TIM3_IRQHandler(void);
- void USART3_IRQHandler(void);
- while(1){}
- }
- void TIM3_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
- {
- //zad1
- // USART_SendData(USART3, 'A');
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
- }
- }
- void USART3_IRQHandler(void)
- {
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
- {
- //zad2
- //USART3->DR -= 32;
- //zad3
- //if(USART3->DR == 'a')
- // GPIO_ToggleBits(GPIOD, GPIO_Pin_12);
- //if(USART3->DR == 's')
- // GPIO_ToggleBits(GPIOD, GPIO_Pin_13);
- //if(USART3->DR == 'd')
- // GPIO_ToggleBits(GPIOD, GPIO_Pin_14);
- //if(USART3->DR == 'f')
- // GPIO_ToggleBits(GPIOD, GPIO_Pin_15);
- switch (n%3)
- {
- case 0:
- lewa = USART3->DR;
- n++;
- break;
- case 1:
- znak = USART3->DR;
- n++;
- break;
- case 2:
- prawa = USART3->DR;
- USART_SendData(USART3, '=');
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
- if (znak == '+') wynik = (lewa-48)+(prawa-48);
- if (znak == '-') wynik = (lewa-48)-(prawa-48);
- if (znak == '*') wynik = (lewa-48)*(prawa-48);
- if (wynik/10 > 0)
- {
- USART_SendData(USART3, (wynik/10)+48);
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
- USART_SendData(USART3, (wynik%10)+48);
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
- }else{
- USART_SendData(USART3, wynik+48);
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
- }
- USART_SendData(USART3, 10);
- // while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
- n++;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement