main.c
来自「8051试验程序 基础教材」· C语言 代码 · 共 237 行
C
237 行
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "target.h"
#include "lcd.h"
#include "led.h"
/* defines */
#define TRUE 1
#define FALSE 0
#define RIDDLE 0
#define SOLUTION 1
#define NO_WORDS 4
#define MAX_CHAR 16
/* globals */
/* word will hold a copy of current riddle and solution in data memory */
struct
{
char riddle[MAX_CHAR];
char solution[MAX_CHAR];
} word;
/* riddles and solutions are stored in code memory */
const char __code constData[NO_WORDS][MAX_CHAR] = { "www.iar.com", "EMBEDDED", "SOLUTIONS", "MICROCONTROLLER" };
/* achieved level 1-4 */
unsigned char level = 0;
/* solution flag */
unsigned char solved = FALSE;
/* current moving character */
unsigned char c = '_';
/* max position for current word */
unsigned char maxPos = 0;
/* wheel (pot.) current position */
unsigned char pos = 0;
/* ADC snapshot value */
unsigned char value = 0;
/**********************/
/* main program entry */
/**********************/
int main()
{
/* target/sytem initialization */
target_init();
/* port initialization */
port_init();
/* LCD initialization */
lcd_init();
/* LED initialization */
led_init();
/* set line\row */
send_lcd_command( FIRST_LINE );
/* write string to display */
send_lcd_text("www.iar.com");
/* set line\row */
send_lcd_command( SECOND_LINE );
/* write string to display */
send_lcd_text("Push button...");
for( ;; )
{
/* button pushed? */
if( !P1_bit.INT1 )
{
/* light display */
lcd_light( ON );
/* wait for button release */
while( !P1_bit.INT1 );
/* enable ADC for continious burst conversion */
ADINS |= 0x01;
ADMODA_bit.BURST0 = 1;
ADCON0 |= 0x05;
/* reset level */
level = 0;
while( level < NO_WORDS )
{
/* copy riddle and solution to */
for( int i=0; i<MAX_CHAR; ++i )
{
word.solution[i] = constData[level][i];
/* clear riddle */
word.riddle[i] = ' ';
}
/* max pos of current word */
maxPos = strlen(word.solution);
/* generate riddle == shuffle characters */
for( int i=0; i<maxPos; ++i )
{
unsigned char x;
do
{
do
{
x = rand() / 1490;
} while( x >= maxPos );
}while( word.riddle[x] != ' ' );
word.riddle[x] = constData[level][i];
}
/* clear display */
send_lcd_command( CLR_DISP );
/* write string to display */
send_lcd_command( FIRST_LINE );
send_lcd_text( word.riddle );
while( !solved )
{
/* read pot. */
value = AD0DAT0;
if( (pos != (value/16)) && (pos <= maxPos) )
{
/* clear old marker */
send_lcd_command( SECOND_LINE | pos );
send_lcd_char(' ');
}
/* calc. new pos */
pos = value / 16;
if( pos >= maxPos )
{
pos = maxPos-1;
}
/* write the marker */
send_lcd_command( SECOND_LINE | pos );
send_lcd_char( c );
if( !P1_bit.INT1 )
{
unsigned char oldC = c;
/* swap the bytes */
c = *(word.riddle + pos);
*(word.riddle + pos) = oldC;
/* update "word" on display */
send_lcd_command( FIRST_LINE | pos );
send_lcd_char( oldC );
/* update the marker */
send_lcd_command( SECOND_LINE | pos );
send_lcd_char( c );
solved = TRUE;
for( int i=0; i<maxPos; ++i )
{
if( word.riddle[i] == word.solution[i] )
continue;
solved = FALSE;
}
/* wait for button release */
while( !P1_bit.INT1 );
}
}
for( int i=0; i<4; ++i )
{
/* clear display */
send_lcd_command( CLR_DISP );
for(int j=0; j<20000; ++j);
/* write string to display */
/* set line\row */
send_lcd_command( FIRST_LINE );
send_lcd_text( word.riddle );
for(int j=0; j<20000; ++j);
}
/* go to next level */
level++;
/* lit level LED */
led_on( level );
/* reset flag */
solved = FALSE;
}
/* */
led_off( 1 );
led_off( 2 );
led_off( 3 );
led_off( 4 );
/* clear display */
send_lcd_command( CLR_DISP );
/* write string to display */
send_lcd_command( FIRST_LINE );
send_lcd_text("You did it!");
/* set line\row */
send_lcd_command( SECOND_LINE );
/* write string to display */
send_lcd_text("Push button...");
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?