timer_example2.c

Go to the documentation of this file.
00001 #include "timer.h"
00002 /*
00003 
00004 Description
00005 *******************
00006 
00007 This example will start a timer, and generate a pwm on the output
00008 
00009 To get this example running on your STK1000, connect a 10 pin cable from J1 to J15 (LED) and from J2 to J25 (SWITCH). We will use the LEDs as output and the switches as input. When pressing the switch labelled SW1 (connected to Timer/Counter instance 2, channel 0), the RC value for the  corresponding timer will be loaded into the RA register of a Timer/Counter instance 1, channel 1. The SWITCH is connected to a Timer/Counter input, while the LED is connected to a output. When the dip-switch is pressed, it will read a new value for the timer/counter from the RA for the input channel and load this into RA for the output channel.
00010 
00011 The pulse width(while the LED is shining) reflects the RA value captured from the input.
00012 
00013 */
00014 
00015 
00016 
00017 /*      Useful pin behaviour definitions
00018         Used for compare trigger actions */
00019 #define NOOP                    0
00020 #define SET                             1
00021 #define CLEAR                   2
00022 #define TOGGLE                  3
00023 
00024 #define NONE                    0
00025 #define RISING_EDGE             1
00026 #define FALLING_EDGE    2
00027 #define EACH_EDGE               3
00028 
00029 
00030 /*      Generic definitions */
00031 #define TRUE                    1
00032 #define FALSE                   0
00033 
00034 /* Prototypes for initialization for Timer/Counter channels */
00035 /* Added to keep main smaller and more readable */
00036 void init_timer_input( unsigned int instance, unsigned int channel);
00037 void init_timer_output( unsigned int instance, unsigned int channel);
00038 
00039 int main( int argc, char * argv[] )
00040 {
00041 
00042         avr32_pio_t *piob = (void *) AVR32_PIOB_ADDRESS;
00043 
00044 
00045         /* The channel number and instance is used in several functions */
00046         /* It's defined as local variable for ease-of-use and readability */
00047         int output_instance = 1;
00048         int output_channel = 1;
00049 
00050         int input_instance = 2;
00051         int input_channel = 0;
00052 
00053         /* Used for reading the RA value for the input Timer/Counter instance */
00054         int ra=0;
00055 
00056         /* Enable Timer module on PIOB */
00057         piob->pdr = 0x0000ffff;
00058         piob->asr = 0x0000ffff;
00059 
00060         /* Init the Timer */
00061         init_timer_input(input_instance, input_channel);
00062         init_timer_output(output_instance, output_channel);
00063 
00064         /* Set the compare triggers*/
00065         timer_write_ra(output_instance, output_channel, 0x2000 );
00066 
00067         /* Start the timers */
00068         timer_start(output_instance, output_channel);
00069         timer_start(input_instance, input_channel);
00070 
00071         while(1){
00072                 ra = timer_read_ra(input_instance, input_channel);
00073                 if(ra>0){
00074                         timer_write_ra(output_instance, output_channel, ra);
00075                         ra=0;
00076                 }
00077         }
00078         return 0;
00079 }
00080 
00081 
00082 void init_timer_input( unsigned int instance, unsigned int channel)
00083 {
00084         struct timer_capture_opt_t capture_opt;
00085 
00086         /* Options for Capture mode */
00087         capture_opt.channel = channel;
00088         capture_opt.tcclks = TIMER_CLOCK_SOURCE_TC5;
00089         capture_opt.clki = FALSE;
00090         capture_opt.burst = FALSE;
00091         capture_opt.ldbstop = FALSE;
00092         capture_opt.ldbdis = FALSE;
00093         capture_opt.etrgedg = FALLING_EDGE;
00094         capture_opt.abetrg = FALSE; /* Select TIOB */
00095         capture_opt.cpctrg = TRUE;
00096         capture_opt.ldra = FALLING_EDGE;
00097         capture_opt.ldrb = NONE;
00098 
00099         /*Initialize the timer*/
00100         timer_init_capture(instance, &capture_opt);
00101 }
00102 
00103 
00104 void init_timer_output( unsigned int instance, unsigned int channel)
00105 {
00106         struct timer_waveform_opt_t waveform_opt;
00107 
00108         /* Options for Waveform genration */
00109         waveform_opt.channel = channel;
00110         waveform_opt.tcclks = TIMER_CLOCK_SOURCE_TC4;
00111         waveform_opt.clki = FALSE;
00112         waveform_opt.burst = FALSE;
00113         waveform_opt.cpcstop = FALSE;
00114         waveform_opt.cpcdis = FALSE;
00115         waveform_opt.eevtedg = FALSE;
00116         waveform_opt.eevt = 0;
00117         waveform_opt.enetrg = FALSE;
00118         waveform_opt.wavsel = TIMER_WAVEFORM_SEL_UP_MODE;
00119 
00120         waveform_opt.acpa = CLEAR;
00121         waveform_opt.acpc = SET;
00122         waveform_opt.aeevt = NOOP;
00123         waveform_opt.aswtrg = NOOP;
00124 
00125         waveform_opt.bcpb = NOOP;
00126         waveform_opt.bcpc = NOOP;
00127         waveform_opt.beevt = NOOP;
00128         waveform_opt.bswtrg = NOOP;
00129 
00130         /* Initialize the timer */
00131         timer_init_waveform(instance, &waveform_opt);
00132 }

Generated on Fri Oct 13 09:41:12 2006 for AVR32110 Using the AVR32 Timer/Counter by  doxygen 1.4.7