广工嵌入式系统课程设计——单片机售票机

        呵呵,这就是大学时期最后一个课程设计,虽然称为嵌入式系统XXX,但是用的只是51单片机。。。

 写了一下这个售票机小程序,感觉还不错,所以就贴出来跟大家分享一下。

 

/***************************************************
 File name : 2333.c
 Created date : 2013-11-19 22:43
 E-mail : luhuadong@163.com
 课程设计——单片机售票机(程序三)
 **************************************************/
 
#include <reg52.h>
 
#define uchar unsigned char
#define uint unsigned int
 
typedef struct ticket       //定义ticket结构体,用于存放各种票类
{                             
       uint mode;      //地点
       uint price;       //票价
}ticket;
 
sbit first = P1^5;    //数码管个位
sbit second = P1^4;      //数码管十位
sbit third = P1^3;   //数码管百位
sbit fourth = P1^2; //数码管千位
 
sbit k = P1^1;        //k 设置位
//共阴极数码管(正接)码表
uchar code table[11] = {0x3f,0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x00};
 
/******** 延时xms毫秒子函数 ********/
void delayms(uint xms)
{
       uint i, j;
       for(i=0; i<xms; i++)
              for(j=0; j<110; j++);
}
 
/************ 数码管动态显示子函数 ************/
void display(uint a, uint b,uint c, uint d)
{
       first = second = third = fourth = 1;       //初始化关闭位选
       first = 0;  //显示个位
       P2 = table[a];
       delayms(5);
       first = 1;
       second = 0;    //显示十位
       P2 = table[b];
       delayms(5);
       second = 1;   
       third = 0; //显示百位
       P2 = table[c];
       delayms(5);
       third = 1;
       fourth = 0;      //显示千位
       P2 = table[d];
       delayms(5);
       fourth = 1;     
       P2 = table[10];      //关闭段选
}
 
/****************** 主函数 *********************/
void main()
{
       uint a=0, b=0, c=0, d=0;      //a,b, c, d 分别为个,十,百,千位
       uchar temp = 0x00;       //保存P0口状态
       int flag = 0;           //matrixkeyboard 标志位
 
       ticket gz = {1, 0};   //初始化三个地点
       ticket sh = {2, 0};   //(gz:广州 sh:上海 bj:北京)
       ticket bj = {3, 0};   //代号分别是:1, 2, 3
      
       uint mode = 0;       //按地点分类
       uint price = 0; //客户购买的单价
       uint amount = 0;//客户购买的票数
       uint total = 0;  //客户购买的总价
 
       while(1)
       {
              display(a%10, b%10, c%10, d%10);
//------------------------------------------------------------------------------------------
              P1 = 0xfb;      //column_1(P1.2)
              P0 = 0xff;
              temp = P0;
              if(temp != 0xff)
              {
                     delayms(1);    //去抖动延时
                     temp = P0;
                     if(temp != 0xff)
                     {
                            switch(temp)
                            {
                                   //"广州"按键
                                   case 0x7f : if(k == 1) { mode =gz.mode ;}
                                                        if(k == 0){price = gz.price; a=b=c=d=0;}
                                                        break;            //row_1
                                   //"十位"按键
                                   case 0xbf : b++; break;  //row_2
                            }
                            flag = 1;  //键盘标志位
                            while(flag)      //等待放键程序(十分关键)
                            {
                                   temp = P0;
                                   if(temp != 0xff)
                                   {
                                          display(a%10, b%10, c%10,d%10);
                                          P1 = 0xfb;      //P1是键盘和数码管共用的,调用显示程序后要复位
                                   }
                                   else flag = 0;
                            }
                     }
              }//end of scan column_1
//------------------------------------------------------------------------------------------
              P1 = 0xf7;      //column_2(P1.3)
              P0 = 0xff;
              temp = P0;
              if(temp!=0xff)
              {
                     delayms(1);    //去抖动延时
                     temp = P0;
                     if(temp != 0xff)
                     {
                            switch(temp)
                            {
                                   //"千位"按键
                                   case 0x7f : d++; break;  //row_1
                                   //"上海"按键
                                   case 0xbf : if(k == 1) { mode =sh.mode ;}
                                                        if(k == 0){price = sh.price; a=b=c=d=0;}
                                                        break;            //row_2
                            }
                            flag = 1;  //键盘标志位
                            while(flag)      //等待放键程序
                            {
                                   temp = P0;
                                   if(temp != 0xff)
                                   {
                                          display(a%10, b%10, c%10,d%10);
                                          P1 = 0xf7;      //P1是键盘和数码管共用的,调用显示程序后要复位
                                   }
                                   else flag = 0;
                            }
                     }
              }//end of scan column_2
//------------------------------------------------------------------------------------------
 
              P1 = 0xef;      //column_3(P1.4)
              P0 = 0xff;
              temp = P0;
              if(temp!=0xff)
              {
                     delayms(1);    //去抖动延时
                     temp = P0;
                     if(temp != 0xff)
                     {
                            switch(temp)
                            {
                                   //"个位"按键
                                   case 0x7f : a++; break;  //row_1
                                   //"百位"按键
                                   case 0xbf : c++; break;  //row_2
                            }
                            flag = 1;  //键盘标志位
                            while(flag)      //等待放键程序
                            {
                                   temp = P0;
                                   if(temp != 0xff)
                                   {
                                          display(a%10, b%10, c%10,d%10);
                                          P1 = 0xef;      //P1是键盘和数码管共用的,调用显示程序后要复位
                                   }
                                   else flag = 0;
                            }
                     }
              }//end of scan column_3
//------------------------------------------------------------------------------------------
             
              P1 = 0xdf;      //column_4(P1.5)
              P0 = 0xff;
              temp = P0;
              if(temp!=0xff)
              {
                     delayms(1);    //去抖动延时
                     temp = P0;
                     if(temp != 0xff)
                     {
                            switch(temp)
                            {
                                   //"北京"按键
                                   case 0x7f : if(k == 1) { mode =bj.mode ;}
                                                        if(k == 0){price = bj.price; a=b=c=d=0;}
                                                        break;            //row_1
                                   //"SET"按键
                                   case 0xbf : if(k == 1)
                                                        {
                                                               price= (a%10)+(b%10)*10+(c%10)*100+(d%10)*1000;
                                                               if(mode== gz.mode) {gz.price = price;}
                                                               elseif(mode == sh.mode) {sh.price = price;}
                                                               elseif(mode == bj.mode) {bj.price = price;}
                                                               a = b= c = d = 0;  //清零
                                                               price= mode = 0;
                                                        }
                                                        if(k == 0)
                                                        {
                                                               if(price!= 0)
                                                               {
                                                                      amount= (a%10)+(b%10)*10+(c%10)*100+(d%10)*1000;
                                                                      total= price * amount + total;
                                                                      d= (total/1000)%10;
                                                                      c= (total/100)%10;
                                                                      b= (total/10)%10;
                                                                      a= (total/1)%10;
                                                                      price= 0;
                                                               }
                                                               else
                                                               {
                                                                      total= 0;
                                                                      a= b = c = d = 0;  //清零
                                                               }
                                                        }
                                                        break;            //row_2
                            }
                            flag = 1;  //键盘标志位
                            while(flag)      //等待放键程序
                            {
                                   temp = P0;
                                   if(temp != 0xff)
                                   {
                                          display(a%10, b%10, c%10,d%10);
                                          P1 = 0xdf;      //P1是键盘和数码管共用的,调用显示程序后要复位
                                   }
                                   else flag = 0;
                            }
                     }
              }//end of scan column_4
       }//end of while(1)
}//end of main
 
/***********************************************end ***********************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿基米东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值