1 | #include <p33FJ64GS606.h>
|
2 | #include <stdlib.h>
|
3 | #include <delay.h>
|
4 |
|
5 | // Select Internal FRC at POR
|
6 | _FOSCSEL(FNOSC_FRC & IESO_OFF);
|
7 | // Enable Clock Switching and Configure Primary Oscillator in XT mode
|
8 | _FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE);
|
9 |
|
10 | _FWDT(FWDTEN_OFF) //disable the watchdog timer
|
11 | _FICD(JTAGEN_OFF & ICS_PGD1);//disable JTAG, enable debugging on PGx1 pins
|
12 |
|
13 | void configureOscillator(void);
|
14 | void initTimer1(void);
|
15 | void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void);
|
16 | void enableInterrupts(void);
|
17 | void disableInterrupts(void);
|
18 |
|
19 |
|
20 | int main(void) {
|
21 | configureOscillator();
|
22 | initTimer1();
|
23 | enableInterrupts();
|
24 |
|
25 | //outputs to 0
|
26 | _LATB12 = 0;
|
27 | _LATB13 = 0;
|
28 | _LATB14 = 0;
|
29 |
|
30 | // led outputs
|
31 | _TRISB12 = 0;
|
32 | _TRISB13 = 0;
|
33 | _TRISB14 = 0;
|
34 |
|
35 |
|
36 | while (1) {
|
37 |
|
38 | _LATB12 = 1;
|
39 | _LATB13 = 0;
|
40 |
|
41 | //Warteschleife
|
42 | int i = 0;
|
43 | for(i=0; i<30;i++)
|
44 | {
|
45 | int o = 0;
|
46 | for(o=0; o<10000;o++) {}
|
47 |
|
48 | }
|
49 | _LATB12 = 1;
|
50 | _LATB13 = 1;
|
51 |
|
52 | //Warteschleife
|
53 | for(i=0; i<30;i++)
|
54 | {
|
55 | int o = 0;
|
56 | for(o=0; o<10000;o++) {}
|
57 | }
|
58 |
|
59 | }
|
60 | }
|
61 |
|
62 |
|
63 | void configureOscillator(void)
|
64 | {
|
65 | // Configure PLL prescaler, PLL postscaler, PLL divisor
|
66 | PLLFBD=150; // M=152
|
67 | CLKDIVbits.PLLPOST=0; // N2=2
|
68 | CLKDIVbits.PLLPRE=5; // N1=7
|
69 |
|
70 | // Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
|
71 | __builtin_write_OSCCONH(0x01);
|
72 | __builtin_write_OSCCONL(OSCCON | 0x01);
|
73 |
|
74 | // Wait for Clock switch to occur
|
75 | while (OSCCONbits.COSC!= 0b001);
|
76 | // Wait for PLL to lock
|
77 | while (OSCCONbits.LOCK!= 1);
|
78 | return;
|
79 | }
|
80 |
|
81 | void initTimer1(void)
|
82 | {
|
83 | T1CONbits.TON = 0;// Disable Timer
|
84 | T1CONbits.TCS = 0;// Select internal instruction cycle clock
|
85 | T1CONbits.TGATE = 0;// Disable Gated Timer mode
|
86 | T1CONbits.TCKPS = 0b11;// Select 1:256 Prescaler
|
87 | //11 = 1:256 prescale value
|
88 | //10 = 1:64 prescale value
|
89 | //01 = 1:8 prescale value
|
90 | //00 = 1:1 prescale value
|
91 |
|
92 | TMR1 = 0x00; // Clear timer register
|
93 | PR1 = 20000; // Load the period value
|
94 |
|
95 | IPC0bits.T1IP = 1;// Set Timer1 Interrupt Priority Level
|
96 | IFS0bits.T1IF = 0;// Clear Timer1 Interrupt Flag
|
97 | IEC0bits.T1IE = 1;// Enable Timer1 interrupt
|
98 | T1CONbits.TON = 1;// Start Timer
|
99 | return;
|
100 | }
|
101 |
|
102 |
|
103 | void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
|
104 | {
|
105 | /* Clear Timer1 interrupt */
|
106 | IFS0bits.T1IF = 0;
|
107 |
|
108 | LATBbits.LATB14 = ~LATBbits.LATB14;
|
109 | }
|
110 |
|
111 | void enableInterrupts(void)
|
112 | {
|
113 | /* Set CPU IPL to 0, enable level 1-7 interrupts */
|
114 | /* No restoring of previous CPU IPL state performed here */
|
115 | SRbits.IPL = 0;
|
116 | //SRbits.IPL = 0 funktioniert nur in Release .... in Debug wird bei einem Wert von 0 der PIC "halted"
|
117 | //SRbits.IPL = 1 (=IPC0bits.T1IP) funktioniert sowohl im Debug als auch im Release
|
118 | return;
|
119 | }
|
120 |
|
121 |
|
122 | void disableInterrupts(void)
|
123 | {
|
124 | /* Set CPU IPL to 7, disable level 1-7 interrupts */
|
125 | /* No saving of current CPU IPL setting performed here */
|
126 | SRbits.IPL = 7;
|
127 | return;
|
128 | }
|