Go Down

Topic: Arduino Timer Question (Read 646 times) previous topic - next topic

Nick Gammon

I presume it is the same protocol. The send library uses Timer 2, you may have to rework it slightly for the Attiny.

mitko

#6
Nov 02, 2012, 10:27 am Last Edit: Nov 02, 2012, 10:38 am by mitko Reason: 1

I presume it is the same protocol. The send library uses Timer 2, you may have to rework it slightly for the Attiny.

so as far as I know from assembler the attiny85 dont have timer2 and I have to use Timer1 , and in order tgo sync it with the atmega328p receive programm I have to use attiny internal 8MHz.
I changed the code this way :
Code: [Select]

//
//  ManchesterSend.cpp
//  
//
//  Created by Nick Gammon on 22/01/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#include <ManchesterSend.h>

namespace ManchesterSend {
 
 // which pin to send on
 byte sendPin;

 // current bits yet to be sent
 volatile unsigned long outData;
 
 // how many bits
 volatile byte bitsToGo;
 
 // the current bit
 byte currentBit;
 
 // have we sent the first of the two pulses?
 boolean sentFirst;


// initialize by setting send pin to output
//   also configure Timer 2 to interrupt us every 500 uS
void begin (const byte pin)
{
 
 sendPin = pin;
 
 digitalWrite (sendPin, HIGH);
 pinMode (sendPin, OUTPUT);
 
 bitsToGo = 0;
 
 // reset Timer 2
 TCCR1A = 0;
 TCCR1B = 0;
 
 // Timer 2 - gives us our 500 uS counting interval
 // 16 MHz clock (62.5 nS per tick) - prescaled by 64
 //  counter increments every 4 uS.
 
 // So we count 125 of them, giving exactly 500 uS (1 mS)
 TCCR1A = _BV (WGM21) ;   // CTC mode
 OCR1A  = 124;            // count up to 125  (zero relative!!!!)
 
 // Timer 2 - interrupt on match
 TIMSK1 = _BV (OCIE1A);   // enable Timer2 Interrupt
 TCNT1 = 0;    
 
 // Reset prescalers
 GTCCR = _BV (PSRASY);        // reset prescaler now
 // start Timer 2
 TCCR1B =   _BV (CS22) ;  // prescaler of 64

} // end of ManchesterSend::begin

// write one byte (blocking)
void write (const byte data)
 {
 
 outData = (((unsigned long) data) << SYNC_BITS) | (1UL << (SYNC_BITS - 1));  
 bitsToGo = SYNC_BITS + 8;
 
 // wait for data to go                            
 while (bitsToGo)
   {}  // block until ISR pulls bits out

 delay (1);  // give last pulse time to go in case it was a LOW
 } // end of ManchesterSend::write

// Timer 2 interrupt here:
 
// a 1 is 01
// a 0 is 10

void isr ()
 {
 
 // if out of bits, set line to high and exit  
 if (bitsToGo == 0)
   {
   digitalWrite (sendPin, HIGH);
   return;
   }  // end of nothing to do
 
 // second pulse is the right way around
 if (sentFirst)
   {
   if (currentBit == 0)
     digitalWrite (sendPin, LOW);        
   else    
     digitalWrite (sendPin, HIGH);      
   
   // read for next one
   sentFirst = false;
   // one less bit
   bitsToGo--;  // that bit has been sent
   return;
   }  // end of done second bit
 
 // get low-order bit
 currentBit = outData & 1;
 // ready for next time
 outData = outData >> 1;
 // send inverse of bit
 if (currentBit == 0)
   digitalWrite (sendPin, HIGH);        
 else    
   digitalWrite (sendPin, LOW);  
 // next time we sent other way around
 sentFirst = true;    
 
 } // end of ManchesterSend::isr

 
} // end namespace ManchesterSend


// Timer 2 compare: call our ISR
ISR (TIMER1_COMPA_vect)
{
 ManchesterSend::isr ();
}  // end of TIMER2_COMPA_vect


Please tell me if I changed correct the code to work with Timer1, the code compile for atmega328 :)
but when I try for attiny85 I get:
Code: [Select]
C:\Users\XaKeRa\Desktop\arduino-0023\libraries\ManchesterSend\ManchesterSend.cpp: In function 'void ManchesterSend::begin(byte)':
C:\Users\XaKeRa\Desktop\arduino-0023\libraries\ManchesterSend\ManchesterSend.cpp:42: error: 'TCCR1A' was not declared in this scope
C:\Users\XaKeRa\Desktop\arduino-0023\libraries\ManchesterSend\ManchesterSend.cpp:43: error: 'TCCR1B' was not declared in this scope
C:\Users\XaKeRa\Desktop\arduino-0023\libraries\ManchesterSend\ManchesterSend.cpp:50: error: 'WGM21' was not declared in this scope
C:\Users\XaKeRa\Desktop\arduino-0023\libraries\ManchesterSend\ManchesterSend.cpp:54: error: 'TIMSK1' was not declared in this scope
C:\Users\XaKeRa\Desktop\arduino-0023\libraries\ManchesterSend\ManchesterSend.cpp:58: error: 'PSRASY' was not declared in this scope
C:\Users\XaKeRa\Desktop\arduino-0023\libraries\ManchesterSend\ManchesterSend.cpp:60: error: 'CS22' was not declared in this scope

Go Up