2022-04-28 05:22 PM
I am simulating stm32f103c6 in proteus,I want to do timer2 interrupt ,I set pre = 7200-1,psc = 10000-1,so 1s to timer2 update interrupt.But time is not correct,it does not work.
I look up information,it is said I need to change stm32 clk to 8MHz,not 72MHz.How to do that?I read function systeminit,which is too complicated to find answer.
Also,can i use systick clk to make delay_us delay_ms in Proteus?I tried but it does not work
2022-04-28 07:25 PM
This is not a Proteus support forum.
Unfortunately "it does not work" is an unhelpful description.
What DOES it do? What SHOULD it do?
>>Time is not correct
How? Fast, Slow? What is it? What do you expect it to be?
The first should divide by 72,000,000 if you slow the clock to 8 MHz, this will take 9x longer
If SystemInit() does not initialize the HSE/PLL the processor will continue running at 8 MHz that the HSI clock starts the processor with.
You're not going to be able to 1us interrupts. SysTick is typically good for 1ms tick interrupts. It is a 24-bit down counter, not sure I'd use it to measure us, the wrapping math is a bit hairy. Figure out the PLL and SystemInit()first.
2022-04-28 08:00 PM
I use a led toggle to see how timer2 update interrupt work.
I. clk is 72HMz, I set time2 pre = 7200-1,psc = 10000-1,Proteus crystal is 8M。led does not change,does nothing,nothing happen
II. I change clk to 8MHz,set pre = 800-1 psc = 10000-1.Proteus crystal is 8M It should update interrupt timeout is 1s,but I use a watch to count,it is not 1s,it is about 2s.If I change 10000 to 5000, it is 1.21s.I was wondering if clk is 8MHz,I don`t know how to make stm32 work on 8MHz
#if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
/* #define SYSCLK_FREQ_HSE HSE_VALUE */
#define SYSCLK_FREQ_24MHz 24000000
#else
#define SYSCLK_FREQ_HSE HSE_VALUE
/* #define SYSCLK_FREQ_24MHz 24000000 */
/* #define SYSCLK_FREQ_36MHz 36000000 */
/* #define SYSCLK_FREQ_48MHz 48000000 */
/* #define SYSCLK_FREQ_56MHz 56000000 */
//#define SYSCLK_FREQ_72MHz 72000000
#endif
#ifdef SYSCLK_FREQ_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_24MHz
uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_36MHz
uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_48MHz
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_56MHz
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_72MHz
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
#else /*!< HSI Selected as System Clock source */
uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
#endif
static void SetSysClock(void)
{
#ifdef SYSCLK_FREQ_HSE
SetSysClockToHSE();
#elif defined SYSCLK_FREQ_24MHz
SetSysClockTo24();
#elif defined SYSCLK_FREQ_36MHz
SetSysClockTo36();
#elif defined SYSCLK_FREQ_48MHz
SetSysClockTo48();
#elif defined SYSCLK_FREQ_56MHz
SetSysClockTo56();
#elif defined SYSCLK_FREQ_72MHz
SetSysClockTo72();
#endif
/* If none of the define above is enabled, the HSI is used as System clock
source (default after reset) */
}
III. I use systick timer to make delay_us and delay_ms,it doesn`t delay
static u32 fac_ms=0; //ms延时�?乘数
void delay_init(u8 SYSCLK)
{
#if SYSTEM_SUPPORT_OS //如果需�?支�?OS.
u32 reload;
#endif
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); //选择外部时钟 HCLK/8
// HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);//SysTick频率为HCLK
fac_us=SYSCLK; //�?论是�?�使用OS,fac_us都需�?使用
#if SYSTEM_SUPPORT_OS //如果需�?支�?OS.
reload=SYSCLK; //�?秒钟的计数次数 �?��?为K
reload*=1000000/delay_ostickspersec; //根�?�delay_ostickspersec设定溢出时间
//reload为24�?寄存器,最大值:16777216,在72M下,约�?�0.233s左�?�
fac_ms=1000/delay_ostickspersec; //代表OS�?�以延时的最少�?��?
SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;//开�?�SYSTICK中断
SysTick->LOAD=reload; //�?1/OS_TICKS_PER_SEC秒中断一次
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //开�?�SYSTICK
#else
#endif
}
void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms; //时间加载(SysTick->LOAD为24bit)
SysTick->VAL =0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}