Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Hi Robert & rescue161:Hi Mike,
I normally use interrupts and as many of the device hardware facilities as possible when working on our products, but as an example for someone just learning C, I tried to keep my example program as straightforward as possible; no interrupts, no special hardware features & no C shortcuts or compound operations.
I know it can be made rather faster and far more code efficient, but at the cost of functional clarity & I thought that was most important.
(The gamma correction in your other article is something I thought it needed, though. It needs more PWM levels ideally to work well with that).
/*********************************************************************
* *
*********************************************************************/
void main()
{ unsigned char step = 0; // PWM step, 0..63
unsigned char index = 0; // image index, 0..119
unsigned char speed = 0; // PWM frames per image step
unsigned char toggle[64] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
ANSELA = 0b00000000; //
TRISA = 0b00000000; //
LATA = 0b00111111; // for active lo LEDs
// LATA = 0b00000000; // for active hi LEDs
while(1) //
{ index = 120; //
while(index--) //
{ speed = 1; // speed, 1..255
/*
* speed = 1 -> 210,136 cycles ( 26.267-ms)
* speed = 10 -> 2,091,736 cycles ( 261.467-ms)
* speed = 20 -> 4,182,136 cycles ( 522.767-ms)
* speed = 40 -> 8,362,936 cycles (1045.367-ms)
*/
do //
{ step = 0; // 64 step PWM period
do // "
{ LATA ^= toggle[step]; // 25 cycle (3.125-us) steps
toggle[step] = 0; // "
step++; // "
} while(step<64); // 1672 cycle (209-us) period
/*
* rebuild toggle array (71 cycles, 8.875-usecs)
*/
toggle[img1[index+ 0]] |= 1; // insert LED0 (RA0) toggle bit
toggle[img1[index+24]] |= 2; // insert LED1 (RA1) toggle bit
toggle[img1[index+48]] |= 4; // insert LED2 (RA2) toggle bit
toggle[img1[index+72]] |= 16; // insert LED3 (RA4) toggle bit
toggle[img1[index+96]] |= 32; // insert LED4 (RA5) toggle bit
toggle[0] ^= ~LATA; // initialize first element
} while(--speed); //
} //
} //
} //
/*
* 120 steps, 3' per step.
*/
const uint8_t img1[] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 4, 10, 24,
32, 24, 10, 4, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 4, 10, 24,
32, 24, 10, 4, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Hi Mike,Forgive me, guys. I was just trying to help.
I was curious, though, so I attempted a version of Robert's example program with a toggle table PWM driver in the main loop. Here's an excerpt, if you're at all interested...