#include "io.h" GPIO_InitTypeDef GPIO_InitStructure; void GPIOinit(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // A GPIO_InitStructure.GPIO_Pin = TIM1_OUT; // Output test signal GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = LCD_SCK | LCD_MOSI; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Alternate Function Push Pull GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = LCD_MISO; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // Input Floating GPIO_Init(GPIOA, &GPIO_InitStructure); // B GPIO_InitStructure.GPIO_Pin = Key1 | Key2 | Key3 | Key4 | Key5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = LCD_RS | LCD_RESET | KeyGND; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, LCD_RESET); GPIO_ResetBits(GPIOB, KeyGND); } //======================================================================== void SPIinit(void) { SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set); SPI_CalculateCRC(SPI1, DISABLE); SPI_Cmd(SPI1, ENABLE); } //======================================================================== void ADCinit(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); ADC_InitTypeDef ADC_InitStructure; RCC_ADCCLKConfig(RCC_PCLK2_Div2); ADC_Cmd(ADC1, DISABLE); ADC_Cmd(ADC2, DISABLE); ADC_DeInit(ADC1); ADC_DeInit(ADC2); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_Init(ADC2, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5); ADC_Cmd(ADC1, ENABLE); ADC_DMACmd(ADC1 , ENABLE ) ; ADC_SoftwareStartConvCmd(ADC1, DISABLE); ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5); ADC_Cmd(ADC2, ENABLE); ADC_SoftwareStartConvCmd(ADC2, ENABLE); } //======================================================================== void TIMERinit(void) { TIM_TimeBaseInitTypeDef timer; TIM_OCInitTypeDef TIM_OCConfig; TIM_OCStructInit(&TIM_OCConfig); // TIM1 Port A8 OUT TIMER1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); timer.TIM_Prescaler = 60; timer.TIM_Period = 4-1; TIM_TimeBaseInit(TIM1, &timer); TIM_OCConfig.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCConfig.TIM_OutputState = TIM_OutputState_Enable; TIM_OC1Init(TIM1, &TIM_OCConfig); TIM_Cmd(TIM1, ENABLE); TIM1->BDTR |= TIM_BDTR_MOE; // TIM2 Start ADC1 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); timer.TIM_Prescaler = 0; timer.TIM_Period = 30-1; // F= 60/Period, MHZ (120MHZ clc) TIM_TimeBaseInit(TIM2, &timer); TIM_OCConfig.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCConfig.TIM_OutputState = TIM_OutputState_Enable; TIM_OC2Init(TIM2, &TIM_OCConfig); TIM_Cmd(TIM2, ENABLE); // TIM3 KeyAntiBug & FPS RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseInitTypeDef base_timer; TIM_TimeBaseStructInit(&base_timer); base_timer.TIM_Prescaler = 60-1; base_timer.TIM_Period = 1000-1; // 60, 1000 -> 1mks TIM_TimeBaseInit(TIM3, &base_timer); TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); NVIC_EnableIRQ(TIM3_IRQn); TIM_Cmd(TIM3, ENABLE); } //======================================================================== void DMAinit(unsigned int addressbuffer, unsigned int SizeBuffer) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_InitStructure.DMA_MemoryBaseAddr = addressbuffer; DMA_InitStructure.DMA_BufferSize = SizeBuffer; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR); DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_Cmd(DMA1_Channel1, ENABLE); DMA_ITConfig (DMA1_Channel1, DMA_IT_TC, ENABLE); } //======================================================================== void EXTI_init(void) { EXTI_InitTypeDef EXTI_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource7); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9); EXTI_InitStruct.EXTI_Line = EXTI_Line6 | EXTI_Line7 | EXTI_Line8 | EXTI_Line9; EXTI_InitStruct.EXTI_LineCmd = ENABLE; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_Init(&EXTI_InitStruct); } //======================================================================== void SetBright(uint8_t Bright) { GPIO_InitStructure.GPIO_Pin = LCD_LED; // GPIO_Mode_IPD = 0x28, // GPIO_Mode_IPU = 0x48, // GPIO_Mode_Out_PP = 0x10, if (Bright==0) { GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOB, &GPIO_InitStructure); } if (Bright==1) { GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); } if (Bright==2) { GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, LCD_LED); } } void Usart_init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //Initializing GPIO (USART1 TX) GPIO_InitTypeDef usartGpioInitStruct; usartGpioInitStruct.GPIO_Pin = GPIO_Pin_2;//GPIO_Pin_9; usartGpioInitStruct.GPIO_Mode = GPIO_Mode_AF_PP; usartGpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &usartGpioInitStruct); //Initializing GPIO (USART1 RX) usartGpioInitStruct.GPIO_Pin = GPIO_Pin_3;//GPIO_Pin_10; usartGpioInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &usartGpioInitStruct); //Configuring USART itself USART_InitTypeDef usartInitStruct; usartInitStruct.USART_BaudRate = 115200; usartInitStruct.USART_WordLength = USART_WordLength_8b; usartInitStruct.USART_StopBits = USART_StopBits_1; usartInitStruct.USART_Parity = USART_Parity_No; usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &usartInitStruct); USART_Cmd(USART2, ENABLE); //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } void SendByteToUsart(uint8_t data) { while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} //Checking Transmission complete flag USART_SendData(USART2, data); } void SendStringToUsart(char *string) { while(*string) { while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} //Checking Transmission complete flag USART_SendData(USART2, *string); string++; } }