void main(void) #endif { /* Enables the HSI clock for PORTC */ RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTC, ENABLE); /* Configure all unused PORT pins to low power consumption */ PORT_StructInit(&PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_All & ~(PORT_Pin_0)); PORT_Init(MDR_PORTC, &PORT_InitStructure); /* Configure PORTC pin 0 for output to switch LEDs on/off */ PORT_InitStructure.PORT_Pin = PORT_Pin_0; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTC, &PORT_InitStructure); /* Enables the HSI clock for IWDG */ RST_CLK_PCLKcmd(RST_CLK_PCLK_IWDG,ENABLE); /* Enable IWDG */ IWDG_Enable(); /* Enables write access to IWDG_PR,IWDG_RLR registers */ IWDG_WriteAccessEnable(); /* Set IWDG Prescaler value */ IWDG_SetPrescaler(IWDG_Prescaler_128); /* Wait when Prescaler Value was updated */ while (IWDG_GetFlagStatus(IWDG_FLAG_PVU) != 1) { } /* Set IWDG Reload value */ IWDG_SetReload(0xFFF); /* Infinite loop */ while (1) { /* Load counter value and blink LED after delay */ Delay(1000000); IWDG_ReloadCounter(); BlinkLED1(1,30000); } }
void main(void) #endif { /* Enables the HSI clock for PORTB */ RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTB, ENABLE); /* Configure all unused PORT pins to low power consumption */ PORT_StructInit(&PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_All & ~(PORT_Pin_0)); PORT_Init(MDR_PORTB, &PORT_InitStructure); /* Configure PORTB pin 10 for output to switch LEDs on/off */ PORT_InitStructure.PORT_Pin = PORT_Pin_0; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTB, &PORT_InitStructure); /* Enables the HSI clock for WWDG */ RST_CLK_PCLKcmd(RST_CLK_PCLK_WWDG,ENABLE); NVIC_EnableIRQ(WWDG_IRQn); /* Set WWDG Prescaler value*/ WWDG_SetPrescaler (WWDG_Prescaler_8); /* Enable WWDG and load start counter value*/ WWDG_Enable(0x7F); /* Enables the WWDG Early Wakeup interrupt */ WWDG_EnableIT(); /* Infinite loop */ for(NumBlink = 0; NumBlink < 15;) { if (wwdg_flag == SET) { BlinkLED1(1,30000); wwdg_flag = RESET; } } LEDOn(LED1); NVIC_DisableIRQ(WWDG_IRQn); while(1); }
void PowerMonitor_Init(void) { uint32_t temp32u; COMP_InitTypeDef COMP_InitStructure; COMP_CVRefInitTypeDef COMP_CVRefInitStructure; PORT_InitTypeDef PORT_InitStructure; COMP_StructInit(&COMP_InitStructure); COMP_InitStructure.COMP_PlusInputSource = COMP_PlusInput_CVREF; COMP_InitStructure.COMP_MinusInputSource = COMP_MinusInput_IN1; COMP_Init(&COMP_InitStructure); COMP_CVRefStructInit(&COMP_CVRefInitStructure); COMP_CVRefInitStructure.COMP_CVRefSource = COMP_CVREF_SOURCE_AVdd; COMP_CVRefInitStructure.COMP_CVRefRange = COMP_CVREF_RANGE_Up; COMP_CVRefInitStructure.COMP_CVRefScale = COMP_CVREF_SCALE_14_div_32; COMP_CVRefInit(&COMP_CVRefInitStructure); COMP_Cmd(ENABLE); COMP_CVRefCmd(ENABLE); while(COMP_GetCfgFlagStatus(COMP_CFG_FLAG_READY) != SET); // Setup GPIO PORT_StructInit(&PORT_InitStructure); PORT_InitStructure.PORT_Pin = (1 << POWER_MONITOR_PIN); PORT_Init(POWER_MONITOR_PORT, &PORT_InitStructure); // Wait until power supply is stable temp32u = DWT_StartDelayUs(50000); while (DWT_DelayInProgress(temp32u)) { if (COMP_GetFlagStatus(COMP_STATUS_FLAG_SY) == SET) { // False triggering - restart delay temp32u = DWT_StartDelayUs(50000); } } // Read and clear comparator result latch COMP_GetResultLatch(); COMP_ITConfig(ENABLE); NVIC_EnableIRQ(COMPARATOR_IRQn); }
void prvSetupHardware( void ) { PORT_InitTypeDef PORT_InitStructure; UART_InitTypeDef UART_InitStructure; //SSP_InitTypeDef SPI_InitStructure; TIMER_CntInitTypeDef sTIM_CntInit; // TIMER_ChnInitTypeDef sTIM_ChnInit; //CLK /* Enable HSE clock oscillator */ RST_CLK_HSEconfig(RST_CLK_HSE_ON); while(RST_CLK_HSEstatus() == ERROR); RST_CLK_CPUclkSelection(RST_CLK_CPUclkCPU_C3); MDR_RST_CLK -> CPU_CLOCK |= 2;//CPU_C1 set HSE RST_CLK_HSIcmd(DISABLE); /* Enable the RTCHSE clock on all ports */ RST_CLK_PCLKcmd(ALL_PORTS_CLK, ENABLE); PORT_StructInit(&PORT_InitStructure);//reset struct /************************ LCD Initialization *************************/ /* Configure PORTA pins for data transfer to/from LCD */ PORT_InitStructure.PORT_Pin = LCD_DATA_BUS_8; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_Init(MDR_PORTA, &PORT_InitStructure); /* Configure PORTE pin4 and pin5 for LCD crystals control */ PORT_InitStructure.PORT_Pin = KS0108_CS1 | KS0108_CS2 | KS0108_RS; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_Init(MDR_PORTE, &PORT_InitStructure); PORT_Init(MDR_PORTE, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = KS0108_EN | KS0108_RW | KS0108_RES; PORT_Init(MDR_PORTC, &PORT_InitStructure); PORT_SetBits(MDR_PORTA, LCD_DATA_BUS_8); PORT_SetBits(MDR_PORTE, KS0108_CS1 | KS0108_CS2 | KS0108_RS); PORT_SetBits(MDR_PORTC, KS0108_EN | KS0108_RW | KS0108_RES); //Timer1 // TIMER1 RST_CLK_PCLKcmd(RST_CLK_PCLK_TIMER1,ENABLE); /* Reset all TIMER1 settings */ TIMER_DeInit(MDR_TIMER1); TIMER_BRGInit(MDR_TIMER1,TIMER_HCLKdiv1); /* TIM1 configuration ------------------------------------------------*/ /* Initializes the TIMERx Counter ------------------------------------*/ sTIM_CntInit.TIMER_IniCounter = 0; sTIM_CntInit.TIMER_Prescaler = 150;// sTIM_CntInit.TIMER_Period = 82; sTIM_CntInit.TIMER_CounterMode = TIMER_CntMode_ClkFixedDir; sTIM_CntInit.TIMER_CounterDirection = TIMER_CntDir_Up; sTIM_CntInit.TIMER_EventSource = TIMER_EvSrc_None; sTIM_CntInit.TIMER_FilterSampling = TIMER_FDTS_TIMER_CLK_div_1; sTIM_CntInit.TIMER_ARR_UpdateMode = TIMER_ARR_Update_Immediately; sTIM_CntInit.TIMER_ETR_FilterConf = TIMER_Filter_1FF_at_TIMER_CLK; sTIM_CntInit.TIMER_ETR_Prescaler = TIMER_ETR_Prescaler_None; sTIM_CntInit.TIMER_ETR_Polarity = TIMER_ETRPolarity_NonInverted; sTIM_CntInit.TIMER_BRK_Polarity = TIMER_BRKPolarity_NonInverted; TIMER_CntInit (MDR_TIMER1,&sTIM_CntInit); NVIC_EnableIRQ(Timer1_IRQn); TIMER_ITConfig(MDR_TIMER1,TIMER_STATUS_CNT_ARR, ENABLE); /* TMR1 enable */ TIMER_Cmd (MDR_TIMER1,ENABLE); // TIMER2 RST_CLK_PCLKcmd(RST_CLK_PCLK_TIMER2,ENABLE); /* Reset all TIMER1 settings */ TIMER_DeInit(MDR_TIMER2); TIMER_BRGInit(MDR_TIMER2,TIMER_HCLKdiv1); /* TIM2 configuration ------------------------------------------------*/ /* Initializes the TIMERx Counter ------------------------------------*/ sTIM_CntInit.TIMER_IniCounter = 0; sTIM_CntInit.TIMER_Prescaler = 0xf;// sTIM_CntInit.TIMER_Period = 0xffff; sTIM_CntInit.TIMER_CounterMode = TIMER_CntMode_ClkFixedDir; sTIM_CntInit.TIMER_CounterDirection = TIMER_CntDir_Up; sTIM_CntInit.TIMER_EventSource = TIMER_EvSrc_None; sTIM_CntInit.TIMER_FilterSampling = TIMER_FDTS_TIMER_CLK_div_1; sTIM_CntInit.TIMER_ARR_UpdateMode = TIMER_ARR_Update_Immediately; sTIM_CntInit.TIMER_ETR_FilterConf = TIMER_Filter_1FF_at_TIMER_CLK; sTIM_CntInit.TIMER_ETR_Prescaler = TIMER_ETR_Prescaler_None; sTIM_CntInit.TIMER_ETR_Polarity = TIMER_ETRPolarity_NonInverted; sTIM_CntInit.TIMER_BRK_Polarity = TIMER_BRKPolarity_NonInverted; TIMER_CntInit (MDR_TIMER2,&sTIM_CntInit); NVIC_EnableIRQ(Timer2_IRQn); TIMER_ITConfig(MDR_TIMER2,TIMER_STATUS_CNT_ARR, ENABLE); /* TMR2 enable */ TIMER_Cmd (MDR_TIMER2,ENABLE); // TIMER3 RST_CLK_PCLKcmd(RST_CLK_PCLK_TIMER3,ENABLE); /* Reset all TIMER3 settings */ TIMER_DeInit(MDR_TIMER3); TIMER_BRGInit(MDR_TIMER3,TIMER_HCLKdiv1); /* TIM3 configuration ------------------------------------------------*/ /* Initializes the TIMERx Counter ------------------------------------*/ sTIM_CntInit.TIMER_IniCounter = 0; sTIM_CntInit.TIMER_Prescaler = 0; sTIM_CntInit.TIMER_Period = 0xFFFF; sTIM_CntInit.TIMER_CounterMode = TIMER_CntMode_EvtFixedDir; sTIM_CntInit.TIMER_CounterDirection = TIMER_CntDir_Up; sTIM_CntInit.TIMER_EventSource = TIMER_EvSrc_CH1; sTIM_CntInit.TIMER_FilterSampling = TIMER_FDTS_TIMER_CLK_div_1; sTIM_CntInit.TIMER_ARR_UpdateMode = TIMER_ARR_Update_Immediately; sTIM_CntInit.TIMER_ETR_FilterConf = TIMER_Filter_1FF_at_TIMER_CLK; sTIM_CntInit.TIMER_ETR_Prescaler = TIMER_ETR_Prescaler_None; sTIM_CntInit.TIMER_ETR_Polarity = TIMER_ETRPolarity_NonInverted; sTIM_CntInit.TIMER_BRK_Polarity = TIMER_BRKPolarity_NonInverted; TIMER_CntInit (MDR_TIMER3,&sTIM_CntInit); // NVIC_EnableIRQ(Timer3_IRQn); // TIMER_ITConfig(MDR_TIMER3,TIMER_STATUS_CNT_ARR, ENABLE); /* Enable TIMER3 */ TIMER_Cmd(MDR_TIMER3,ENABLE); /************************ UART1 Initialization *************************/ PORT_StructInit(&PORT_InitStructure);//reset struct PORT_InitStructure.PORT_Pin = PORT_Pin_12|PORT_Pin_13;//RX TX PORT_InitStructure.PORT_FUNC = PORT_FUNC_OVERRID; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTE, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = PORT_Pin_14;//EN RS485 PORT_InitStructure.PORT_OE =PORT_OE_OUT; PORT_InitStructure.PORT_PD = PORT_PD_OPEN; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_Init(MDR_PORTE, &PORT_InitStructure); RS485_TX_OFF; //UART1 RST_CLK_PCLKcmd(RST_CLK_PCLK_UART1,ENABLE); /* Set the HCLK division factor = 1 for UART1,UART2*/ UART_BRGInit(MDR_UART1, UART_HCLKdiv1); UART_DeInit(MDR_UART1); /* Disable interrupt on UART1 */ NVIC_DisableIRQ(UART1_IRQn); /* Initialize UART_InitStructure */ UART_InitStructure.UART_BaudRate = 38400; UART_InitStructure.UART_WordLength = UART_WordLength8b; UART_InitStructure.UART_StopBits = UART_StopBits1;//PKDU2 niobyi UART_InitStructure.UART_Parity = UART_Parity_No; UART_InitStructure.UART_FIFOMode = UART_FIFO_OFF; UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_RXE | UART_HardwareFlowControl_TXE; /* Configure UART1 parameters*/ UART_Init(MDR_UART1,&UART_InitStructure); NVIC_EnableIRQ(UART1_IRQn); UART_ITConfig(MDR_UART1,UART_IT_RX, ENABLE); /* Enables UART1 peripheral */ UART_Cmd(MDR_UART1,ENABLE); //PORT /************************ Joystick Initialization *************************/ /* Configure PORTC pins 10..14 for input to handle joystick events */ PORT_StructInit(&PORT_InitStructure);//reset struct PORT_InitStructure.PORT_Pin = ( PORT_Pin_10 | PORT_Pin_11 | PORT_Pin_12 | PORT_Pin_13 | PORT_Pin_14 ); PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_SPEED = PORT_OUTPUT_OFF; PORT_Init( MDR_PORTC, &PORT_InitStructure ); /************************ LEDs Initialization *************************/ PORT_StructInit( &PORT_InitStructure );//reset struct /* Configure PORTD pins 10..14 for output to switch LEDs on/off */ PORT_InitStructure.PORT_Pin = LEDs_PINs; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init( MDR_PORTD, &PORT_InitStructure ); /* All LEDs switch off */ PORT_ResetBits(MDR_PORTD, LEDs_PINs); }
void main(void) #endif { /* Enables the HSI clock on PORTD */ RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTD, ENABLE); /* Configure all unused PORT pins to low power consumption */ PORT_StructInit(&PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_All & ~(PORT_Pin_10 | PORT_Pin_11)); PORT_Init(MDR_PORTD, &PORT_InitStructure); /* Configure PORTD pins 10..11 for output to switch LEDs on/off */ PORT_InitStructure.PORT_Pin = (PORT_Pin_10 | PORT_Pin_11); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTD, &PORT_InitStructure); /* Enables the HSI clock for BKP control */ RST_CLK_PCLKcmd(RST_CLK_PCLK_BKP,ENABLE); /* RTC reset */ BKP_RTC_Reset(ENABLE); BKP_RTC_Reset(DISABLE); #ifdef RTC_HSI_CLK /* Configure RTCHSI as RTC clock source */ RST_CLK_HSIadjust(25); RST_CLK_HSIclkPrescaler(RTCHS_PRESC); BKP_RTCclkSource(BKP_RTC_HSIclk); RST_CLK_RTC_HSIclkEnable(ENABLE); #endif #ifdef RTC_HSE_CLK /* Configure RTCHSE as RTC clock source */ RST_CLK_HSEconfig(RST_CLK_HSE_ON); while (RST_CLK_HSEstatus()!=SUCCESS); RST_CLK_RTC_HSEclkEnable(ENABLE); RST_CLK_HSEclkPrescaler(RTCHS_PRESC); BKP_RTCclkSource(BKP_RTC_HSEclk); #endif #ifdef RTC_LSI_CLK /* Configure LSI as RTC clock source */ RST_CLK_LSIadjust(12); BKP_RTCclkSource(BKP_RTC_LSIclk); while (RST_CLK_LSIstatus()!=SUCCESS); #endif #ifdef RTC_LSE_CLK /* Configure LSE as RTC clock source */ RST_CLK_LSEconfig(RST_CLK_LSE_ON); while (RST_CLK_LSEstatus()!=SUCCESS); BKP_RTCclkSource(BKP_RTC_LSEclk); #endif /* Set the RTC prescaler value */ BKP_RTC_WaitForUpdate(); #ifdef RTC_HSI_CLK BKP_RTC_SetPrescaler(PRESC_VALUE_HS); #endif #ifdef RTC_HSE_CLK BKP_RTC_SetPrescaler(PRESC_VALUE_HS); #endif #ifdef RTC_LSI_CLK BKP_RTC_SetPrescaler(PRESC_VALUE_LS); #endif #ifdef RTC_LSE_CLK BKP_RTC_SetPrescaler(PRESC_VALUE_LS); #endif /* Set the RTC alarm value */ BKP_RTC_WaitForUpdate(); BKP_RTC_SetAlarm(ALARM_VALUE); /* Set the RTC counter value */ BKP_RTC_WaitForUpdate(); BKP_RTC_SetCounter(COUNT_VALUE); /* Enable all RTC interrupts */ BKP_RTC_ITConfig(BKP_RTC_IT_ALRF | BKP_RTC_IT_SECF | BKP_RTC_IT_OWF,ENABLE); NVIC_EnableIRQ(BKP_IRQn); /* RTC enable */ BKP_RTC_WaitForUpdate(); BKP_RTC_Enable(ENABLE); while(1) { } }
void GPIO_init(void) { RST_CLK_PCLKcmd(ALL_PORTS_CLK, ENABLE); PORT_StructInit(&PORT_InitStructure); /* Configure PORTA pins */ PORT_InitStructure.PORT_Pin = (PORT_Pin_0); PORT_InitStructure.PORT_PD = PORT_PD_OPEN; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTA, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_4); PORT_InitStructure.PORT_PD = PORT_PD_DRIVER; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTA, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_6|PORT_Pin_7); PORT_InitStructure.PORT_FUNC = PORT_FUNC_OVERRID; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_PD = PORT_PD_DRIVER; PORT_InitStructure.PORT_PULL_UP = PORT_PULL_UP_OFF; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_Init(MDR_PORTA, &PORT_InitStructure); /* Configure PORTB pin */ PORT_InitStructure.PORT_Pin = (PORT_Pin_5); PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTB, &PORT_InitStructure); /* Configure PORTC pin */ PORT_InitStructure.PORT_Pin = (PORT_Pin_8|PORT_Pin_9|PORT_Pin_10); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTB, &PORT_InitStructure); /* Configure PORTD pin */ PORT_InitStructure.PORT_Pin = (PORT_Pin_0|PORT_Pin_1|PORT_Pin_2| \ PORT_Pin_3|PORT_Pin_4|PORT_Pin_5| \ PORT_Pin_6|PORT_Pin_7); PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTD, &PORT_InitStructure); /* Configure PORTF pin */ PORT_InitStructure.PORT_Pin = (PORT_Pin_0|PORT_Pin_1); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_OVERRID; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTF, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_2); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_ALTER; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTF, &PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_6); PORT_InitStructure.PORT_PD = PORT_PD_DRIVER; PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_InitStructure.PORT_PULL_UP = PORT_PULL_UP_ON; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTE, &PORT_InitStructure); RS485_TX_OFF; }
void main(void) #endif { RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTD, ENABLE); /* Configure all unused PORT pins to low power consumption */ PORT_StructInit(&PORT_InitStructure); PORT_InitStructure.PORT_Pin = (PORT_Pin_All & ~(PORT_Pin_10 | PORT_Pin_11 | PORT_Pin_12 | PORT_Pin_13 | PORT_Pin_14)); PORT_Init(MDR_PORTD, &PORT_InitStructure); /* Configure PORTD pins 10..14 for output to switch LEDs on/off */ PORT_InitStructure.PORT_Pin = (PORT_Pin_10 | PORT_Pin_11 | PORT_Pin_12 | PORT_Pin_13 | PORT_Pin_14); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTD, &PORT_InitStructure); /* Consequently turn all three used LEDs on and off */ LEDOn(LED1); Delay(4*BLINK_DELAY); LEDOff(LED1); Delay(4*BLINK_DELAY); LEDOn(LED2); Delay(4*BLINK_DELAY); LEDOff(LED2); Delay(4*BLINK_DELAY); LEDOn(LED3); Delay(4*BLINK_DELAY); LEDOff(LED3); Delay(4*BLINK_DELAY); /* Infinite loop that demonstrates different input clock sources using */ while (1) { /* Set RST_CLK to default */ RST_CLK_DeInit(); RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTD, ENABLE); /* 1. CPU_CLK = HSI clock */ /* Enable HSI clock source */ RST_CLK_HSIcmd(ENABLE); /* Switch LED2 on and wait for HSI ready status */ LEDOn(LED2); Delay(BLINK_DELAY); if (RST_CLK_HSIstatus() == SUCCESS) /* Good HSI clock */ { /* Switch LED2 off */ LEDOff(LED2); /* Select HSI clock on the CPU clock MUX */ RST_CLK_CPUclkSelection(RST_CLK_CPUclkHSI); /* LED1 blinking with HSI clock as input clock source */ BlinkLED1(BLINK_NUM, BLINK_DELAY); } else /* HSI timeout */ { IndicateError(); } /* 2. CPU_CLK = HSI/2 clock */ /* Enable HSI clock source */ RST_CLK_HSIcmd(ENABLE); /* Disable CPU_PLL */ RST_CLK_CPU_PLLcmd(DISABLE); /* Select HSI/2 clock as CPU_PLL input clock source */ RST_CLK_CPU_PLLconfig(RST_CLK_CPU_PLLsrcHSIdiv2, 1); /* Switch LED2 on and wait for HSI ready status */ LEDOn(LED2); Delay(BLINK_DELAY); if (RST_CLK_HSIstatus() == SUCCESS) /* Good HSI clock */ { /* Switch LED2 off */ LEDOff(LED2); /* Set CPU_C3_prescaler to 1 */ RST_CLK_CPUclkPrescaler(RST_CLK_CPUclkDIV1); /* Switch CPU_C2_SEL to CPU_C1 clock instead of CPU_PLL output */ RST_CLK_CPU_PLLuse(DISABLE); /* LED1 blinking with HSI/2 clock as input clock source */ BlinkLED1(BLINK_NUM, BLINK_DELAY); } else /* HSI timeout */ { IndicateError(); } /* 3. CPU_CLK = 7*HSE/2 clock */ /* Enable HSE clock oscillator */ RST_CLK_HSEconfig(RST_CLK_HSE_ON); /* Switch LED2 on and wait for HSE ready status */ LEDOn(LED2); Delay(BLINK_DELAY); if (RST_CLK_HSEstatus() == SUCCESS) /* Good HSE clock */ { /* Select HSE clock as CPU_PLL input clock source */ /* Set PLL multiplier to 7 */ RST_CLK_CPU_PLLconfig(RST_CLK_CPU_PLLsrcHSEdiv1, 7); /* Enable CPU_PLL */ RST_CLK_CPU_PLLcmd(ENABLE); /* Switch LED2 on and wait for PLL ready status */ if (RST_CLK_HSEstatus() == SUCCESS) /* Good CPU PLL */ { /* Switch LED2 off */ LEDOff(LED2); /* Set CPU_C3_prescaler to 2 */ RST_CLK_CPUclkPrescaler(RST_CLK_CPUclkDIV2); /* Set CPU_C2_SEL to CPU_PLL output instead of CPU_C1 clock */ RST_CLK_CPU_PLLuse(ENABLE); /* Select CPU_C3 clock on the CPU clock MUX */ RST_CLK_CPUclkSelection(RST_CLK_CPUclkCPU_C3); /* LED1 blinking with 7*HSE/2 clock as input clock source */ BlinkLED1(BLINK_NUM, BLINK_DELAY); } else /* CPU_PLL timeout */ { IndicateError(); } } else /* HSE timeout */ { IndicateError(); } /* 4. CPU_CLK = LSI clock */ /* Enable LSI clock source */ RST_CLK_LSIcmd(ENABLE); /* Switch LED2 on and wait for LSI ready status */ LEDOn(LED2); Delay(BLINK_DELAY); if (RST_CLK_LSIstatus() == SUCCESS) /* Good LSI clock */ { /* Switch LED2 off */ LEDOff(LED2); /* Select LSI clock on the CPU clock MUX */ RST_CLK_CPUclkSelection(RST_CLK_CPUclkLSI); /* LED1 blinking with LSI clock as input clock source */ BlinkLED1(BLINK_NUM, BLINK_DELAY); } else /* LSI timeout */ { IndicateError(); } } }
//-----------------------------------------------------------------// // Setup IO Ports //-----------------------------------------------------------------// void HW_PortInit(void) { PORT_InitTypeDef PORT_InitStructure; PORT_DeInit(MDR_PORTA); PORT_DeInit(MDR_PORTB); PORT_DeInit(MDR_PORTC); PORT_DeInit(MDR_PORTD); PORT_DeInit(MDR_PORTE); PORT_DeInit(MDR_PORTF); // default output value is 0 // Set some outputs the default design values of 1 //PORT_SetBits(MDR_PORTA, (1<<BUZ1) | (1<<BUZ2) ); PORT_SetBits(MDR_PORTB, (1<<LGREEN) | (1<<LRED) ); /* Reset PORT initialization structure parameters values PORT_InitStruct->PORT_Pin = PORT_Pin_All; PORT_InitStruct->PORT_OE = PORT_OE_IN; PORT_InitStruct->PORT_PULL_UP = PORT_PULL_UP_OFF; PORT_InitStruct->PORT_PULL_DOWN = PORT_PULL_DOWN_OFF; PORT_InitStruct->PORT_PD_SHM = PORT_PD_SHM_OFF; PORT_InitStruct->PORT_PD = PORT_PD_DRIVER; PORT_InitStruct->PORT_GFEN = PORT_GFEN_OFF; PORT_InitStruct->PORT_FUNC = PORT_FUNC_PORT; PORT_InitStruct->PORT_SPEED = PORT_OUTPUT_OFF; PORT_InitStruct->PORT_MODE = PORT_MODE_ANALOG; */ //================= PORTA =================// PORT_StructInit(&PORT_InitStructure); // Typical digital inputs: PORT_InitStructure.PORT_Pin = (1<<OVERLD) | (1<<ENC_BTN); PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_Init(MDR_PORTA, &PORT_InitStructure); // Digital input with pull-up PORT_InitStructure.PORT_Pin = (1<<EEN); PORT_InitStructure.PORT_PULL_UP = PORT_PULL_UP_ON; PORT_Init(MDR_PORTA, &PORT_InitStructure); // Typical digital outputs: PORT_InitStructure.PORT_Pin = (1<<CLIM_SEL); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTA, &PORT_InitStructure); // Timer outputs to buzzer (TMR1.CH2, TMR1.CH2N) PORT_InitStructure.PORT_Pin = (1<<BUZ1) | (1<<BUZ2); PORT_InitStructure.PORT_FUNC = PORT_FUNC_ALTER; PORT_Init(MDR_PORTA, &PORT_InitStructure); // USART1 pins PORT_StructInit(&PORT_InitStructure); // TX pin PORT_InitStructure.PORT_Pin = 1<<TXD1; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST; PORT_InitStructure.PORT_FUNC = PORT_FUNC_OVERRID; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_Init(MDR_PORTA, &PORT_InitStructure); // RX pin PORT_InitStructure.PORT_Pin = 1<<RXD1; PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_Init(MDR_PORTA, &PORT_InitStructure); // debug /* PORT_StructInit(&PORT_InitStructure); PORT_InitStructure.PORT_Pin = (1<<TXD1) | (1<<RXD1); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_Init(MDR_PORTA, &PORT_InitStructure); */ //================= PORTB =================// PORT_StructInit(&PORT_InitStructure); // Typical digital inputs: buttons and encoder PORT_InitStructure.PORT_Pin = (1<<SB_ESC) | (1<<SB_LEFT) | (1<<SB_RIGHT) | (1<<SB_OK) | (1<<SB_MODE) | (1<<ENCA) | (1<<ENCB) | (1<<PG) ; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_Init(MDR_PORTB, &PORT_InitStructure); // Power good // TODO: add interrupt PORT_InitStructure.PORT_Pin = (1<<PG) ; PORT_Init(MDR_PORTB, &PORT_InitStructure); // Leds and buttons SB_ON, SB_OFF PORT_InitStructure.PORT_Pin = (1<<LGREEN) | (1<<LRED); PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTB, &PORT_InitStructure); //================= PORTC =================// PORT_StructInit(&PORT_InitStructure); // LCD Backlight (TMR3.CH1) PORT_InitStructure.PORT_Pin = (1<<LCD_BL); PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_FUNC = PORT_FUNC_ALTER; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_Init(MDR_PORTC, &PORT_InitStructure); // I2C PORT_InitStructure.PORT_Pin = (1<<SCL) | (1<<SDA); PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST; PORT_Init(MDR_PORTC, &PORT_InitStructure); //================= PORTD =================// PORT_StructInit(&PORT_InitStructure); // Analog functions PORT_InitStructure.PORT_Pin = (1<<VREF_P) | (1<<VREF_N) | (1<<TEMP_IN) | (1<<UADC) | (1<<IADC) ; PORT_Init(MDR_PORTD, &PORT_InitStructure); // LCD CLK and CS PORT_InitStructure.PORT_Pin = (1<<LCD_CLK) | (1<<LCD_CS) ; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_FUNC = PORT_FUNC_ALTER; PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_Init(MDR_PORTD, &PORT_InitStructure); // MOSI PORT_InitStructure.PORT_Pin = (1<<LCD_MOSI); PORT_InitStructure.PORT_PULL_DOWN = PORT_PULL_DOWN_ON; PORT_Init(MDR_PORTD, &PORT_InitStructure); //================= PORTE =================// PORT_StructInit(&PORT_InitStructure); // LCD RST and SEL, Load disable output PORT_InitStructure.PORT_Pin = (1<<LCD_RST) | (1<<LCD_SEL) | (1<<LDIS); PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_Init(MDR_PORTE, &PORT_InitStructure); // cooler PWM output (TMR3.CH3N) PORT_InitStructure.PORT_Pin = (1<<CPWM); PORT_InitStructure.PORT_FUNC = PORT_FUNC_OVERRID; PORT_Init(MDR_PORTE, &PORT_InitStructure); // voltage and current PWM outputs (TMR2.CH1N, TMR2.CH3N) PORT_InitStructure.PORT_Pin = (1<<UPWM) | (1<<IPWM); PORT_InitStructure.PORT_FUNC = PORT_FUNC_ALTER; PORT_Init(MDR_PORTE, &PORT_InitStructure); //================= PORTF =================// PORT_StructInit(&PORT_InitStructure); // Feedback channel select, converter enable PORT_InitStructure.PORT_Pin = (1<<EN) | (1<<STAB_SEL) ; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_Init(MDR_PORTF, &PORT_InitStructure); // USART2 functions PORT_StructInit(&PORT_InitStructure); // TX pin PORT_InitStructure.PORT_Pin = 1<<TXD2; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST; PORT_InitStructure.PORT_FUNC = PORT_FUNC_OVERRID; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_Init(MDR_PORTF, &PORT_InitStructure); // RX pin PORT_InitStructure.PORT_Pin = 1<<RXD2; PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_Init(MDR_PORTF, &PORT_InitStructure); }