c - How to count number of switches and switch corresponding number of LEDs by applying masking? -


i using de0 board , wanted count number of slide switches in on position (there 10 of these), , lit corresponding number of led (10 of these too) on board. don't know how use result masking iowr. thank

alt_u16 sw;  volatile int = 0;  alt_u16 count = 0x0;  alt_u16 mask = 0x0001;  alt_u16 sw2;  int countswitches(alt_u16 sw);  int alt_main (void) {     while(1)     {            sw = iord_altera_avalon_pio_data(switches_base);           iowr_altera_avalon_pio_data(leds_base, sw2);     }     return 0; }  int countswitches(alt_u16 sw) {     for(i = 0; < 10; i++)     {         if (sw & mask)         {             count++;         }          mask = mask >> 1;     }     return count;     }  void turnledson(alt_u16 sw2) {     sw2 = countswitches(sw); } 

function countswitches not correct, should be:

int countswitches(alt_u16 sw) {     alt_u16 mask = 1;      for(i = 0; < 10; i++)     {         if (sw & mask)         {             count++;         }          mask = mask << 1;     }      return count;     } 

btw function return number of switches on, doesn't return position.

iowr_altera_avalon_pio_data(base, data) wants bitfield data: each bit set 1 set 1 corresponding output of port (base) passed. means must set bits of sw2 variable based on switches position.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -