ATTINY Beacon

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:

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

// ### 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);
  }
}

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

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

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:

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.

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.

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

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

      PORTB=portvalue;

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

      ASM_INC(portvalue);

...which becomes...

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

...which becomes something like...

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)...

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 );
  }
}

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