garvind25
Full Member level 3
Hi there,
I was going through Mazidi's STM32 book and came across this code:
This is basically a code to blink an LED with .5 sec on /off period. The code in bold letters seems to be a clock generator. Depending on the value passed to delayMs routine by the main program (here it is 500) the clock pulse gets generated. in this case, it seems to generate a on/off pulse of 1 sec time period with 50 % duty cycle.
I am unable to understand how this clock pulse generation code works. How has the upper limit of i (3195) been calculated? As per the other examples in the book, the same code is used to generate a clock pulse of 8ms etc (by passing n=8 to delayMs from main program).
Also is there any simpler way to generate a generic clock pulse signal of required on/off time duration?
Looking forward to any answers.
Thanks and Regards,
Arvind Gupta
I was going through Mazidi's STM32 book and came across this code:
Code:
#include "stm32fxx.h"
void delayMs(int n);
int main(void){
RCC -> AHB1ENR |= 1;
GPIOA -> MODER &= ~0x00000C00;
GPIOA -> MODER |= 0x00000400;
while(1) {
GPIO -> ODR |= 0X00000020;
delayMs(500);
GPIO -> ODR &= ~0X00000020;
delayMs(500);
}
}
[B]void delayMs(int n) {
int i;
for(; n>0; n--)
for (i=0; i<3195; i++);[/B]
}
This is basically a code to blink an LED with .5 sec on /off period. The code in bold letters seems to be a clock generator. Depending on the value passed to delayMs routine by the main program (here it is 500) the clock pulse gets generated. in this case, it seems to generate a on/off pulse of 1 sec time period with 50 % duty cycle.
I am unable to understand how this clock pulse generation code works. How has the upper limit of i (3195) been calculated? As per the other examples in the book, the same code is used to generate a clock pulse of 8ms etc (by passing n=8 to delayMs from main program).
Also is there any simpler way to generate a generic clock pulse signal of required on/off time duration?
Looking forward to any answers.
Thanks and Regards,
Arvind Gupta