/*****************************************************
Project : 餐饮自动点菜服务系统
Version : 2.10
Date : 2010-4-28
Author : DOLL
Company : MicSin
Comments: 餐饮自动点菜服务系统
Chip type : ATmega16
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 96
*****************************************************/
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong; // Type Define
#include <mega16l.h>
#include <delay.h>
//****************************************************
// 定义说明
#define fosc 8000000 // 晶振频率
#define LED_COM0 PORTC.0 // LED显示位选
#define LED_COM1 PORTC.1
#define LED_COM2 PORTC.6
#define LED_COM3 PORTC.7
#define KEY_SEG0 DDRD.3 //按键部分【segment:部分】
#define KEY_SEG1 DDRD.4
#define KEY_SEG2 DDRD.5
#define BEEP PORTD.2
// WDTCR
#define WDTOE 4
#define WDE 3
#define WDP2 2
#define WDP1 1
#define WDP0 0
// Serial Port
#define TXB8 UCSRB.0
#define RXB8 1
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
// USATR
#define FRAMING_ERROR (1<<FE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define MPCM UCSRA.0
#define TXCIE UCSRB.6
#define RXCIE UCSRB.7
#define TXEN UCSRB.3 // 发送允许
#define RXEN UCSRB.4 // 接收允许
// Macro define //大量的宏定义
#define _WDR() #asm("wdr") //函数的宏定义
#define _SEI() #asm("sei")
#define _CLI() #asm("cli")
//命令字
#define mUartHeadCode 0x55
#define mH_setnum_irq 0xf0 //设置房号请求
#define mH_setnum_ack 0xf1 //设置房号请求回应
#define mH_setnum_dat 0xf2 //设置房号数据
#define SetNum_Access_Code1 0xAA //特征代码
#define SetNum_Access_Code2 0xBB
#define SetNum_Access_Code3 0xCC
#define SetNum_Access_Code4 0xDD
#define SetNum_Access_Code5 0xEE
/*
0x55,0xf0,0x00,0xAA,0xBB,0xCC,0xDD,0xEE,0x00,0x00,0x00,0x00,0x00,add //to PT
0x55,0xf1,0x00,0xAA,0xBB,0xCC,0xDD,0xEE,0x00,0x00,0x00,0x00,0x00,add //PT ACK
0x55,0xf2,0x00,0xAA,0xBB,0xCC,0xDD,0xEE,0xXX,0xYY,0xZZ,0x00,0x00,add //to PT
0x55,0xf1,0x00,0xAA,0xBB,0xCC,0xDD,0xEE,0xXX,0xYY,0xZZ,0x00,0x00,add //PT ACK
XX: channel (0~3)通道号
YY: Layer (层号)
ZZ: Room (房号)
*/
#define mRxDataLength 14
#define mKeyBuffLength 20
#define mPassWordLength 8
#define mRoomBuffLength 4
register uint KeyValue @2; //register是寄存器类型, 因为寄存器访问速度最快
bit Key_N; // 取消 *
bit Key_7;
bit Key_4;
bit Key_1;
bit Key_0;
bit Key_8;
bit Key_5;
bit Key_2;
bit Key_Y; // 确定 #
bit Key_9;
bit Key_6;
bit Key_3;
#pragma used+ //为什么上面不用#pragma used+
bit Key_A;
bit Key_B;
bit Key_C;
bit Key_D;
#pragma used-
//bit bPassWordInputEnable;
bit bRxEn; // 允许接收标志
bit bRxOK; // 接收有效标志
bit bAckSucess; // 回应成功标志
bit bTxBuffEmpty; // 发送缓冲区空标志
uchar cRxDelayCnt; // 接收延时计数器(ms)
uchar cRxCnt; // 接收字节计数器
uchar cRxChkSum; // 校验和
uchar cTxCnt; // 发送字节计数器
uchar cTxCommand; // 被发送的命令
uchar cChannelNum;
uchar cLayerNum;
uchar cRoomNum;
uint iBeep_Cnt; // 蜂鸣器延时计数器
uint cWait_Cnt;
uchar cRxBuff[mRxDataLength]; // 接收缓冲区
uchar cTxBuff[mRxDataLength]= // 发送缓冲区
{0x55,0xf0,0x00,0xAA,0xBB,0xCC,0xDD,0xEE,0x00,0x00,0x00,0x00,0x00,0x41};
uchar cRxData[mRxDataLength]; // 接收数据
#define mcTxCommand 1
#define mcRxCommand 1
#define mcChannelNum 8
#define mcLayerNum 9
#define mcRoomNum 10
uchar Led_Buff[4]; // LED显示缓冲区
//uchar Str_temp[4]; // 字符串临时数组(数据传递)
uchar Key_ch_0[mRoomBuffLength];
uchar Key_ch_1[mRoomBuffLength];
uchar Key_ch_2[mRoomBuffLength];
uchar Key_ch_3[mRoomBuffLength];
flash uchar Led_digit[] = // led 送显字符码,如显示小数点,当前字符减0x20
{0x28,0xEB,0x32,0xA2,0xE1,0xA4,0x24,0xEA,0x20,0xE0};
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
flash uchar Led_char[] = // 显示字符
{0x34,0x77,0x24,0xBF,0x23,0x27,0x74,0xF7,0x37,0x65,0xA4,0x35,0x29};
// E, r, G, _, d, o, F,— , c, h, S, t, U
/*
flash uchar PassWord[mPassWordLength] =
{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
*/
#define mChar_E 0 // 字符 E
#define mChar_r 1 // 字符 r
#define mChar_G 2 // 字符 G
#define mChar__ 3 // 字符 _
#define mChar_d 4 // 字符 d
#define mChar_o 5 // 字符 o
#define mChar_F 6
#define mChar_— 7
#define mChar_c 8
#define mChar_h 9
#define mChar_S 10
#define mChar_t 11
#define mChar_U 12
//*****************************************************
// 中断服务程序
//*****************************************************
//--------------------------------------------------------------
interrupt [USART_RXC] void uart_rx_isr(void) // 串口接收中断
{
uchar data;
data = UDR;
cRxDelayCnt = 100; // 接收延时计数器清零
if(bRxEn && (cRxCnt = mRxDataLength))
{
cRxBuff[cRxCnt] = data;
cRxCnt++;
if(cRxCnt >= mRxDataLength)
{
bRxEn = 0;
if(data == cRxChkSum)
{
if(!bRxOK)
{
bRxOK = 1;
}
}
}
else
{
cRxChkSum += data; // 计算校验和
}
}
else
{
if(!bRxOK && (data == mUartHeadCode)) // 头码匹配,允许接收
{
bRxEn = 1;
cRxCnt = 1;
cRxBuff[0] = data;
cRxChkSum = data;
}
}
}
//--------------------------------------------------------------
interrupt [USART_TXC] void uart_tx_isr(void) // 串口发送中断
{
cTxCnt++;
if(cTxCnt mRxDataLength)
{
UDR = cTxBuff[cTxCnt];
}
else
{
bTxBuffEmpty = 1;
UCSRB |= 0x10; // 允许接收
}
}
//--------------------------------------------------------------
interrupt [TIM2_COMP] void timer2_comp_isr(void) // 中断时间 1ms
{
static uchar t2_cnt; // 中断计数器
static uint Key_Old;
static uint Key_New;
static uint Key_Last;
static uchar Key_Act; // 键盘活动标志
uchar dat;
if(--t2_cnt > 3)
{
t2_cnt = 3;
}
dat = PINB & 0x0F;
PORTA = 0xFF;
PORTC = 0xFF; // 关闭显示
switch(t2_cnt)
{
case 0:
PORTA = Led_Buff[3];
LED_COM0 = 0;
Key_Old = Key_New; // 保存键值
KEY_SEG2 = 0; // 准备扫描 SEG0
KEY_SEG0 = 1;
break;
case 1:
PORTA = Led_Buff[2];
LED_COM1 = 0;
*((uchar*)(&Key_New) + 1) = dat | 0xF0; //强制转换
评论0