Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
1,151 Views

alt_getchar returning wrong value

int main() { int noia = 0; alt_putstr("Proc 2!\n"); /* get the mutex device handle */ alt_mutex_dev* mutex = altera_avalon_mutex_open( "/dev/mutex" ); /* acquire the mutex, setting the value to one */ altera_avalon_mutex_lock( mutex, 1 ); if(altera_avalon_mutex_is_mine(mutex)) { alt_printf("%x\n", IORD_32DIRECT(SHARED_MEM_BASE, 0x00)); noia = alt_getchar(); alt_printf("%x\n", noia); if (noia == 1) altera_avalon_mutex_unlock( mutex ); } return 0; } 

 

This is my code, however alt_getchar() is returning a wrong value, not what i typed
0 Kudos
5 Replies
Altera_Forum
Honored Contributor I
80 Views

Hi, Are you new to C programs ? 

 

Your printf will print hexadecimal value. because of "%x" 

 

try  

noia = alt_getchar(); alt_printf("%c\n", noia);  

 

But later in your code I see if (noia == 1)

If you type with keyboard, alt_getchar may return between 40 and 124 (decimal values). 

 

So if you want you condition to be true you have to Press Alt with 0 then 0 then 1. 

 

If you want your condition to be true when you type '1' with keyboard, try  

if (noia == '1') 

 

noia should be declared as char (not "int"). 

 

 

Or this is an other problem.
Altera_Forum
Honored Contributor I
80 Views

It's another problem, seems like my buffer has already some info on it. I don't type anything and some value appear in the alt_getchar, like 1a, 2b etc. 

Thanks for your info thought :)
Altera_Forum
Honored Contributor I
80 Views

Be aware that alt_getchar() uses a "direct" driver. See Documents. 

Have you tried with C usual getchar() ?
Altera_Forum
Honored Contributor I
80 Views

I'd guess that the result range of alt_getchar() is -1 (no char available) and 0..255 for a received byte (it could also return indications of abort and framing errors) - so the return value could well be 'int' not 'char'. 

 

getchar() will pull in a lot of libc code, and may also (by default at least) require that you type <enter> before making any data available.
Altera_Forum
Honored Contributor I
80 Views

 

--- Quote Start ---  

It's another problem, seems like my buffer has already some info on it. I don't type anything and some value appear in the alt_getchar, like 1a, 2b etc. 

Thanks for your info thought :) 

--- Quote End ---  

 

If your problem is simply with chars already stored in buffer, what you commonly do in C is putting a while (getchar() != EOF); loop before starting. 

EOF is usually the portable version of the -1 result value suggested by dsl.
Reply