Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

buttons and leds

Altera_Forum
Honored Contributor II
1,284 Views

Hello all and hippo, 

 

Can anyone out there help me with buttons and leds ? 

 

I am running uclinux on cyclone ii 2c35 nios ii dev board. 

 

I have 8 leds, 2 7-segment leds, 4 buttons and 16x2 character LCD. 

 

Are there any apps out there there would use these devices ? 

 

I also need drivers, and sample app using these. 

 

Any help would be appreciated.  

 

Thanks.
0 Kudos
7 Replies
Altera_Forum
Honored Contributor II
461 Views

I'm not using the drivers for these devices, so I leave them out of the kernel compile. I could never get the button device /dev/btn to work. 

 

What I do instead is just directly read and write the ports. So I have 

# include "nios2_system.h" 

VUINT16 *button_address = (VUINT16*) na_button_pio; 

VUINT16 *led_7seg_address = (VUINT16*) na_seven_seg_pio; 

VUINT8 *led_address = (VUINT8*) na_led_pio; 

 

and then just read and write to these ports. 

 

For the buttons: 

int get_button_press(void) { 

int button_value = ~*button_address; 

 

if (button_value & 0x08) 

return 1; 

else if (button_value & 0x04) 

return 2; 

else if (button_value & 0x02) 

return 3; 

else if (button_value & 0x01) 

return 4; 

else 

return 0; 

 

For the leds just read and write values. I can share my digit code and alphabetic characters for the seven segment display if you want. 

 

Cheers, 

Josh
0 Kudos
Altera_Forum
Honored Contributor II
461 Views

Forgot to mention that I am not using the LCD. Wish I could, but I don't have any spare expansion headers on my board.

0 Kudos
Altera_Forum
Honored Contributor II
461 Views

Hi jmarshall, 

 

Thanks. I would love to have your code so that i can play and learn it. 

 

thanks. 

 

 

--- Quote Start ---  

originally posted by jmarshall@Aug 16 2006, 01:01 AM 

forgot to mention that i am not using the lcd. wish i could, but i don't have any spare expansion headers on my board. 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=17646) 

--- quote end ---  

 

--- Quote End ---  

0 Kudos
Altera_Forum
Honored Contributor II
461 Views

jmarshall, 

 

Can you also let me have your makefile as wel. I have been having trouble compiling stuff. 

 

Any examples and instructions would be great. thanks. 

 

 

 

--- Quote Start ---  

originally posted by albertyong88+aug 16 2006, 03:25 am--><div class='quotetop'>quote (albertyong88 @ aug 16 2006, 03:25 am)</div> 

--- quote start ---  

hi jmarshall, 

 

thanks. i would love to have your code so that i can play and learn it. 

 

thanks. 

 

<!--quotebegin-jmarshall@Aug 16 2006, 01:01 AM 

forgot to mention that i am not using the lcd. wish i could, but i don&#39;t have any spare expansion headers on my board. 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=17646) 

--- quote end ---  

 

--- Quote End ---  

 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=17650)</div> 

[/b] 

--- Quote End ---  

0 Kudos
Altera_Forum
Honored Contributor II
461 Views

 

--- Quote Start ---  

originally posted by jmarshall@Aug 16 2006, 02:01 PM 

i&#39;m not using the drivers for these devices, so i leave them out of the kernel compile. i could never get the button device /dev/btn to work. 

 

what i do instead is just directly read and write the ports. so i have 

# include "nios2_system.h" 

vuint16 *button_address = (vuint16*) na_button_pio; 

vuint16 *led_7seg_address = (vuint16*) na_seven_seg_pio; 

vuint8 *led_address = (vuint8*) na_led_pio; 

 

and then just read and write to these ports. 

 

for the buttons: 

int get_button_press(void) { 

int button_value = ~*button_address; 

 

if (button_value & 0x08) 

  return 1; 

else if (button_value & 0x04) 

  return 2; 

else if (button_value & 0x02) 

  return 3; 

else if (button_value & 0x01) 

  return 4; 

else 

  return 0; 

 

for the leds just read and write values. i can share my digit code and alphabetic characters for the seven segment display if you want. 

 

cheers, 

josh 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=17645) 

--- quote end ---  

 

--- Quote End ---  

 

 

hi all, i would like to c the code oso. thx for sharing! http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif
0 Kudos
Altera_Forum
Honored Contributor II
461 Views

For the seven segment displays, here are the hex codes: 

static unsigned char hex_digits = {    0x01, 0x4f, 0x12, 0x06, 0x4c, /* 0-4 */    0x24, 0x20, 0x0f, 0x00, 0x04, /* 5-9 */    0x08, 0x60, 0x72, 0x42, 0x30, 0x38 /* a-f */ }; static unsigned char hex_alphabet = {    0x88, 0xe0, 0xf2, 0xc2, 0xb0, 0xb8, 0x84, /* a,b,c,d,e,f,g */    0xe8, 0xfb, 0xc7, 0xf0, 0xf1, 0xb6, 0xea, /* h,i,j,k,l,m,n  (m is 3 horizontal bars)*/    0xe2, 0x98, 0x8c, 0xfa, 0xa4, 0xb9, 0xe3, /* o,p,q,r,s,t,u */    0xff, 0xc8, 0xc4, 0x92 /* w,x,y,z */ }; 

Note that not all the characters can be represented, particularly m, v, w. s & z end up like 2 and 5. 

 

Then I have the following two functions: 

VUINT16 *led_7seg_address = (VUINT16*) na_seven_seg_pio; char getSevenSegmentRep(char c, int decimalpoint) {    int dp = 0;    if (decimalpoint) dp = 0xff;    else dp = 0x7f;    if (c >= 0 && c <= 0xf) {        return (hex_digits | 0x80) & dp;    }    else if (c >= &#39;0&#39; && c <= &#39;9&#39;) {        return (hex_digits | 0x80) & dp;    }    else if (c >= &#39;a&#39; && c <= &#39;z&#39;) {        return (hex_alphabet | 0x80) & dp;    }    else if (c >= &#39;A&#39; && c <= &#39;Z&#39;) {        return (hex_alphabet | 0x80) & dp;    }    else        return 0xdd; } void putToLed(char char1, char char2, int decimalPoint1, int decimalPoint2) {    *led_7seg_address = getSevenSegmentRep(char1, decimalPoint1) << 8                                      | getSevenSegmentRep(char2, decimalPoint2); } 

 

Then use like so: 

putToLed(&#39;a&#39;,&#39;c&#39;, 0, 0); 

 

Someone else asked about the makefile. I have set this up as part of the user build, changing the config definition file as described in the wiki. 

The makefile itself looks like this, again as described in wiki: 

EXEC = app OBJC = app.o all: $(EXEC) $(EXEC): $(OBJS)    $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) romfs:    $(ROMFSINST) /bin$(EXEC) clean:    rm -rf $(EXEC) *.elf *.gdb *.o 

 

Note that you can&#39;t just copy and paste this, as it needs a TAB instead of those spaces as indents. 

 

Hope this helps. 

- Josh
0 Kudos
Altera_Forum
Honored Contributor II
461 Views

 

--- Quote Start ---  

originally posted by jmarshall@Aug 16 2006, 08:52 PM 

for the seven segment displays, here are the hex codes: 

static unsigned char hex_digits = {    0x01, 0x4f, 0x12, 0x06, 0x4c, /* 0-4 */    0x24, 0x20, 0x0f, 0x00, 0x04, /* 5-9 */    0x08, 0x60, 0x72, 0x42, 0x30, 0x38 /* a-f */ }; static unsigned char hex_alphabet = {    0x88, 0xe0, 0xf2, 0xc2, 0xb0, 0xb8, 0x84, /* a,b,c,d,e,f,g */    0xe8, 0xfb, 0xc7, 0xf0, 0xf1, 0xb6, 0xea, /* h,i,j,k,l,m,n  (m is 3 horizontal bars)*/    0xe2, 0x98, 0x8c, 0xfa, 0xa4, 0xb9, 0xe3, /* o,p,q,r,s,t,u */    0xff, 0xc8, 0xc4, 0x92 /* w,x,y,z */ }; 

note that not all the characters can be represented, particularly m, v, w. s & z end up like 2 and 5. 

 

then i have the following two functions: 

vuint16 *led_7seg_address = (vuint16*) na_seven_seg_pio; char getsevensegmentrep(char c, int decimalpoint) {    int dp = 0;    if (decimalpoint) dp = 0xff;    else dp = 0x7f;    if (c >= 0 && c <= 0xf) {        return (hex_digits | 0x80) & dp;    }    else if (c >= &#39;0&#39; && c <= &#39;9&#39;) {        return (hex_digits | 0x80) & dp;    }    else if (c >= &#39;a&#39; && c <= &#39;z&#39;) {        return (hex_alphabet | 0x80) & dp;    }    else if (c >= &#39;a&#39; && c <= &#39;z&#39;) {        return (hex_alphabet | 0x80) & dp;    }    else        return 0xdd; } void puttoled(char char1, char char2, int decimalpoint1, int decimalpoint2) {    *led_7seg_address = getsevensegmentrep(char1, decimalpoint1) << 8                                      | getsevensegmentrep(char2, decimalpoint2); } 

 

then use like so: 

puttoled(&#39;a&#39;,&#39;c&#39;, 0, 0); 

 

someone else asked about the makefile. i have set this up as part of the user build, changing the config definition file as described in the wiki. 

the makefile itself looks like this, again as described in wiki: 

exec = app objc = app.o all: $(exec) $(exec): $(objs)    $(cc) $(ldflags) -o $@ $(objs) $(ldlibs) romfs:    $(romfsinst) /bin$(exec) clean:    rm -rf $(exec) *.elf *.gdb *.o 

 

note that you can&#39;t just copy and paste this, as it needs a tab instead of those spaces as indents. 

 

hope this helps. 

- josh 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=17661)</div> 

--- Quote End ---  

 

 

Jmarshall, 

 

Thanks. I can now get the hang of looking at devices , and map them to hardware. 

 

I am still fiddling around with some code ... 

 

Once I am done, I will post this on the wiki for everyone ...
0 Kudos
Reply