Go Down

Topic: ATTINY Beacon (Read 1 time) previous topic - next topic

armadillo

Dec 06, 2012, 02:33 pm Last Edit: Dec 06, 2012, 05:37 pm by armadillo Reason: 1
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: [Select]
// ### 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);
 }
}

Erni

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

armadillo

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

Erni

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.

armadillo

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.

Coding Badly

#5
Dec 07, 2012, 12:30 am Last Edit: Dec 07, 2012, 12:33 am by Coding Badly Reason: 1
Code: [Select]
void broadcast(int N_cycles)
{
  unsigned int portvalue;
...
}


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


Code: [Select]
      PORTB=portvalue;

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


Code: [Select]
      ASM_INC(portvalue);

...which becomes...

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

...which becomes something like...

Code: [Select]
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: [Select]
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 );
  }
}


armadillo

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

Go Up