Hello Mike,
Within the header file for the MCU derivative, you should find the following entries -
typedef unsigned char byte;
typedef unsigned int word;
What this means is that you can use the byte data type in lieu of the standard data type of unsigned char. Similarly with using word in lieu of unsigned int. With C we refer to a "variable" as having a specific data type. The word "register" has a very specific meaning in C.
The first example that you give would require that variable foo be initialised prior to the shift operation.
foo >>= 1; // This is short-hand for
foo = foo >> 1;
Yes, you could use -
LED = pattern & 1;
Good observation! That is one I missed in attempting to demonstrate the equivalence of the assembly and C snippets.
I was not specific about the wait function, but you will obviously need one. While the looping method can be calibrated to produce the right amount of delay for a specific bus frequency, a better method is to make use of a timer module. An approach that is often used is to generate "tick" interrupts at regular intervals, typically 1 to 10 milliseconds. This can be used for general delay timing, provided you can tolerate a timing uncertainty of one tick interval - if the required timing parameter is N intervals, the actual delay will fall somewhere between N-1 and N intervals.
The chosen tick interval should not be too short, where the frequent execution of the ISR code would occupy a significant percentage of the processing time. With say, 500 microsecond interval and a 10MHz bus frequency, the number of cycles between interrupts would be 5000 - probably acceptable with short ISR code. However, a tick period of 50 microseconds would appear unacceptable in most instances.
The following code snippet assumes use of a TPM channel, using software compare mode, although there are other possible alteratives. Assume a 10 MHz bus frequency, and a TPM prescale factor of 4, the following ISR code is a possibility for a tick interval of 5ms.
#define INCRVAL 12500 // 5ms interval with 10MHz bus, TPM prescale 4
volatile word delay_cnt = 0; // volatile modifier is required for this global variable
__interrupt 9 void ISR_TPM1C0( void)
{
TPM1C0V += INCRVAL; // Next compare value
TPM1C0_CH0F = 0; // Clear flag
if (delay_cnt) delay_cnt--;
}
The delay function -
void wait( word delay)
{
delay_cnt = delay;
while (delay_cnt) // Wait for timeout
__RESET_WATCHDOG();
}
Regards,
Mac