Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tue Aug 6 11:05:12 NZST 2019. Having no lusjk with interrupts for Timer1
- //..so am trying polling.
- //11:36. waitForSecondPulse(void) works OK. Now going to incorporate that into main serial routine.
- unsigned int big = 60000;
- unsigned int big2;
- int bitNumber=0;
- byte bitLevel, byteResult;
- void setup() {
- TCCR1B = 0x00; //Disable Timer1 while we set it up
- TCCR1A = 0x00; //Timer1 Control Reg A: Normal port operation
- TCNT1 = 0; //Reset Timer Count to 0
- //TIFR1 |= (1<<TOV1); //Make sure TOV1 flag starts off down
- pinMode(8, INPUT);
- pinMode(LED_BUILTIN, OUTPUT); //for flash signals eg LongLeadinTone
- Serial.begin(115200);
- //delay(3000);
- }
- void loop() {
- Serial.println("TopL");
- seekLeadingTone2();
- for (int i =0;i<8;i++) {
- Serial.print(i);
- inputOneBitFrame();
- }
- Serial.println(byteResult);
- Serial.println("GotL");
- }
- void inputOneBitFrame(void) { //wait for long pulse followed by optional short one. Record result in byteResult
- getFirstLowPulse(); //measure time taken and put into "big"
- waitForSecondPulse(); //wait big/2 for middle of second pulse, if any
- testBitLevel();
- recordBitLevel();
- }
- void waitForSecondPulse(void) { //set up timer1 to mark time for half length of big initial low pulse
- Serial.println("T");
- TIFR1 |= (1<<TOV1); //Clear Timer Overflow Flag
- big2 = 0xffff-big/2;
- TCNT1 = big2; // count up for half initial low pulse
- Serial.println(TCNT1);
- TCCR1B |= (1 << CS12)|(1<<CS10); //start timer with /1024 prescale
- //Now wait for Timer1 to roll over ffff->0
- while((TIFR1 & (1<<TOV1))==0); //loop til TOV1 goes high
- //------------------if here TOV1 gone high. Indicates rollover.
- TCCR1B=0; //stop timer 1 (plus kill other flags that don'r matter
- Serial.println("G");
- }
- void getFirstLowPulse(void){ // wait for first part of bit frame to pass Hi-LO-Hi.Tme it if it's first pulse of byte.
- waitForPinToGoLow();
- TCNT1=0;
- startTimer1();
- waitForPinToGoHigh();
- stopTimer1();
- big=TCNT1;
- }
- void testBitLevel(void) { //look at second short pulse. Could be Hi=1 or Low =0
- bitLevel=0;
- if (digitalRead(8)==HIGH) bitLevel=1;
- waitForPinToGoHigh(); //in case i'ts a short low. Have to get ready for nex read of long pulse
- }
- void recordBitLevel(void) { //write the 1 or zero into shifted result to build up byteResult in 8 long pulses (+ possible short ones)
- byteResult =(byteResult<<1) | bitLevel;
- }
- //-----------------------------usual routines for egges from t15 stream....................
- void waitForPinToGoLow(void) { //Pin 8
- while (digitalRead(8) == HIGH) {}
- // Serial.println("pin8 now low...");
- }
- void waitForPinToGoHigh(void) { //Pin 8
- while (digitalRead(8) == LOW) {}
- //Serial.println("pin8 now high...");
- }
- void startTimer1(void) { //101 in CS0..CS2. Gives /1024
- TCCR1B |= (1 << CS12) | (1 << CS10);
- }
- void stopTimer1(void) {
- TCCR1B &= 0B11111000; //CS0:2 = 0
- }
- void seekLeadingTone2(void) {
- digitalWrite(LED_BUILTIN, HIGH);
- startTimer1();
- while(TCNT1<30000){ //10000 to low for /1024
- if (digitalRead(8) == LOW) {
- TCNT1=0;}
- }
- stopTimer1();
- digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
- TCNT1=0;
- //--------------------------end of usual routines for t15 edges----------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment