📄 count_binary.c
字号:
{
#ifdef LCD_DISPLAY_NAME
FILE *lcd = (FILE*) arg;
LCD_PRINTF(lcd, "%c%s 0x%x\n", ESC, ESC_COL2_INDENT5, count);
#endif
}
/* count_all merely combines all three peripherals counting */
static void count_all( void* arg )
{
count_led();
count_sevenseg();
count_lcd( arg );
printf("%02x, ", count);
}
static void handle_button_press(alt_u8 type, FILE *lcd)
{
/* Button press actions while counting. */
if (type == 'c')
{
switch (edge_capture)
{
/* Button 1: Output counting to LED only. */
case 0x1:
count_led();
break;
/* Button 2: Output counting to SEVEN SEG only. */
case 0x2:
count_sevenseg();
break;
/* Button 3: Output counting to D only. */
case 0x4:
count_lcd( lcd );
break;
/* Button 4: Output counting to LED, SEVEN_SEG, and D. */
case 0x8:
count_all( lcd );
break;
/* If value ends up being something different (shouldn't) do
same as 8. */
default:
count_all( lcd );
break;
}
}
/* If 'type' is anything else, assume we're "waiting"...*/
else
{
switch (edge_capture)
{
case 0x1:
printf( "Button 1\n");
edge_capture = 0;
break;
case 0x2:
printf( "Button 2\n");
edge_capture = 0;
break;
case 0x4:
printf( "Button 3\n");
edge_capture = 0;
break;
case 0x8:
printf( "Button 4\n");
edge_capture = 0;
break;
default:
printf( "Button press UNKNOWN!!\n");
}
}
}
/*******************************************************************************
* int main() *
* *
* Implements a continuous loop counting from 00 to FF. 'count' is the loop *
* counter. *
* The value of 'count' will be displayed on one or more of the following 3 *
* devices, based upon hardware availability: LEDs, Seven Segment Display, *
* and the LCD Display. *
* *
* During the counting loop, a switch press of SW0-SW3 will affect the *
* behavior of the counting in the following way: *
* *
* SW0 - Only the LED will be "counting". *
* SW1 - Only the Seven Segment Display will be "counting". *
* SW2 - Only the LCD Display will be "counting". *
* SW3 - All devices "counting". *
* *
* There is also a 7 second "wait", following the count loop, *
* during which button presses are still *
* detected. *
* *
* The result of the button press is displayed on STDOUT. *
* *
* NOTE: These buttons are not de-bounced, so you may get multiple *
* messages for what you thought was a single button press! *
* *
* NOTE: References to Buttons 1-4 correspond to SW0-SW3 on the Development *
* Board. *
******************************************************************************/
int main(void)
{
int i;
int wait_time;
FILE * lcd;
count = 0;
/* Initialize the LCD, if there is one.
*/
lcd = LCD_OPEN();
lcd_init( lcd );
/* Initialize the button pio. */
#ifdef BUTTON_PIO_BASE
init_button_pio();
#endif
/* Initial message to output. */
initial_message();
/* Continue 0-ff counting loop. */
while( 1 )
{
usleep(100000);
if (edge_capture != 0)
{
/* Handle button presses while counting... */
handle_button_press('c', lcd);
}
/* If no button presses, try to output counting to all. */
else
{
count_all( lcd );
}
/*
* If done counting, wait about 7 seconds...
* detect button presses while waiting.
*/
if( count == 0xff )
{
LCD_PRINTF(lcd, "%c%s %c%s %c%s Waiting...\n", ESC, ESC_TOP_LEFT,
ESC, ESC_CLEAR, ESC, ESC_COL1_INDENT5);
printf("\nWaiting...");
edge_capture = 0; /* Reset to 0 during wait/pause period. */
/* Clear the 2nd. line of the LCD screen. */
LCD_PRINTF(lcd, "%c%s, %c%s", ESC, ESC_COL2_INDENT5, ESC,
ESC_CLEAR);
wait_time = 0;
for (i = 0; i<70; ++i)
{
printf(".");
wait_time = i/10;
LCD_PRINTF(lcd, "%c%s %ds\n", ESC, ESC_COL2_INDENT5,
wait_time+1);
if (edge_capture != 0)
{
printf( "\nYou pushed: " );
handle_button_press('w', lcd);
}
usleep(100000); /* Sleep for 0.1s. */
}
/* Output the "loop start" messages before looping, again.
*/
initial_message();
lcd_init( lcd );
}
count++;
}
LCD_CLOSE(lcd);
return 0;
}
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -