Mega16单片机
程序 interface.h
/*
移植时要完成以下函数:
LcdClrScrn(); //清屏
ShowARROW(SysSclt); //在某一行显现箭头
DisShowARROW(SysSclt); //在某一行不显现箭头
ShowLine(0, “Copy Right”); //在某一行显现字符串
uchar GetKey(); //按键检测函数。
回来Key_Up Key_Down Key_Right Key_Left
*/
#ifndef __INTERFACE_H__
#define __INTERFACE_H__
#include “KEY.h”
//extern void Interface(void);
#define GOTO_CHILD 0XFC
#define FROM_FATHER 0XFC
#define GOTO_FATHER 0XFD //之前设置为0xff 不对,有问题,子界面回来时,不改写
#define FROM_CHILD 0XFD
//##########################################################
#define INT8U unsigned char
INT8U Key; //体系按键.
INT8U KeyDir = FROM_FATHER; //用来各个界面之间的切换时传递参数.
//==============================================================
//体系界面函数声明区.(一定要声明!) 有几个不同的界面就有几个函数
void UI_Idle(void);
void UI_Help(void);
void UI_Time();
void UI_Temperature();
void UI_Play();
void UI_Set();
void (*pUI)()=UI_Idle; //界面指针初始化为IDLE函数. 函数指针
//pUI();为当时while()大循环下履行的函数
#define UI_GOTO(index) {pUI=(index);}
#define UI_GOTO_CHILD(index) {pUI=(index); KeyDir=GOTO_CHILD;}
#define UI_GOTO_FATHER(index) {pUI=(index); KeyDir=GOTO_FATHER;}
//========================================================================
//以下是各个界面的函数完成.每个函数都要在上面声明!
//闲暇状况下的循环.
void UI_Idle()
{
static INT8U SysSclt=0; //当时选中的是哪一项
switch(Key)
{
//进入菜单.
case FROM_FATHER:
case FROM_CHILD:
//case GOTO_FATHER:
LcdClrScrn();
// 0 1 2 3 4
#define IDLE_MENU_MAX 4
ShowLine(0,”1.Temperature”);
ShowLine(1,”2.Time”);
ShowLine(2,”3.Play Games”);
ShowLine(3,”4.Set “);
ShowLine(4,”5.Help”);
ShowLine(7,” select”);
ShowARROW(SysSclt);
break;
//箭头上移
case Key_Up:
DisShowARROW(SysSclt);
if(SysSclt==0)SysSclt=IDLE_MENU_MAX;
else SysSclt–;
ShowARROW(SysSclt);
break;
//箭头下移
case Key_Down:
DisShowARROW(SysSclt);
SysSclt++;
if(SysSclt>IDLE_MENU_MAX)SysSclt=0;
ShowARROW(SysSclt);
break;
//确认 进入 子项
case Key_Right:
if(SysSclt == 0) //Temperature
{
UI_GOTO_CHILD(UI_Temperature);
//KeyDir = GOTO_CHILD;
break;
}
if(SysSclt == 1) //Time
{
UI_GOTO_CHILD(UI_Time);
//KeyDir = GOTO_CHILD;
break;
}
if(SysSclt == 2) //Time
{
UI_GOTO_CHILD(UI_Play);
//KeyDir = GOTO_CHILD;
break;
}
if(SysSclt == 3) //Time
{
UI_GOTO_CHILD(UI_Set);
//KeyDir = GOTO_CHILD;
break;
}
if(SysSclt == 4) //help
{
UI_GOTO_CHILD(UI_Help);
//KeyDir = GOTO_CHILD;
break;
}
default:break;
}//switch
}
void UI_Temperature()
{
switch(Key)
{
//进入菜单.
case FROM_FATHER:
//case FROM_CHILD:
LcdClrScrn();
ShowLine(0, “The”);
ShowLine(2, “Temperature”);
ShowLine(4, “is 27”);
//改天加DS18B20实践测一个
ShowLine(7, “back”);
break;
case Key_Left:
UI_GOTO_FATHER(UI_Idle);
break;
default:break;
}//switch
}
void UI_Time()
{
switch(Key)
{
//进入菜单.
case FROM_FATHER:
//case FROM_CHILD:
LcdClrScrn();
ShowLine(0, “The Time is “);
ShowLine(2, “09-12-24”);
ShowLine(4, “0:01 1234567890”);
ShowLine(7, “back”);
break;
case Key_Left:
UI_GOTO_FATHER(UI_Idle);
break;
default:break;
}//switch
}
void UI_Play()
{
switch(Key)
{
//进入菜单.
case FROM_FATHER:
//case FROM_CHILD:
LcdClrScrn();
ShowLine(0, “YOU get to “);
ShowLine(1, “go back to”);
ShowLine(2, “STUDY”);
ShowLine(4, “Made dy LYQ”);
ShowLine(7, “back”);
break;
case Key_Left:
UI_GOTO_FATHER(UI_Idle);
break;
default:break;
}//switch
}
void UI_Set()
{
switch(Key)
{
//进入菜单.
case FROM_FATHER:
//case FROM_CHILD:
LcdClrScrn();
ShowLine(0, “Nothing NOW”);
ShowLine(7, “back”);
break;
case Key_Left:
UI_GOTO_FATHER(UI_Idle);
break;
default:break;
}//switch
}
void UI_Help()
{
switch(Key)
{
//进入菜单.
case FROM_FATHER:
//case FROM_CHILD:
LcdClrScrn();
ShowLine(0, “Copy Right”);
ShowLine(2, “I love China”);
ShowLine(4, “Made dy LYQ”);
ShowLine(7, “back”);
break;
case Key_Left:
UI_GOTO_FATHER(UI_Idle);
break;
default:break;
}//switch
}
//体系的大循环.
void Interface(void)
{
//===============================================
//以下为按键的处理.
Key = GetKey();
if(KeyDir!=0) //KeyDir=GOTO_CHILD or KeyDir=GOTO_FATHER;
{//树立一种传递信息的变量.
Key = KeyDir;
KeyDir = 0;
}
//===============================================
//以下为界面的彼此调用
pUI();
}
#endif
========================================================================
==========================================================================
#############################################################################
#############################################################################
#ifndef __KEY_H__
#define __KEY_H__
#define Key_Up 0x11
#define Key_Down 0x12
#define Key_Left 0x13
#define Key_Right 0x14
//uchar Key_Scan();
void delay_ms(unsigned int t)
{
unsigned int i,j;
for(i=0;i
}
unsigned char Key_Scan()
{
unsigned char key;
DDRA|=0X0f; //输出
PORTA|=0Xff; //上拉
DDRA&=~0X0f; //输入
if(PINA==0xfe)
{
delay_ms(10);
if(PINA==0xfe)
{
while(PINA==0xfe); //等候松手
return Key_Up;
}
}
if(PINA==0xfd)
{
delay_ms(10);
if(PINA==0xfd)
{
while(PINA==0xfd);
return Key_Down;
}
}
if(PINA==0xfb)
{
delay_ms(10);
if(PINA==0xfb)
{
while(PINA==0xfb);
return Key_Left;
}
}
if(PINA==0xf7)
{
delay_ms(10);
if(PINA==0xf7)
{
while(PINA==0xf7);
return Key_Right;
}
}
return 0;//没有键按下回来0
}
unsigned char GetKey()
{
return Key_Scan();
}
#endif
====================================================
#####################################################
#######################################################
-===============================================================
#ifndef __PG12864_H__
#define __PG12864_H__
#include
#define uchar unsigned char
#define uint unsigned int
#define WR_L() PORTD&=~(1)
#define WR_H() PORTD|=(1)
#define RD_L() PORTD&=~(1<<1)
#define RD_H() PORTD|=(1<<1)
#define CE_L() PORTD&=~(1<<2)
#define CE_H() PORTD|=(1<<2)
#define CD_L() PORTD&=~(1<<3)
#define CD_H() PORTD|=(1<<3)
#define RST_L() PORTD&=~(1<<4)
#define RST_H() PORTD|=(1<<4)
void delay(uint t);
uchar read_status()
{
uchar status;
DDRB=0;//端口b置为输入
RD_L();
WR_H();
CE_L();
CD_H();
status=PINB;
return status;
}
void check_status()
{
uchar s;
DDRB=0XFF;//端口b置为输出
while((s&0x03)!=0x03)
s=read_status();//等候位1,2置为。指令读写准备好。数据读写准备好
}
//void init_txt_12864();
void write_data(uchar data);
void write_cmd1(uchar cmd);
void write_cmd2(uchar data,uchar cmd);
void write_cmd3(uchar data1,uchar data2,uchar cmd);
//uchar read_status();
//void clear_screen();
void display_char(uchar x,uchar y,uchar ch);
//void init_graphic_12864();
//void display_HZ(uchar x,uchar y,uchar *hz); ////x 0-3 y 0-7
//void display_string(uchar x,uchar y,uchar *s);
/*
int main()
{
uchar i,j=0;
init_txt_12864();
display_string(0,0,ch);
delay(100);
clear_screen();
init_graphic_12864();
for(i=0;i<15;i++)
if(i>=8)
display_HZ(1,i-8,HZ2[i]);
else
display_HZ(0,i,HZ2[i]);
while(1);
//{//调整文本区首地址,能够完成移屏;
////i++;
// write_cmd3(i++,0,0×40);//文本区首地址;
// delay(10000);
//}
}
*/
//#include <pg12864.h>
//处在x y处显现汉字hz
void display_HZ(uchar x,uchar y,uchar *hz)////x 0-3 y 0-7
{
uchar i,j=0;
for(i=0;i<16;i++)
{
write_cmd3(((j/2)<<4)|(y*2),x,0x24);//地址指针设置..低地址,高地址,指令
write_cmd2(hz[j++],0xc0);
write_cmd2(hz[j++],0xc0);
}
}
//处在x y处显现ch
void display_char(uchar x,uchar y, uchar ch)//x 0-7 y 0-15
{
write_cmd3((((x*2)<<4)+y),0x00,0x24);//地址指针方位
write_cmd2(ch-32,0xc0);//t6963c字符表与ASCII差32@@@@@@@@@@@@@@@@
}
void display_string(uchar x,uchar y, uchar *s)
{
uchar i=0;
while(*s)
{
display_char( x, y+(i++),*s);
s++;
if(i>=16)
{
x++;
i=0;
}
}
}
void delay(uint t)
{
uint i,j;
for(i=0;i
}
void init_txt_12864()
{
//初始化端口
DDRD=0XFF;
DDRB=0XFF;
PORTB=0XFF;
//液晶复位
RST_L();
delay(1);
RST_L();
//一切操控引脚置高
WR_H();
RD_H();
CE_H();
CD_H();
RST_H();
//初始化指令
check_status();
write_cmd3(0x01,0x00,0x21);//光标指针设置
check_status();
write_cmd3(0x00,0x00,0x40);//文本区首地址
check_status();
//加上下面2行代码,只显现4行?而不是8行!!
//write_cmd3(16,0×00,0x41);//文本区宽度。字节数/行&&&&&
//check_status();
write_cmd1(0x80);//显现方法设置,正常显现
check_status();
write_cmd1(0x94);//。。显现状况设置 1 0 1 0 N3 N2 N1 N0 文本显现,光标不显现,不闪耀
//write_cmd1(0x98);//图形方法显现,不显现字母,只打点
check_status();
write_cmd1(0xa1);//光标形状设置1 0 1 0 0 N2 N1 N0
}
void init_graphic_12864()
{
//初始化端口
DDRD=0XFF;
DDRB=0XFF;
PORTB=0XFF;
//液晶复位
RST_L();
delay(1);
RST_L();
//一切操控引脚置高
WR_H();
RD_H();
CE_H();
CD_H();
RST_H();
//初始化指令
check_status();
write_cmd3(0x01,0x00,0x21);//光标指针设置
check_status();
write_cmd3(0x00,0x00,0x42);//图形区首地址
check_status();
write_cmd3(16,0×00,0x43);//图形区宽度
check_status();
write_cmd1(0x80);//显现方法设置,正常显现
check_status();
//write_cmd1(0x94);//。。显现状况设置 1 0 1 0 N3 N2 N1 N0 文本显现,光标不显现,不闪耀
write_cmd1(0x98);//图形方法显现,不显现字母,只打点
check_status();
write_cmd1(0xa0);//光标形状设置1 0 1 0 0 N2 N1 N0
}
void clear_screen()
{
uint i;
write_cmd3(0x00,0x00,0x24);//地址指针设置
check_status();
write_cmd1(0xb0);//数据主动写,每写一次,地址指针主动加一
check_status();
for(i=0;i<8192;i++)
{
check_status();
write_data(0X00);
}
check_status();
write_cmd1(0xb2);//数据主动写完毕
}
void write_data(uchar data)
{
RD_H();
CD_L();
CE_L();
WR_L();
PORTB=data;
asm(“nop”);
//delay(1);
WR_H();
CE_H();
CD_H();
}
void write_cmd1(uchar cmd)
{
RD_H();
CD_H();
CE_L();
WR_L();
PORTB=cmd;
asm(“nop”);
//delay(1);//须延时80ns左右
WR_H();
CE_H();
CD_L();
}
//先送参数,再送指令
void write_cmd2(uchar data,uchar cmd)
{
check_status();
write_data(data);
check_status();
write_cmd1(cmd);
}
void write_cmd3(uchar data1,uchar data2,uchar cmd)
{
check_status();
write_data(data1);
check_status();
write_data(data2);
check_status();
write_cmd1(cmd);
}
//#pragma data:code
uchar HZ[4][32]={
{0x00,0xF8,0x3F,0x90,0x12,0x10,0x09,0x20,0x3F,0xFE,0x22,0x02,0x42,0x00,0x3F,0xFC,0x04,0x00,0x07,0xF0,0x0C,0x20,0x0B,0x40,0x10,0xC0,0x63,0x30,0x8C,0x0E,0x30,0x04},/*”爱”,0*/
{0x06,0x40,0x38,0x50,0x08,0x48,0x08,0x48,0x08,0x40,0xFF,0xFE,0x08,0x40,0x08,0x48,0x0E,0x28,0x38,0x30,0xC8,0x20,0x08,0x50,0x09,0x92,0x08,0x0A,0x28,0x06,0x10,0x02},/*”我”,1*/
{0x01,0x00,0x01,0x00,0x21,0x08,0x3F,0xFC,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00},/*”中”,2*/
{0x08,0x80,0x0C,0x88,0x10,0x9C,0x30,0xE0,0x53,0x80,0x90,0x84,0x10,0x84,0x10,0x7C,0x11,0x00,0x01,0x00,0x7F,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00}/*”华”,3*/
};
uchar HZ2[][32]=
{
{0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x7F,0xFC,0x20,0x04,0x00,0x00},/*”山”,0*/
{0x02,0x00,0x02,0x00,0x7F,0xFC,0x04,0x00,0x08,0x80,0x08,0x80,0x10,0x80,0x1F,0xF8,0x00,0x80,0x08,0xA0,0x0C,0x90,0x18,0x88,0x10,0x8C,0x22,0x84,0x01,0x80,0x00,0x80},/*”东”,1*/
{0x08,0x00,0x0B,0xFE,0x48,0x20,0x48,0x20,0x4B,0xFE,0x4A,0x22,0x4A,0x22,0x4A,0x22,0x4A,0x22,0x52,0x22,0x52,0x2A,0x12,0x24,0x20,0x20,0x40,0x20,0x80,0x20,0x00,0x20},/*”师”,2*/
{0x04,0x20,0x04,0x20,0xFF,0xFE,0x04,0x60,0x40,0x00,0x31,0xF8,0x91,0x08,0x61,0x08,0x49,0x08,0x09,0x38,0x11,0x10,0xE1,0x00,0x21,0x04,0x21,0x04,0x20,0xFC,0x20,0x00},/*”范”,3*/
{0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x02,0x80,0x02,0x80,0x02,0x40,0x04,0x40,0x04,0x20,0x08,0x10,0x10,0x18,0x20,0x0E,0x40,0x04},/*”大”,4*/
{0x01,0x08,0x10,0x8C,0x0C,0xC8,0x08,0x90,0x7F,0xFE,0x40,0x04,0x8F,0xE8,0x00,0x40,0x00,0x80,0x7F,0xFE,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x02,0x80,0x01,0x00},/*”学”,5*/
{0x10,0x80,0x10,0x80,0x50,0x80,0x51,0xFC,0x7D,0x54,0x52,0x54,0x94,0x94,0x14,0x94,0x19,0x24,0x32,0x24,0xD4,0x44,0x10,0x84,0x11,0x04,0x12,0x04,0x14,0x28,0x00,0x10},/*”物”,6*/
{0x00,0x00,0x03,0xFC,0xFA,0x44,0x22,0x44,0x23,0xFC,0x22,0x44,0xFA,0x44,0x23,0xFC,0x22,0x44,0x20,0x40,0x23,0xFC,0x38,0x40,0xC0,0x40,0x00,0x40,0x0F,0xFE,0x00,0x00},/*”理”,7*/
{0x00,0x00,0x08,0x00,0x08,0x04,0x0F,0xFE,0x08,0x00,0x08,0x00,0x0F,0xFC,0x00,0x04,0x00,0x04,0x00,0x24,0x7F,0xF4,0x00,0x04,0x00,0x04,0x00,0x48,0x00,0x30,0x00,0x00},/*”与”,8*/
{0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x01,0x02,0x01,0x02,0x00,0xFE,0x00,0x00},/*”电”,9*/
{0x00,0x00,0x3F,0xF0,0x00,0x20,0x00,0x40,0x00,0x80,0x01,0x00,0x01,0x00,0x01,0x04,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00},/*”子”,10*/
{0x06,0x08,0x78,0x88,0x08,0x48,0x08,0x48,0xFE,0x08,0x18,0x88,0x1C,0x48,0x2A,0x48,0x28,0x0E,0x48,0x78,0x8B,0x88,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08},/*”科”,11*/
{0x01,0x08,0x10,0x8C,0x0C,0xC8,0x08,0x90,0x7F,0xFE,0x40,0x04,0x8F,0xE8,0x00,0x40,0x00,0x80,0x7F,0xFE,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x02,0x80,0x01,0x00},/*”学”,12*/
{0x01,0x08,0x10,0x8C,0x0C,0xC8,0x08,0x90,0x7F,0xFE,0x40,0x04,0x8F,0xE8,0x00,0x40,0x00,0x80,0x7F,0xFE,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x02,0x80,0x01,0x00},/*”学”,13*/
{0x00,0x80,0xF8,0x40,0x8F,0xFE,0x94,0x04,0xA0,0x00,0xA3,0xF8,0x90,0x00,0x88,0x00,0x8F,0xFE,0xA9,0x20,0x91,0x20,0x81,0x20,0x82,0x22,0x82,0x22,0x84,0x22,0x88,0x1E}/*”院”,14*/
};
uchar ch[]=”http://hi.baidu.com/282280072/blog”;
//#pragma data:data
void ShowLine(uchar x,uchar *s)
{
display_string(x,0,s);
}
void LcdClrScrn()
{
clear_screen();
}
void ShowARROW(uchar line)
{
display_string(line,13,”<<=");
}
void DisShowARROW(uchar line)
{
display_string(line,13,” “);
}
#endif