/** * @brief Example main entry point. * @param None * @retval None */ void main(void) { uint8_t state = 0x00; /* GPIO Configuration -----------------------------------------------------*/ GPIO_Config(); /* Configure LCD mounted on STM8-128 EVAL board ----------------------------*/ LCD_Config(); /* Clock configuration -----------------------------------------------------*/ CLK_Config(); enableInterrupts(); while (1) { while(state == OscillatorStatus) {} state = OscillatorStatus; /* Update the selected master clock oscillator */ /* joystick right */ if(OscillatorStatus == 0x00) { CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); /* Print on LCD line2*/ LCD_SetCursorPos(LCD_LINE2, 0); LCD_Print(" CLK = HSI "); } /* joystick up */ if(OscillatorStatus == 0x01) { CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); /* Print on LCD line2*/ LCD_SetCursorPos(LCD_LINE2, 0); LCD_Print(" CLK = HSE "); } /* joystick down */ if(OscillatorStatus == 0x02) { CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_LSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); /* Print on LCD line2*/ LCD_SetCursorPos(LCD_LINE2, 0); LCD_Print(" CLK = LSI "); } } }
int main(void) { // Initialise the clock to have a /1 prescaler and use the external crystal clock source for accuracy. CLK_DeInit(); CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); // Reset ("de-initialise") GPIO port D. GPIO_DeInit(GPIOD); // Initialise pin 0 of port D by setting it as: // - an output pin, // - using a push-pull driver, // - at a low logic level (0V), and // - 10MHz. GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST); LCDInit(); // Init the LCD DecodeInit(); // Init the GPS decoding DrawScreen(); // Setup Screen and Buffer // Infinite loop. for(;;) { // Blink Debug LED GPIO_WriteReverse(GPIOD, GPIO_PIN_0); DrawDemo(); } }
////////////////////////main end/////////////////// void sysclk_ini(void) { CLK_HSECmd(ENABLE);//外部时钟开?? CLK_LSICmd(ENABLE);//内部低频RC开?? CLK_HSICmd(ENABLE);//内部高频RC开???? CLK_ClockSwitchCmd(ENABLE);//切换使能?? CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,CLK_SOURCE_HSE,DISABLE,CLK_CURRENTCLOCKSTATE_DISABLE);//切换配置到外部时钟,关闭原来时钟 }
//设置内部时钟16M为主时钟 //输入参数:无 //输出参数:无 void Set_HSI(void) { //CLK_DeInit(); //恢复默认设置 CLK_HSICmd(ENABLE); //内部时钟开 while(SET != CLK_GetFlagStatus(CLK_FLAG_HSIRDY)); //等待内部时钟稳定 CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); //不分频,16M CLK_ClockSwitchCmd(ENABLE); //时钟切换使能 //切换配置 CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,CLK_SOURCE_HSI,DISABLE,CLK_CURRENTCLOCKSTATE_DISABLE); }
void clockInit(void) { /* Turn on internal high speed clock and use it */ CLK_HSICmd(ENABLE); CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, ENABLE, CLK_CURRENTCLOCKSTATE_ENABLE); CLK_LSICmd(DISABLE); }
/******************************************************************************* * 名 称: Bsp_ConfgiSysClk * 功 能: 配置CPU系统时钟 * 入口参数: 枚举SYS_CLK_E ; 可选参数为: * HSI_2M = 0 内部高速RC振荡源16M, 分频倍率设置为 1/8 可得到2M * HSI_4M, 内部高速RC振荡源16M, 分频倍率设置为 1/4 可得到4M * HSI_8M, 内部高速RC振荡源16M, 分频倍率设置为 1/2 可得到8M * HSI_16M, 内部高速RC振荡源16M, 分频倍率设置为 1/1 可得到16M * LSI_128K, 内部低速RC时钟源,频率固定为128KHz * HSE_XTAL, 外部高速时钟 (频率由用于外接晶振决定,无分频选项) * 出口参数: 无 * 作 者: Roger-WY * 创建日期: 2014-08-20 * 修 改: * 修改日期: * 备 注: 建议使用优化代码模式 *******************************************************************************/ void Bsp_ConfgiSysClk(SYS_CLK_E _iSysClk) { #if 1 /* 空间不足,优化代码, 只保留需要的时钟设置 */ if (_iSysClk == HSI_16M) { /* 自动切换, 切换到内部高速时钟HSI, 禁止系统时钟中断, 关闭当前的时钟源 */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); /* 设置内部高速RC时钟HSI为时钟源,分频系数为1 */ while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET); /* 等待HSI时钟稳定 */ } #else switch (_iSysClk) { case HSE_XTAL: /* 自动切换, 切换到外部高速时钟HSE, 禁止系统时钟中断, 关闭当前的时钟源 */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); break; case LSI_128K: /* 自动切换, 切换到内部低速时钟LSI, 禁止系统时钟中断, 关闭当前的时钟源 */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_LSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); break; case HSI_16M: /* 自动切换, 切换到内部高速时钟HSI, 禁止系统时钟中断, 关闭当前的时钟源 */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); /* 设置内部高速RC时钟HSI为时钟源,分频系数为1 */ while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET); /* 等待HSI时钟稳定 */ break; case HSI_8M: /* 自动切换, 切换到内部高速时钟HSI, 禁止系统时钟中断, 关闭当前的时钟源 */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2); /* 设置内部高速RC时钟HSI为时钟源,分频系数为2 */ while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET); /* 等待HSI时钟稳定 */ break; case HSI_4M: /* 自动切换, 切换到内部高速时钟HSI, 禁止系统时钟中断, 关闭当前的时钟源 */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV4); /* 设置内部高速RC时钟HSI为时钟源,分频系数为4 */ while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET); /* 等待HSI时钟稳定 */ break; case HSI_2M: /* 自动切换, 切换到内部高速时钟HSI, 禁止系统时钟中断, 关闭当前的时钟源 */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8); /* 设置内部高速RC时钟HSI为时钟源,分频系数为8 */ while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET); /* 等待HSI时钟稳定 */ break; default: break; } #endif }
/** * @brief Configure system clock to run at Maximum clock speed and output the * system clock on CCO pin * @param None * @retval None */ static void CLK_Config(void) { ErrorStatus status = FALSE; CLK_DeInit(); /* Configure the Fcpu to DIV1*/ CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); /* Configure the HSI prescaler to the optimal value */ CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); /* Output Fcpu on CLK_CCO pin */ CLK_CCOConfig(CLK_OUTPUT_CPU); /* Configure the system clock to use HSE clock source and to run at 24Mhz */ status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); while (ButtonPressed == FALSE) { } /* Configure the system clock to use HSI clock source and to run at 16Mhz */ status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); }
/** * @brief Configure system clock to use HSE as source clock and to enable the * Clock Security System (CSS) * @param None * @retval None */ static void CLK_Config(void) { ErrorStatus status = ERROR; CLK_DeInit(); status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); /*Enable CSS interrupt */ CLK_ITConfig(CLK_IT_CSSD, ENABLE); /* Enable CCS */ CLK_ClockSecuritySystemEnable(); /* Output Fcpu on CLK_CCO pin */ CLK_CCOConfig(CLK_OUTPUT_MASTER); enableInterrupts(); }
void dev_clkInit(void) { CLK_DeInit(); CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); /* #if (STM8_FREQ_MHZ > 16) // Set High Speed External 24MHz CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, DISABLE); #else // Set High Speed Internal 16MHz CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); #endif */ // tolto sopra // usa HSE anche se = 16 // nel nostro caso quarzo esterno da 16 MHz CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, DISABLE); }
void main(void) { //CFG->GCR |= 0x01; //disable swim pin /* Configure the Fcpu to DIV1*/ CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); /* select Clock = 16 MHz */ CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); /* Configure the system clock to use HSI clock source and to run at 16Mhz */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); CLK_HSICmd(ENABLE); FLASH_Config(); EXTI_DeInit(); //You should add: // Define FLASH programming time //FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD); //optbyte1 = FLASH_ReadOptionByte(0x4803); //255 by def //optbyte2 = FLASH_ReadOptionByte(0x4804); //255 by def /* if (optbyte != 765) { FLASH_Unlock(FLASH_MEMTYPE_DATA); // unlock data memory by passing the RASS key in the proper order FLASH_ProgramOptionByte(0x4803, 0xFD); // byte OPT2 resides at address x4803, write a 1 to bit 7. This will also write to the NOPT2 complement byte FLASH_Lock(FLASH_MEMTYPE_DATA); // re-lock data memory } */ //General purpose timer TIM4_Config(); #ifdef DFS_90 HotAir_Config(); #endif #ifndef DFS_90 GPIO_Init(CONTROL_GPIO_PORT, CONTROL_GPIO_PIN, GPIO_MODE_OUT_PP_LOW_FAST); #endif Temperature_ADC_Config(); STM_EVAL_SEGInit(SEG1); STM_EVAL_SEGInit(SEG2); STM_EVAL_SEGInit(SEG3); STM_EVAL_LEDInit(LEDA); STM_EVAL_LEDInit(LEDB); STM_EVAL_LEDInit(LEDC); STM_EVAL_LEDInit(LEDD); STM_EVAL_LEDInit(LEDE); STM_EVAL_LEDInit(LEDF); STM_EVAL_LEDInit(LEDG); STM_EVAL_LEDInit(LEDP); #ifndef SOLDERING_VAR2 //STM_EVAL_LEDInit(LEDP); #endif // STM_EVAL_SEGOn(SEG1); // STM_EVAL_SEGOn(SEG2); // STM_EVAL_SEGOn(SEG3); // // STM_EVAL_LEDOn(LEDA); // STM_EVAL_LEDOn(LEDB); // STM_EVAL_LEDOn(LEDC); // STM_EVAL_LEDOn(LEDD); // STM_EVAL_LEDOn(LEDE); // STM_EVAL_LEDOn(LEDF); // STM_EVAL_LEDOn(LEDG); // // STM_EVAL_LEDOff(LEDA); // STM_EVAL_LEDOff(LEDB); // STM_EVAL_LEDOff(LEDC); // STM_EVAL_LEDOff(LEDD); // STM_EVAL_LEDOff(LEDE); // STM_EVAL_LEDOff(LEDF); // STM_EVAL_LEDOff(LEDG); // STM_EVAL_LEDOff(LEDP); // // STM_EVAL_SEGOff(SEG1); // STM_EVAL_SEGOff(SEG2); // STM_EVAL_SEGOff(SEG3); //STM_EVAL_LEDOn(LEDP); //GPIO_Init(ENC_DN_BUTTON_PORT, ENC_DN_BUTTON_PIN, GPIO_MODE_IN_PU_NO_IT); //GPIO_Init(ENC_UP_BUTTON_PORT, ENC_UP_BUTTON_PIN, GPIO_MODE_IN_PU_NO_IT); STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_REED, BUTTON_MODE_GPIO); ssegInit(); ssegWriteStr("1.0.1", 5, SEG1); enableInterrupts(); #ifdef DFS_90 HotAir_Main(); #endif #ifdef SOLDERING_HAKKO_A1321 Soldering_Main(); #endif while(1) { /* #if 0 for (level = 0; level < calUpperLimit; level +=5) { Delayms(1); } for (level = calUpperLimit; level > 0; level -=5) { Delayms(1); } #else RawData = GetAdcValue(ADC_SOLDER_TEMP_CHANNEL); SmoothData = SmoothData - (LPF_Beta * (SmoothData - RawData)); level = ((int)SmoothData)*26; #endif */ } }