贪吃蛇蛇移动代码keil+c+proteus实现
#include<LPC21XX.h>
#include"lcd12864.h"
INT32U snakemove(void);
void delay(INT32U ms);
#define LCD12864_CTL_NO 11
#define LCD12864_DAT_NO 16
#define LCD12864_CSA (1<<(LCD12864_CTL_NO))
#define LCD12864_CSB (1<<(LCD12864_CTL_NO+1))
#define LCD12864_RS (1<<(LCD12864_CTL_NO+2))
#define LCD12864_RW (1<<(LCD12864_CTL_NO+3))
#define LCD12864_EN (1<<(LCD12864_CTL_NO+4))
#define LCD12864_CLR IO0CLR
#define LCD12864_SET IO0SET
#define LCD12864_DIR IO0DIR
#define LCD12864_PIN IO0PIN
#define LCD12864_SEL0 PINSEL0
#define LCD12864_SEL1 PINSEL1
#define LCD_STARTROW 0xC0
#define LCD_PAGE 0xB8
#define LCD_LINE 0x40
#define SINGLE_SCREEN_LINE 64
#define LCD_DispOn 0x3F
void vWriteCMD2(INT8U ucCMD)
{
LCD12864_CLR=LCD12864_RS;
LCD12864_CLR=LCD12864_RW;
LCD12864_CLR=0xFF <<LCD12864_DAT_NO;
LCD12864_SET=ucCMD<<LCD12864_DAT_NO;
LCD12864_SET=LCD12864_EN;
LCD12864_CLR=LCD12864_EN;
}
void WriteData2(INT8U ucData)
{
LCD12864_SET=LCD12864_RS;
LCD12864_CLR=LCD12864_RW;
LCD12864_CLR=0xFF <<LCD12864_DAT_NO;
LCD12864_SET=ucData<<LCD12864_DAT_NO;
LCD12864_SET=LCD12864_EN;
LCD12864_CLR=LCD12864_EN;
}
void CleanAll2(void)
{
INT8U page,lin;
LCD12864_SET=LCD12864_CSA;
LCD12864_SET=LCD12864_CSB;
for(page=0;page<8;page++)
{
vWriteCMD2(LCD_PAGE+page);
vWriteCMD2(LCD_LINE);
for(lin=0;lin<64;lin++)
WriteData2(0x00);
}
}
void LCDInit2(void)
{
LCD12864_SEL0 &=0x003FFFFF;
LCD12864_DIR |=0x00FFF800;
LCD12864_CLR |=0x00FFF800;
vWriteCMD2(LCD_DispOn);
vWriteCMD2(LCD_STARTROW);
CleanAll2();
}
void writeByte(uint8 page,uint8 lin,uint8 ucData)
{
if(lin/SINGLE_SCREEN_LINE==0)
{
LCD12864_SET=LCD12864_CSA;
LCD12864_CLR=LCD12864_CSB;
}
else{
LCD12864_CLR=LCD12864_CSA;
LCD12864_SET=LCD12864_CSB;
}
vWriteCMD2(LCD_PAGE+page);
vWriteCMD2(LCD_LINE+lin%SINGLE_SCREEN_LINE);
WriteData2(ucData);
}
INT32U snakemove(void)
{
INT8U k=4;
INT8U h=12;
INT8U a=12;
INT32U j,i=0;
INT32U n=4;
INT8U lin=0,page=0;
INT8U empty[4]={0x00,0x00,0x00,0x00};
INT8U snake[4]={0x0f,0x0f,0x0f,0x0f};
INT8U snake1[12]={0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f};
for(lin=0;lin<12;lin++)
writeByte(page,lin,snake1[lin]);
for(i=0;;i++)
{
if(n!=0)
{
for(lin=a;lin<a+k;lin++)
{
j=lin-h;
writeByte(page,lin,snake[lin%k]);
writeByte(page,j,empty[j%k]);
}
}
else
{
for(lin=a;lin<a+k;lin++)
{
j=lin-h;
writeByte(page,lin,snake[lin%k]);
writeByte(page,lin+k,snake[lin%k]);
writeByte(page,j,empty[j%k]);
}
lin=lin+4;
h=h+4;
}
n--;
lin=lin%128;
a=lin;
delay(200);
}
return 1;
}
INT32S main()
{
LCDInit2();
snakemove();
}
void delay(INT32U ms)
{
INT32U i=0;
for(;ms>0;ms--)
{
for(i=0;i<2500;i++)
{
;
;
;
;
}
}
}