timer_example1.c

Go to the documentation of this file.
00001 /* ************************************************************************
00002 
00003 
00004 
00005 Copyright (c) 2006, Atmel Corporation All rights reserved. 
00006 
00007 
00008 
00009 Redistribution and use in source and binary forms, with or without
00010 
00011 modification, are permitted provided that the following conditions are met:
00012 
00013 
00014 
00015 1. Redistributions of source code must retain the above copyright notice,
00016 
00017 this list of conditions and the 
00018 
00019 following disclaimer. 
00020 
00021 
00022 
00023 2. Redistributions in binary form must reproduce the above copyright notice,
00024 
00025 this list of conditions and the following disclaimer in the documentation
00026 
00027 and/or other materials provided with the distribution.
00028 
00029 
00030 
00031 3. The name of ATMEL may not be used to endorse or promote products 
00032 
00033 derived from this software without specific prior written permission.  
00034 
00035 
00036 
00037 THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS 
00038 
00039 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00040 
00041 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
00042 
00043 PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY 
00044 
00045 DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00046 
00047 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00048 
00049 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00050 
00051 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00052 
00053 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00054 
00055 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
00056 
00057 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
00058 
00059 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00060 
00061 
00062 
00063 POSSIBILITY OF SUCH DAMAGE. 
00064 
00065 
00066 
00067 ************************************************************************ */
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 #include "timer.h"
00076 
00077 
00078 
00079 #define CPU_HZ 20000000
00080 
00081 
00082 
00083 /* Useful pin behaviour definitions
00084 
00085     Used for compare trigger actions */
00086 
00087 
00088 
00089 #define NOOP    0
00090 
00091 #define SET     1
00092 
00093 #define CLEAR   2
00094 
00095 #define TOGGLE  3
00096 
00097 
00098 
00099 /* Generic definitions */
00100 
00101 #define TRUE    1
00102 
00103 #define FALSE   0
00104 
00105 
00106 
00107 /* PIO MASK */
00108 
00109 #define LED_MASK 0x000000ff
00110 
00111 
00112 
00113 void timer_delay( void );
00114 
00115 
00116 
00117 int main( int argc, char * argv[] )
00118 
00119 {
00120 
00121 
00122 
00123   struct timer_waveform_opt_t waveform_opt;
00124 
00125   volatile struct avr32_pio_t *piob = &AVR32_PIOC;
00126 
00127   volatile struct avr32_pio_t *pioc = &AVR32_PIOC; /* used for leds */
00128 
00129 
00130 
00131   /* The channel number and instance is used in several functions */
00132 
00133   /* It's defined as local variable for ease-of-use causes */
00134 
00135   int channel = 1;
00136 
00137   int timer_instance = 1;
00138 
00139   
00140 
00141   /* Enable Timer module on PIOB */
00142 
00143   piob->pdr = 0x0003ffff;
00144 
00145   piob->asr = 0x0003ffff;
00146 
00147   
00148 
00149   pioc->per = LED_MASK; /* pio enable */
00150 
00151   pioc->oer = LED_MASK; /* output enable */
00152 
00153   pioc->puer = LED_MASK; /* pull up enable */
00154 
00155   pioc->ower = LED_MASK; /* direct writing to output register */
00156 
00157 
00158 
00159   /* Options for Waveform genration */
00160 
00161   waveform_opt.channel = channel;
00162 
00163   waveform_opt.tcclks = TIMER_CLOCK_SOURCE_TC1;
00164 
00165   waveform_opt.clki = FALSE;
00166 
00167   waveform_opt.burst = FALSE;
00168 
00169   
00170 
00171   waveform_opt.cpcstop = FALSE;
00172 
00173   waveform_opt.cpcdis = FALSE;
00174 
00175   waveform_opt.eevtedg = FALSE;
00176 
00177   waveform_opt.eevt = 0;
00178 
00179   waveform_opt.enetrg = FALSE;
00180 
00181   waveform_opt.wavsel = TIMER_WAVEFORM_SEL_UPDOWN_MODE;
00182 
00183   
00184 
00185   waveform_opt.acpa = SET;
00186 
00187   waveform_opt.acpc = CLEAR;
00188 
00189   waveform_opt.aeevt = NOOP;
00190 
00191   waveform_opt.aswtrg = NOOP;
00192 
00193   
00194 
00195   waveform_opt.bcpb = NOOP;
00196 
00197   waveform_opt.bcpc = NOOP;
00198 
00199   waveform_opt.beevt = NOOP;
00200 
00201   waveform_opt.bswtrg = NOOP;
00202 
00203   
00204 
00205   /* Init the Timer */
00206 
00207   timer_init_waveform(timer_instance, &waveform_opt);
00208 
00209   timer_write_ra(timer_instance, channel, 0x0400 );
00210 
00211   timer_write_rb(timer_instance, channel, 0x0800 );
00212 
00213   timer_write_rc(timer_instance, channel, 0xffff );
00214 
00215   timer_start(timer_instance, channel);
00216 
00217 
00218 
00219   while(1){
00220 
00221     /* put the timer status out on the LEDS */
00222 
00223     pioc->odsr = timer_read_tc(timer_instance,channel) >> 8;
00224 
00225     timer_delay();
00226 
00227   }
00228 
00229 }
00230 
00231 
00232 
00233 void timer_delay( void )
00234 
00235 {
00236 
00237   unsigned long i;
00238 
00239   
00240 
00241   for(i=0; i<CPU_HZ/40; i++);
00242 
00243 }
00244 

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