BASIC compiler/interpreter for PIC32MX/MZ-80K (suspended)
リビジョン | 245 (tree) |
---|---|
日時 | 2018-06-28 09:31:36 |
作者 | kmorimatsu |
Debug PWM
@@ -151,19 +151,26 @@ | ||
151 | 151 | case EXTRA_ANALOG: |
152 | 152 | return lib_analog(v0); |
153 | 153 | case EXTRA_PWM: |
154 | - // Timer 3 will be used. Music must be stopped. | |
155 | - // Use OC3/OC4. Therefore there can be two PWM outputs. | |
156 | - // OC3: RD10, RC14; OC4: RD11, RE5, RC13 | |
157 | - // PWM x[,y[,z]]; where x is duty %, y is timer speed (Hz), and z is either OC3/OC4 | |
158 | 154 | lib_pwm(g_libparams[1],g_libparams[2],v0); |
159 | 155 | return v0; |
156 | + case EXTRA_SERIAL: | |
157 | + // SERIAL x[,y[,z]] | |
158 | + // where x is baud rate. If zero, stop using it. | |
159 | + // y is parity setting; 0: 8 bit no parity, 1: 8 bit even parity, | |
160 | + // 2: 8 bit odd parity, 3: 9 bit no parity | |
161 | + // z is input buffer size. If z=0 (default), the size will be calculated automatically. | |
162 | + // Calculation is: z=BAUD/10/60/2*3 (z=BAUD/400), which is 1.5 times more size required for 1/60 sec. | |
160 | 163 | case EXTRA_SERIALOUT: |
161 | - // SERIAL x; where x is baud rate. If zero, stop using it. | |
162 | - // SERIALOUT x; where x is 8 bit data | |
164 | + // SERIALOUT x | |
165 | + // where x is 8 bit data. If FIFO buffer is full, wait until ready. | |
163 | 166 | case EXTRA_SERIALIN: |
164 | - // SERIALIN(); | |
167 | + // SERIALIN([x]) | |
168 | + // x=0 (default): return data. If no data remaining, return -1. | |
169 | + // x=1: If data exist(s) return 1, if not, return 0. | |
170 | + case EXTRA_SPI: | |
165 | 171 | case EXTRA_SPIOUT: |
166 | 172 | case EXTRA_SPIIN: |
173 | + case EXTRA_SPIFUNC: | |
167 | 174 | // TODO: Implement IO functions |
168 | 175 | return v0; |
169 | 176 | default: |
@@ -78,7 +78,11 @@ | ||
78 | 78 | EXTRA_PWM =EXTRA_STEP*10, |
79 | 79 | EXTRA_SERIALOUT =EXTRA_STEP*11, |
80 | 80 | EXTRA_SERIALIN =EXTRA_STEP*12, |
81 | - EXTRA_SPIOUT =EXTRA_STEP*13, | |
82 | - EXTRA_SPIIN =EXTRA_STEP*14, | |
81 | + EXTRA_SERIAL =EXTRA_STEP*13, | |
82 | + EXTRA_SERIALFNC =EXTRA_STEP*14, | |
83 | + EXTRA_SPIOUT =EXTRA_STEP*15, | |
84 | + EXTRA_SPIIN =EXTRA_STEP*16, | |
85 | + EXTRA_SPI =EXTRA_STEP*17, | |
86 | + EXTRA_SPIFUNC =EXTRA_STEP*18, | |
83 | 87 | // MAX 63 |
84 | 88 | }; |
@@ -14,7 +14,7 @@ | ||
14 | 14 | |
15 | 15 | void lib_pwm(int duty, int freq, int num){ |
16 | 16 | /* |
17 | - OCxCON=0x800D; | |
17 | + OCxCON=0x000D; | |
18 | 18 | OCxCONbits.ON=0; |
19 | 19 | OCxCONbits.SIDL=0; // Continue operation in Idle mode |
20 | 20 | OCxCONbits.OC32=0; // OCxR<15:0> and OCxRS<15:0> are used for comparisons to the 16-bit timer source |
@@ -38,7 +38,8 @@ | ||
38 | 38 | 1457-95455: 1/1 |
39 | 39 | |
40 | 40 | */ |
41 | - static int prevfreq=0; | |
41 | + static int prevfreq=0, prevPR3=0; | |
42 | + int t; | |
42 | 43 | if (duty==0) { |
43 | 44 | // Continuous output of L signal |
44 | 45 | switch(num){ |
@@ -77,7 +78,7 @@ | ||
77 | 78 | } |
78 | 79 | } |
79 | 80 | // PWM mode |
80 | - if (freq!=prevfreq) { | |
81 | + if (freq!=prevfreq || PR3!=prevPR3) { | |
81 | 82 | // Initialize PWM system |
82 | 83 | OC3CONbits.ON=0; |
83 | 84 | OC4CONbits.ON=0; |
@@ -122,7 +123,18 @@ | ||
122 | 123 | err_invalid_param(); |
123 | 124 | } |
124 | 125 | TMR3=0; |
126 | + prevPR3=PR3; | |
127 | + prevfreq=freq; | |
125 | 128 | } |
129 | + // Wait until Timer3 will reset | |
130 | + if (T3CONbits.ON) { | |
131 | + IFS0bits.T3IF=0; | |
132 | + while (IFS0bits.T3IF==0); | |
133 | + }; | |
134 | + // Stop and reset timer | |
135 | + T3CONCLR=0x8000; | |
136 | + TMR3=PR3-1; | |
137 | + // New PWM setting follows | |
126 | 138 | switch(num){ |
127 | 139 | case 1: |
128 | 140 | // Use OC3/RD10 |
@@ -130,9 +142,10 @@ | ||
130 | 142 | TRISDCLR=1<<10; |
131 | 143 | OC3R=0; |
132 | 144 | OC3RS=(((int)PR3+1)*duty) / 1000; |
133 | - OC3CON=0x800D; | |
134 | - // Start timer (if stopped) and OC3 | |
135 | - T3CONbits.ON=1; | |
145 | + OC3CON=0x000D; | |
146 | + // Start OC3 and timer | |
147 | + OC3CONSET=0x8000; | |
148 | + T3CONSET=0x8000; | |
136 | 149 | break; |
137 | 150 | case 2: |
138 | 151 | // Use OC4/RD11 |
@@ -140,9 +153,15 @@ | ||
140 | 153 | TRISDCLR=1<<11; |
141 | 154 | OC4R=0; |
142 | 155 | OC4RS=(((int)PR3+1)*duty) / 1000; |
143 | - OC4CON=0x800D; | |
144 | - // Start timer (if stopped) and OC4 | |
145 | - T3CONbits.ON=1; | |
156 | + OC4CON=0x000D; | |
157 | + // Wait until Timer3 will reset | |
158 | + if (T3CONbits.ON) { | |
159 | + IFS0bits.T3IF=0; | |
160 | + while (IFS0bits.T3IF==0); | |
161 | + }; | |
162 | + // Start OC4 and timer | |
163 | + OC4CONSET=0x8000; | |
164 | + T3CONSET=0x8000; | |
146 | 165 | break; |
147 | 166 | default: |
148 | 167 | err_invalid_param(); |
@@ -602,6 +602,8 @@ | ||
602 | 602 | |
603 | 603 | } |
604 | 604 | void stop_music(){ |
605 | + // Disable interrupt | |
606 | + IEC0bits.CS0IE=0; | |
605 | 607 | // Stop Timer3 and Timer4 |
606 | 608 | T3CONCLR=0x8000; |
607 | 609 | T4CONCLR=0x8000; |