K33rg4t3
Full Member level 3

Hey
I have some questions about generating a 10kHz square signal on PIC, take a look at the pseudocode:
as I said, how to calculate the us count to wait while having only 8 bit integers?
10kHz means 0.0001 seconds interval.... (100 us)
all I have figured out so far is using some kind of if condition for every frequency but this is silly because I want it to work for freq = 10, 11, 12, 13 ETC kHz so I need a calculation.
THIS would require a floating point division am I right?
I have some questions about generating a 10kHz square signal on PIC, take a look at the pseudocode:
Code:
void main()
{
int freq = 10; // 10 kHz
while(1)
{
// QUESTION 1: I have only 8 bit integer, how can I calculate it?
int usToWait = 1 / freq; ?? < - how to calculate it
// QUESTION 2: wil it be accurate?
while(usToWait)
{
__delay_us(1);
usToWait--;
}
// do something
}
}
as I said, how to calculate the us count to wait while having only 8 bit integers?
10kHz means 0.0001 seconds interval.... (100 us)
all I have figured out so far is using some kind of if condition for every frequency but this is silly because I want it to work for freq = 10, 11, 12, 13 ETC kHz so I need a calculation.
THIS would require a floating point division am I right?