一、主要功能
本項目使用Proteus8.12仿真51單片機控制器進行雙機通信,使用MAX232通信模塊、LCD1602液晶、按鍵模塊、蜂鳴器LED模塊等。
系統(tǒng)運行后,LCD1602顯示開機界面,當從機病床按鍵按下,主機LCD1602顯示說按病床號且對應(yīng)指示燈亮,蜂鳴器報警。此時主機中護士應(yīng)答鍵按下,則清除本次病床號,同時對應(yīng)LED滅。當多床號鍵被按下,LCD1602依次順序顯示,此時主機中護士應(yīng)答鍵被按下,按優(yōu)先順序依次處理,LED滅。
主要功能:
(1)從機具有8床位呼叫;
(2)主機LCD顯示和應(yīng)答;
(3)燈光報警提示;
(4)順序排列應(yīng)答處理。
二、硬件資源
51單片機核心模塊、MAX232模塊、LCD1602液晶模塊、蜂鳴器模塊、LED電路、按鍵模塊。
三、軟件設(shè)計
(1)主機程序:
/*
#include "appdemo.h"
#include "key.h"
#include "lcd1602.h"
#include "beep.h"
#include "time.h"
#include "uart.h"
#define LED_PORT P1
_sys_ctrl sys_ctrl;
//系統(tǒng)參數(shù)初始化
void sys_parm_init(void)
{
sys_ctrl.id=0;
sys_ctrl.sta=1;
sys_ctrl.cnt=0;
}
//系統(tǒng)開機界面顯示
void sys_open_show(void)
{
lcd1602_clear();
lcd1602_show_string(0,0,"Ward Call System");
lcd1602_show_string(0,1," Quiet Please ");
while(RI==0);
lcd1602_clear();
lcd1602_show_string(0,0,"ID ID ID ID ");
lcd1602_show_string(0,1,"ID ID ID ID ");
}
//系統(tǒng)參數(shù)控制
void sys_parm_ctrl(void)
{
u8 key=0;
static u8 cnt=0;
u8 i=0;
while(1)
{
key=key_scan(0);
//如果護士應(yīng)答,則清除本次呼叫
if(key==KEY1_PRESS)
{
cnt++;
//應(yīng)答后床位號刷新顯示,取消顯示應(yīng)答床位的ID
switch(cnt)
{
case 1: lcd1602_show_num(2,0,' ',0);break;
case 2: lcd1602_show_num(6,0,' ',0);break;
case 3: lcd1602_show_num(10,0,' ',0);break;
case 4: lcd1602_show_num(14,0,' ',0);break;
case 5: lcd1602_show_num(2,1,' ',0);break;
case 6: lcd1602_show_num(6,1,' ',0);break;
case 7: lcd1602_show_num(10,1,' ',0);break;
case 8: lcd1602_show_num(14,1,' ',0);break;
}
//對應(yīng)床位指示燈熄滅
switch(UART_RX_BUF[cnt-1])
{
case 1: LED_PORT|=0X01;break;
case 2: LED_PORT|=0X02;break;
case 3: LED_PORT|=0X04;break;
case 4: LED_PORT|=0X08;break;
case 5: LED_PORT|=0X10;break;
case 6: LED_PORT|=0X20;break;
case 7: LED_PORT|=0X40;break;
case 8: LED_PORT|=0X80;break;
}
sys_ctrl.cnt--;
if(sys_ctrl.cnt<=0)
{
sys_ctrl.cnt=0;
cnt=0;
}
}
break;
}
}
//系統(tǒng)數(shù)據(jù)顯示
void sys_data_show(void)
{
while(1)
{
//病房有鍵按下
if(sys_ctrl.sta==0)
{
//按照按下順序LCD顯示床位號
switch(sys_ctrl.cnt)
{
case 1: lcd1602_show_num(2,0,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
case 2: lcd1602_show_num(6,0,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
case 3: lcd1602_show_num(10,0,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
case 4: lcd1602_show_num(14,0,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
case 5: lcd1602_show_num(2,1,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
case 6: lcd1602_show_num(6,1,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
case 7: lcd1602_show_num(10,1,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
case 8: lcd1602_show_num(14,1,UART_RX_BUF[sys_ctrl.cnt-1],1);break;
}
//對應(yīng)床位指示燈顯示
switch(UART_RX_BUF[sys_ctrl.cnt-1])
{
case 1: LED_PORT&=0XFE;break;
case 2: LED_PORT&=0XFD;break;
case 3: LED_PORT&=0XFB;break;
case 4: LED_PORT&=0XF7;break;
case 5: LED_PORT&=0XEF;break;
case 6: LED_PORT&=0XDF;break;
case 7: LED_PORT&=0XBF;break;
case 8: LED_PORT&=0X7F;break;
}
BEEP=0; //蜂鳴器響
delay_ms(1000);
BEEP=1; //蜂鳴器關(guān)閉
sys_ctrl.sta=1;
}
break;
}
}
//主機應(yīng)用系統(tǒng)程序
void appdemo_show(void)
{
sys_parm_init(); //系統(tǒng)參數(shù)初始化
UART_Init();
lcd1602_init();
// time0_init(); //定時50ms
sys_open_show(); //系統(tǒng)開機界面顯示
while(1)
{
sys_data_show();//系統(tǒng)數(shù)據(jù)顯示
sys_parm_ctrl();//系統(tǒng)參數(shù)控制
}
}
void UART_IRQn() interrupt 4
{
ES=0;//關(guān)閉中斷
if(RI)
{
RI=0;
if(sys_ctrl.cnt
{
UART_RX_BUF[sys_ctrl.cnt]=SBUF-0x30;
//連續(xù)兩次相同鍵視為一次有效
if(sys_ctrl.cnt>=1)
if(UART_RX_BUF[sys_ctrl.cnt]==UART_RX_BUF[sys_ctrl.cnt-1])
sys_ctrl.cnt-=1;
sys_ctrl.cnt++;
}
sys_ctrl.sta=0;
}
ES=1;//打開中斷
}
//定時器0中斷函數(shù)
void time0(void) interrupt 1
{
static u8 i=0;
TH0=0X4C; //給定時器賦初值,定時50ms
TL0=0X00;
i++;
if(i>=4)//間隔500ms
{
i=0;
}
}
(2)從機程序
#include "appdemo.h"
#include "key.h"
#include "time.h"
#include "uart.h"
sbit LED=P0^0;
_sys_ctrl sys_ctrl;
//系統(tǒng)參數(shù)初始化
void sys_parm_init(void)
{
}
//從機應(yīng)用系統(tǒng)程序
void appdemo_show(void)
{
u8 key=0;
sys_parm_init();
// time0_init();
UART_Init();
while(1)
{
key=key_scan(0);
if(key!=0)LED=!LED;
if(key==KEY1_PRESS)UART_SendData('1');
else if(key==KEY2_PRESS)UART_SendData('2');
else if(key==KEY3_PRESS)UART_SendData('3');
else if(key==KEY4_PRESS)UART_SendData('4');
else if(key==KEY5_PRESS)UART_SendData('5');
else if(key==KEY6_PRESS)UART_SendData('6');
else if(key==KEY7_PRESS)UART_SendData('7');
else if(key==KEY8_PRESS)UART_SendData('8');
}
}
四、實驗現(xiàn)象
評論
查看更多