Anthony Van Herrewege wrote: > My SDI line is on pin 0 of port A. Now, to determine > wether SDI should be high or low, I shift the [x][y] > byte by k (k starting at 7, going to 0) and AND it > with 0b00000001. Then to pull the line low, I AND LATA > with 0b11111110, to pull it high, I OR LATA with > 0b00000001. > > Is this the best way, or can I, since I put SDI on pin > 0, just say something like: TRISA = (volume[x][y] >> > k) & 0b00000001? Anthony: A few things... 1. #define isn't the correct way to declare your volume[][] array. 2. In C, array subscripts start at 0... So if you've declared an array of size 2 (e.g., "int array[2];"), it contains elements array[0] and array[1]; there's no array[2]. 3. This is scary: for (k = 7; k >= 0; k--) If k is unsigned, it's by definition ALWAYS greater than or equal to 0... The loop will never terminate. 4. An easier, faster, more code-efficient way to do your bit- shifting is: unsigned char k, temp; .... temp = volume[i][j]; for (k = 0b10000000; k != 0; k = k >> 1) { pga_latch = pga_latch & 0b11111110; if (temp & k) { pga_latch = pga_latch | 0b00000001; } } .... This puts the array lookup outside the loop so you only do it once per byte, and it reduces the number of shifts per byte from 28 to 8. It also gets rid of the "else" by always pulling SDI low, then pulling it high when appropriate; it didn't look as though that'd be a problem. 5. Does your C compiler support bit-wide variables? If so, it'd make your code more readable and less error-prone, since you could simply write "SDI = 0" or whatever, instead of the complicated logical operations. If not, "set" and "clear" macros would do almost as well. -Andy === Andrew Warren -- aiw@cypress.com === Principal Design Engineer === Cypress Semiconductor Corporation === === Opinions expressed above do not === necessarily represent those of === Cypress Semiconductor Corporation -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics