AN123

AN123
U S I N G T H E DAC A S A F U N C T I O N G E N E R A T O R
Relevant Devices
Implementation
The main routine of this program is a command
C8051F020, C8051F021, C8051F022, and
interpreter that sets parameters for the Timer 4
C8051F023.
interrupt service routine (ISR) which manages the
DAC updates. The Timer 4 interrupts occur at a
predetermined rate set at compile time. In the
Introduction
included software example, this value is stored in
This document describes how to implement an the constant <SAMPLE_RATE_DAC>. The
interrupt driven multifunction generator on C8051 Timer 4 ISR updates the DAC and calculates or
devices using the on-chip digital-to-analog con- looks up the next output value based on the waveverter (DAC).
form settings.
This application note applies to the following devices:
Setting up the DAC
Features
•
Four different waveforms expandable to any
periodic function defined in a table.
- Sine Wave (Table Defined)
- Square Wave (Calculated)
- Triangle Wave (Calculated)
- Saw Tooth Wave (Calculated)
•
•
Allows selection of the frequency and amplitude of waveform at run time.
An interactive interface with a PC using the
serial communications port and HyperTerminal
or an equivalent program.
Key Points
•
•
•
Output waveforms have 16-bit frequency resolution using the phase accumulator approach.
The on-chip DAC’s can support waveform generation up to 50 kHz.
By using a 16-bit lookup table with a 12-bit
DAC, error in the amplitude is virtually eliminated.
Any free DAC, referred to as DACn, may be used
to generate waveforms. In this example DACn is
used in left-justified mode with output scheduling
based on Timer 4 overflows. Refer to the data sheet
for specific information on how to set the DACnCN register to specify DACn modes.
When the DAC is configured to left-justified mode,
16-bit data can be written to the 12-bit data register
with no shifting required. In this case, the 4 least
significant bits are ignored.
In this example, DACn updates occur on Timer 4
overflows, meaning writes to DACnH and DACnL
have no immediate effect on the DAC output, but
instead are held until the next Timer 4 overflow.
Another important note is that the internal voltage
reference must be enabled by setting the appropriate bits in the REFnCN register before the DAC
can be used.
Sampling Rate
The sampling rate is configured by initializing the
Timer 4 reload value with the number of SYSCLK
Rev. 1.1 12/03
Copyright © 2003 by Silicon Laboratories
AN123-DS11
AN123
cycles between interrupts. This number is negative from 0 to 65535, and a vertical 2’s complement
because C8051 timers are up-counters and can be amplitude axis ranging from -32768 to 32767.
calculated using the following formula:
All waveforms generated use a 16-bit phase accumulator which keeps track of where the output
( – SYSCLK )
Timer 4 Reload = ---------------------------------------------------------waveform is on the horizontal axis. This phase
SAMPLE_RATE_DAC
accumulator provides a frequency resolution of
1.2 Hz, given a DAC update rate of 80 kHz. Based
The maximum sampling rate allowed by the DAC
on waveform settings, the first stage of Timer 4
is approximately 100 kHz, given by the 10 µs outISR either calculates or looks up the next DAC output settling time. However, use caution when
put level corresponding to the phase accumulator.
selecting the DAC sampling rate because all
The phase accumulator is incremented by the variinstructions in the longest path of the ISR must be
able <phase_add> every time the Timer 4 ISR is
executed before the next Timer 4 interrupt, or the
called. The magnitude of <phase_add> is deteroutput frequency will be affected. For example,
mined by the desired output frequency based on
using a SYSCLK of 22.1 MHz and a DAC update
rate of 80 kHz allows 276 SYSCLK cycles for the this formula:
ISR to finish execution. The main trade-off is
PHASE_PRECISION
phase_add = frequency × ----------------------------------------------------------SAMPLE_RATE_DAC
between the sampling rate and the execution time
of the Timer 4 ISR. One way execution time of the
where PHASE_PRECISION = 65536
ISR can be reduced to achieve a higher sampling
rate is by removing the gain adjustment stage. Also
note that the maximum output frequency is limited
The entries in the lookup table and the results of the
to no more than one half the sampling rate (Nyquist
initial calculations are full-scale values. The second
theorem).
stage of the Timer 4 ISR scales the output level
according to the <amplitude> parameter specified
Waveform Generation
at the command prompt.
Waveform generation occurs entirely in the
The final processing stage converts the scaled
Timer 4 ISR and is implemented in three stages.
2’s complement value to an unsigned unipolar
The 2D playing field, shown in Figure 1, is used to value prior to delivery to the DAC. This is accomdefine one period of any periodic function. It has plished by adding 32768 to the 2’s complement
two 16-bit axes, a horizontal phase axis ranging
32767
64
128
16384
32767
192
255
8-bit table index
65535
16-bit phase axis
0
49152
-32768
Figure 1. One Period of a Table Defined Sine Wave
2
Rev. 1.1
AN123
value. An efficient way to implement this operation Calculated Waveforms
is to XOR the 2’s complement value with 0x8000.
Table Defined Waveforms
As mentioned above, waveform generation consists
of three stages before samples are written to the
DAC. The output of the first stage, which
determines the full scale output value, can either
result from a calculation or a table lookup. A
lookup table can be used if the output is not quickly
or easily calculated. The main trade-off is sampling
speed vs. code size.
Phase Error
Figure 1 shows one period of a sine wave. A
lookup table containing 256 samples of this
waveform is used to approximate a true sine wave.
Keep in mind that the lookup table can
approximate any other periodic waveform. If the
output is set to “sine wave” at the command
prompt, the Timer 4 ISR performs a lookup to
obtain the output, using the eight most significant
bits of the phase accumulator as the table index.
The truncation to 8-bits introduces an error which
can be interpreted as an instantaneous phase error
or a slight error in the waveform amplitude. The
frequency resolution, which is determined by the
16-bit accumulator, is not affected by the truncation
because the error is not accumulated across
multiple samples.
Stage one of the Timer 4 ISR calculates the full
scale output value of the waveform corresponding
to the 16-bit phase accumulator. Since using the
full 16-bit precision of the phase accumulator in the
calculation does not require many clock cycles,
both the amplitude and phase error are less than in
table-defined waveforms.
Square Wave
The algorithm used to calculate the output value of
the square wave is quite simple. As shown in
Figure 2, if the phase accumulator is in the first half
of the cycle, then the output is set to the maximum
value of +32767. Otherwise, the output is set to the
minimum value (-32768). The most significant bit
of the phase accumulator contains enough information to determine the output value of the square
wave.
Triangle Wave
The calculation of a triangle wave involves the
equation of 2 lines with opposite slope. From
Figure 3, the slope is +2 in the first half and -2 in
the second half.
Saw Tooth Wave
Amplitude Error
Amplitude error can be introduced from two
sources, a low resolution amplitude or phase axis.
Since the DAC has a 12-bit output resolution, error
resulting from the amplitude axis can be eliminated
by storing 16-bit values in the lookup table. Amplitude error that results from the phase axis can only
be corrected by increasing the number of entries in
the lookup table. Increasing the number of table
entries will stabilize the instantaneous frequency
by reducing the phase error, at the expense of
increased code size.
Rev. 1.1
3
AN123
The equation of a saw tooth wave is a straight line
with a slope of 1. Figure 4 shows one period of a
full scale saw tooth wave.
32767
0
16384
32767
32767
49152
65535
-32768
0
16384
32767
49152
65535
Figure 3. One period of a calculated
triangle wave
-32768
Figure 2. One period of a calculated
square wave
32767
0
16384
32767
49152
65535
-32768
Figure 4. One period of a calculated
saw tooth wave
4
Rev. 1.1
AN123
Software Example
//----------------------------------------------------------------------------// DAC1_fgen1.c
//----------------------------------------------------------------------------//
// AUTH: BW,FB
// DATE: 2 OCT 01
//
// Target: C8051F02x
// Tool chain: KEIL C51
//
// Description:
//
Example source code which outputs waveforms on DAC1. DAC1’s output is
//
scheduled to update at a rate determined by the constant
//
<SAMPLE_RATE_DAC>, managed and timed by Timer4.
//
//
Implements a 256-entry full-cycle sine table of 16-bit precision. Other
//
waveforms supported are square, triangle, and saw tooth.
//
//
The output frequency is determined by a 16-bit phase adder.
//
At each DAC update cycle, the phase adder value is added to a running
//
phase accumulator, <phase_accumulator>, the upper bits of which are used
//
to access the sine lookup table.
//
//
The program is controlled through UART using HyperTerminal running on a
//
PC. All commands are two characters in length and have optional
//
frequency and amplitude arguments. Note that the amplitude parameter
//
cannot be specified unless the frequency is also specified.
//
//
Command Format:
//
//
XX [frequency] [amplitude]
//
//
where XX denotes the command
//
//
Command List:
//
//
SQ - Square Wave
//
SI - Sine Wave
//
TR - Triangle Wave
//
SA - Saw Tooth Wave
//
OF - Output OFF
//
?? - Help
//----------------------------------------------------------------------------// Includes
//----------------------------------------------------------------------------#include
#include
#include
#include
#include
<c8051f020.h>
<stdio.h>
<string.h>
<ctype.h>
<stdlib.h>
// SFR declarations
//----------------------------------------------------------------------------// 16-bit SFR Definitions for ‘F02x
//-----------------------------------------------------------------------------
Rev. 1.1
5
AN123
sfr16
sfr16
sfr16
sfr16
sfr16
sfr16
sfr16
sfr16
sfr16
sfr16
sfr16
sfr16
DP
TMR3RL
TMR3
ADC0
ADC0GT
ADC0LT
RCAP2
T2
RCAP4
T4
DAC0
DAC1
=
=
=
=
=
=
=
=
=
=
=
=
0x82;
0x92;
0x94;
0xbe;
0xc4;
0xc6;
0xca;
0xcc;
0xe4;
0xf4;
0xd2;
0xd5;
//
//
//
//
//
//
//
//
//
//
//
//
data pointer
Timer3 reload value
Timer3 counter
ADC0 data
ADC0 greater than window
ADC0 less than window
Timer2 capture/reload
Timer2
Timer4 capture/reload
Timer4
DAC0 data
DAC1 data
//----------------------------------------------------------------------------// Function PROTOTYPES
//----------------------------------------------------------------------------void
void
void
void
main (void);
SYSCLK_Init (void);
PORT_Init (void);
UART0_Init (void);
void
void
long
void
Timer4_Init (int counts);
Timer4_ISR (void);
pow(int x, int y);
Print_Command_List(void);
void
void
void
void
void
void
void
Sine(void);
Square(void);
Triangle(void);
Saw(void);
Off(void);
Help(void);
Error(void);
//----------------------------------------------------------------------------// Global CONSTANTS
//----------------------------------------------------------------------------#define SYSCLK
22118400
// SYSCLK frequency in Hz
#define BAUDRATE
9600
// Baud rate of UART in bps
#define SAMPLE_RATE_DAC 80000L
// DAC sampling rate in Hz
#define PHASE_PRECISION 65536
// range of phase accumulator
#define command_length 2
#define command_size 3
// command length is 2 characters
// command size is 3 bytes
typedef struct Command_Table_Type {
char command[command_size];
void (*function_ptr)(void);
}Command_Table_Type;
//
//
//
//
//
typedef enum Waveform {
SQUARE,
// the different possible output
// waveforms
6
when a command is entered, it is
compared to the command field of
of the table. If there is a match
then the the function located at
function_ptr will be executed
Rev. 1.1
AN123
SINE,
TRIANGLE,
SAW,
OFF
}Waveform;
typedef union lng {
long Long;
int Int[2];
} lng;
// access a long variable as two
// 16-bit integer values
//----------------------------------------------------------------------------// Global Variables
//-----------------------------------------------------------------------------
unsigned long frequency = 1000;
// frequency of output in Hz,
// defaults to 1000 Hz
unsigned int phase_add = 1000 * PHASE_PRECISION / SAMPLE_RATE_DAC;
unsigned int amplitude = 100 * 655;
// 655 is a scaling factor
// see the Timer 4 ISR
Waveform output_waveform = OFF;
char input_str[16]= ““;
#define num_commands 6
Command_Table_Type code function_table[num_commands + 1] = {
{“SQ”, Square},
{“SI”, Sine},
{“TR”, Triangle},
{“SA”, Saw},
{“OF”, Off},
{“??”, Help},
{““, Error}
};
// a full cycle, 16-bit, 2’s complement sine wave lookup table
int code SINE_TABLE[256] = {
0x0000,
0x18f8,
0x30fb,
0x471c,
0x5a82,
0x6a6d,
0x7641,
0x7d8a,
0x7fff,
0x7d8a,
0x7641,
0x6a6d,
0x5a82,
0x471c,
0x30fb,
0x0324,
0x1c0b,
0x33de,
0x49b4,
0x5cb4,
0x6c24,
0x776c,
0x7e1d,
0x7ff6,
0x7ce3,
0x7504,
0x68a6,
0x5842,
0x447a,
0x2e11,
0x0647,
0x1f19,
0x36ba,
0x4c3f,
0x5ed7,
0x6dca,
0x7884,
0x7e9d,
0x7fd8,
0x7c29,
0x73b5,
0x66cf,
0x55f5,
0x41ce,
0x2b1f,
0x096a,
0x2223,
0x398c,
0x4ebf,
0x60ec,
0x6f5f,
0x798a,
0x7f09,
0x7fa7,
0x7b5d,
0x7255,
0x64e8,
0x539b,
0x3f17,
0x2826,
0x0c8b,
0x2528,
0x3c56,
0x5133,
0x62f2,
0x70e2,
0x7a7d,
0x7f62,
0x7f62,
0x7a7d,
0x70e2,
0x62f2,
0x5133,
0x3c56,
0x2528,
0x0fab,
0x2826,
0x3f17,
0x539b,
0x64e8,
0x7255,
0x7b5d,
0x7fa7,
0x7f09,
0x798a,
0x6f5f,
0x60ec,
0x4ebf,
0x398c,
0x2223,
Rev. 1.1
0x12c8,
0x2b1f,
0x41ce,
0x55f5,
0x66cf,
0x73b5,
0x7c29,
0x7fd8,
0x7e9d,
0x7884,
0x6dca,
0x5ed7,
0x4c3f,
0x36ba,
0x1f19,
0x15e2,
0x2e11,
0x447a,
0x5842,
0x68a6,
0x7504,
0x7ce3,
0x7ff6,
0x7e1d,
0x776c,
0x6c24,
0x5cb4,
0x49b4,
0x33de,
0x1c0b,
7
AN123
0x18f8,
0x0000,
0xe708,
0xcf05,
0xb8e4,
0xa57e,
0x9593,
0x89bf,
0x8276,
0x8000,
0x8276,
0x89bf,
0x9593,
0xa57e,
0xb8e4,
0xcf05,
0xe708,
0x15e2,
0xfcdc,
0xe3f5,
0xcc22,
0xb64c,
0xa34c,
0x93dc,
0x8894,
0x81e3,
0x800a,
0x831d,
0x8afc,
0x975a,
0xa7be,
0xbb86,
0xd1ef,
0xea1e,
0x12c8,
0xf9b9,
0xe0e7,
0xc946,
0xb3c1,
0xa129,
0x9236,
0x877c,
0x8163,
0x8028,
0x83d7,
0x8c4b,
0x9931,
0xaa0b,
0xbe32,
0xd4e1,
0xed38,
0x0fab,
0xf696,
0xdddd,
0xc674,
0xb141,
0x9f14,
0x90a1,
0x8676,
0x80f7,
0x8059,
0x84a3,
0x8dab,
0x9b18,
0xac65,
0xc0e9,
0xd7da,
0xf055,
0x0c8b,
0xf375,
0xdad8,
0xc3aa,
0xaecd,
0x9d0e,
0x8f1e,
0x8583,
0x809e,
0x809e,
0x8583,
0x8f1e,
0x9d0e,
0xaecd,
0xc3aa,
0xdad8,
0xf375,
0x096a,
0xf055,
0xd7da,
0xc0e9,
0xac65,
0x9b18,
0x8dab,
0x84a3,
0x8059,
0x80f7,
0x8676,
0x90a1,
0x9f14,
0xb141,
0xc674,
0xdddd,
0xf696,
0x0647,
0xed38,
0xd4e1,
0xbe32,
0xaa0b,
0x9931,
0x8c4b,
0x83d7,
0x8028,
0x8163,
0x877c,
0x9236,
0xa129,
0xb3c1,
0xc946,
0xe0e7,
0xf9b9,
0x0324,
0xea1e,
0xd1ef,
0xbb86,
0xa7be,
0x975a,
0x8afc,
0x831d,
0x800a,
0x81e3,
0x8894,
0x93dc,
0xa34c,
0xb64c,
0xcc22,
0xe3f5,
0xfcdc,
};
code char string0[] = “\n\n*** OUTPUT IS NOW A “;
code char string1[] = “\n\n----------------------------------\n\n”;
//----------------------------------------------------------------------------// MAIN Routine
//----------------------------------------------------------------------------void main (void) {
char i;
char* arg_ptr1;
char* arg_ptr2;
// counting variable
// pointers to command line parameters
long temp_frequency;
int temp_amplitude;
// used to hold the values input from the
// keyboard while they are error checked
int printed_amplitude = 100;
// a separate copy of amplitude because
// temp_amplitude is written over
void (*f)(void);
// function pointer used to call the proper
// function from the command table
WDTCN = 0xde;
WDTCN = 0xad;
// Disable watchdog timer
SYSCLK_Init ();
PORT_Init ();
// initializations for wave generation
REF0CN = 0x03;
// enable internal VREF generator
DAC1CN = 0x97;
// enable DAC1 in left-justified mode
Timer4_Init(SYSCLK/SAMPLE_RATE_DAC);
// using Timer4 as update scheduler
// initialize T4 to update DAC1
// after (SYSCLK cycles)/sample have
// passed.
// initialization for command input
8
Rev. 1.1
AN123
UART0_Init ();
EA = 1;
// Enable global interrupts
Print_Command_List();
while(1){
// get user input
printf (“ENTER A COMMAND:>”);
gets(input_str,sizeof(input_str));
// wait for input
input_str[0] = toupper(input_str[0]); // convert the two characters
input_str[1] = toupper(input_str[1]); // in the command to uppercase
// Parse the command
for (i = 0; i < num_commands; i++){
// strncmp() returns 0 if the first two arguments are the same string
// set <i> for the command that matched
if (0 == strncmp(input_str, function_table[i].command, command_length)){
arg_ptr1 = strchr (input_str, ‘ ‘);
arg_ptr1++;
// point to the frequency
arg_ptr2 = strchr(arg_ptr1, ‘ ‘);
arg_ptr2++;
// point to amplitude
temp_frequency = atol(arg_ptr1);
temp_amplitude = atol(arg_ptr2);
// check to make sure entered frequency is valid
if (temp_frequency) {
frequency = temp_frequency;
} else {
printf(“\n** Frequency will not change\n”);
}
// check to make sure entered amplitude is valid
if ((temp_amplitude > 0) && (temp_amplitude <=100)){
// multiply by 655 to be divided by 65535 (16-bit shift) in the
// ISR; this is an optimization to reduce the number of
// instructions executed in the ISR
amplitude = temp_amplitude * 655;
printed_amplitude = temp_amplitude;
} else {
printf(“\n** Amplitude will not change\n”);
}
Rev. 1.1
9
AN123
printf(“\nFREQUENCY: %ld Hz”, frequency);
printf(“\nAMPLITUDE: %d %% of VREF/2”, printed_amplitude);
EA = 0;
// Disable Interrupts to avoid
// contention between the ISR
// and the following code.
// set the frequency
phase_add = frequency * PHASE_PRECISION / SAMPLE_RATE_DAC;
break;
} // end if
}// end for
// call the associated function
f = (void *) function_table[i].function_ptr;
f();
EA = 1;
// re-enable interrupts
} // end while(1)
} // end main
//----------------------------------------------------------------------------// Init Routines
//----------------------------------------------------------------------------//----------------------------------------------------------------------------// SYSCLK_Init
//----------------------------------------------------------------------------//
// This routine initializes the system clock to use a 22.1184MHz crystal
// as its clock source.
//
void SYSCLK_Init (void)
{
int i;
// delay counter
OSCXCN = 0x67;
// start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ;
// Wait for osc. to start up
while (!(OSCXCN & 0x80)) ;
// Wait for crystal osc. to settle
OSCICN = 0x88;
// select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//----------------------------------------------------------------------------// PORT_Init
//----------------------------------------------------------------------------//
// Configure the Crossbar and GPIO ports
10
Rev. 1.1
AN123
//
void PORT_Init (void)
{
XBR0
= 0x04;
XBR1
= 0x00;
XBR2
= 0x40;
P0MDOUT |= 0x01;
}
// Enable UART0
// Enable crossbar and weak pull-up
// Set TX0 pin to push-pull
//----------------------------------------------------------------------------// Timer4_Init
//----------------------------------------------------------------------------// This routine initializes Timer4 in auto-reload mode to generate interrupts
// at intervals specified in <counts>.
//
void Timer4_Init (int counts)
{
T4CON = 0;
// STOP timer; set to auto-reload mode
CKCON |= 0x40;
// T4M = ‘1’; Timer4 counts SYSCLKs
RCAP4 = -counts;
// set reload value
T4 = RCAP4;
EIE2 |= 0x04;
// enable Timer4 interrupts
T4CON |= 0x04;
// start Timer4
}
//----------------------------------------------------------------------------// UART0_Init
//----------------------------------------------------------------------------//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{
SCON0
= 0x50;
// SCON0: mode 1, 8-bit UART, enable RX
TMOD
= 0x20;
// TMOD: timer 1, mode 2, 8-bit reload
TH1
= -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1
= 1;
// start Timer1
CKCON |= 0x10;
// Timer1 uses SYSCLK as time base
PCON |= 0x80;
// SMOD0 = 1
TI0
= 1;
// Indicate TX0 ready
}
//----------------------------------------------------------------------------// Print_Command_List
//----------------------------------------------------------------------------//
// Prints the command list to the standard output.
//
void Print_Command_List (void)
{
printf (“\n\
SQ - Square Wave\n\
SI - Sine Wave\n\
TR - Triangle Wave\n\
SA - Saw Tooth Wave\n\
OF - Output OFF\n\
?? - Help\n\n”);
}
Rev. 1.1
11
AN123
//----------------------------------------------------------------------------// Sine
//----------------------------------------------------------------------------//
// Sets output to a sine wave.
//
void Sine (void)
{
output_waveform = SINE;
// print this message: *** OUTPUT IS NOW A SINE WAVE
printf (“%sSINE WAVE%s”,string0,string1);
Print_Command_List();
}
//----------------------------------------------------------------------------// Square
//----------------------------------------------------------------------------//
// Sets output to a square wave.
//
void Square (void)
{
output_waveform = SQUARE;
// print this message: *** OUTPUT IS NOW A SQUARE WAVE
printf (“%sSQUARE WAVE%s”,string0,string1);
Print_Command_List();
}
//----------------------------------------------------------------------------// Triangle
//----------------------------------------------------------------------------//
// Sets output to a triangle wave.
//
void Triangle (void)
{
output_waveform = TRIANGLE;
// print this message: *** OUTPUT IS NOW A TRIANGLE WAVE
printf (“%sTRIANGLE WAVE%s”,string0,string1);
Print_Command_List();
}
//----------------------------------------------------------------------------// Saw
//----------------------------------------------------------------------------//
// Sets output to a saw tooth wave.
//
void Saw (void)
{
output_waveform = SAW;
// print this message: *** OUTPUT IS NOW A SAW TOOTH WAVE
printf (“%sSAW TOOTH WAVE”,string0,string1);
Print_Command_List();
}
12
Rev. 1.1
AN123
//----------------------------------------------------------------------------// Off
//----------------------------------------------------------------------------//
// Sets output to zero volts DC.
//
void Off (void)
{
printf (“\n\n*** OUTPUT OFF”,string1);
output_waveform = OFF;
Print_Command_List();
}
//----------------------------------------------------------------------------// Help
//----------------------------------------------------------------------------//
// Prints the command list.
//
void Help (void)
{
Print_Command_List();
}
//----------------------------------------------------------------------------// Error
//----------------------------------------------------------------------------//
// Indicates that an invalid command was entered at the command prompt.
//
void Error(void)
{
printf (“
***INVALID INPUT = %s\n”, input_str);
}
//*****************************************************************************
// Interrupt Handlers
//*****************************************************************************
//----------------------------------------------------------------------------// Timer4_ISR -- Wave Generator
//----------------------------------------------------------------------------//
// This ISR is called on Timer4 overflows. Timer4 is set to auto-reload mode
// and is used to schedule the DAC output sample rate in this example.
// Note that the value that is written to DAC1 during this ISR call is
// actually transferred to DAC1 at the next Timer4 overflow.
//
void Timer4_ISR (void) interrupt 16 using 3
{
static unsigned phase_acc = 0;
// holds phase accumulator
int temp1;
// the temporary value that passes
// through 3 stages before being written
// to DAC1
Rev. 1.1
13
AN123
int code *table_ptr;
// pointer to the lookup table
lng temporary_long;
// holds the result of a 16-bit multiply
T4CON &= ~0x80;
// clear T4 overflow flag
table_ptr = SINE_TABLE;
phase_acc += phase_add;
// increment phase accumulator
// set the value of <temp1> to the next output of DAC1 at full-scale
// amplitude; the rails are +32767, -32768
switch (output_waveform){
case SINE:
// read the table value
temp1 = *(table_ptr + (phase_acc >> 8));
break;
case SQUARE:
// if in the first half-period, then high
if ( (phase_acc & 0x8000) == 0 ) {
temp1 = 32767;
} else {
temp1 = -32768;
}
break;
case TRIANGLE:
// in first half-period, then y = mx + b
if ( (phase_acc & 0x8000) == 0 ) {
temp1 = (phase_acc << 1) - 32768;
// else, in the second half of period
} else {
temp1 = -(phase_acc << 1) + 32767;
}
break;
case SAW:
temp1 = phase_acc - 32768;
break;
case OFF:
14
Rev. 1.1
AN123
temp1 = -32768;
break;
default:
while(1);
}
// Adjust the Gain
temporary_long.Long = (long) ((long)temp1 * (long)amplitude);
temp1 = temporary_long.Int[0];
// same as temporary_long >> 16
// Add a DC bias to make the rails 0 to 65535
// Note: the XOR with 0x8000 translates the bipolar quantity into
// a unipolar quantity.
DAC1 = 0x8000 ^ temp1;
}
Rev. 1.1
15
AN123
Contact Information
Silicon Laboratories Inc.
4635 Boston Lane
Austin, TX 78735
Tel: 1+(512) 416-8500
Fax: 1+(512) 416-9669
Toll Free: 1+(877) 444-3032
Email: [email protected]
Internet: www.silabs.com
The information in this document is believed to be accurate in all respects at the time of publication but is subject to change without notice.
Silicon Laboratories assumes no responsibility for errors and omissions, and disclaims responsibility for any consequences resulting from
the use of information included herein. Additionally, Silicon Laboratories assumes no responsibility for the functioning of undescribed features
or parameters. Silicon Laboratories reserves the right to make changes without further notice. Silicon Laboratories makes no warranty, representation or guarantee regarding the suitability of its products for any particular purpose, nor does Silicon Laboratories assume any liability
arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation consequential or incidental damages. Silicon Laboratories products are not designed, intended, or authorized for use in applications intended to
support or sustain life, or for any other application in which the failure of the Silicon Laboratories product could create a situation where personal injury or death may occur. Should Buyer purchase or use Silicon Laboratories products for any such unintended or unauthorized application, Buyer shall indemnify and hold Silicon Laboratories harmless against all claims and damages.
Silicon Laboratories and Silicon Labs are trademarks of Silicon Laboratories Inc.
Other products or brandnames mentioned herein are trademarks or registered trademarks of their respective holders.
16
Rev. 1.1
Similar pages