how do perform bit testing? From what i googled it involved bitread();
bitread() would work. The difficult part would be building your tables, probably.
/* Old code. New code has the vertical columns of bits all encoded into
* just one byte
* const PROGMEM bool state_aT[] = {0, 0, 0, 1, 1, 1, 1, 1, 1,.............};
* const PROGMEM bool state_bT[] = {1, 1, 1, 1, 1, 1, 0, 0, 0, ............};
* const PROGMEM bool state_cT[] = {1, 0, 0, 1, 0, 0, 1, 1, 1, 1...........};
*/
#define ASTATE 0 // a in bit 0
#define BSTATE 1 // b in bit 1
#define CSTART 2 // c in bit 2
// Convenient macro for defining the state bits
#define MAKEABC(A, B, C) ((A<<ASTATR)+(B<<BSTATE)+(C<<CSTATE))
const PROGMEM uint8_t states[] = {
MAKEABC(0,1,1), // 0
MAKEABC(0,1,0),
MAKEABC(0,1,0),
MAKEABC(1,1,1),
MAKEABC(1,1,0), // 4
MAKEABC(1,1,0),
MAKEABC(1,0,1),
MAKEABC(1,0,1),
// :
};
ISR(TIMER1_COMPA_vect) {
uint8_t T;
if (j >= 5760) {
j = 0;
}
T = pgm_read_byte_near(states + j);
if (bitread(T, ASTATE)) {
PORTG |= (1 << switch_a_top);
PORTB &= ~(1 << switch_a_bot);
//Serial.print("hi");
} else {
PORTG &= ~(1 << switch_a_top);
PORTB |= (1 << switch_a_bot);
}
if (bitread(T, BSTATE)) {
PORTH |= (1 << switch_b_top);
PORTB &= ~(1 << switch_b_bot);
} else {
PORTH &= ~(1 << switch_b_top);
PORTB |= (1 << switch_b_bot);
}
// etc
What am am trying is to generate a modified PWM signal that averages as a sine wave.
You really haven't defined exactly what it is you're trying to do very well.
First of all, you seem to be manipulating at least 6 outputs.
Your original picture has weird banding, probably due to the resolution of the plot. It looks mostly like a sine wave, but there are "problems." It's not clear wheter you're varying pulse width, or pulse frequency, or what that gap is in the middle, for example. The code you posted doesn't have any meaningful comments.
I can "guess" that you're driving transistors for three phases of half-H-bridge circuits for a 3-phase motor, and that you want equal width pulses with the duty cycle varying like a sine function, but it's just a guess.
You haven't told us the pulse width, or how accurate you need things, or where 5760 came from.
Grr.