I want to generate continuous 20MHz pulse using function delay_us() in ccs.
I used PIC18F4580 and the crystal was 10MHz with PLL. So that, I can get the clock is 40MHz.
When I compile the code; there is no error. However when I burn into PIC and test using oscilloscope, there was no pulse exist.
Does it correct the way I'm wrote my code?
Because when Im tried let say for 500Hz or 5kHz the code was successfull.
Or does it because of the PIC cannot support for too high frequency?
This is small portion of code please upload full code so that all register declarations are displayed. Instead use a timer to create pulse that is much accurate method using which you can also get variations in high precision.
Hi,
Please visit this link:https://www.ccsinfo.com/forum/viewtopic.php?t=22467 for Timer tutorial for CCS compiler. I am not much familiar with CCS compiler, so I cannot help you much for coding. To understand pulse generation using timer you need to understand timer operation of PIC and then its easy for coding once you understand it.This is the full code? you need to declare values of registers associated with the port to use it for digital IO as some I/O ports are also multiplexed with ADC, SPI,I2C etc.
Even without delay, you can get 40/(5+2) = ~ 5.7MHz output.
You better use CPLD or FPGA for this purpose.
And delay function requeires unsigned int value.
PIC18 if runs at 40Mhz means it will have 10 MIPS instruction time (40/4) normally.
So if you try the code below without any delay, you will get to know the maximum time you can get from a PIC.
Code:
while (1)
{
output_high(PIN_B1);
output_low(PIN_B1);
}
and delay_us does not work with fractional, it only needs integer value greater than 1
No, instead of change pin state two times, you can just invert it state portb ^= 0xFF; for example.
Faster way is to change state of whole port:
portb = 0x00;
portb = 0xFF;
But you will loose 2 cycles for while loop. So you can just repeat changing state as much as you have a free memory for that. This will give you ~40/2 MHz