📄 dac.lst
字号:
184 void Timer4_Init (int counts)
185 {
186 1 T4CON = 0; // STOP timer; set to auto-reload mode
187 1 CKCON |= 0x40; // T4M = ‘1’; Timer4 counts SYSCLKs
188 1 RCAP4 = -counts; // set reload value
189 1 T4 = RCAP4;
190 1 EIE2 |= 0x04; // enable Timer4 interrupts
191 1 T4CON |= 0x04; // start Timer4
192 1 }
193 //-----------------------------------------------------------------------------
194 // UART0_Init
195 //-----------------------------------------------------------------------------
196 //
197 // Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
198 //
199 void UART0_Init (void)
200 {
201 1 SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
202 1 TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
203 1 TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
204 1 TR1 = 1; // start Timer1
205 1 CKCON |= 0x10; // Timer1 uses SYSCLK as time base
206 1 PCON |= 0x80; // SMOD0 = 1
207 1 TI0 = 1; // Indicate TX0 ready
208 1 }
209 //-----------------------------------------------------------------------------
210 // Print_Command_List
211 //-----------------------------------------------------------------------------
212 // Prints the command list to the standard output.
213 //
214 void Print_Command_List (void)
215 {
216 1 printf ("\n\
*** WARNING C206 IN LINE 216 OF DAC.C: 'printf': missing function-prototype
*** ERROR C267 IN LINE 216 OF DAC.C: 'printf': requires ANSI-style prototype
217 1 SQ - Square Wave\n\
218 1 SI - Sine Wave\n\
219 1 TR - Triangle Wave\n\
220 1 SA - Saw Tooth Wave\n\
221 1 OF - Output OFF\n\
222 1 ?? - Help\n\n");
223 1 }
224 //-----------------------------------------------------------------------------
225 // Sine
226 //-----------------------------------------------------------------------------
227 //
228 // Sets output to a sine wave.
229 //
230 void Sine (void)
231 {
232 1 output_waveform = SINE;
233 1 // print this message: *** OUTPUT IS NOW A SINE WAVE
234 1 printf ("%sSINE WAVE%s",string0,string1);
235 1 Print_Command_List();
236 1 }
237 //-----------------------------------------------------------------------------
C51 COMPILER V7.05 DAC 08/28/2005 16:58:27 PAGE 5
238 // Square
239 //-----------------------------------------------------------------------------
240 //
241 // Sets output to a square wave.
242 //
243 void Square (void)
244 {
245 1 output_waveform = SQUARE;
246 1 // print this message: *** OUTPUT IS NOW A SQUARE WAVE
247 1 printf ("%sSQUARE WAVE%s",string0,string1);
248 1 Print_Command_List();
249 1 }
250 //-----------------------------------------------------------------------------
251 // Triangle
252 //-----------------------------------------------------------------------------
253 //
254 // Sets output to a triangle wave.
255 //
256 void Triangle (void)
257 {
258 1 output_waveform = TRIANGLE;
259 1 // print this message: *** OUTPUT IS NOW A TRIANGLE WAVE
260 1 printf ("%sTRIANGLE WAVE%s",string0,string1);
261 1 Print_Command_List();
262 1 }
263 //-----------------------------------------------------------------------------
264 // Saw
265 //-----------------------------------------------------------------------------
266 //
267 // Sets output to a saw tooth wave.
268 //
269 void Saw (void)
270 {
271 1 output_waveform = SAW;
272 1 // print this message: *** OUTPUT IS NOW A SAW TOOTH WAVE
273 1 printf ("%sSAW TOOTH WAVE",string0,string1);
274 1 Print_Command_List();
275 1 }
276 //-----------------------------------------------------------------------------
277 // Off
278 //-----------------------------------------------------------------------------
279 //
280 // Sets output to zero volts DC.
281 //
282 void Off (void)
283 {
284 1 printf ("\n\n*** OUTPUT OFF",string1);
285 1 output_waveform = OFF;
286 1 Print_Command_List();
287 1 }
288 //-----------------------------------------------------------------------------
289 // Help
290 //-----------------------------------------------------------------------------
291 //
292 // Prints the command list.
293 //
294 void Help (void)
295 {
296 1 Print_Command_List();
297 1 }
298 //-----------------------------------------------------------------------------
299 // Error
C51 COMPILER V7.05 DAC 08/28/2005 16:58:27 PAGE 6
300 //-----------------------------------------------------------------------------
301 //
302 // Indicates that an invalid command was entered at the command prompt.
303 //
304 void Error(void)
305 {
306 1 printf (" ***INVALID INPUT = %s\n", input_str);
307 1 }
308 //*****************************************************************************
309 // Interrupt Handlers
310 //*****************************************************************************
311 //-----------------------------------------------------------------------------
312 // Timer4_ISR -- Wave Generator
313 //-----------------------------------------------------------------------------
314 //
315 // This ISR is called on Timer4 overflows. Timer4 is set to auto-reload mode
316 // and is used to schedule the DAC output sample rate in this example.
317 // Note that the value that is written to DAC1 during this ISR call is
318 // actually transferred to DAC1 at the next Timer4 overflow.
319 //
320 void Timer4_ISR (void) interrupt 16 using 3
321 {
322 1 static unsigned phase_acc = 0; // holds phase accumulator
323 1 int temp1; // the temporary value that passes
324 1 // through 3 stages before being written
325 1 // to DAC1
326 1 int code *table_ptr; // pointer to the lookup table
327 1 lng temporary_long; // holds the result of a 16-bit multiply
328 1 T4CON &= ~0x80; // clear T4 overflow flag
329 1 table_ptr = SINE_TABLE;
330 1 phase_acc += phase_add; // increment phase accumulator
331 1 // set the value of <temp1> to the next output of DAC1 at full-scale
332 1 // amplitude; the rails are +32767, -32768
333 1 switch (output_waveform){
334 2 case SINE:
335 2 // read the table value
336 2 temp1 = *(table_ptr + (phase_acc >> 8));
337 2 break;
338 2 case SQUARE:
339 2 // if in the first half-period, then high
340 2 if ( (phase_acc & 0x8000) == 0 ) {
341 3 temp1 = 32767;
342 3 } else {
343 3 temp1 = -32768;
344 3 }
345 2 break;
346 2 case TRIANGLE:
347 2 // in first half-period, then y = mx + b
348 2 if ( (phase_acc & 0x8000) == 0 ) {
349 3 temp1 = (phase_acc << 1) - 32768;
350 3 // else, in the second half of period
351 3 } else {
352 3 temp1 = -(phase_acc << 1) + 32767;
353 3 }
354 2 break;
355 2 case SAW:
356 2 temp1 = phase_acc - 32768;
357 2 break;
358 2 case OFF:
359 2 temp1 = -32768;
360 2 break;
361 2 default:
C51 COMPILER V7.05 DAC 08/28/2005 16:58:27 PAGE 7
362 2 while(1);
363 2 }
364 1 // Adjust the Gain
365 1 temporary_long.Long = (long) ((long)temp1 * (long)amplitude);
366 1 temp1 = temporary_long.Int[0]; // same as temporary_long >> 16
367 1 // Add a DC bias to make the rails 0 to 65535
368 1 // Note: the XOR with 0x8000 translates the bipolar quantity into
369 1 // a unipolar quantity.
370 1 DAC1 = 0x8000 ^ temp1;
371 1 }
C51 COMPILATION COMPLETE. 3 WARNING(S), 1 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -