Pages: [1]   Go Down
Author Topic: Arduino Timer Question  (Read 639 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys I am using two rf devices to transmit data, the transmitter is attiny85 and receiver is atmega328p, but now I am trying to execute another code while the receiver is reading data when I  do it I get request time out in my c# application.
I am using MANCHESTER library to transmit the number data smiley
Is there a way to use delegate or something to interupt the manchester 1 minute delay and execute the code for turning on my TV ?

Code
Code:
#include <MANCHESTER.h>

#define RxPin 4

void setup()
{
 MANCHESTER.SetRxPin(RxPin); //user sets rx pin default 4
 MANCHESTER.SetTimeOut(1000); //user sets timeout default blocks
 Serial.begin(9600); // Debugging only
}//end of setup

void loop()
{
 unsigned int data = MANCHESTER.Receive();
 Serial.println(data);
}//end of loop


After Serial.println(data);
Code:
char c = Serial.read();
if(c == 'p')
{
//this method just send a special IR impulse in order to control the mp3 player at home
PlayMedia();
}
« Last Edit: November 01, 2012, 03:39:34 pm by mitko » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14013
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not familiar with the manchester library, can you post a link?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not familiar with the manchester library, can you post a link?
Manchester.cpp - http://dox.bg/files/dw?a=14b160bdf3
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a Manchester library (not the one you posted) which uses pin change interrupts and thus keeps going while you do other things.

It is described here:

http://www.gammon.com.au/forum/?id=11506

The library is on that page, a fairly small download, plus example code in the car receiver.

Scroll down to: "Manchester receiving library".
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a Manchester library (not the one you posted) which uses pin change interrupts and thus keeps going while you do other things.

It is described here:

http://www.gammon.com.au/forum/?id=11506

The library is on that page, a fairly small download, plus example code in the car receiver.

Scroll down to: "Manchester receiving library".
Ok I found receiver and send library and I downloaded them both but I have one question is it possibble to upload the transmiter sketch to attiny85 with arduino UNO AS ISP and then upload the receiver to my arduino uno ?
I presume that ManchesterSend is the same as ManchesterTransmiter
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//
//  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 smiley
but when I try for attiny85 I get:
Code:
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
« Last Edit: November 02, 2012, 04:38:03 am by mitko » Logged

Pages: [1]   Go Up
Jump to: