Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SD.h>
- File dataFile;
- const int chipSelect = 4;
- //Clock is at 2MHz
- // 5000 Hz = 2 000 000 / 400
- long int timer1_period = 200;
- //Work with timer 1 since it's 16 bit
- void timer1_init()//makes timer 1 a 5000Hz counter
- {
- //up to ICR1, PWM mode (mode 14)
- TCCR1B |= (1<<WGM13);
- TCCR1B &= ~(1<<WGM12);
- TCCR1A |= (1<<WGM11);
- TCCR1A |= (1<<WGM10);
- // set ICR1 to get a period of 20 ms
- ICR1=timer1_period;
- // initialize the duty cycle to be 50% why?
- OCR1AH= timer1_period>>8;
- OCR1AL= timer1_period&=255;
- //choose the mode to be set at OCR1A, clear at rollover
- TCCR1A |= (1<<(COM1A1));
- TCCR1A &= ~(1<<(COM1A0));
- TCCR1A |= (1<<(COM1B1));
- TCCR1A &= ~(1<<(COM1B0));
- //set the frequency by modifying the prescaler /8.
- TCCR1B &= ~(1<<(CS12));
- TCCR1B |= (1<<(CS11));
- TCCR1B &= ~(1<<(CS10));
- //enable interrupt
- TIMSK1 |= (1<<(TOIE1));
- }
- const uint8_t BUFFER_SIZE = 512;
- uint8_t BUFFER[2][BUFFER_SIZE];
- int active_buff=0;
- int buffer_index=0;
- const int LED_pin = 13;
- bool led_status = 0;
- long int ledcnt = 0;
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Starting up");
- pinMode(LED_pin, OUTPUT);
- pinMode(10, OUTPUT);
- Serial.println("Starting SD card");
- if(!SD.begin(chipSelect)){
- Serial.println("Initialization failed. Exiting");
- return;
- }
- Serial.println("Finished initializing. Opening file");
- dataFile = SD.open("data.txt", FILE_WRITE);
- if(dataFile)
- {
- Serial.println("Opened successfully");
- }else{
- Serial.println("Error opening file");
- }
- //Initialize ICR
- timer1_init();
- //enable interrupts
- // interrupts();
- sei(); //global interrupts
- }
- void loop()
- {
- //if(write_buffer)
- // save_data(BUFFER[1-active_buffer]);
- if(ledcnt > 5000)
- {
- ledcnt = 0;
- digitalWrite(LED_pin, led_status);
- led_status = 1-led_status;
- }
- }
- //Clunky, but this is TIMER1_OVF
- ISR(TIMER1_OVF_vect)
- {
- //add data to BUFFER[active_buffer]
- //increment buffer_index
- //if(buffer_index > BUFFER_SIZE)
- // buffer_index = 0;
- // active_buffer = 1-active_buffer;
- // write_buffer = true;
- ledcnt++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement