Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <SD.h>
  2. File dataFile;
  3. const int chipSelect = 4;
  4.  
  5. //Clock is at 2MHz
  6. // 5000 Hz = 2 000 000 / 400
  7. long int timer1_period = 200;
  8. //Work with timer 1 since it's 16 bit
  9.  
  10. void timer1_init()//makes timer 1 a 5000Hz counter
  11. {
  12. //up to ICR1, PWM mode (mode 14)
  13. TCCR1B |= (1<<WGM13);
  14. TCCR1B &= ~(1<<WGM12);
  15. TCCR1A |= (1<<WGM11);
  16. TCCR1A |= (1<<WGM10);
  17.  
  18. // set ICR1 to get a period of 20 ms
  19. ICR1=timer1_period;
  20.  
  21. // initialize the duty cycle to be 50% why?
  22. OCR1AH= timer1_period>>8;
  23. OCR1AL= timer1_period&=255;
  24.  
  25. //choose the mode to be set at OCR1A, clear at rollover
  26. TCCR1A |= (1<<(COM1A1));
  27. TCCR1A &= ~(1<<(COM1A0));
  28. TCCR1A |= (1<<(COM1B1));
  29. TCCR1A &= ~(1<<(COM1B0));
  30.  
  31.  
  32. //set the frequency by modifying the prescaler /8.
  33. TCCR1B &= ~(1<<(CS12));
  34. TCCR1B |= (1<<(CS11));
  35. TCCR1B &= ~(1<<(CS10));
  36.  
  37. //enable interrupt
  38. TIMSK1 |= (1<<(TOIE1));
  39.  
  40. }
  41.  
  42.  
  43. const uint8_t BUFFER_SIZE = 512;
  44. uint8_t BUFFER[2][BUFFER_SIZE];
  45. int active_buff=0;
  46. int buffer_index=0;
  47.  
  48.  
  49.  
  50.  
  51.  
  52. const int LED_pin = 13;
  53. bool led_status = 0;
  54. long int ledcnt = 0;
  55. void setup()
  56. {
  57.  
  58. Serial.begin(9600);
  59.  
  60. Serial.println("Starting up");
  61.  
  62. pinMode(LED_pin, OUTPUT);
  63. pinMode(10, OUTPUT);
  64.  
  65. Serial.println("Starting SD card");
  66.  
  67. if(!SD.begin(chipSelect)){
  68. Serial.println("Initialization failed. Exiting");
  69. return;
  70. }
  71.  
  72. Serial.println("Finished initializing. Opening file");
  73.  
  74. dataFile = SD.open("data.txt", FILE_WRITE);
  75.  
  76. if(dataFile)
  77. {
  78. Serial.println("Opened successfully");
  79. }else{
  80. Serial.println("Error opening file");
  81. }
  82.  
  83.  
  84. //Initialize ICR
  85. timer1_init();
  86.  
  87. //enable interrupts
  88. // interrupts();
  89. sei(); //global interrupts
  90.  
  91.  
  92.  
  93.  
  94. }
  95.  
  96. void loop()
  97. {
  98.  
  99.  
  100. //if(write_buffer)
  101. // save_data(BUFFER[1-active_buffer]);
  102.  
  103. if(ledcnt > 5000)
  104. {
  105. ledcnt = 0;
  106. digitalWrite(LED_pin, led_status);
  107. led_status = 1-led_status;
  108. }
  109. }
  110.  
  111.  
  112. //Clunky, but this is TIMER1_OVF
  113. ISR(TIMER1_OVF_vect)
  114. {
  115. //add data to BUFFER[active_buffer]
  116. //increment buffer_index
  117. //if(buffer_index > BUFFER_SIZE)
  118. // buffer_index = 0;
  119. // active_buffer = 1-active_buffer;
  120. // write_buffer = true;
  121.  
  122. ledcnt++;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement