A waveform lookup function.
This takes two parameters, one is the value of the WS register, a 3-bit value representing the specific waveform to select. The second parameter is a counter. Since the waveform lookup is 256 entries long and represents ¼ of a wave, the lower 10 bits of the counter are used to represent the 1024 steps in the complete waveform.
The return value is an unsigned short integer, in which the msb is used as a sign-bit. When the msb is 1, the output value represents a negative output.
More notes on this will follow.
There is nothing new in this table, I have simply pulled together various notes already in the field - specifically from:
Olli Niemitalo,
Matthew Gambrell,
Hellwig Geisse,
Jani
Code: Select all
//
// tbl.c is a pre-generated lookup table
//
#include "tbl.c"
/*
/-\
| |
Waveform 1 + + + ABCD
| |
\-/
/-\
| |
Waveform 2 + +---+ ABCX
/-\ /-\
| | |
Waveform 3 + + + ABAB
/+ /+
| | | |
Waveform 4 + +-+ +-- AXAX
^ ^
| | |
Waveform 5 +-+-+---- EEXX
^
| |
Waveform 6 +-+-+---- EFXX
| |
v
+---+
| |
Waveform 7 + + + GGHH
| |
+---+
|\
| \
Waveform 8 + --- + IJKL
\ |
\|
*/
unsigned short int WaveSelect(unsigned char ws, unsigned short int count)
{
unsigned char index = (unsigned char) count;
switch (ws | (count & 0x0300))
{
// rising quarter wave Shape A
case 0x0000:
case 0x0001:
case 0x0002:
case 0x0202:
case 0x0003:
case 0x0203:
return logsinTable[index];
// falling quarter wave Shape B
case 0x0100:
case 0x0101:
case 0x0102:
case 0x0302:
return logsinTable[index ^ 0xFF];
// rising quarter wave -ve Shape C
case 0x0200:
return logsinTable[index] | 0x8000;
// falling quarter wave -ve Shape D
case 0x0300:
return logsinTable[index ^ 0xFF] | 0x8000;
// fast wave +ve Shape E
case 0x0004:
case 0x0005:
case 0x0105:
return logsinTable[(index ^ ((index & 0x80)?0xFF:0x00)) << 1];
// fast wave -ve Shape F
case 0x0104:
return logsinTable[(index ^ ((index & 0x80)?0xFF:0x00)) << 1] | 0x8000;
// square wave +ve Shape G
case 0x0006:
case 0x0106:
return 0;
// square wave -ve Shape H
case 0x0206:
case 0x0306:
return 0x8000;
// Shape I
case 0x0007:
return index << 3;
// Shape J
case 0x0107:
return index << 3 | 0x800;
// Shape K
case 0x0207:
return (index ^ 0xFF) << 3 | 0x8800;
// Shape L
case 0x0307:
return (index ^ 0xFF) << 3 | 0x8000;
}
// Shape X
return 0x0C00;
}