Sei sulla pagina 1di 2

1. #include <p24fj128ga006.

h>
2. //_FOSCSEL(FNOSC_FRC & IESO_OFF) //PAra una explicacion del
porque de estos macros, refierase a el ejemplo PLL_POSC_C30
3. //_FOSC(FCKSM_CSECMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_XT)
4. #define FCY 40000000
5. #define BAUDRATE 9600
6. #define BRGVAL ((FCY/BAUDRATE)/16)-1
7. unsigned int i;
8. void make_switch(void);
9. int main(void){
10. make_switch();
11. // Configure Oscillator to operate the device at 40Mhz
12. // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
13. // Fosc= 8M*40(2*2)=80Mhz for 8M input clock
14. //PLLFBD=38; // M=40
15. //CLKDIVbits.PLLPOST=0; // N1=2
16. //CLKDIVbits.PLLPRE=0; // N2=2
17. //OSCTUN=0; // Tune FRC oscillator, if FRC is used
18. //RCONbits.SWDTEN=0; // Disable Watch Dog Timer
19. //while(OSCCONbits.LOCK!=1) {}; // Wait for PLL to lock
20. U1MODEbits.STSEL = 0; // 1-stop bit
21. U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
22. U1MODEbits.ABAUD = 0; // Auto-Baud Disabled
23. U1MODEbits.BRGH = 0; // Low Speed mode
24. U1BRG = BRGVAL; // BAUD Rate Setting for 9600
25. U1STAbits.UTXISEL0 = 0; // Interrupt after one Tx character is
transmitted
26. U1STAbits.UTXISEL1 = 0;
27. IEC0bits.U1TXIE = 1; // Enable UART Tx interrupt
28. U1MODEbits.UARTEN = 1; // Enable UART
29. U1STAbits.UTXEN = 1; // Enable UART Tx
30. /* wait at least 104 usec (1/9600) before sending first char */
31. for(i = 0; i < 4160; i++)
32. {
33. Nop();
34. }
35.
36. TRISA = 0;
37. AD1PCFGL = 0xFFFF;
38. LATA = 0xFF;
39. U1TXREG = 'a'; // Transmit one character
40. while(1)
41. {
42. }
43. }
44. void __attribute__((__interrupt__)) _U1TXInterrupt(void)
45. {
46. IFS0bits.U1TXIF = 0; // clear TX interrupt flag
47. TRISA = 0;
48. AD1PCFGL = 0xFFFF;
49. LATA = 0xFF;
50. U1TXREG = 'a'; // Transmit one character
51. }
52. void make_switch(void){
53. CLKDIVbits.PLLPOST = 0; //hasta este punto FVCO es
180MHz, FREF debe estar entre 0.8 y 8MHz, FREF = FIN/N1, N1 =
PLLPRE+2
54. //para nuestro caso FIN = 4MHz,
escogemos N1=2, para esto PLLPRE debe ser igual a 0.
55. CLKDIVbits.PLLPRE = 0; //tenemos FREF=2MHz y
FVCO=180MHz, finalmente M=FVCO/FREF=90, pero PLLDIV = M-2, asi
que PLLDIV = 88
56. PLLFBD = 80;
57. //Modificaremos el registro OSCCON,
58. __builtin_write_OSCCONH(0x03); //Modificamos NOSC,
escogemos la opcion de oscilador primario con PLL, por tanto
NOSC = 011, consecuentemente OSCCONH =3;
59. __builtin_write_OSCCONL(0x01); //El registro OSCCON
contiene el tipo del oscilador actualmente seleccionado, y
tambien contiene bits para seleccionar el
60. //nuevo oscilador que se le
asignara al sistema, el cambio se realiza poniendo en 1 el bit
OSWEN del registro OSCCON
61. while (OSCCONbits.COSC != 0x3); //Esperamos que el
hardware informe que el cambio de oscilador se ha realizado con
exito.
62. while(OSCCONbits.LOCK != 1) {}; //Esperamos a que se
halla cumplido el tiempo de inicio del PLL
}

Potrebbero piacerti anche