如果你是一个程序设计师,现在老板要求你基于STC89C52RC单片机写一个程序,能实现排队叫号取号,且只能使用一个单片机,老板的要求是:取号的最大数为20号,按下按键k3取号,并在lcd屏上显示当前排队人数以及所取到的号码,叫号时,通过矩形按键确定所要叫的号码,并在lcd屏上显示所叫的号码,同时蜂鸣器发声,请问为了能完成老板的任务,你会怎么写这段代码
时间: 2024-02-28 16:57:13 浏览: 62
首先,需要连接LCD显示屏和矩形按键、蜂鸣器、按键k3等外设到单片机上。然后,根据老板的要求,可以按照以下步骤编写程序:
1. 初始化LCD显示屏和矩形按键、蜂鸣器、按键k3等外设。
2. 定义一个变量来记录当前排队人数,初始值为0。
3. 当按下按键k3时,将当前排队人数加1,并在LCD屏上显示当前排队人数和所取到的号码。
4. 当按下矩形按键时,读取按键对应的号码,并将其显示在LCD屏上,并通过蜂鸣器发出声音。
5. 当排队人数达到20时,显示“排队已满”的提示信息,并禁止继续取号。
6. 重复步骤3~5,直到所有号码被取完。
下面是一个简单的示例代码,可能需要根据具体硬件和需求做一些修改:
```c
#include <reg52.h>
#include <stdio.h>
#include <string.h>
#define LCD_RS P1_0
#define LCD_RW P1_1
#define LCD_EN P1_2
#define LCD_DAT P2
sbit BEEP = P3^7;
sbit K3 = P3^2;
sbit KEY1 = P3^3;
sbit KEY2 = P3^4;
sbit KEY3 = P3^5;
sbit KEY4 = P3^6;
void Delayms(unsigned int ms)
{
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 120; j++);
}
void LCD_WriteCmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_DAT = cmd;
LCD_EN = 1;
Delayms(1);
LCD_EN = 0;
}
void LCD_WriteData(unsigned char dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DAT = dat;
LCD_EN = 1;
Delayms(1);
LCD_EN = 0;
}
void LCD_Init()
{
LCD_WriteCmd(0x38); // function set: 8-bit, 2-line, 5x7 font
LCD_WriteCmd(0x0C); // display control: display on, cursor off, blink off
LCD_WriteCmd(0x01); // clear display
LCD_WriteCmd(0x06); // entry mode set: cursor move right, no shift
}
void LCD_Clear()
{
LCD_WriteCmd(0x01); // clear display
}
void LCD_SetCursor(unsigned char row, unsigned char col)
{
unsigned char addr;
if (row == 0)
addr = 0x00 + col;
else
addr = 0x40 + col;
LCD_WriteCmd(0x80 | addr); // set DD RAM address
}
void LCD_PrintString(unsigned char row, unsigned char col, char *str)
{
LCD_SetCursor(row, col);
while (*str)
{
LCD_WriteData(*str++);
}
}
unsigned char KEY_Read()
{
unsigned char keyval = 0xFF;
if (!KEY1)
keyval = 1;
else if (!KEY2)
keyval = 2;
else if (!KEY3)
keyval = 3;
else if (!KEY4)
keyval = 4;
return keyval;
}
void main()
{
unsigned char queue_num = 0;
char buf[16];
LCD_Init();
while (1)
{
if (K3)
{
if (queue_num < 20)
{
queue_num++;
sprintf(buf, "No.%02d", queue_num);
LCD_PrintString(0, 0, "Queue: ");
LCD_PrintString(0, 7, buf);
}
else
{
LCD_PrintString(0, 0, "Queue is full! ");
}
while (K3);
}
unsigned char keyval = KEY_Read();
if (keyval != 0xFF)
{
sprintf(buf, "No.%02d", keyval);
LCD_PrintString(1, 0, "Calling: ");
LCD_PrintString(1, 9, buf);
BEEP = 1;
Delayms(100);
BEEP = 0;
}
}
}
```
阅读全文