Pages: [1]   Go Down
Author Topic: Low Tec signal generator  (Read 744 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
God Member
*****
Karma: 0
Posts: 511
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on a fuel efficiency gauge and needed a way to test on the bench.  This is basically two duinos.  One (the plain chip and crystal) has been programmed in arduino to simulate a vehicle vss and injector signal, and the other is running the mpg gauge code.  The test code is at the bottom.  Now it isn't single digit microsecond accurate, but it also isn't limited by the number of timers either.  With this approach you can generate a lot of different signals, i.e. buttons, etc.  And since both the mpg gauge and the signal generator are connected to the computer simultaneously it is quite possible to have a PC program with all kinds of scenarios that tell the signal generator what to generate and get the result from the gunio to see if it is correct.  Maybe someone will find the techniques useful, the microseconds function is going to break in 0012 though smiley-sad





Code:
//GPL Software
//cheesey signal generator, made for bench testing the mpguino
//signal generator pins
#define sig1Pin 2   //inj
#define sig2Pin 3  

//overflow counter used by millis()  
extern volatile unsigned long timer0_overflow_count;  
unsigned long lastMicroSeconds=millis() * 1000;
unsigned long microSeconds (void){  
  unsigned long tmp_timer0_overflow_count;
  unsigned long tmp;
  byte tmp_tcnt0;
  cli(); //disable interrupts
  tmp_timer0_overflow_count = timer0_overflow_count;
  tmp_tcnt0 = TCNT0;
  sei(); // enable interrupts
  tmp = ((tmp_timer0_overflow_count << 8) + tmp_tcnt0) * 4;  
  if((tmp<=lastMicroSeconds) && (lastMicroSeconds<4290560000ul))
    return microSeconds();  
  lastMicroSeconds=tmp;
  return tmp;  
}


unsigned long elapsedMicroseconds(unsigned long startMicroSeconds, unsigned long msec ){  
  if(msec >= startMicroSeconds)  
    return msec-startMicroSeconds;  
  return 4294967295 - (startMicroSeconds-msec);  
}  


/*
metro signal generator notes:
//#define vssPulsesPerMile 10000ul  
//#define microSecondsPerGallon 267857143ul //injector flow rate  
@20mph, 4th gear
~48mpg
~44000uS injector open time per 1/2 second
18 inj pulses per 1/2 second
55 vss pulses per 1/2 second
therefore:
sig1, off for 2444uS, on for 25333uS
sig2, on for 16000uS, off for 2180uS
*/
unsigned long sig1Start; //injector
byte sig1State = HIGH; //injector 1=closed
unsigned long sig2Start; //vss
byte sig2State = LOW; //vss

#define sig1PinOffTime 2444
#define sig1PinOnTime 25333
#define sig2PinOnTime 16000
#define sig2PinOffTime 2180


void setup (void){    
  pinMode(sig1Pin,OUTPUT);  
  pinMode(sig2Pin,OUTPUT);  
}    

void loop (void){    

  digitalWrite(sig1Pin,sig1State);
  digitalWrite(sig2Pin,sig2State);
  sig1Start = microSeconds();
  sig2Start = sig1Start;
  while(true){
    //snapshot of the time variables:
    unsigned long tmp = microSeconds();  
    unsigned long tmp1 = elapsedMicroseconds(sig1Start, tmp);  
    unsigned long tmp2 = elapsedMicroseconds(sig2Start, tmp);  

    //sig 1 timing logic
    if(sig1State==HIGH){
      if (tmp1>=sig1PinOnTime){
        sig1Start=tmp;
        sig1State=LOW;
        digitalWrite(sig1Pin,sig1State);
      }
    }else{
      if (tmp1>=sig1PinOffTime){
        sig1Start=tmp;
        sig1State=HIGH;
        digitalWrite(sig1Pin,sig1State);
      }
    }  

    //sig 2 timing logic
    if(sig2State==HIGH){
      if (tmp2>=sig2PinOnTime){
        sig2Start=tmp;
        sig2State=LOW;
        digitalWrite(sig2Pin,sig2State);
      }
    }else{
      if (tmp2>=sig2PinOffTime){
        sig2Start=tmp;
        sig2State=HIGH;
        digitalWrite(sig2Pin,sig2State);
      }
    }  

    

     //sig3?  sigX?

  }  
}    
« Last Edit: June 07, 2008, 01:05:53 pm by dcb » Logged

Pages: [1]   Go Up
Jump to: