Pages: [1]   Go Down
Author Topic: ATTINY Beacon  (Read 1037 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
I love YaBB 1G - SP1!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hia, I've found this old thread where someone under the nick Anymouse mentions having used the attiny as a morse code beacon in the AM band:
http://arduino.cc/forum/index.php/topic,8456.msg68177.html#msg68177

I will very much appreciate to know how complex is to "port" the code in that thread (below) to be used on ATTINY's? I don't think is as simple as changing pin numbers unfortunately.
/a

Code:
// ### INC ### Increment Register (reg = reg + 1)
#define ASM_INC(reg) asm volatile ("inc %0" : "=r" (reg) : "0" (reg))

void setup()  
{

  Serial.begin(9600);
  DDRB=0xFF;  //Port B all outputs
  //Do one dit to determine approximate frequency
  millisAtStart=millis();
  dit();
  millisAtEnd=millis();
  Serial.print(millisAtEnd-millisAtStart);  
  Serial.print(" ");
  Serial.print((length_dit+pause_dit)*period_broadcast*256/(millisAtEnd-millisAtStart)/2);
  Serial.print("kHz ");
  Serial.println();
}

void loop()  
{
  dah();
  dit();
  dah();
  dah();
  pause();
  dah();
  dit();
  dah();
  dah();
  pause();
  dah();
  dah();
  dit();
  dit();
  pause();
  pause();
}

void dit(void)
{
  for(int i=0;i<length_dit;i++)
  {
    broadcast(period_broadcast);
  }
  for(int i=0;i<pause_dit;i++)
  {
    dontbroadcast(period_broadcast);
  }
}


void dah(void)
{
  for(int i=0;i<length_dah;i++)
  {
    broadcast(period_broadcast);
  }
  for(int i=0;i<pause_dah;i++)
  {
    dontbroadcast(period_broadcast);
  }
}

void pause(void)
{
  for(int i=0;i<length_pause;i++)
  {
    dontbroadcast(period_broadcast);
  }
}

void broadcast(int N_cycles)
{
  unsigned int portvalue;
  for (int i=0;i<N_cycles;i++)
  {
    portvalue=0;
    do
    {
      PORTB=portvalue;
      ASM_INC(portvalue);
    }
    while(portvalue<255);
  }
}

void dontbroadcast(int N_cycles)
{
  unsigned int portvalue;
  PORTB=0x00;
  for (int i=0;i<N_cycles;i++)
  {
    portvalue=0;
    do
    {
      ASM_INC(portvalue);
      //add some assembly No OPerations to keep timing the same
      asm volatile ("NOP");  
    }
    while(portvalue<255);
  }
}
« Last Edit: December 06, 2012, 11:37:47 am by armadillo » Logged

Denmark
Offline Offline
Edison Member
*
Karma: 36
Posts: 1084
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I justed tryed the sketch on a Attiny85.
It is now sitting on my desk, and morsing with a Led on pin PB3.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
I love YaBB 1G - SP1!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ola erni,

and if you put a wire on the other out pins and tune a radio to 1337 AM do you hear the code also?

that's the part i'm more interested on knowing how to work out.

tx!
/a
Logged

Denmark
Offline Offline
Edison Member
*
Karma: 36
Posts: 1084
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My Atiiny is running at 8mHz so I would expect an output Freq. of  668kHz, and that is what is shown in the serial monitor:

Quote
196 668kHz

You will have to figure out what pin is the outputpin on the Attiny, I haven't studied the sketch in deep, actually I don't understand the ASM part of it.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
I love YaBB 1G - SP1!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks for checking that out, I hope to receive my ATTINY next week and try it out. Hopefully someone else can chime in and give us a hint on the asm part.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13036
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void broadcast(int N_cycles)
{
  unsigned int portvalue;
...
}

...the variable portvalue is assigned to a pair of registers.


Code:
      PORTB=portvalue;

...the least significant eight bits of portvalue are written to the I/O register for PORTB.


Code:
      ASM_INC(portvalue);

...which becomes...

Code:
asm volatile ("inc %0" : "=r" (portvalue) : "0" (portvalue))

...which becomes something like...

Code:
asm volatile ("inc r24")

...which just increments the value in the register used to store the least significant eight bits of portvalue.


The whole thing is a silly replacement for this (no assembly required)...

Code:
void broadcast( int N_cycles )
{
  uint8_t portvalue;

  for ( int i=0; i < N_cycles; i++ )
  {
    portvalue = 0;

    do
    {
      PORTB = portvalue;

      ++portvalue;
    }
    while( portvalue < 255 );
  }
}
« Last Edit: December 06, 2012, 06:33:08 pm by Coding Badly » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
I love YaBB 1G - SP1!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Coding Badly, that's very generous from your part.
I'm looking fwd to test it, will report back.
Logged

Pages: [1]   Go Up
Jump to: