Davo wrote:
Getting Interesting. The first routine (NEXTVAL) works OK, however about 30% of the numbers never get generated (meaning some LEDs don't ever get turned on).
The second routine crashes - not had time yet to debug it, but the COP times out when the routine is called.
Yes, there is a problem with the manner in which I attempted to convert the original CRC routine to a PRS in assembler - the sequence was not maximal length, and only 50 percent of the possible codes were generated. However, using the CRC method, I now feel that you will need a 16-bit generator to obtain a much longer sequence length. Apparently successful tests were conducted with the following C code, under simulation.
#include <hidef.h> /* EnableInterrupts macro */
#include "derivative.h" /* Include peripheral declarations */
typedef union {
word wval;
byte bval[2];
} DBYTE;
// Global variables:
DBYTE CRC16;
// Function prototypes:
void update_crc16( word new, word *crc);
void LED_control( byte address);
void LED_on( byte addr);
void LED_off( byte addr);
/**********************************************************************/
void main(void)
{
static byte a;
static word i;
EnableInterrupts;
for( ; ; ) {
update_crc16( 1, &CRC16.wval);
a = CRC16.bval[0] ^ CRC16.bval[1]; // Combine the two bytes
if ((a & 0x3F) < 48) {
a &= 0xBF; // MS bit represents LED state
LED_control( a);
}
__RESET_WATCHDOG();
}
}
/**********************************************************************/
/* Update 16-bit CRC value
* Polynomial: x^16 + x^15 + x^2 + 1 */
#define POLYVAL16 0xA001
void update_crc16( word new, word *crc)
{
word c;
byte i;
c = *crc;
for (i = 0; i < 16; i++) {
if ((c ^ new) & 1)
c = (c >> 1) ^ POLYVAL16;
else
c >>= 1;
new >>= 1;
}
*crc = c;
}
/**********************************************************************/
/* Control of addressed LED */
void LED_control( byte address)
{
if (address & 0x80)
LED_on( address & 0x3F);
else
LED_off( address & 0x3F);
}
With this code the two bytes of the CRC value are XORed (modulo 2 addition). Bit-7 of the result then indicates the next state for the addressed LED, bit-6 is ignored, and the remaining six bits indicate the address of the LED, with values 48-63 as a do nothing condition.
My tests over about one half the sequence length of 65535, showed that each LED is addressed a similar number of times, although not quite equal, for both LED on and LED off conditions. About one half the LEDs will be active at any time.
For the purpose of testing the code, I put no delay in the loop between each call of the CRC function, but in practice this would be necessary for your application.
Regards,
Mac