欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 294|回復(fù): 0
收起左側(cè)

基于51單片機(jī)電梯設(shè)計(jì)內(nèi)外機(jī)程序源碼

[復(fù)制鏈接]
ID:578150 發(fā)表于 2025-11-28 16:17 | 顯示全部樓層 |閱讀模式
內(nèi)機(jī):

  1. /**********************************
  2. 包含頭文件
  3. **********************************/
  4. #include "main.h"
  5. #include "key.h"
  6. #include "tube.h"
  7. #include "timer.h"
  8. #include "uart.h"
  9. #include "interrupt.h"


  10. /**********************************
  11. 變量定義
  12. **********************************/
  13. uchar key_num = 0;                                                                                        //按鍵掃描標(biāo)志位
  14. uint time_num = 0;                                                                                        //10ms計(jì)時(shí)變量

  15. bit flag_send = 0;                      //發(fā)送標(biāo)志位

  16. bit flag_abnormal = 0;                  //電梯異常標(biāo)志位

  17. uchar current_old = 0;                  //上次當(dāng)前樓層
  18. uchar current_floor = 1;                //當(dāng)前樓層
  19. uint target_floor = 1;                  //目標(biāo)樓層
  20. uchar flag_floor_u[10] = {0};           //樓層上標(biāo)志位
  21. uchar flag_floor_d[10] = {0};           //樓層下標(biāo)志位
  22. bit flag_stop = 1;                      //到達(dá)目標(biāo)樓層停留完成否標(biāo)志位

  23. extern bit flag_door;                   //電梯門(mén)的狀態(tài)
  24. uint flag_lift = 0;                     //當(dāng)前升降標(biāo)志位
  25. extern bit flag_bujin_1_o;                                                        //升降步進(jìn)電機(jī)正轉(zhuǎn)標(biāo)志位
  26. extern bit flag_bujin_1_c;                                                        //升降步進(jìn)電機(jī)反轉(zhuǎn)標(biāo)志位
  27. extern bit flag_bujin_2_o;                                                        //門(mén)步進(jìn)電機(jī)正轉(zhuǎn)標(biāo)志位
  28. extern bit flag_bujin_2_c;                                                        //門(mén)步進(jìn)電機(jī)反轉(zhuǎn)標(biāo)志位

  29. bit flag_maopi = 0;                                                                            //第一次毛皮重量標(biāo)志位
  30. ulong weight_maopi = 0;                                                                  //毛皮重量
  31. uint  weight_value = 0;                                                                  //重量值
  32. uint  weight_test = 0;                                                                  //重量值

  33. uchar send_buf[10]={0x99,0x00};         //發(fā)送數(shù)組;

  34. extern uchar uart_buf[16];              //串口接收緩存區(qū)
  35. extern uchar uart_count;                //串口接收計(jì)數(shù)

  36. extern bit flag_timer_begin;                                                //計(jì)時(shí)開(kāi)始標(biāo)志位
  37. extern bit flag_time;                                                                //定時(shí)1s完成標(biāo)志位

  38. extern bit flag_timer_begin_s;          //停留時(shí)間開(kāi)始標(biāo)志位
  39. extern bit flag_time_s;                 //停留時(shí)間到達(dá)標(biāo)志位
  40. extern uint timer1_count_s;             //停留時(shí)間計(jì)數(shù)

  41. extern uchar Speed;

  42. extern bit Stop_Flag ;                                                                                                                        //升降步進(jìn)電機(jī)停止標(biāo)志位
  43. extern bit speed_alarm;                                                                                //速度
  44. extern bit Stop;                                                                                                                        //升降步進(jìn)電機(jī)停止標(biāo)志位

  45. /**********************************
  46. 函數(shù)聲明
  47. **********************************/
  48. void Delay_function(uint x);                                                //延時(shí)函數(shù)(ms)
  49. void Key_function(void);                                                        //按鍵函數(shù)
  50. void Monitor_function(void);                                                //監(jiān)測(cè)函數(shù)
  51. void Display_function(void);                                                //顯示函數(shù)
  52. void Manage_function(void);                                                        //處理函數(shù)


  53. /****
  54. *******        主函數(shù)
  55. *****/
  56. void main()
  57. {
  58.   Timer0_Init();                                                       //定時(shí)器0初使化函數(shù)
  59.   Delay_function(50);                                                          //延時(shí)50ms
  60.   Uart_Init();                                                                      //串口初始化函數(shù)
  61.   Delay_function(50);                   //延時(shí)50ms
  62.   Exter0_Init();                                                                  //外部中斷0初使化函數(shù)
  63.   Delay_function(50);                   //延時(shí)50ms
  64.   
  65.         while(1)
  66.         {
  67.                 Key_function();                                                                                //按鍵函數(shù)
  68. //                Monitor_function();                                                                        //監(jiān)測(cè)函數(shù)
  69.                 Display_function();                                                                        //顯示函數(shù)
  70.                 Manage_function();                                                                        //處理函數(shù)
  71.    
  72.                 Delay_function(10);                                                                        //延時(shí)10ms
  73.                 time_num++;                                                                                        //計(jì)時(shí)變量+1
  74.                 if(time_num == 5000)
  75.                 {
  76.                         time_num = 0;
  77.                 }
  78.         }
  79. }

  80. /****
  81. *******        延時(shí) x ms函數(shù)
  82. *****/
  83. void Delay_function(uint x)
  84. {
  85.         uint m,n;
  86.         for(m=x;m>0;m--)
  87.     for(n=110;n>0;n--);
  88. }

  89. /****
  90. *******按鍵函數(shù)
  91. *****/
  92. void Key_function(void)
  93. {
  94.         key_num = Matrix_Keyboard_Scan();                   //按鍵掃描
  95.         if(key_num != 0xff)                                                                                //有按鍵按下
  96.         {
  97.                 switch(key_num)
  98.                 {
  99.                         case 1:                                                                                                                //按鍵1,一樓按下
  100.         if(current_floor > 1)       //如果當(dāng)前樓層高于一樓,標(biāo)記一樓下
  101.           flag_floor_d[1] = 1;
  102.         else if(current_floor < 1)  //如果當(dāng)前樓層低于一樓,標(biāo)記一樓上
  103.           flag_floor_u[1] = 1;
  104.                                 
  105.                                 Stop = 0;
  106.                         break;

  107.                         case 2:                                                                                                                //按鍵2,二樓按下
  108.         if(current_floor > 2)       //如果當(dāng)前樓層高于二樓,標(biāo)記二樓下
  109.           flag_floor_d[2] = 2;
  110.         else if(current_floor < 2)  //如果當(dāng)前樓層低于二樓,標(biāo)記二樓上
  111.           flag_floor_u[2] = 2;
  112.                                 
  113.                                 Stop = 0;
  114.                         break;

  115.                         case 3:                                                                                                                //按鍵3,三樓按下
  116.         if(current_floor > 3)       //如果當(dāng)前樓層高于三樓,標(biāo)記三樓下
  117.           flag_floor_d[3] = 3;
  118.         else if(current_floor < 3)  //如果當(dāng)前樓層低于三樓,標(biāo)記三樓上
  119.           flag_floor_u[3] = 3;
  120.                                 
  121.                                 Stop = 0;
  122.                         break;

  123.       case 4:                                                                                                        //按鍵4,開(kāi)門(mén)
  124.         if(flag_bujin_1_o == 0 && flag_bujin_1_c == 0) //當(dāng)前電梯未運(yùn)行
  125.         {
  126.           flag_stop = 0;                  //標(biāo)記未停留完成
  127.           if(flag_bujin_2_c == 1)         //如果正在關(guān)門(mén),將門(mén)打開(kāi)
  128.           {
  129.             flag_door = 0;
  130.           }
  131.           else                           //還沒(méi)關(guān)門(mén),清除停留時(shí)間計(jì)數(shù)
  132.           {
  133.             timer1_count_s = 0;
  134.           }
  135.         }
  136.                         break;

  137.       case 5:                                                                                                        //按鍵5,關(guān)門(mén)
  138.         if(flag_bujin_1_o == 0 && flag_bujin_1_c == 0 && YL==1 && INFRARED == 0) //當(dāng)前電梯未運(yùn)行
  139.         {
  140.           if(flag_bujin_2_o == 1)         //如果正在開(kāi),將門(mén)關(guān)閉
  141.           {
  142.             flag_stop = 1;
  143.             flag_door = 1;
  144.           }
  145.           else                            //如果門(mén)未在運(yùn)動(dòng),直接停留完成
  146.           {
  147.             timer1_count_s = 3000;
  148.           }
  149.         }
  150.                         break;

  151.                         case 6:                                                                                                        //按鍵6,呼叫
  152.         RELAY = ~RELAY;
  153.                         RELAY = 0;
  154. //                                while(key_num==6);
  155.                         break;

  156.                         case 7:                                                                                                        //按鍵7,停止
  157.                                 Stop = ~Stop;
  158. //                                while(key_num==7);
  159.                         break;

  160.                         case 8:                                                                                                        
  161.                                 if(Speed==3)
  162.                                         Speed=0;
  163.                                 else
  164.                                         Speed=3;
  165. //                                while(key_num==8);
  166.                         break;
  167.                         
  168.                         case 9:                                                                                                        

  169.                         break;
  170.                                 
  171.                         default:
  172.                         break;
  173.                 }
  174.         }
  175. }

  176. /****
  177. *******監(jiān)測(cè)函數(shù)
  178. *****/
  179. void Monitor_function(void)
  180. {
  181.   if(uart_count >= 2)         //如果接收到兩個(gè)字節(jié),標(biāo)記相應(yīng)樓層
  182.   {
  183.     if(uart_buf[1] == 0x11)
  184.     {
  185.       flag_floor_d[1] = 1;
  186.                         if(current_floor==1)
  187.                         {
  188.                                 flag_stop = 0;                  //標(biāo)記未停留完成
  189.                                 if(flag_bujin_2_c == 1)         //如果正在關(guān)門(mén),將門(mén)打開(kāi)
  190.                                 {
  191.                                         flag_door = 0;
  192.                                 }
  193.                                 else                           //還沒(méi)關(guān)門(mén),清除停留時(shí)間計(jì)數(shù)
  194.                                 {
  195.                                         timer1_count_s = 0;
  196.                                 }
  197.                         }
  198.                         
  199.                         Stop = 0;
  200.     }
  201.     else if(uart_buf[1] == 0x21)
  202.     {
  203.       flag_floor_u[2] = 2;
  204.                         if(current_floor==2)
  205.                         {
  206.                                 flag_stop = 0;                  //標(biāo)記未停留完成
  207.                                 if(flag_bujin_2_c == 1)         //如果正在關(guān)門(mén),將門(mén)打開(kāi)
  208.                                 {
  209.                                         flag_door = 0;
  210.                                 }
  211.                                 else                           //還沒(méi)關(guān)門(mén),清除停留時(shí)間計(jì)數(shù)
  212.                                 {
  213.                                         timer1_count_s = 0;
  214.                                 }
  215.                         }
  216.                         
  217.                         Stop = 0;
  218.     }
  219.     else if(uart_buf[1] == 0x22)
  220.     {
  221.       flag_floor_d[2] = 2;
  222.                         if(current_floor==2)
  223.                         {
  224.                                 flag_stop = 0;                  //標(biāo)記未停留完成
  225.                                 if(flag_bujin_2_c == 1)         //如果正在關(guān)門(mén),將門(mén)打開(kāi)
  226.                                 {
  227.                                         flag_door = 0;
  228.                                 }
  229.                                 else                           //還沒(méi)關(guān)門(mén),清除停留時(shí)間計(jì)數(shù)
  230.                                 {
  231.                                         timer1_count_s = 0;
  232.                                 }
  233.                         }
  234.                         
  235.                         Stop = 0;
  236.     }
  237.     else if(uart_buf[1] == 0x31)
  238.     {
  239.       flag_floor_u[3] = 3;
  240.                         if(current_floor==3)
  241.                         {
  242.                                 flag_stop = 0;                  //標(biāo)記未停留完成
  243.                                 if(flag_bujin_2_c == 1)         //如果正在關(guān)門(mén),將門(mén)打開(kāi)
  244.                                 {
  245.                                         flag_door = 0;
  246.                                 }
  247.                                 else                           //還沒(méi)關(guān)門(mén),清除停留時(shí)間計(jì)數(shù)
  248.                                 {
  249.                                         timer1_count_s = 0;
  250.                                 }
  251.                         }
  252.                         
  253.                         Stop = 0;
  254.     }
  255.     else if(uart_buf[1] == 0x32)
  256.     {
  257.       flag_floor_d[3] = 3;
  258.                         if(current_floor==3)
  259.                         {
  260.                                 flag_stop = 0;                  //標(biāo)記未停留完成
  261.                                 if(flag_bujin_2_c == 1)         //如果正在關(guān)門(mén),將門(mén)打開(kāi)
  262.                                 {
  263.                                         flag_door = 0;
  264.                                 }
  265.                                 else                           //還沒(méi)關(guān)門(mén),清除停留時(shí)間計(jì)數(shù)
  266.                                 {
  267.                                         timer1_count_s = 0;
  268.                                 }
  269.                         }
  270.                         
  271.                         Stop = 0;
  272.     }
  273. //    else if(uart_buf[1] == 0x41)
  274. //    {
  275. //      flag_floor_u[4] = 4;
  276. //    }
  277. //    else if(uart_buf[1] == 0x42)
  278. //    {
  279. //      flag_floor_d[4] = 4;
  280. //    }
  281. //    else if(uart_buf[1] == 0x51)
  282. //    {
  283. //      flag_floor_u[5] = 5;
  284. //    }
  285. //    else if(uart_buf[1] == 0x52)
  286. //    {
  287. //      flag_floor_d[5] = 5;
  288. //    }
  289. //    else if(uart_buf[1] == 0x61)
  290. //    {
  291. //      flag_floor_u[6] = 6;
  292. //    }
  293. //    else if(uart_buf[1] == 0x62)
  294. //    {
  295. //      flag_floor_d[6] = 6;
  296. //    }
  297. //    else if(uart_buf[1] == 0x71)
  298. //    {
  299. //      flag_floor_u[7] = 7;
  300. //    }
  301. //    else if(uart_buf[1] == 0x72)
  302. //    {
  303. //      flag_floor_d[7] = 7;
  304. //    }
  305. //    else if(uart_buf[1] == 0x82)
  306. //    {
  307. //      flag_floor_u[8] = 8;
  308. //    }

  309.     Uart_Buf_Clear();         //清除接收緩存區(qū)
  310.   }
  311. }

  312. /****
  313. *******顯示函數(shù)
  314. *****/
  315. void Display_function(void)
  316. {
  317.         Display_Tube_1(current_floor);  //數(shù)碼管顯示當(dāng)前樓層
  318.   
  319.   if(flag_lift == 0)              //LED 顯示方向電梯狀態(tài)
  320.   {
  321.     LED_U = 1;
  322.     LED_D = 1;
  323.   }
  324.   else if(flag_lift == 1)
  325.   {
  326.     LED_U = 1;
  327.     LED_D = 0;
  328.   }
  329.   else if(flag_lift == 2)
  330.   {
  331.     LED_U = 0;
  332.     LED_D = 1;
  333.   }
  334.         
  335.   if(current_old != current_floor)      //如果樓層改變,發(fā)送當(dāng)前樓層
  336.   {
  337. //    send_buf[1] = (current_floor/10*0x10 + current_floor%10);
  338.                
  339.          if(flag_lift == 1)          //如果電梯方向?yàn)橄陆担宄?dāng)前樓層下標(biāo)志位
  340.          {
  341.                         if(current_floor == 1)
  342.                                 send_buf[1] = 0x01;
  343.                         else if(current_floor == 2)
  344.                                 send_buf[1] = 0x03;
  345.                         else if(current_floor == 3)
  346.                                 send_buf[1] = 0x04;
  347.                 }
  348.                 else if(flag_lift == 2)          //如果電梯方向?yàn)樯仙宄?dāng)前樓層上標(biāo)志位
  349.                 {
  350.                         if(current_floor == 1)
  351.                                 send_buf[1] = 0x01;
  352.                         else if(current_floor == 2)
  353.                                 send_buf[1] = 0x02;
  354.                         else if(current_floor == 3)
  355.                                 send_buf[1] = 0x04;
  356.                 }
  357.                                 
  358.     Uart_Sent_Str(send_buf);
  359.     current_old = current_floor;
  360.   }
  361. }

  362. /****
  363. *******處理函數(shù)
  364. *****/
  365. void Manage_function(void)
  366. {
  367.   if(flag_lift == 0)                            //如果當(dāng)前電梯停止
  368.   {
  369. //                Uart_Sent_Str("123");
  370. //                Delay_function(1000);
  371.                
  372.     if(flag_stop == 1 && flag_door == 0)
  373.     {
  374.       if(flag_floor_d[1] == 1 || flag_floor_u[1] == 1)                 //一樓按下,目標(biāo)樓層為一樓
  375.       {
  376.         target_floor = 1;
  377.       }
  378.       else if(flag_floor_d[2] == 2 || flag_floor_u[2] == 2)            //二樓按下,目標(biāo)樓層為二樓
  379.       {
  380.         target_floor = 2;
  381.       }
  382.       else if(flag_floor_d[3] == 3 || flag_floor_u[3] == 3)            //三樓按下,目標(biāo)樓層為三樓
  383.       {
  384.         target_floor = 3;
  385.       }

  386.     }
  387.   }
  388.         else if(flag_lift == 1)                            //如果電梯當(dāng)前方向下降
  389.   {
  390.     if(flag_floor_d[3] == 3)                    //如果三樓下
  391.     {
  392.       if(current_floor > 3) //如果當(dāng)前高于三樓,目標(biāo)樓層等于三樓
  393.       {
  394.         target_floor = 3;
  395.       }
  396.     }

  397.     else if(flag_floor_d[2] == 2)           //如果二樓下
  398.     {
  399.       if(current_floor > 2) //如果當(dāng)前高于二樓,目標(biāo)樓層等于二樓
  400.       {
  401.         target_floor = 2;
  402.       }
  403.     }
  404.     else if(flag_floor_d[1] == 1)           //如果一樓
  405.     {
  406.       if(current_floor > 1) //如果當(dāng)前高于一樓,目標(biāo)樓層等于一樓
  407.         target_floor = 1;
  408.     }
  409.   }
  410.   else if(flag_lift == 2)                            //如果當(dāng)前電梯上升
  411.   {
  412.     if(flag_floor_u[2] == 2)                //如果二樓上
  413.     {
  414.       if(current_floor < 2) //如果當(dāng)前樓層低于二樓,目標(biāo)樓層等于二樓
  415.       {
  416.         target_floor = 2;
  417.       }
  418.     }
  419.     else if(flag_floor_u[3] == 3)           //如果三樓上
  420.     {
  421.       if(current_floor < 3) //如果當(dāng)前樓層低于三樓,目標(biāo)樓層等于三樓
  422.       {
  423.         target_floor = 3;
  424.       }
  425.     }

  426.   }
  427.   
  428.   if(current_floor > target_floor)          //如果當(dāng)前樓層比目標(biāo)樓層高,電梯下降
  429.   {
  430.     flag_timer_begin = 0;
  431.     flag_stop = 0;
  432.     flag_lift = 1;
  433.     if(flag_bujin_2_o == 0 && flag_bujin_2_c == 0 && flag_door == 0)
  434.     {
  435.       flag_bujin_1_c = 1;
  436.       flag_bujin_1_o = 0;
  437.     }
  438.     flag_send = 0;
  439.   }
  440.   else if(current_floor < target_floor)     //如果當(dāng)前樓層比目標(biāo)樓層低,電梯上升
  441.   {
  442.     flag_timer_begin = 0;
  443.     flag_stop = 0;
  444.     flag_lift = 2;
  445.     if(flag_bujin_2_o == 0 && flag_bujin_2_c == 0 && flag_door == 0)
  446.     {
  447.       flag_bujin_1_c = 0;
  448.       flag_bujin_1_o = 1;
  449.     }
  450.     flag_send = 0;
  451.   }
  452.   else                                      //當(dāng)前樓層等于目標(biāo)樓層
  453.   {
  454.     if(flag_stop == 0)              //如果沒(méi)有停留完成
  455.     {
  456.       if(flag_door == 0)            //如果門(mén)是關(guān)閉的,開(kāi)門(mén)
  457.       {
  458.         flag_bujin_2_o = 1;
  459.         flag_bujin_2_c = 0;
  460.       }
  461.       else if(flag_door == 1)       //如果門(mén)已經(jīng)打開(kāi)了
  462.       {
  463.         flag_timer_begin_s = 1;     //開(kāi)啟停留定時(shí)
  464.       }
  465.                         
  466.       if(YL==0|| INFRARED == 0) //如果超重了或觸發(fā)紅外防夾,進(jìn)行蜂鳴器提醒,清除停留時(shí)間計(jì)數(shù)
  467.       {
  468.         timer1_count_s = 0;
  469.                                 if(time_num % 15 == 0)
  470.                                 {
  471.                                         BEEP = ~BEEP;
  472.                                 }
  473.       }
  474.       else
  475.       {
  476.         BEEP = 1;
  477.       }
  478.                         
  479.       if(flag_time_s == 1)          //如果停留時(shí)間到達(dá)
  480.       {
  481.         flag_timer_begin_s = 0;
  482.         flag_time_s = 0;
  483.         flag_stop= 1;
  484.       }
  485.     }
  486.                
  487.     if(flag_stop == 1)              //如果停留完成
  488.     {
  489.       if(flag_door == 1)            //如果門(mén)是開(kāi)啟的,關(guān)門(mén)
  490.       {
  491.         flag_bujin_2_o = 0;
  492.         flag_bujin_2_c = 1;
  493.         if((INFRARED == 0 || YL == 0) && flag_bujin_2_c == 1)  //如果觸發(fā)防夾,標(biāo)記未停留完成
  494.         {
  495.           flag_door = 0;
  496.           flag_stop = 0;
  497.         }
  498.       }
  499.       else if(flag_door == 0)      //如果門(mén)已關(guān)閉清除當(dāng)前按下樓層標(biāo)志位
  500.       {
  501.         if(flag_lift == 1)          //如果電梯方向?yàn)橄陆担宄?dāng)前樓層下標(biāo)志位
  502.         {
  503. //          if(flag_send == 0)
  504. //          {
  505. ////                                                        send_buf[1] = (current_floor/10*0x10 + current_floor%10);
  506. //                                                if(current_floor == 1)
  507. //                                                        send_buf[1] = 0x01;
  508. //                                                else if(current_floor == 2)
  509. //                                                        send_buf[1] = 0x03;
  510. //                                                else if(current_floor == 3)
  511. //                                                        send_buf[1] = 0x04;
  512. //                                                
  513. //            Uart_Sent_Str(send_buf);
  514. //            flag_send = 1;
  515. //          }
  516.           flag_floor_d[target_floor] = 0;
  517.         }
  518.         if(flag_lift == 2)          //如果電梯方向?yàn)樯仙宄?dāng)前樓層上標(biāo)志位
  519.         {
  520. //          if(flag_send == 0)
  521. //          {
  522. ////            send_buf[1] = (current_floor/10*0x10 + current_floor%10);
  523. //                                                if(current_floor == 1)
  524. //                                                        send_buf[1] = 0x01;
  525. //                                                else if(current_floor == 2)
  526. //                                                        send_buf[1] = 0x02;
  527. //                                                else if(current_floor == 3)
  528. //                                                        send_buf[1] = 0x04;
  529. //                                                
  530. //            Uart_Sent_Str(send_buf);
  531. //            flag_send = 1;
  532. //          }
  533.           flag_floor_u[target_floor] = 0;
  534.         }
  535.         if(flag_lift == 0)
  536.         {
  537. //                                        if(flag_send == 0)
  538. //          {
  539. ////            send_buf[1] = (current_floor/10*0x10 + current_floor%10);
  540. //                                                if(current_floor == 1)
  541. //                                                        send_buf[1] = 0x01;
  542. //                                                else if(current_floor == 2)
  543. //                                                        send_buf[1] = 0x02;
  544. //                                                else if(current_floor == 3)
  545. //                                                        send_buf[1] = 0x04;
  546. //                                                
  547. //            Uart_Sent_Str(send_buf);
  548. //            flag_send = 1;
  549. //          }

  550.           flag_floor_d[target_floor] = 0;
  551.           flag_floor_u[target_floor] = 0;
  552.         }
  553.         flag_timer_begin = 1;       //開(kāi)啟定時(shí)
  554.       }
  555.     }
  556.   }
  557.         
  558.   if(flag_time == 1)                //定時(shí)500ms到達(dá),清除電梯運(yùn)動(dòng)標(biāo)志位
  559.   {
  560.     flag_timer_begin = 0;
  561.     flag_lift = 0;
  562.     flag_time = 0;
  563.     flag_floor_d[current_floor] = 0;
  564.     flag_floor_u[current_floor] = 0;
  565.   }
  566.         
  567.         if(speed_alarm == 1)
  568.         {
  569.                 BEEP = 0;
  570.                 Delay_function(2000);
  571.                 BEEP = 1;
  572.                 speed_alarm = 0;
  573.         }
  574.         
  575.         if(YW == 0)
  576.         {
  577.                 if(time_num % 15 == 0)
  578.                 {
  579.                         BEEP = ~BEEP;
  580.                 }
  581.         }
  582.         else
  583.         {
  584.                 if(YL == 1 && INFRARED == 1)
  585.                         BEEP = 1;
  586.         }
  587. }

復(fù)制代碼



外機(jī):
  1. /**********************************
  2. 包含頭文件
  3. **********************************/
  4. #include "main.h"
  5. #include "key.h"
  6. #include "uart.h"


  7. /**********************************
  8. 變量定義
  9. **********************************/
  10. uchar key_num = 0;                                                                                        //按鍵掃描標(biāo)志位                        
  11. uint time_num = 0;                                                                                        //10ms計(jì)時(shí)變量

  12. extern uchar uart_buf[16];              //串口接收緩存區(qū)
  13. extern uchar uart_count;                //串口接收計(jì)數(shù)

  14. uchar send_buf[24]={0x01,0x00};                //發(fā)送數(shù)組


  15. /**********************************
  16. 函數(shù)聲明
  17. **********************************/
  18. void Delay_function(uint x);                                                //延時(shí)函數(shù)(ms)
  19. void Key_function(void);                                                                //按鍵函數(shù)
  20. void Manage_function(void);                                                        //處理函數(shù)


  21. /****
  22. *******        主函數(shù)
  23. *****/
  24. void main(void)
  25. {
  26.   Uart_Init();                  //串口初始化
  27.   Delay_function(50);
  28.   LED1 = 0;
  29.   
  30.         while(1)
  31.         {
  32.                 Key_function();                                                                                        //按鍵函數(shù)
  33.                 Manage_function();                                                                        //處理函數(shù)

  34.                 Delay_function(10);                                                                        //延時(shí)10ms
  35.                 time_num++;                                                                                                        //計(jì)時(shí)變量+1
  36.                 if(time_num == 5000)
  37.                 {
  38.                         time_num = 0;
  39.                 }
  40.         }
  41. }

  42. /****
  43. *******        延時(shí) x ms函數(shù)
  44. *****/
  45. void Delay_function(uint x)
  46. {
  47.         uint m,n;
  48.         for(m=x;m>0;m--)
  49.     for(n=110;n>0;n--);
  50. }

  51. /****
  52. *******按鍵函數(shù)
  53. *****/
  54. void Key_function(void)
  55. {
  56.         key_num = Chiclet_Keyboard_Scan(0);                  //按鍵掃描
  57.         if(key_num != 0)                                                                                        //有按鍵按下
  58.         {
  59.                 switch(key_num)
  60.                 {
  61.                         case 1:                                                                                                                //按鍵1,發(fā)送一樓上
  62.                                 send_buf[1] = 0x11;
  63.         Uart_Sent_Str(send_buf);
  64.                         break;

  65.                         case 2:                                                                                                                //按鍵2,發(fā)送二樓上
  66.                                 send_buf[1] = 0x21;
  67.         Uart_Sent_Str(send_buf);
  68.                         break;

  69.                         case 3:                                                                                                                //按鍵3,發(fā)送二樓下
  70.                                 send_buf[1] = 0x22;
  71.         Uart_Sent_Str(send_buf);
  72.                         break;
  73.       

  74.                         case 4:                                                                                                                //按鍵4,發(fā)送三樓下
  75.                                 send_buf[1] = 0x32;
  76.         Uart_Sent_Str(send_buf);
  77.                         break;      

  78.                         default:
  79.                         break;
  80.                 }
  81.         }
  82. }

  83. /****
  84. *******處理函數(shù)
  85. *****/
  86. void Manage_function(void)
  87. {
  88.         if(uart_count >= 2)         //如果接收到兩個(gè)字節(jié),解析數(shù)據(jù),點(diǎn)亮對(duì)應(yīng)樓層的LED
  89.   {
  90.     P1 = 0xff;
  91.     if(uart_buf[1] == 0x01)   
  92.     {
  93.       LED1 = 0;
  94.     }
  95.     else if(uart_buf[1] == 0x02)
  96.     {
  97.       LED2 = 0;
  98.     }
  99.     else if(uart_buf[1] == 0x03)
  100.     {
  101.       LED3 = 0;
  102.     }
  103.     else if(uart_buf[1] == 0x04)
  104.     {
  105.       LED4 = 0;
  106.     }
  107.    
  108.     Uart_Buf_Clear();         //清除接收緩存區(qū)
  109.   }
  110. }
復(fù)制代碼


原理圖: 無(wú)
仿真: 無(wú)
代碼: 電梯-實(shí)物源程序-C語(yǔ)言源碼.7z (32.72 KB, 下載次數(shù): 0)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

無(wú)效樓層,該帖已經(jīng)被刪除

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表