Guest

carlhako virtual wall

By: a guest on Aug 14th, 2010  |  syntax: C  |  size: 1.22 KB  |  hits: 256  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #define F_CPU 14745600
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8. #include <inttypes.h>
  9.  
  10.  
  11.  
  12. // PIN DEFINITIONS:
  13.  
  14. void realtimeclock_setup() {
  15.   // setup Timer0:
  16.   // CTC (Clear Timer on Compare Match mode)
  17.   // TOP set by OCR0A register
  18.   TCCR0A |= (1<<WGM01);
  19.   // no pre scaler
  20.   TCCR0B |= (1<<CS00);
  21.   // generate 70.217khz which i can use every 2nd cycle to generate aprox 35khz (35108hz)
  22.   OCR0A = 210;
  23.   // enable interrupt on compare event
  24.   TIMSK0 |= (1<<OCIE0A);
  25. }
  26.  
  27. volatile int8_t counter1;
  28. volatile int8_t counter2;
  29.  
  30. SIGNAL(SIG_OUTPUT_COMPARE0A) {
  31.   counter1++;
  32.   counter2++;
  33. }
  34.  
  35. int main() {
  36.   realtimeclock_setup();
  37.  
  38.   // set pin as output
  39.   DDRC |= (1<<PC5);
  40.  
  41.   // turn on interrupt handler
  42.         sei();
  43.         int8_t led_status;
  44.         while(1) {
  45.                 if (counter1 == 2) { // generate 35khz carrier signal
  46.                         if (led_status) PORTC |= (1<<PC5); // turn on ir led
  47.                 }
  48.                 if (counter1 > 3) {
  49.                         PORTC &= ~(1<<PC5); // turn off ir led
  50.                         counter1 = 0; // reset counter1
  51.                 }
  52.                
  53.                 if (counter2 == 34) { // generate 500hz signal
  54.                         led_status = 1;
  55.                         //counter2 = 0;
  56.                 }
  57.                 if (counter2 > 69) {
  58.                         led_status = 0;
  59.                         counter2 = 0;
  60.                 }
  61.                
  62.         }
  63.  
  64.  
  65.   return 0;
  66. }