yo123
...when i select BRG value =103; then only uart work. how could this possible
4e6/4/9600 - 1 = 103
If Fcy is equal to 4 MHz, then, with BRGH = 1, 103 is the correct value for 9600baud.
So, here's the thing...
You say that Fcy is 8 MHz, but your UART functionality seems to indicate that Fcy is 4 MHz. Well...
If you have a 'scope you can configure the Reference Clock Output to be some value that depends on Fosc, and see if you get what you expect.
However...
If your board has an LED, here is a quick way to determine that Fcy is 8 MHz rather than 4 MHz. Easier than trying to figure out the Reference Clock stuff, and doesn't require a 'scope.
(If your board doesn't have an LED, add one, in series with an appropriate resistor, to one of your output pins. I have found that having an LED is always useful when evaluting new CPUs or new functionality, even if the final project doesn't have an LED.)
Create a project PIC for your PIC. Add a file named, say, Main.c, containing something like the following
// Put configuration bits here
#include <xc.h>
// To use __delay_ms(), you must define FCY before including <libpic30.h>
//
// If your Fcy is actually 8 MHz, __delay_ms(1000) gives 1 second delay.
// If your Fcy is actually 4 MHz, __delay_ms(1000) gives 2 second delay.
// If your Fcy is something else, different delays will be generated.
//
#define FCY 8000000
#include <libpic30.h>
#define LED1 LATAbits.LATA8
#define LED1_TRIS TRISAbits.TRISA8
int main()
{
// If you have any code that affects the clock, put it here
LED1_TRIS = 0;
while (1) {
LED1 = 1;
__delay_ms(1000);
LED1 = 0;
__delay_ms(1000);
} // End of main loop
} // End of main
If your Fosc is 16 MHz (Fcy = 8 MHz), you will see the LED blink one-second-on, one-second-offf.
If your Fosc is 8 MHz (Fcy = 4 MHz), you will see the LED blink two-seconds-on, two-seconds-off.
Bottom line: Verify clock frequency before you implement UART code or any other code that depends on Fosc. The quick and easy way to eye-ball it is to make an LED blink at an easily observable rate. Clock errors are almost always off by a factor of 2 or 4 or more, so verification is easy.
Now, if it turns out that your Fcy is not what you expected, then you have to give us some more information about your circuit, your crystal, etc. For example, you say you have an external
oscillator, but the HS mode in your configuration pragma is for a
crystal. What do you have on your board? Oscillator or Crystal?
You should also tell us which device you are using. Sometimes it makes a difference to people who would like to help.
Finally: Did you mean to leave JTAG enabled? Nothing to do with clock frequency, but that is a common source of problems that may crop up later on, after you get the clock configuration fixed.
Recommendation: Turn it off unless you really need it and have appropriate connections to the JTAG pins so that they won't interfere with normal operation with no external device attached to the JTAG pins.
Regards,
Dave
post edited by davekw7x - 2018/08/11 08:50:12