Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By urbanze
#67554
schufti wrote:ok, so you see that the deviation @71kHz is much lower than @83 kHz, which is not what is expected.

The scope can't show anything about accuracy of frequency but would show irregularities (glitches) caused by background tasks (e.g. wifi)


@Dakota:
Well it is easy with a Arduino Uno or Attiny85, it will be fun that ESP8266 won't make it.

Why? Each µC is built for a certain purpose. Is it same fun that AT don't do wifi?
But you are correct about learning, that is allways good.
BTW: I get lots of fun at my day job ....


yes, have ~400Hz of glitch/floating. but if project don't need accuracy, only some "Hz's", is possible i think. :P
User avatar
By urbanze
#67556
Dakota wrote:Well, the ESP8266 can generate and modulate a TV signal maybe a low frequency AM radio as well.
But you're right again, you can never presume anything. But you have to try.
If you have fun in your daily work, you are a lucky guy. :)
Regards, take care.

Pd. Can I see your code ? I can't generate any wave with OS_TIMER_US ? How do you get a sinus wave with OS_TIMER_US ?


No, signal is square wave, but some capacitor's solve problem if you can. Code:

#include <user_interface.h>

os_timer_t t0;

void setup()
{
pinMode(D3, OUTPUT);

os_timer_setfn(&t0, pwm, NULL);
os_timer_arm_us(&t0, 6, true);//with 6uS, generate 12uS square wave with 50% duty cycle. About 83kHz. With 7uS, get 14uS and 71kHz.
}

void loop()
{

}

void pwm(void*z)
{
GPO ^= 1 << D3;
}
User avatar
By Dakota
#67584 Thanks again for your help.
Could you please check what frequency give this ?
In my radio receiver at 775 khz I can hear a signal modulated :)

Code: Select all
#define FREQUENCY    80   
#include <ESP8266WiFi.h>

extern "C" {
#define USE_US_TIMER 1
#include "user_interface.h"
#define os_timer_arm_us(a, b, c) ets_timer_arm_new(a, b, c, 0)
}

#define Antenna D8

// Generator for 77,5 kHz (DCF-77) by Jonas woerner (c)
#include <Time.h>
byte dcfBits[60] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 };
//int Minuten = 15;
//int Stunden = 12;
const int anzahlMinutenBits = 7;
const int anzahlStundenBits = 6;
const int offsetMinutenBits = 21;
const int offsetStundenBits = 29;
byte MinutenBits[anzahlMinutenBits] = { 0 };
byte StundenBits[anzahlStundenBits] = { 0 };
int parity = 0;
// lowered signal. always on:
const byte pin1  =  11;    // = OC2A. goes to antenna
const byte pin2  =  12;    // set to LOW. goes via  22 ohms to antenna
const byte led1  =  10;
const byte led2  =   8;
const int stdled = 13;

os_timer_t t0;
 


void setup()
{
 
// ------------------begin ESP8266'centric----------------------------------
WiFi.forceSleepBegin();                  // turn off ESP8266 RF
delay(1);                                // give RF section time to shutdown
system_update_cpu_freq(FREQUENCY);
// ------------------end ESP8266'centric------------------------------------


 
  DCF_BITS(15,13);  //set time to 15:13

  pinMode(Antenna,OUTPUT);

os_timer_setfn(&t0, pwm, NULL);

os_timer_arm_us(&t0, 6, true);


 

}

void loop()
{
 
  for (int i = 0;i<58;++i)
  {
    modulate(dcfBits[i]);

    os_timer_arm_us(&t0, 6, true);//with 6uS, generate 12uS square wave with 50% duty cycle. About 83kHz. With 7uS, get 14uS and 71kHz.

    delay(1000);
  }

    delay(1000);
}


void pwm(void*z)
{
GPO ^= 1 << Antenna;
}


void DCF_BITS(int Minuten, int Stunden)
{
 
  //MINUTE\\

    if (Minuten > 39)
  {
    MinutenBits[6] = 1;
    Minuten -= 40;
  }
    if (Minuten > 19)
  {
    MinutenBits[5] = 1;
    Minuten -= 20;
  }
 
    if (Minuten > 9)
  {
    MinutenBits[4] = 1;
    Minuten -= 10;
  }

  for (int i = 0; i < 4; ++i)
  {
    //MinutenBits[i] = (Minuten & (1 << i)) ? true : false;

    if ((Minuten & (1 << i)) > 0)
    {
      MinutenBits[i] = true;
    }
    else
    {
      MinutenBits[i] = false;
    }
  }
  for (int i = 0; i < anzahlMinutenBits; ++i)
      {
        dcfBits[offsetMinutenBits + i] = MinutenBits[i];
      }
  //Stunde\\

    if (Stunden > 19)
  {
    StundenBits[5] = 1;
    Stunden -= 20;
  }
     if (Stunden > 9)
  {
    StundenBits[4] = 1;
    Stunden -= 10;
  }
   

 

   for (int i = 0; i < 4; ++i)
   {
     //MinutenBits[i] = (Minuten & (1 << i)) ? true : false;

     if ((Stunden & (1 << i)) > 0)
     {
       StundenBits[i] = true;
     }
     else
     {
       StundenBits[i] = false;
     }
   }

   for (int i = 0; i < anzahlStundenBits; ++i)
   {
     dcfBits[offsetStundenBits + i] = StundenBits[i];
   }

   /*for (int n = 0; n < 6; ++n)
   {

   }*/
   ////////////////////////////
   {
     parity += dcfBits[21];
     parity += dcfBits[22];
     parity += dcfBits[23];
     parity += dcfBits[24];
     parity += dcfBits[25];
     parity += dcfBits[26];
     parity += dcfBits[27];
     if (parity % 2 == 0)
     {
       dcfBits[28] = 0;
     }
     else
     {
       dcfBits[28] = 1;
     }
     parity = 0;
   }
   ////////////////////////////
   {
     parity += dcfBits[29];
     parity += dcfBits[30];
     parity += dcfBits[31];
     parity += dcfBits[32];
     parity += dcfBits[33];
     parity += dcfBits[34];
     if (parity % 2 == 0)
     {
       dcfBits[35] = 0;
     }
     else
     {
       dcfBits[35] = 1;
     }
     parity = 0;
   }
   /////////////////////////////
   {
     parity += dcfBits[36];
     parity += dcfBits[37];
     parity += dcfBits[38];
     parity += dcfBits[39];
     parity += dcfBits[40];
     parity += dcfBits[41];
     parity += dcfBits[42];
     parity += dcfBits[43];
     parity += dcfBits[44];
     parity += dcfBits[45];
     parity += dcfBits[46];
     parity += dcfBits[47];
     parity += dcfBits[48];
     parity += dcfBits[49];
     parity += dcfBits[50];
     parity += dcfBits[51];
     parity += dcfBits[52];
     parity += dcfBits[53];
     parity += dcfBits[54];
     parity += dcfBits[55];
     parity += dcfBits[56];
     parity += dcfBits[57];
     if (parity % 2 == 0)
     {
       dcfBits[58] = 0;
     }
     else
     {
       dcfBits[58] = 1;
     }
     parity = 0;
   }
}


void modulate(byte b)
{
 
   if (b == 0)
   {
     os_timer_arm_us(&t0, 1, false);
     delay(100);

     
   }
   else
   {
   
     os_timer_arm_us(&t0, 1, false);
     delay(200);

     
   }
 
}
User avatar
By urbanze
#67586 with 12uS (you use this), frequency is about 71kHz. Now, at end of your code, you put 1uS !, it's the "X" of topic. Theoretically, esp does not make above 250kHz, 1uS give 500kHz and doesn't work, but you used "FALSE", this not make a loop with timer, just an "callback", if use true, timer work like an loop. :(

Dakota wrote:Thanks again for your help.
Could you please check what frequency give this ?
In my radio receiver at 775 khz I can hear a signal modulated :)

Code: Select all
#define FREQUENCY    80   
#include <ESP8266WiFi.h>

extern "C" {
#define USE_US_TIMER 1
#include "user_interface.h"
#define os_timer_arm_us(a, b, c) ets_timer_arm_new(a, b, c, 0)
}

#define Antenna D8

// Generator for 77,5 kHz (DCF-77) by Jonas woerner (c)
#include <Time.h>
byte dcfBits[60] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 };
//int Minuten = 15;
//int Stunden = 12;
const int anzahlMinutenBits = 7;
const int anzahlStundenBits = 6;
const int offsetMinutenBits = 21;
const int offsetStundenBits = 29;
byte MinutenBits[anzahlMinutenBits] = { 0 };
byte StundenBits[anzahlStundenBits] = { 0 };
int parity = 0;
// lowered signal. always on:
const byte pin1  =  11;    // = OC2A. goes to antenna
const byte pin2  =  12;    // set to LOW. goes via  22 ohms to antenna
const byte led1  =  10;
const byte led2  =   8;
const int stdled = 13;

os_timer_t t0;
 


void setup()
{
 
// ------------------begin ESP8266'centric----------------------------------
WiFi.forceSleepBegin();                  // turn off ESP8266 RF
delay(1);                                // give RF section time to shutdown
system_update_cpu_freq(FREQUENCY);
// ------------------end ESP8266'centric------------------------------------


 
  DCF_BITS(15,13);  //set time to 15:13

  pinMode(Antenna,OUTPUT);

os_timer_setfn(&t0, pwm, NULL);

os_timer_arm_us(&t0, 6, true);


 

}

void loop()
{
 
  for (int i = 0;i<58;++i)
  {
    modulate(dcfBits[i]);

    os_timer_arm_us(&t0, 6, true);//with 6uS, generate 12uS square wave with 50% duty cycle. About 83kHz. With 7uS, get 14uS and 71kHz.

    delay(1000);
  }

    delay(1000);
}


void pwm(void*z)
{
GPO ^= 1 << Antenna;
}


void DCF_BITS(int Minuten, int Stunden)
{
 
  //MINUTE\\

    if (Minuten > 39)
  {
    MinutenBits[6] = 1;
    Minuten -= 40;
  }
    if (Minuten > 19)
  {
    MinutenBits[5] = 1;
    Minuten -= 20;
  }
 
    if (Minuten > 9)
  {
    MinutenBits[4] = 1;
    Minuten -= 10;
  }

  for (int i = 0; i < 4; ++i)
  {
    //MinutenBits[i] = (Minuten & (1 << i)) ? true : false;

    if ((Minuten & (1 << i)) > 0)
    {
      MinutenBits[i] = true;
    }
    else
    {
      MinutenBits[i] = false;
    }
  }
  for (int i = 0; i < anzahlMinutenBits; ++i)
      {
        dcfBits[offsetMinutenBits + i] = MinutenBits[i];
      }
  //Stunde\\

    if (Stunden > 19)
  {
    StundenBits[5] = 1;
    Stunden -= 20;
  }
     if (Stunden > 9)
  {
    StundenBits[4] = 1;
    Stunden -= 10;
  }
   

 

   for (int i = 0; i < 4; ++i)
   {
     //MinutenBits[i] = (Minuten & (1 << i)) ? true : false;

     if ((Stunden & (1 << i)) > 0)
     {
       StundenBits[i] = true;
     }
     else
     {
       StundenBits[i] = false;
     }
   }

   for (int i = 0; i < anzahlStundenBits; ++i)
   {
     dcfBits[offsetStundenBits + i] = StundenBits[i];
   }

   /*for (int n = 0; n < 6; ++n)
   {

   }*/
   ////////////////////////////
   {
     parity += dcfBits[21];
     parity += dcfBits[22];
     parity += dcfBits[23];
     parity += dcfBits[24];
     parity += dcfBits[25];
     parity += dcfBits[26];
     parity += dcfBits[27];
     if (parity % 2 == 0)
     {
       dcfBits[28] = 0;
     }
     else
     {
       dcfBits[28] = 1;
     }
     parity = 0;
   }
   ////////////////////////////
   {
     parity += dcfBits[29];
     parity += dcfBits[30];
     parity += dcfBits[31];
     parity += dcfBits[32];
     parity += dcfBits[33];
     parity += dcfBits[34];
     if (parity % 2 == 0)
     {
       dcfBits[35] = 0;
     }
     else
     {
       dcfBits[35] = 1;
     }
     parity = 0;
   }
   /////////////////////////////
   {
     parity += dcfBits[36];
     parity += dcfBits[37];
     parity += dcfBits[38];
     parity += dcfBits[39];
     parity += dcfBits[40];
     parity += dcfBits[41];
     parity += dcfBits[42];
     parity += dcfBits[43];
     parity += dcfBits[44];
     parity += dcfBits[45];
     parity += dcfBits[46];
     parity += dcfBits[47];
     parity += dcfBits[48];
     parity += dcfBits[49];
     parity += dcfBits[50];
     parity += dcfBits[51];
     parity += dcfBits[52];
     parity += dcfBits[53];
     parity += dcfBits[54];
     parity += dcfBits[55];
     parity += dcfBits[56];
     parity += dcfBits[57];
     if (parity % 2 == 0)
     {
       dcfBits[58] = 0;
     }
     else
     {
       dcfBits[58] = 1;
     }
     parity = 0;
   }
}


void modulate(byte b)
{
 
   if (b == 0)
   {
     os_timer_arm_us(&t0, 1, false);
     delay(100);

     
   }
   else
   {
   
     os_timer_arm_us(&t0, 1, false);
     delay(200);

     
   }
 
}