⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 line_call.c

📁 此程序为一排队机呼叫器,主要功能是:用键盘控制液晶显示实现485通讯,具体协议可参考《排队机通讯协议050609.doc》.编译环境为ICC-AVR6.3,采用了ATmega8芯片,晶振速率为6.00
💻 C
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////
// 排队机呼叫器 1.1版
// 主要功能:键盘+液晶+485通讯
//  具体协议参考《排队机通讯协议050609.doc》
// 编译环境 ICC-AVR6.3      
// 代码目标:Target : Mega8L
// 晶振速率:   6.0000Mhz
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iom8v.h>
#include <macros.h>
extern unsigned char read_flash(unsigned int add);

#define SELF_ADDRESS   0x1fff
#define MASTER_ADDRESS   0x1ff1

#define UChar unsigned char
UChar MasterAddr= 1;
UChar SelfAddr = 9+  1;

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>main.h

unsigned gStatus=10;//初始状态为'A.',即显示本机地址,等待登录
//unsigned gStatus=12;//初始状态为'F.',即功能
void init_devices(void);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<main.h

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>keyboard
unsigned char Key=0,KeyPre=0,KeyCount=0;
unsigned char KeySet[ ]=
{ 0,
  '?', '?', '-', '?',
  'N','/','*','?',
  '?', '0', '.','?', 
  '1', '2', '3','E',
  '4', '5', '6', '?',
  '7', '8', '9', '+' 
 };
 
long int  NumKeyIn=0;
void InitKeyBoard(void);
void int0_isr(void);   //键盘中断服务程序
void KeyScan(void);
void delay_1ms(void);  //延时
void delay(unsigned int n); //延时
void Beep(unsigned char temp);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<keyboard

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>LCD.h
/*****************************************
DATA AND MARK WAS DISPLAYED ON THE LCD
*****************************************/
void lcdwriteCommandData(UChar CommData,UChar RamAdd,UChar lcddata);
void dsp_data(long int x, UChar dot);
void dsp_status( UChar status);
void dsp_Wait_SerNum(unsigned int W, unsigned int S );
void InitLCD(void);
#pragma data:code
 const UChar lcdtable[]={
                       0x0e,0x0b, //'0' 
					   0x06,0x00, //'1' 
					   0x0c,0x07, //'2'
					   0x0e,0x05, //'3'
					   0x06,0x0c, //'4'
                       0x0a,0x0d, //'5'  
					   0x0a,0x0f, //'6'
					   0x0e,0x00, //'7'
					   0x0e,0x0f, //'8' 
					   0x0e,0x0d, //'9'
					   
					   0x0E,0x0E,   //'A.'     10
					   0x02,0x0f,   //'b.'     11
					   0x08,0x0b,   //'C.'     12
   					   0x06,0x07,   //'d.'	   13			   
   					   0x08,0x0f,   //'E.'      14
   					   0x08,0x0e,   //'F.'      15
  					   0x01,0x0b,   //'G.'     16
                       0x01,0x0b,   //'H.'     17
					   0x01,0x0b,   //'I.'	     18
					   0x01,0x0b,   //'J.'       19				   
					   0x01,0x0b,   //'L.'      20
					   0x0d,0x0e,   //'P.'       21
					   0x09,0x0a,   //'T.'       22 
					   0x07,0x0b,   //'U.'       23      
                       };
					   
					   
#pragma data:data
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<LCD.h
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Comm.h

#define RXBUFLENGTH 16


UChar CommReceiveFlag=0;
UChar RxBuf[RXBUFLENGTH],TxBuf[RXBUFLENGTH];
void uart0_init(void);
void putchar(UChar c);
int puts(char *s);
void send_message(UChar TargetAddr,UChar MsgType,UChar * Msgbuf);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Comm.h

void main(void)
{
  UChar i;
 UChar NumLed=1; //初始状态为数字键盘 

 UChar dot=0;
 
 UChar MsgBuf[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 
 DDRD=0xF0;	    /* 端口D 高4位输出   控制液晶*/
 PORTD=0x1F;	/* Rw=0 Dc=0 E=0 CS1=1*/
 DDRB=0xff;     /* 端口B 输出        控制液晶数据线*/
 PORTB=0xff;
 
 DDRC |=0x20; //键盘数字键切换,输出
 PORTC |=0x20;//数字灯点亮
 
 init_devices();
/*
 putchar(0x0a); //换行
 putchar(0x0d); //回车	  
 putchar(0x0a); //换行
 
 puts("---------------------------------------------");
 puts("             排队机呼叫器                     "); 
 puts("            你好!北京                    ");
 puts("欢迎你使用此系统!");
 puts("             如有什么意见与我联系                    ");
 */  
 while(1)  //进入循环处理
 {      	               	   
    KeyScan();
   
    if(Key || CommReceiveFlag)Beep(100);
       
	//dsp_data(KeyCount,0);	   
	if( gStatus==10  )
	   {
	        if( KeyPre=='N')
			{
		           NumLed=1;//使能数字键
	       		   PORTC |=0x20;		   		   	 
			       
				    //进入登录状态
				   {
 				   //等待输入操作员编号
				     gStatus=23;
                     NumKeyIn=0;
					 dsp_data(NumKeyIn,0);
	    			 dsp_status(gStatus);				   
				   }
			}	   
	   }
	else
	{
	 if(KeyCount==5)
	 switch(KeyPre)
	 {	 
	   case 'N':         	 
			       
				    //退出登录状态				   
				     gStatus=10;
                    //通知服务器,本终端退出登录,服务器收回最新分配的任务
    				 send_message(MasterAddr, 'd', MsgBuf);
 				     //显示待登录状态
					 dsp_data(SelfAddr-9,0);
	                 dsp_status(gStatus);  //  'A' 显示当前呼叫器地址,等待登录
				   			   				   
	   			   break;
				   
		case '0': 
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
                   if(NumKeyIn<100000)		
		           NumKeyIn=(NumKeyIn*10+KeyPre-0x30) ;
                   dsp_data(NumKeyIn,0);
				   dsp_status(gStatus);	               	   
	   			   break;							   
		case '1':   
                   if(NumLed) //数字键有效
		           {		
                     if(NumKeyIn<100000)		
		             NumKeyIn=(NumKeyIn*10+KeyPre-0x30 ) ;
				   }	
				   else //功能键 转移
				   {
                     //		   ####04   ,此时键入的数据为窗口号
				     NumKeyIn=NumKeyIn*100+0x4;
				     for(i=0;i<6;i++)
						 {
						   MsgBuf[5-i]=NumKeyIn%10+'0';
						   NumKeyIn=NumKeyIn/10;
					     }
					 send_message(MasterAddr, 'c', MsgBuf);  				      
				   }               	   
				   dsp_data(NumKeyIn,0);
			    	   dsp_status(gStatus);
	   			   break;	
		case '2':
                   if(NumLed) //数字键有效
		           {		
                       if(NumKeyIn<100000)		
	    	           NumKeyIn=(NumKeyIn*10+KeyPre-0x30) ;
                       
					 }	
					 else  //延后
					 {
					  //		   ####05   ,此时键入的数据代表欲延后的人次
				     NumKeyIn=NumKeyIn*100+0x5;
				     for(i=0;i<6;i++)
						 {
						   MsgBuf[5-i]=NumKeyIn%10+'0';
						   NumKeyIn=NumKeyIn/10;
					     }
					 send_message(MasterAddr, 'c', MsgBuf);  //1完毕,2重呼,3取消
					 } 
					 dsp_data(NumKeyIn,0);
			    	   dsp_status(gStatus);              	   
	   			   break;		   		
	case '/':   //功能键,切换数字键和功能键
                   NumLed=!  NumLed;
	       		   if(NumLed) {PORTC |=0x20;}
		   		   else PORTC &= 0xDF;
				   break;		   		
	case '*':   //清屏幕
	    	         NumKeyIn=0;
                     dsp_data(NumKeyIn,0);
			    	 dsp_status(gStatus);
				   break;		   		

    case '-':   //弃号
                   //####02,####代表客户流水号
//				     NumKeyIn=(NumKeyIn%10000)*100+0x2;
                     NumKeyIn=0x2;
				     for(i=0;i<6;i++)
						 {
						   MsgBuf[5-i]=NumKeyIn%10+'0';
						   NumKeyIn=NumKeyIn/10;
					     }
					 send_message(MasterAddr, 'c', MsgBuf);  
	    	         NumKeyIn=0;
                     dsp_data(NumKeyIn,0);
			    	 dsp_status(gStatus);
				   break;		   		
    case '+':   //插入号
                   //####03,####代表待插入客户流水号
//				     NumKeyIn=(NumKeyIn%10000)*100+0x3;
				     NumKeyIn=NumKeyIn*100+0x3;
				     for(i=0;i<6;i++)
						 {
						   MsgBuf[5-i]=NumKeyIn%10+'0';
						   NumKeyIn=NumKeyIn/10;
					     }
					 send_message(MasterAddr, 'c', MsgBuf);  
	    	         NumKeyIn=0;
                     dsp_data(NumKeyIn,0);
			    	 dsp_status(gStatus);
				   break;		   		
    case 'E':   //顺呼
                   //####00,顺呼,####代表刚处理过客户
//				     if(gStatus==15)NumKeyIn=(NumKeyIn%10000)*100+0x0;
				     if(gStatus==15)NumKeyIn=0x0;
				     for(i=0;i<6;i++)
						 {
						   MsgBuf[5-i]=NumKeyIn%10+'0';
						   NumKeyIn=NumKeyIn/10;
					     }
					 if(gStatus==15)       send_message(MasterAddr, 'c', MsgBuf);  //顺呼
					 else if(gStatus==23)send_message(MasterAddr, 'a', MsgBuf);  //工位号
					 else if(gStatus==21)send_message(MasterAddr, 'b', MsgBuf);  //密码
					     
	    	         NumKeyIn=0;
                     dsp_data(NumKeyIn,0);
			    	 dsp_status(gStatus);
				   break;		   		
    case '.':   //重呼
                   //####01,重呼,####代表客户流水号
//				     NumKeyIn=NumKeyIn%10000*100+0x1;
				     NumKeyIn=0x1;
				     for(i=0;i<6;i++)
						 {
						   MsgBuf[5-i]=NumKeyIn%10+'0';
						   NumKeyIn=NumKeyIn/10;
					     }
					 send_message(MasterAddr, 'c', MsgBuf);  
	    	         NumKeyIn=0;
                     dsp_data(NumKeyIn,0);
			    	 dsp_status(gStatus);
				   break;		   		
				   			      	   
	   default:break;
	 }
	}	
	
	
	 //通讯
	 if(CommReceiveFlag)
	 {
       CommReceiveFlag=0;
	   switch(gStatus)
	   {
	     case 23:
		            dsp_data(0,0);
                    dsp_status(gStatus);  
		           break;

	     case 21:
		            dsp_data(0,0);
                    dsp_status(gStatus);  
		           break;	
		
	     case 15:
		           // dsp_data(NumKeyIn,4);
					dsp_Wait_SerNum(NumKeyIn/10000, NumKeyIn%10000 );
                    dsp_status(gStatus);  
					NumKeyIn=0;
		           break;		      
	   }  
	 }	 
 	
  }  
}


//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
// SelfAddr=read_flash(SELF_ADDRESS) ;
// SelfAddr= (SelfAddr % 29) +10;  // 呼叫器的地址10-39
// MasterAddr=read_flash(MASTER_ADDRESS);
 InitLCD();    
 InitKeyBoard();
  
 uart0_init();
 
 MCUCR = 0x0A;
 GICR  =   0x00;//禁止外部中断
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>keyboard

void InitKeyBoard(void)
{
  //键盘行线
  DDRB  &= 0xE0;//PB口低5位输入
  PORTB |=0x1F;//PB口低5位输入上拉
  //键盘列线
  DDRC   |= 0x0F;//PC口低4位输出
  PORTC &=0xF0;//PC口低4位输出低电平  
}
 void delay_1us(void)//延时,用于液晶
 {
   unsigned int i;
     for(i=1;i<10;i++);
 }
 void delay_1ms(void)  //延时
 {
  unsigned int i;
  for(i=1;i<115;i++);
 
 }
void delay(unsigned int n) //延时
{
 unsigned int i=0;
 while(i<n)
 {
  delay_1ms();
  i++;
  }
}

#pragma interrupt_handler int0_isr:2
void int0_isr(void)   //键盘中断服务程序
{
  return;  
}

void KeyScan(void)
{
  
  //键盘行线
  DDRB  &= 0xC0;//PB口低6位输入
  PORTB |=0x3F;//PB口低6位输入上拉
  //键盘列线
  DDRC   |= 0x0F;//PC口低4位输出
  PORTC &=0xF0;//PC口低4位输出低电平
  //行输入 ,列输出,
  delay(8);   //延时约5毫秒

                          				//行扫描
 if       ((PINB  &   0x01) ==0)Key=1;			    //第一行
 else if((PINB &    0x02) ==0)Key=5; 			 // 第二行  
 else if((PINB &	0x04) ==0)Key=9;				 // 第三行
 else if((PINB &	0x08) ==0)Key=13;			// 第四行
 else if((PINB &	0x10 )==0)Key=17;			// 第五行
 else if((PINB &	0x20 )==0)Key=21;			// 第六行
 else Key=0;

 //行输出,列输入
 DDRC  &=0xF0; PORTC |=  0x0F;		//列输入拉高
 DDRB  |=0x3F;  PORTB  &=0xC0;  //行输出低电平 
		
 delay(8);   //延时约5毫秒

 if			   ((PINC&0x01)==0) Key=Key+0;              //列扫描
 else if	 ((PINC&0x02)==0) Key=Key+1;
 else if	 ((PINC&0x04)==0) Key=Key+2;
 else if	 ((PINC&0x08)==0) Key=Key+3;
 else        Key=0;
 // dsp_data(Key,0);
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -