00001 #include "../part.h"
00002 #include "pio_defines.h"
00003
00004
00005 #define black 0x00
00006 #define red 0x01
00007 #define green 0x04
00008 #define yellow 0x05
00009 #define blue 0x10
00010 #define purple 0x11
00011 #define light_blue 0x14
00012 #define white 0x15
00013 char color_array[8]={black,purple,blue,light_blue,green,yellow,red,white};
00014
00015 #define USE_RGB_LEDS 1
00016
00017 #define INPUT_BITMASK 0x0000ff00
00018 #define OUTPUT_BITMASK 0x000000ff
00019 #define BITMASK 0x000000ff
00020
00021 unsigned int log_2(unsigned int x);
00022 unsigned int ones32(unsigned int x);
00023
00024 int main( void )
00025 {
00026 avr32_pio_t *piob = (void *) AVR32_PIOB_ADDRESS;
00027
00028 char input, output;
00029
00030
00031 piob->pdr = 0x00000000;
00032 piob->oer = OUTPUT_BITMASK;
00033 piob->odr = INPUT_BITMASK;
00034 piob->ifer = INPUT_BITMASK;
00035 piob->codr = 0xFFFFffff;
00036
00037 while(1){
00038 input = (~(piob->pdsr >> 8) & BITMASK);
00039 output = (piob->pdsr) & BITMASK;
00040 if( input != output){
00041 piob->codr = OUTPUT_BITMASK;
00042 if(USE_RGB_LEDS)
00043 piob->sodr = color_array[ log_2(input) ] ;
00044 else
00045 piob->sodr = input;
00046 }
00047
00048 }
00049 }
00050
00051 unsigned int log_2(register unsigned int x)
00052 {
00053 x |= (x >> 1);
00054 x |= (x >> 2);
00055 x |= (x >> 4);
00056 x |= (x >> 8);
00057 x |= (x >> 16);
00058
00059 return(ones32(x >> 1));
00060 }
00061
00062 unsigned int ones32(register unsigned int x)
00063 {
00064
00065 x -= ((x >> 1) & 0x55555555);
00066 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
00067 x = (((x >> 4) + x) & 0x0f0f0f0f);
00068 x += (x >> 8);
00069 x += (x >> 16);
00070
00071 return(x & 0x0000003f);
00072 }