How to get better Clock signal performance


i posted while generating synchronous serial interface.  going build in sw (vs using of hardware components.  wrote quick program based on "blink without delay" used micros() me granularity level needed.  attempting generate simple 9600bps clock (data come later).  setting io directly, bypassing digitalwrite().  seeing on oscope ~7us fluctuation.  gathering due granularity of micros() vs. interval of ~107us 9600.  expected accuracy arduino?  there way improve it? can see video of scope here:

http://youtu.be/buamw8lewb4

code: [select]
// constants won't change. used here to
// set pin numbers:
const int ledpin =  13;      // number of led pin
const int buttonpin = 12;

// variables change:
int ledstate = low;             // ledstate used set led
long previousmicros = 0;        // store last time led updated

// follow variables long because time, measured in miliseconds,
// become bigger number can stored in int.
long microtosec = 1000000;
long baudrate = 9600;
long interval = 49; //(1 * microtosec) /baudrate;           // interval @ blink (milliseconds)
unsigned long currentmicros = 0;
#define pc7 7

void setup() {
  // set digital pin output:
  pinmode(ledpin, output);   
  serial.println(interval); 
}

void loop()
{
  // here you'd put code needs running time.

  // check see if it's time blink led; is, if the
  // difference between current time , last time blinked
  // led bigger interval @ want to
  // blink led.
  currentmicros = micros();

  if(currentmicros - previousmicros > interval) {
    // save last time blinked led
    previousmicros = currentmicros; 

    // if led off turn on , vice-versa:
    if (ledstate == low)
    {
      ledstate = high;
      portc |= 1<<pc7;       // sets output bit 2 high
    }
    else
    {
      ledstate = low;
      portc &= ~(1<<pc7);
    }
    // set led ledstate of variable:
    //digitalwrite(ledpin, ledstate);
  }
}

should
code: [select]
if(currentmicros - previousmicros >= interval)


Arduino Forum > Using Arduino > Project Guidance > How to get better Clock signal performance


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support