| The Datasheet Archive - 100 Million Datasheets from 7500 Manufacturers. |
Relevant Devices This application note applies following devices:
Top Searches for this datasheetAN123 Relevant Devices This application note applies following devices: Implementation main routine this program command C8051F020, C8051F021, C8051F022, interpreter that sets parameters Timer C8051F023. interrupt service routine (ISR) which manages updates. Timer interrupts occur predetermined rate compile time. Introduction included software example, this value stored This document describes implement constant <SAMPLE_RATE_DAC>. interrupt driven multifunction generator C8051 Timer updates calculates devices using on-chip digital-to-analog con- looks next output value based waveverter (DAC). form settings. Features Four different waveforms expandable periodic function defined table. Sine Wave (Table Defined) Square Wave (Calculated) Triangle Wave (Calculated) Tooth Wave (Calculated) Allows selection frequency amplitude waveform time. interactive interface with using serial communications port HyperTerminal equivalent program. Setting free DAC, referred DACn, used generate waveforms. this example DACn used left-justified mode with output scheduling based Timer overflows. Refer data sheet specific information DACnCN register specify DACn modes. When configured left-justified mode, 16-bit data written 12-bit data register with shifting required. this case, least significant bits ignored. this example, DACn updates occur Timer overflows, meaning writes DACnH DACnL have immediate effect output, instead held until next Timer overflow. Another important note that internal voltage reference must enabled setting appropriate bits REFnCN register before used. Points Output waveforms have 16-bit frequency resolution using phase accumulator approach. on-chip DAC's support waveform generation kHz. using 16-bit lookup table with 12-bit DAC, error amplitude virtually eliminated. Sampling Rate sampling rate configured initializing Timer reload value with number SYSCLK Rev. 12/03 Copyright 2003 Silicon Laboratories AN123-DS11 AN123 cycles between interrupts. This number negative from 65535, vertical complement because C8051 timers up-counters amplitude axis ranging from -32768 32767. calculated using following formula: waveforms generated 16-bit phase accumulator which keeps track where output SYSCLK Timer Reload -waveform horizontal axis. This phase SAMPLE_RATE_DAC accumulator provides frequency resolution given update rate kHz. Based maximum sampling rate allowed waveform settings, first stage Timer approximately kHz, given outISR either calculates looks next output settling time. However, caution when level corresponding phase accumulator. selecting sampling rate because phase accumulator incremented variinstructions longest path must able <phase_add> every time Timer executed before next Timer interrupt, called. magnitude <phase_add> deteroutput frequency will affected. example, mined desired output frequency based using SYSCLK 22.1 update rate allows SYSCLK cycles this formula: finish execution. main trade-off PHASE_PRECISIONphase_add frequency -SAMPLE_RATE_DAC between sampling rate execution time Timer ISR. execution time where PHASE_PRECISION 65536 reduced achieve higher sampling rate removing gain adjustment stage. Also note that maximum output frequency limited entries lookup table results more than half sampling rate (Nyquist initial calculations full-scale values. second theorem). stage Timer scales output level according <amplitude> parameter specified Waveform Generation command prompt. Waveform generation occurs entirely final processing stage converts scaled Timer implemented three stages. complement value unsigned unipolar playing field, shown Figure used value prior delivery DAC. This accomdefine period periodic function. plished adding 32768 complement 16-bit axes, horizontal phase axis ranging 32767 16384 8-bit table index 32767 49152 65535 16-bit phase axis -32768 Figure Period Table Defined Sine Wave Rev. AN123 value. efficient implement this operation Calculated Waveforms complement value with 0x8000. Table Defined Waveforms mentioned above, waveform generation consists three stages before samples written DAC. output first stage, which determines full scale output value, either result from calculation table lookup. lookup table used output quickly easily calculated. main trade-off sampling speed code size. Phase Error Figure shows period sine wave. lookup table containing samples this waveform used approximate true sine wave. Keep mind that lookup table approximate other periodic waveform. output "sine wave" command prompt, Timer performs lookup obtain output, using eight most significant bits phase accumulator table index. truncation 8-bits introduces error which interpreted instantaneous phase error slight error waveform amplitude. frequency resolution, which determined 16-bit accumulator, affected truncation because error accumulated across multiple samples. Amplitude Error Amplitude error introduced from sources, resolution amplitude phase axis. Since 12-bit output resolution, error resulting from amplitude axis eliminated storing 16-bit values lookup table. Amplitude error that results from phase axis only corrected increasing number entries lookup table. Increasing number table entries will stabilize instantaneous frequency reducing phase error, expense increased code size. Stage Timer calculates full scale output value waveform corresponding 16-bit phase accumulator. Since using full 16-bit precision phase accumulator calculation does require many clock cycles, both amplitude phase error less than table-defined waveforms. Square Wave algorithm used calculate output value square wave quite simple. shown Figure phase accumulator first half cycle, then output maximum value +32767. Otherwise, output minimum value (-32768). most significant phase accumulator contains enough information determine output value square wave. Triangle Wave calculation triangle wave involves equation lines with opposite slope. From Figure slope first half second half. Tooth Wave Rev. AN123 equation tooth wave straight line with slope Figure shows period full scale tooth wave. 32767 32767 16384 32767 49152 65535 -32768 16384 32767 49152 65535 Figure period calculated triangle wave -32768 Figure period calculated square wave 32767 16384 32767 49152 65535 -32768 Figure period calculated tooth wave Rev. AN123 Software Example //-// DAC1_fgen1.c //-// AUTH: BW,FB DATE: Target: C8051F02x Tool chain: KEIL Description: Example source code which outputs waveforms DAC1. DAC1's output scheduled update rate determined constant <SAMPLE_RATE_DAC>, managed timed Timer4. Implements 256-entry full-cycle sine table 16-bit precision. Other waveforms supported square, triangle, tooth. output frequency determined 16-bit phase adder. each update cycle, phase adder value added running phase accumulator, <phase_accumulator>, upper bits which used access sine lookup table. program controlled through UART using HyperTerminal running commands characters length have optional frequency amplitude arguments. Note that amplitude parameter cannot specified unless frequency also specified. Command Format: [frequency] [amplitude] where denotes command Command List: Square Wave Sine Wave Triangle Wave Tooth Wave Output Help //-// Includes //-#include #include #include #include #include <c8051f020.h> <stdio.h> <string.h> <ctype.h> <stdlib.h> declarations //-// 16-bit Definitions `F02x Rev. AN123 sfr16 sfr16 sfr16 sfr16 sfr16 sfr16 sfr16 sfr16 sfr16 sfr16 sfr16 sfr16 TMR3RL TMR3 ADC0 ADC0GT ADC0LT RCAP2 RCAP4 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 void void long void void void void void void void void main (void); SYSCLK_Init (void); PORT_Init (void); UART0_Init (void); Timer4_Init (int counts); Timer4_ISR (void); pow(int Print_Command_List(void); Sine(void); Square(void); Triangle(void); Saw(void); Off(void); Help(void); Error(void); //-// Global CONSTANTS //-#define SYSCLK #define BAUDRATE 22118400 9600 SYSCLK frequency Baud rate UART sampling rate range phase accumulator command length characters command size bytes when command entered, compared command field table. there match then function located function_ptr will executed #define SAMPLE_RATE_DAC 80000L #define PHASE_PRECISION 65536 #define command_length #define command_size typedef struct Command_Table_Type char command[command_size]; void (*function_ptr)(void); }Command_Table_Type; typedef enum Waveform SQUARE, different possible output waveforms Rev. AN123 SINE, TRIANGLE, SAW, }Waveform; typedef union long Long; Int[2]; lng; access long variable 16-bit integer values //-// Global Variables unsigned long frequency 1000; frequency output defaults 1000 unsigned phase_add 1000 PHASE_PRECISION SAMPLE_RATE_DAC; unsigned amplitude 655; Waveform output_waveform OFF; scaling factor Timer char input_str[16]= #define num_commands Command_Table_Type code function_table[num_commands {"SQ", Square}, {"SI", Sine}, {"TR", Triangle}, {"SA", Saw}, {"OF", Off}, {"??", Help}, {"", Error} full cycle, 16-bit, complement sine wave lookup table 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, 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, Rev. AN123 0x18f8, 0x0000, 0xe708, 0xcf05, 0xb8e4, 0xa57e, 0x9593, 0x89bf, 0x8276, 0x8000, 0x8276, 0x89bf, 0x9593, 0xa57e, 0xb8e4, 0xcf05, 0xe708, code char string0[] "\n\n*** OUTPUT code char string1[] "\n\n-\n\n"; 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, //-// MAIN Routine //-void main (void) char char* arg_ptr1; char* arg_ptr2; long temp_frequency; temp_amplitude; printed_amplitude 100; counting variable pointers command line parameters used hold values input from keyboard while they error checked separate copy amplitude because temp_amplitude written over function pointer used call proper function from command table Disable watchdog timer void (*f)(void); WDTCN 0xde; WDTCN 0xad; SYSCLK_Init PORT_Init initializations wave generation REF0CN 0x03; enable internal VREF generator DAC1CN 0x97; enable DAC1 left-justified mode using Timer4 update scheduler initialize update DAC1 after (SYSCLK cycles)/sample have passed. initialization command input Rev. AN123 UART0_Init Print_Command_List(); while(1){ Enable global interrupts user input printf ("ENTER COMMAND:>"); wait input input_str[0] toupper(input_str[0]); convert characters input_str[1] toupper(input_str[1]); command uppercase Parse command num_commands; i++){ strncmp() returns first arguments same string command that matched strncmp(input_str, function_table[i].command, command_length)){ arg_ptr1 strchr (input_str, arg_ptr1++; point frequency arg_ptr2 strchr(arg_ptr1, arg_ptr2++; point amplitude temp_frequency atol(arg_ptr1); temp_amplitude atol(arg_ptr2); check make sure entered frequency valid (temp_frequency) frequency temp_frequency; else printf("\n** Frequency will change\n"); check make sure entered amplitude valid ((temp_amplitude (temp_amplitude <=100)){ multiply divided 65535 (16-bit shift) ISR; this optimization reduce number instructions executed amplitude temp_amplitude 655; printed_amplitude temp_amplitude; else printf("\n** Amplitude will change\n"); Rev. AN123 printf("\nFREQUENCY: Hz", frequency); printf("\nAMPLITUDE: VREF/2", printed_amplitude); Disable Interrupts avoid contention between following code. frequency phase_add frequency PHASE_PRECISION SAMPLE_RATE_DAC; break; call associated function (void function_table[i].function_ptr; f(); while(1) main //-// Init Routines //-//-// SYSCLK_Init //-// This routine initializes system clock 22.1184MHz crystal clock source. void SYSCLK_Init (void) delay counter OSCXCN 0x67; start external oscillator with 22.1184MHz crystal Wait osc. start Wait crystal osc. settle select external oscillator SYSCLK source enable missing clock detector re-enable interrupts (i=0; 256; i++) while (!(OSCXCN 0x80)) OSCICN 0x88; //-// PORT_Init //-// Configure Crossbar GPIO ports Rev. AN123 void PORT_Init (void) XBR0 0x04; XBR1 0x00; XBR2 0x40; P0MDOUT 0x01; Enable UART0 Enable crossbar weak pull-up push-pull //-// Timer4_Init //-// This routine initializes Timer4 auto-reload mode generate interrupts intervals specified <counts>. void Timer4_Init (int counts) T4CON STOP timer; auto-reload mode CKCON 0x40; `1'; Timer4 counts SYSCLKs RCAP4 -counts; reload value RCAP4; EIE2 0x04; enable Timer4 interrupts T4CON 0x04; start Timer4 //-// UART0_Init //-// Configure UART0 using Timer1, <baudrate> 8-N-1. void UART0_Init (void) SCON0 0x50; SCON0: mode 8-bit UART, enable TMOD 0x20; TMOD: timer mode 8-bit reload -(SYSCLK/BAUDRATE/16); Timer1 reload value baudrate start Timer1 CKCON 0x10; Timer1 uses SYSCLK time base PCON 0x80; SMOD0 Indicate ready //-// Print_Command_List //-// Prints command list standard output. void Print_Command_List (void) printf ("\n\ Square Wave\n\ Sine Wave\n\ Triangle Wave\n\ Tooth Wave\n\ Output OFF\n\ Help\n\n"); Rev. AN123 //-// Sine //-// Sets output sine wave. void Sine (void) output_waveform SINE; print this message: OUTPUT SINE WAVE printf ("%sSINE WAVE%s",string0,string1); Print_Command_List(); //-// Square //-// Sets output square wave. void Square (void) output_waveform SQUARE; print this message: OUTPUT SQUARE WAVE printf ("%sSQUARE WAVE%s",string0,string1); Print_Command_List(); //-// Triangle //-// Sets output triangle wave. void Triangle (void) output_waveform TRIANGLE; print this message: OUTPUT TRIANGLE WAVE printf ("%sTRIANGLE WAVE%s",string0,string1); Print_Command_List(); //-// //-// Sets output tooth wave. void (void) output_waveform SAW; print this message: OUTPUT TOOTH WAVE printf ("%sSAW TOOTH WAVE",string0,string1); Print_Command_List(); Rev. AN123 //-// //-// Sets output zero volts void (void) printf ("\n\n*** OUTPUT OFF",string1); output_waveform OFF; Print_Command_List(); //-// Help //-// Prints command list. void Help (void) Print_Command_List(); //-// Error //-// Indicates that invalid command entered command prompt. void Error(void) printf ***INVALID INPUT %s\n", input_str); Interrupt Handlers //-// Timer4_ISR Wave Generator //-// This called Timer4 overflows. Timer4 auto-reload mode used schedule output sample rate this example. Note that value that written DAC1 during this call actually transferred DAC1 next Timer4 overflow. void Timer4_ISR (void) interrupt using static unsigned phase_acc temp1; holds phase accumulator temporary value that passes through stages before being written DAC1 Rev. AN123 code *table_ptr; temporary_long; T4CON ~0x80; table_ptr SINE_TABLE; phase_acc phase_add; increment phase accumulator pointer lookup table holds result 16-bit multiply clear overflow flag value <temp1> next output DAC1 full-scale amplitude; rails +32767, -32768 switch (output_waveform){ case SINE: read table value temp1 *(table_ptr (phase_acc 8)); break; case SQUARE: first half-period, then high (phase_acc 0x8000) temp1 32767; else temp1 -32768; break; case TRIANGLE: first half-period, then (phase_acc 0x8000) temp1 (phase_acc 32768; else, second half period else temp1 -(phase_acc 32767; break; case SAW: temp1 phase_acc 32768; break; case OFF: Rev. AN123 temp1 -32768; break; default: while(1); Adjust Gain temporary_long.Long (long) ((long)temp1 (long)amplitude); temp1 temporary_long.Int[0]; same temporary_long bias make rails 65535 Note: with 0x8000 translates bipolar quantity into unipolar quantity. DAC1 0x8000 temp1; Rev. AN123 Contact Information Silicon Laboratories Inc. 4635 Boston Lane Austin, 78735 Tel: 1+(512) 416-8500 Fax: 1+(512) 416-9669 Toll Free: 1+(877) 444-3032 Email: productinfo@silabs.com Internet: www.silabs.com information this document believed accurate respects time publication subject change without notice. Silicon Laboratories assumes responsibility errors omissions, disclaims responsibility consequences resulting from information included herein. Additionally, Silicon Laboratories assumes responsibility functioning undescribed features parameters. Silicon Laboratories reserves right make changes without further notice. Silicon Laboratories makes warranty, representation guarantee regarding suitability products particular purpose, does Silicon Laboratories assume liability arising application product circuit, specifically disclaims liability, including without limitation consequential incidental damages. Silicon Laboratories products designed, intended, authorized applications intended support sustain life, other application which failure Silicon Laboratories product could create situation where personal injury death occur. Should Buyer purchase Silicon Laboratories products such unintended unauthorized application, Buyer shall indemnify hold Silicon Laboratories harmless against claims damages. Silicon Laboratories Silicon Labs trademarks Silicon Laboratories Inc. Other products brandnames mentioned herein trademarks registered trademarks their respective holders. Rev. Other recent searchesZRGT-MF - ZRGT-MF ZRGT-MF Datasheet series - series series Datasheet uPD70F3739 - uPD70F3739 uPD70F3739 Datasheet SN74HC32 - SN74HC32 SN74HC32 Datasheet SN54HC32 - SN54HC32 SN54HC32 Datasheet RF2422 - RF2422 RF2422 Datasheet PEB2080 - PEB2080 PEB2080 Datasheet PEB2085 - PEB2085 PEB2085 Datasheet CIT1212EMS-S10 - CIT1212EMS-S10 CIT1212EMS-S10 Datasheet CIT1311S-S20 - CIT1311S-S20 CIT1311S-S20 Datasheet CIT1414EPC-S10 - CIT1414EPC-S10 CIT1414EPC-S10 Datasheet CIT1212EMS-S20 - CIT1212EMS-S20 CIT1212EMS-S20 Datasheet CIT1414EPC-S20-02 - CIT1414EPC-S20-02 CIT1414EPC-S20-02 Datasheet CIT1212EMS-S15 - CIT1212EMS-S15 CIT1212EMS-S15 Datasheet CIT1414EPC-S15 - CIT1414EPC-S15 CIT1414EPC-S15 Datasheet MCP2515 - MCP2515 MCP2515 Datasheet M-78F0714 - M-78F0714 M-78F0714 Datasheet 2SC3182N - 2SC3182N 2SC3182N Datasheet
Privacy Policy | Disclaimer |