/****************************************************************************************
简单菜单程序,八个按键.使用AVR单片机.2008/3/24--2008/3/29
编译器为ICCAVR7.14.晶振7.3728M,单片机为ATmega16(用ATmega32在proteus中仿真不成功!)
*****************************************************************************************/
#include <iom16v.h>
#include <macros.h>
#include <stdio.h>
#include "menu.h" //所有变量定义以及函数声明均放在这个头文件里.
#include "delay.h"
#include "lcd_12864.h"
#include "get_key.h"
#define blueon PORTB|=(1<<PORTB5)
#define blueoff PORTB&=~(1<<PORTB5)
#define greenon PORTB|=(1<<PORTB6)
#define greenoff PORTB&=~(1<<PORTB6)
#define redon PORTB|=(1<<PORTB7)
#define redoff PORTB&=~(1<<PORTB7)
//SFR初始化.
void main_init(void)
{
DDRA=0xff; //A口设置为输出.
PORTA=0x00;
DDRB=0xff; //B口设置为输出.
PORTB=0x00;
// ASSR&=~(1<<AS2);
DDRC=0x00; //C口设置为输入.
PORTC=0xff; //AVR没有上拉寄存器,在PORTC中写1即表示打开上拉了.
DDRD=0x00; //D口设置为输入.
PORTD=0xff; //打开D口上拉.
}
void main_menu_initial(void) //LED主菜单初始化.
{
main_menu[0].menu_count=3; //有三个菜单项.
main_menu[0].display=blue;
main_menu[0].subs=NULL; //点亮兰灯
main_menu[0].children_menus=blue_menu;
main_menu[0].parent_menus=NULL;
main_menu[1].menu_count=3;
main_menu[1].display=green; //点亮绿灯
main_menu[1].subs=NULL;
main_menu[1].children_menus=green_menu;
main_menu[1].parent_menus=NULL;
main_menu[2].menu_count=3;
main_menu[2].display=red; //点亮红灯.
main_menu[2].subs=openred;
main_menu[2].children_menus=NULL;
main_menu[2].parent_menus=NULL;
}
void blue_menu_initial(void)
{
blue_menu[0].menu_count=2;
blue_menu[0].display=qr; //点亮兰灯,确认.
blue_menu[0].subs=openblue;
blue_menu[0].children_menus=NULL;
blue_menu[0].parent_menus=main_menu;
blue_menu[1].menu_count=2;
blue_menu[1].display=qx; //取消.
blue_menu[1].subs=closeblue;
blue_menu[1].children_menus=NULL;
blue_menu[1].parent_menus=main_menu;
}
void green_menu_initial(void)
{
green_menu[0].menu_count=2;
green_menu[0].display=qr; //点亮绿灯---确认.
green_menu[0].subs=opengreen;
green_menu[0].children_menus=NULL;
green_menu[0].parent_menus=main_menu;
green_menu[1].menu_count=2;
green_menu[1].display=qx; //----取消.
green_menu[1].subs=closegreen;
green_menu[1].children_menus=NULL;
green_menu[1].parent_menus=main_menu;
}
void led_menu_pro(void)
{
max_item=menu_led->menu_count;
switch(keyval)
{
case 0: break;
case 1: //向上键.
if(user_choosen==0)
{
user_choosen=max_item;
}
refresh=1;
user_choosen--;
break;
case 2: //向下键.
user_choosen++;
if(user_choosen==max_item)
{
user_choosen=0;
}
refresh=1;
break;
case 3: //确认键.
if(menu_led[user_choosen].subs!=NULL)
{
(*menu_led[user_choosen].subs)(); //执行函数.
}
if(menu_led[user_choosen].children_menus!=NULL)
{
temp_choosen=user_choosen;
menu_led=menu_led[user_choosen].children_menus;
user_choosen=0;
}
refresh=1;
break;
case 4: //返回键.
if(menu_led[user_choosen].parent_menus!=NULL)
{
menu_led=menu_led[user_choosen].parent_menus;
}
refresh=1;
break;
default:
break;
}
if(refresh) //是否需要刷新LCD标志位.
{
lcd_clr();
refresh=0;
}
led_menu_show();
}
void led_menu_show(void)
{
uchar n;
max_item=menu_led->menu_count;
if (max_item>=3) //菜单项为3则表示为主菜单.
{
for(n=0;n<3;n++)
{
/*使同一数组元素图形宽度相同,即取最大的图形宽度,这里取96,在数组赋值时也要注意*/
draw_bmp(n*2,20,96,0,menu_led[n].display);
}
select_item(user_choosen); //标记出当前菜单项.
}
else
{
switch(temp_choosen)
{
case 0:
draw_bmp(0,20,96,0,blue);
break;
case 1:
draw_bmp(0,20,96,0,green);
break;
case 2:
draw_bmp(0,20,96,0,red);
break;
default:
break;
}
for(n=0;n<max_item;n++)
{
draw_bmp((n+1)*2,20,32,0,menu_led[n].display);
}
select_item(user_choosen+1);
}
}
void select_item(uchar n)
{
draw_bmp(n*2,2,16,0,curflag);
}
void openblue(void)
{
blueon;
greenoff;
redoff;
}
void opengreen(void)
{
blueoff;
greenon;
redoff;
}
void openred(void)
{
blueoff;
greenoff;
redon;
}
void closeblue(void)
{
blueoff;
}
void closegreen(void)
{
greenoff;
}
/*************************************************
主程序.
**************************************************/
void main(void)
{
main_init();
lcd_init();
lcd_clr();
draw_bmp(0,0,128,0,xtcsh); //函数调用形式为页,列,宽度,数据指针.
draw_bmp(2,8,72,0,writeby);
draw_bmp(4,56,32,1,jiangping);
blueon;
greenon;
redon;
delay_ms(3000); //延时3秒.
lcd_clr();
blueoff;
greenoff;
redoff;
main_menu_initial();
blue_menu_initial();
green_menu_initial();
while(1)
{
keyval=get_key(); //读键.
led_menu_pro();
}
}