360 deg sine table

I am looking for a one-bit sine table for 360 deg (one cycle) for direct digitalWrite(output pin);
because frequency is not relevant to the sine table, there must be someone who has figured out the
direct output to a digital pin, based on 360 interrupts per output cycle..

byte sineTable[deg] //0 or 1

trying to build a 60hz inverter.. sinewave..to drive H-bridge..since the calculations are always the same every cycle, i dont see why there needs to be an accumulator and all the math, when its a done deal each degree..either one or zero...every time..
thanks for any help..

backwoodsjack:
[...]when its a done deal each degree..either one or zero...every time..

I don't see how that is possible. That would generate a rectangular wave.

The only way I can think of to get a sine wave with one bit is to use PWM at a much higher frequency then use a 60hz filter on the output to get your sine. Many of the cheap inverters out there use what they call a "modified sine wave" (yeah, right - advertising speak) where they create a couple of steps where it is off for part of the cycle, on for part of the cycle, off for part then on with reverse polarity for part of the cycle. It sort of looks like a sine wave except for the square transitions between states (which drives all those little gadgets that use capacitive reactance to limit the current (like many wall wart chargers))

thats exactly what i have in mind.. 60cps.. but sample 360 times a second.. then just a one pole hi-current filter directly on the output of the H-bridge..
i just need a table 180 deg would be great ..
thanks

A table of what?

digitalWrite(outputPin,sineTable[degree]);

http://interface.khm.de/index.php/lab/interfaces-advanced/arduino-dds-sinewave-generator/

backwoodsjack:
thats exactly what i have in mind.. 60cps.. but sample 360 times a second.. then just a one pole hi-current filter directly on the output of the H-bridge..
i just need a table 180 deg would be great ..
thanks

your one bit table is either 0 or 1; so if you want 360 values, have 180 '0' followed by 180 '1'.

If instead you want a PWM(8bit) value for sine, you only need 90 degrees of values with a quadrant value (0..3)

if you don't have a polarity output you are going to actually have a 7bit PWM signal biased to 1/2 Vcc.

const uint8_t  sineTable[90] PROGMEM=
{  0,  2,  4,  6,  8, 11, 13, 15, 17, 20, 22, 24, 26, 28, 30, 33,
  35, 37, 39, 41, 43, 44, 45, 47, 50, 52, 54, 56, 58, 60, 62, 64,
  65, 67, 69, 71, 73, 75, 77, 78, 80, 82, 83, 85, 87, 88, 90, 92,
  93, 95, 96, 98, 99,100,102,103,104,106,107,108,109,110,111,113,
 114,115,116,116,117,118,119,120,121,121,122,123,123,124,124,125,
 125,126,126,126,127,127,127,127,127,127};

uint8_t pwmSine(uint16_t angle){
angle = angle % 360;  // only handle 0..359
uint8_t quad= angle / 90; // calculate which quadrant (0..3)
angle = angle % 90;   // calculate angle in quadrant (0..89)
switch(quad){
  case 0: return pgm_read_byte_near(sineTable+angle)+128;
    break;
  case 1: return pgm_read_byte_near(sineTable+89-angle)+128;
   break;
  case 2 : return 128-pgm_read_byte_near(sineTable+angle);
  break;
  case 3 : return 128 -pgm_read_byte_near(sineTable+89-angle);
  break;
  default : ;
  }
}

void setup(){
Serial.begin(9600);
for(uint16_t a=0;a<360;a++){
  Serial.println(pwmSine(a),DEC);
	}
}

void loop(){}

Chuck.

thanku charliesixpack.. i previously looked at that link and gained alot of knowledge there..

todd--
the 90 deg 8bit table -- it has to ultimately resolve down to a 1/0 output per sampletime(interupt interval)
this is my whole point 'm trying to resolve.. instead of all the math, why cant i just have a simple one-bit LUT and be done with it..
i'm not sure what im not seeing.. if that makes any sense..
and if wouldnt have to be 360 samples/sec.. could be 1000.. 360 seems nice..

A one bit LUT of what?

digitalWrite(outputPin,sineTable[degree]);

the sine wave function is created from a single 1/0 output pin driving the H-bridge..
why does everyone re-compute the same calculations over and over each cycle..???
i'm about to just write some code and log the results to a file and have a table..
i was hoping someone had already dun that..

backwoodsjack:
thanku charliesixpack.. i previously looked at that link and gained alot of knowledge there..

todd--
the 90 deg 8bit table -- it has to ultimately resolve down to a 1/0 output per sampletime(interupt interval)
this is my whole point 'm trying to resolve.. instead of all the math, why cant i just have a simple one-bit LUT and be done with it..
i'm not sure what im not seeing.. if that makes any sense..
and if wouldnt have to be 360 samples/sec.. could be 1000.. 360 seems nice..

one bit is either on or off.

A sine wave ranges from + amplitude to - amplitude. What do you expect to see from one bit?

if you are emulating PWM then, the value return by my function is the dwell per degree.

PWM is just a digital value (on or off) that has a period. Arduino's analogWrite(pin,pwmvalue);
uses the hardware to do the period. It uses just one pin. the period can be adjusted here PWM Frequency

Chuck.

if you are emulating PWM then, the value return by my function is the dwell per degree.

OK.. but what i am needing is almost the same thing, just rather than dwell per degree, i am wanting to sample -- per degree-- and either output 0 or 1.. for each degree..
more-or-less same thing.. only much simpler and much much faster

backwoodsjack:
why does everyone re-compute the same calculations over and over each cycle..???

Nobody does. The general practice is to use a small LUT as in reply #7.

backwoodsjack:
if you are emulating PWM then, the value return by my function is the dwell per degree.

OK.. but what i am needing is almost the same thing, just rather than dwell per degree, i am wanting to sample -- per degree-- and either output 0 or 1.. for each degree..
more-or-less same thing.. only much simpler and much much faster

Then you will need a much larger LUT, like 360 entries in your example. If you can put it in progmem, ok. But the output from that would only give you a waveform resolution of 360/60 = 6.

resolution would be 360 per cycle.. and repeated 60 cps..

consider this if u would.. take a snapshot 360 times a second of the output pin..
log that to a table..
now-- no need to 'compute' every cycle..
this is what i'm after..

aarg:
Then you will need a much larger LUT, like 360 entries in your example. If you can put it in progmem, ok. But the output from that would only give you a waveform resolution of 360/60 = 6.

@aarg,
I cann't understand what the OP is asking. Can you explain the question?

What is he asking?

Chuck.

Yeah, it's a workable idea, but the sampling has to be very fast. You have PWM at some much higher frequency than 60 Hz. But instead of calculating it every time, you just do it once and store it in a table for playback.

For example, if you want 4 bit PWM, you need to output values at 60*16 Hz. Obviously, to get a cleaner wave, you need to increase that frequency substantially.

exactly

backwoodsjack:
resolution would be 360 per cycle.. and repeated 60 cps..

consider this if u would.. take a snapshot 360 times a second of the output pin..
log that to a table..
now-- no need to 'compute' every cycle..
this is what i'm after..

so, you do not want a sine wave output, you just want to sample a digital pin 360 times a second?

With one bit your possible values are '0' or '1'?

bool sample(uint8_t pin){
unsigned long timeout=micros(),currentMicros;
uint16_t dwell=0,samples;
currentMicros=micros();

while(currentMicros-timeout)<16667){
   if(currentMicros!=micros()){ // sample this microsecond
     currentMicros = micros();
     if (digitalRead(pin)) dwell++;
     samples++;
     }
   }
return (dwell/(samples/2));  // if pin was high for more than 1/2 the number of samples then return High.
}

Chuck?