~ubuntu-branches/ubuntu/maverick/avr-libc/maverick

« back to all changes in this revision

Viewing changes to doc/examples/largedemo/largedemo.c

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2007-08-09 11:28:01 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070809112801-ps7wognnynio9kz7
Tags: 1:1.4.6-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 * The brightness of the LED is controlled with the PWM.  A number of
11
11
 * methods are implemented to control that PWM.
12
12
 *
13
 
 * $Id: largedemo.c,v 1.1.2.2 2006/01/03 22:49:46 joerg_wunsch Exp $
 
13
 * $Id: largedemo.c,v 1.1.2.3 2007/01/19 22:20:27 joerg_wunsch Exp $
14
14
 */
15
15
 
16
16
#include <stdint.h>
28
28
#define CONTROL_PORT PORTD
29
29
#define CONTROL_DDR  DDRD
30
30
 
31
 
#define TRIGGER_DOWN PD2
32
 
#define TRIGGER_UP   PD3
33
 
#define TRIGGER_ADC  PD4
34
 
#define CLOCKOUT     PD6
35
 
#define FLASH        PD7
 
31
#if defined(__AVR_ATtiny2313__)
 
32
/* no PD7 and no ADC available on ATtiny2313 */
 
33
#  define TRIGGER_DOWN PD2
 
34
#  define TRIGGER_UP   PD3
 
35
#  define FLASH        PD4
 
36
#  define CLOCKOUT     PD6
 
37
#else
 
38
#  define TRIGGER_DOWN PD2
 
39
#  define TRIGGER_UP   PD3
 
40
#  define TRIGGER_ADC  PD4
 
41
#  define CLOCKOUT     PD6
 
42
#  define FLASH        PD7
 
43
#endif
36
44
 
37
45
#if defined(__AVR_ATmega16__)
38
46
#  define PWMDDR     DDRD
41
49
      defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
42
50
#  define PWMDDR     DDRB
43
51
#  define PWMOUT     PB1
 
52
#elif defined(__AVR_ATtiny2313__)
 
53
#  define PWMDDR     DDRB
 
54
#  define PWMOUT     PB3
 
55
#  define HAVE_ADC   0
 
56
#  define USART_RXC_vect USART_RX_vect
 
57
#  define MCUCSR     MCUSR
44
58
#else
45
59
#  error "Unsupported MCU type"
46
60
#endif
64
78
#  define MCUCSR  MCUSR
65
79
#endif
66
80
 
 
81
#if !defined(HAVE_ADC)
 
82
#  define HAVE_ADC 1
 
83
#endif
 
84
 
67
85
#define F_CPU 1000000UL /* CPU clock in Hertz */
68
86
 
69
87
#define SOFTCLOCK_FREQ 100      /* internal software clock */
142
160
    }
143
161
}
144
162
 
 
163
#if HAVE_ADC
145
164
/*
146
165
 * ADC conversion complete.  Fetch the 10-bit value, and feed the
147
166
 * PWM with it.
152
171
  ADCSRA &= ~_BV(ADIE);         /* disable ADC interrupt */
153
172
  intflags.adc_int = 1;
154
173
}
 
174
#endif /* HAVE_ADC */
155
175
 
156
176
/*
157
177
 * UART receive interrupt.  Fetch the character received and buffer
207
227
  OCR1A = 0;                    /* set PWM value to 0 */
208
228
 
209
229
  /* enable pull-ups for pushbuttons */
 
230
#if HAVE_ADC
210
231
  CONTROL_PORT = _BV(TRIGGER_DOWN) | _BV(TRIGGER_UP) | _BV(TRIGGER_ADC);
 
232
#else
 
233
  CONTROL_PORT = _BV(TRIGGER_DOWN) | _BV(TRIGGER_UP);
 
234
#endif
211
235
 
212
236
  /*
213
237
   * Enable Port D outputs: PD6 for the clock output, PD7 for the LED
229
253
  UCSRB = _BV(TXEN)|_BV(RXEN)|_BV(RXCIE); /* tx/rx enable, rx complete intr */
230
254
  UBRRL = (F_CPU / (8 * 9600UL)) - 1;  /* 9600 Bd */
231
255
 
 
256
#if HAVE_ADC
232
257
  /*
233
258
   * enable ADC, select ADC clock = F_CPU / 8 (i.e. 125 kHz)
234
259
   */
235
260
  ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0);
 
261
#endif
236
262
 
237
263
  TIMSK = _BV(TOIE1);
238
264
  sei();                        /* enable interrupts */
370
396
                  "ATmega88"
371
397
#elif defined(__AVR_ATmega168__)
372
398
                  "ATmega168"
 
399
#elif defined(__AVR_ATtiny2313__)
 
400
                  "ATtiny2313"
373
401
#else
374
402
                  "unknown AVR"
375
403
#endif
424
452
                set_pwm(pwm - 10);
425
453
              else if (bit_is_clear(PIND, TRIGGER_UP))
426
454
                set_pwm(pwm + 10);
 
455
#if HAVE_ADC
427
456
              else if (bit_is_clear(PIND, TRIGGER_ADC))
428
457
                mode = MODE_ADC;
 
458
#endif
429
459
              break;
430
460
 
431
461
            case MODE_ADC:
 
462
#if HAVE_ADC
432
463
              if (bit_is_set(PIND, TRIGGER_ADC))
433
464
                mode = MODE_UPDOWN;
434
465
              else
439
470
                  ADCSRA |= _BV(ADIE);
440
471
                  ADCSRA |= _BV(ADSC);
441
472
                }
 
473
#endif /* HAVE_ADC */
442
474
              break;
443
475
            }
444
476
 
455
487
            }
456
488
        }
457
489
 
 
490
#if HAVE_ADC
458
491
      if (intflags.adc_int)
459
492
        {
460
493
          intflags.adc_int = 0;
461
494
          set_pwm(adcval);
462
495
        }
 
496
#endif /* HAVE_ADC */
463
497
 
464
498
      if (intflags.rx_int)
465
499
        {