Forum Discussion
Altera_Forum
Honored Contributor
14 years agoTry something like this... for the sake of clarity, I broke the code into switch functions. With this, when you press a switch, the function associated with it will run on every cycle until a different switch is pressed. For the sake of illustration, if you press switch 0, audio from channel 1 will go to channel 1. If you press switch 1, channel 2 will be routed. If you press 3, both channels will be routed. Any other switch will stop the routing. This code latches in the current button. If you do not want that, uncomment the switch_state = 0 line.
I did not compile this code, but you get the idea.# define aud_in_ch1 (volatile int*) 0x01101020# define aud_out_ch1 (int*) 0x01101030# define aud_in_ch2 (volatile int*) 0x01101040# define aud_out_ch2 (int*) 0x01101050# define switches (volatile char*) 0x01101060
void do_switch_0_function (void)
{
*aud_out_ch1 = *aud_in_ch1;
}
void do_switch_1_function (void)
{
*aud_out_ch2 = *aud_in_ch2;
}
void do_switch_2_function (void)
{
*aud_out_ch1 = *aud_in_ch1;
*aud_out_ch2 = *aud_in_ch2;
}
void do_switch_3_function (void)
{
// do something when switch 3 is pressed.
}
void do_switch_4_function (void)
{
// do something when switch 4 is pressed.
}
void do_switch_5_function (void)
{
// do something when switch 5 is pressed.
}
void do_switch_6_function (void)
{
// do something when switch 6 is pressed.
}
void do_switch_7_function (void)
{
// do something when switch 7 is pressed.
}
int main()
{
int i;
int switch_state = 0;
printf("Hello from Nios II!\n");
while (1)
{
lcd(); // I don't know what lcd () does, but I am assuming it refreshes the lcd.
// switch_state = 0; // uncomment this line for momentary switch activation.
// Change the switch state into an index.
// Only one switch is considered. i.e. if two swithces are
// pressed at the same time, only the first one matters.
for (i = 0; i < 8; ++i)
{
if (*switches & (1 << i))
{
switch_state = i;
break;
}
}
switch (switch_state)
{
case 0: do_switch_0_function (); break;
case 1: do_switch_1_function (); break;
case 2: do_switch_2_function (); break;
case 3: do_switch_3_function (); break;
case 4: do_switch_4_function (); break;
case 5: do_switch_5_function (); break;
case 6: do_switch_6_function (); break;
case 7: do_switch_7_function (); break;
}
}
return 0;
}