找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3274|回复: 0
打印 上一主题 下一主题
收起左侧

MiniARM2300电脑自动打铃器设计与实现ucos操作系统

[复制链接]
跳转到指定楼层
楼主
采用ucos,有详细的说明,经典的ucos操作系统实例详解


单片机源程序如下:
  1. /****************************************Copyright (c)****************************************************
  2. **                            Guangzhou ZHIYUAN electronics Co.,LTD.
  3. **                                      
  4. **
  5. **--------------File Info---------------------------------------------------------------------------------
  6. ** File name:           main.c
  7. ** Last modified Date:  2007-09-01
  8. ** Last Version:        1.0
  9. ** Descriptions:        The main() function example template
  10. **
  11. **--------------------------------------------------------------------------------------------------------
  12. ** Created by:          Zhenghongtao
  13. ** Created date:        2007-09-01
  14. ** Version:             1.0
  15. ** Descriptions:        The original version
  16. **
  17. **--------------------------------------------------------------------------------------------------------
  18. ** Modified by:
  19. ** Modified date:
  20. ** Version:
  21. ** Descriptions:
  22. **
  23. *********************************************************************************************************/
  24. #include "config.h"                                                     /*  系统头文件                  */
  25. #include "i2c.h"

  26. #define TASKKEY_ID              12                                      /*  定义键盘任务的ID            */
  27. #define TASKKEY_PRIO            TASKKEY_ID                              /*  定义键盘任务的优先级        */
  28. #define TASKKEY_STACK_SIZE      512                                     /*  定义键盘任务堆栈大小        */

  29. #define TASKDISP_ID             13                                      /*  定义显示任务的ID            */
  30. #define TASKDISP_PRIO           TASKDISP_ID                             /*  定义显示任务的优先级        */
  31. #define TASKDISP_STACK_SIZE     512                                     /*  定义显示任务堆栈大小        */

  32. #define TASKCTRL_ID             6                                       /*  定义控制任务的ID            */
  33. #define TASKCTRL_PRIO           TASKCTRL_ID                             /*  定义控制任务的优先级        */
  34. #define TASKCTRL_STACK_SIZE     512                                     /*  定义控制任务堆栈大小        */

  35. OS_STK        TaskKeyStk[TASKKEY_STACK_SIZE];                                 /*  定义键盘任务的堆栈          */
  36. OS_STK        TaskDispStk[TASKDISP_STACK_SIZE];                               /*  定义显示任务的堆栈          */
  37. OS_STK        TaskCtrlStk[TASKCTRL_STACK_SIZE];                               /*  定义控制任务的堆栈          */


  38. void TaskKey(void *pdata);                                              /*  TaskKey  键盘任务           */
  39. void TaskDisp(void *pdata);                                             /*  TaskDisp 显示任务           */
  40. void TaskCtrl(void *pdata);                                             /*  TaskCtrl 控制任务           */

  41. void ToDispBuf(void);
  42. void FromDispBuf(void);
  43. void GetTime(void);
  44. void SetTime(void);
  45. void RTC_Exception(void);

  46. OS_EVENT       *GmboxRingCtrl;

  47. unsigned int    GuiMode   = 0;                                          /*  模式                        */
  48. unsigned int    GuiCursor = 8;                                          /*  光标                        */
  49. unsigned int    GuiIndex  = 0;                                          /*  索引                        */
  50. unsigned int    GuiItem   = 0;                                          /*  条目                        */

  51. /*********************************************************************************************************
  52.   时钟结构定义
  53. *********************************************************************************************************/
  54. struct time {
  55.     unsigned char       ucHour;                                         /*  时                          */
  56.     unsigned char       ucMin;                                          /*  分                          */
  57.     unsigned char       ucSec;                                          /*  秒                          */
  58.     unsigned char       ucWeek;                                         /*  星期                        */
  59.     unsigned short      usYear;                                         /*  年                          */
  60.     unsigned char       ucMon;                                          /*  月                          */
  61.     unsigned char       ucDay;                                          /*  日                          */
  62. };
  63. typedef struct time     TIME;
  64. typedef TIME           *PTIME;
  65. /*********************************************************************************************************
  66.   闹钟结构定义
  67. *********************************************************************************************************/
  68. struct alarm {
  69.     unsigned char       ucHour;                                         /*  时                          */
  70.     unsigned char       ucMin;                                          /*  分                          */
  71.     unsigned char       ucSec;                                          /*  秒                          */
  72.     unsigned char       ucEnable;                                       /*  闹钟使能控制                */
  73.     struct {
  74.         unsigned short  usLevel;                                        /*  输出电平控制                */
  75.         unsigned short  usTime;                                         /*  输出时间控制                */
  76.     } c[4];                                                             /*  4路输出控制                 */
  77. };
  78. typedef struct alarm    ALARM;
  79. typedef ALARM          *PALARM;

  80. #define MAX_ALARM 4                                                     /*  最大闹钟个数                */

  81. unsigned char       GucTimeDispBuf[2][8];                               /*  时钟显示缓冲区              */
  82. unsigned char       GucAlarmDispBuf[MAX_ALARM][6][8];                   /*  闹钟显示缓冲区              */

  83. TIME                GtimeCurrentTime;                                   /*  当前时间                    */
  84. ALARM               GalarmRingTime[MAX_ALARM];                          /*  闹钟时间                    */

  85. #define LED0    (1u<<18)
  86. #define LED1    (1u<<19)
  87. #define LED2    (1u<<20)
  88. #define LED3    (1u<<21)

  89. /*********************************************************************************************************
  90.   时钟模式下,下一个光标索引表
  91. *********************************************************************************************************/
  92. const unsigned char GucTimeNextCursor[2][9] = {
  93.     {7,0,1,2,3,4,7,5,7},
  94.     {5,0,1,2,3,4,5,5,5}
  95. };
  96. /*********************************************************************************************************
  97.   时钟模式下,上一个光标索引表
  98. *********************************************************************************************************/
  99. const unsigned char GucTimePreCursor[2][9] = {
  100.     {1,2,3,4,5,7,7,0,7},
  101.     {1,2,3,4,5,0,5,5,5}
  102. };
  103. /*********************************************************************************************************
  104.   时钟模式下,最大值限定表
  105. *********************************************************************************************************/
  106. const unsigned char GucTimeMaxTable[2][8] = {
  107.     {0x09,0x05,0x09,0x05,0x09,0x02,0x1f,0x06},
  108.     {0x09,0x03,0x09,0x01,0x09,0x09,0x1f,0x1f}
  109. };
  110. /*********************************************************************************************************
  111.   闹钟模式下,下一个光标索引表
  112. *********************************************************************************************************/
  113. const unsigned char GucAlarmNextCursor[6][9] = {
  114.     {5,0,1,2,3,4,5,5,5},
  115.     {7,0,1,2,3,4,5,6,7},
  116.     {4,0,1,2,3,4,4,4,4},
  117.     {4,0,1,2,3,4,4,4,4},
  118.     {4,0,1,2,3,4,4,4,4},
  119.     {4,0,1,2,3,4,4,4,4}
  120. };
  121. /*********************************************************************************************************
  122.   闹钟模式下,上一个光标索引表
  123. *********************************************************************************************************/
  124. const unsigned char GucAlarmPreCursor[6][9] = {
  125.     {1,2,3,4,5,0,5,5,5},
  126.     {1,2,3,4,5,6,7,0,7},
  127.     {1,2,3,4,0,4,4,4,4},
  128.     {1,2,3,4,0,4,4,4,4},
  129.     {1,2,3,4,0,4,4,4,4},
  130.     {1,2,3,4,0,4,4,4,4}
  131. };
  132. /*********************************************************************************************************
  133.   闹钟模式下,最大值限定表
  134. *********************************************************************************************************/
  135. const unsigned char GucAlarmMaxTable[6][8] = {
  136.     {0x09,0x05,0x09,0x05,0x09,0x02,0x1f,0x1f},
  137.     {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  138.     {0x09,0x09,0x09,0x09,0x01,0x1f,0x1f,0x1f},
  139.     {0x09,0x09,0x09,0x09,0x01,0x1f,0x1f,0x1f},
  140.     {0x09,0x09,0x09,0x09,0x01,0x1f,0x1f,0x1f},
  141.     {0x09,0x09,0x09,0x09,0x01,0x1f,0x1f,0x1f}
  142. };

  143. /*********************************************************************************************************
  144. ** Function name:           main
  145. **
  146. ** Descriptions:            主函数
  147. **
  148. ** input parameters:
  149. ** output parameters:
  150. **
  151. ** Returned value           
  152. **
  153. **
  154. ** Created by:
  155. ** Created Date:
  156. **--------------------------------------------------------------------------------------------------------
  157. ** Modified by:
  158. ** Modified date:
  159. **--------------------------------------------------------------------------------------------------------
  160. *********************************************************************************************************/
  161. int main(void)
  162. {
  163.     OSInit();                                                           /*  初始化uC/OS-II              */
  164.     OSTaskCreateExt(TaskKey,
  165.                     (void *)0,
  166.                     &TaskKeyStk[TASKKEY_STACK_SIZE - 1],
  167.                     TASKKEY_PRIO,
  168.                     TASKKEY_ID,
  169.                     &TaskKeyStk[0],
  170.                     TASKKEY_STACK_SIZE,
  171.                     (void *)0,
  172.                     OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
  173.                                                                         /*  创建键盘任务                */
  174.     OSStart();                                                          /*  启动多任务操作系统          */
  175.     return  (0);
  176. }
  177. /*********************************************************************************************************
  178. ** Function name:           ZLG7290GetKey
  179. **
  180. ** Descriptions:            zlg7290读取键值
  181. **
  182. ** input parameters:
  183. ** output parameters:
  184. **
  185. ** Returned value           
  186. **
  187. **
  188. ** Created by:
  189. ** Created Date:
  190. **--------------------------------------------------------------------------------------------------------
  191. ** Modified by:
  192. ** Modified date:
  193. **--------------------------------------------------------------------------------------------------------
  194. *********************************************************************************************************/
  195. uint16 ZLG7290GetKey(void)
  196. {
  197.     uint8 temp[2];
  198.    
  199.     i2cRead( 0,                                                         /*  I2C0                        */
  200.              0x70,                                                      /*  器件地址                    */
  201.              temp,                                                      /*  接收数据的缓冲区            */
  202.              1,                                                         /*  器件子地址                  */
  203.              1,                                                         /*  器件子地址类型为单字节型    */
  204.              2);                                                        /*  读取数据的长度              */
  205.     while (i2cGetFlag(0) == I2C_BUSY);                                  /*  等待I2C操作完毕             */
  206.     return (uint16)(temp[0] + (temp[1] * 256));
  207. }
  208. /*********************************************************************************************************
  209. ** Function name:           ZLG7290ShowChar
  210. **
  211. ** Descriptions:            zlg7290显示数值
  212. **
  213. ** input parameters:
  214. ** output parameters:
  215. **
  216. ** Returned value           
  217. **
  218. **
  219. ** Created by:
  220. ** Created Date:
  221. **--------------------------------------------------------------------------------------------------------
  222. ** Modified by:
  223. ** Modified date:
  224. **--------------------------------------------------------------------------------------------------------
  225. *********************************************************************************************************/
  226. void ZLG7290ShowChar(uint8 index, uint8 data)
  227. {
  228.     uint8 buf[2];
  229.    
  230.     buf[0] = (uint8)(0x60 | (index & 0x0f));
  231.     buf[1] = data;
  232.     i2cWrite(0,                                                         /*  I2C0                        */
  233.              0x70,                                                      /*  器件地址                    */
  234.              buf,                                                       /*  要写入数据的缓冲区          */
  235.              0x07,                                                      /*  器件子地址                  */
  236.              1,                                                         /*  器件子地址类型为单字节型    */
  237.              2);                                                        /*  写入的数量                  */
  238.     while (i2cGetFlag(0) == I2C_BUSY);                                  /*  等待I2C操作完毕             */
  239. }
  240. /*********************************************************************************************************
  241. ** Function name:           rtcInit
  242. **
  243. ** Descriptions:            RTC初始化
  244. **
  245. ** input parameters:
  246. ** output parameters:
  247. **
  248. ** Returned value           
  249. **
  250. **
  251. ** Created by:
  252. ** Created Date:
  253. **--------------------------------------------------------------------------------------------------------
  254. ** Modified by:
  255. ** Modified date:
  256. **--------------------------------------------------------------------------------------------------------
  257. *********************************************************************************************************/
  258. void rtcInit(void)
  259. {
  260.     if(ALYEAR != 2007) {                                                /*  初始化一次                  */
  261.         CCR     = 0x00;                                                 /*  禁止时间计数器              */
  262.         PREINT  = Fpclk/32768-1;                                        /*  设置基准时钟分频器          */
  263.         PREFRAC = Fpclk%32768;
  264.         CISS    = 0;                                                    /*  禁止次秒级中断              */
  265.         AMR     = 0xFF;                                                 /*  禁止报警中断                */
  266.         CIIR    = 0x01;                                                 /*  使能秒增量中断              */
  267.         ILR     = 0x07;                                                 /*  清除RTC中断标志             */
  268.         ALYEAR  = 2007;                                                 /*  初始化标志                  */
  269.         
  270.         YEAR    = 2007;                                                 /*  初始化时间寄存器            */
  271.         MONTH   = 11;
  272.         DOM     = 5;
  273.         DOW     = 0;
  274.         HOUR    = 12;
  275.         MIN     = 0;
  276.         SEC     = 0;
  277.         CCR     = 0x11;
  278.     }
  279.     GetTime();                                                          /*  更新当前时间                */
  280. }
  281. /*********************************************************************************************************
  282. ** Function name:           TaskKey
  283. **
  284. ** Descriptions:            键盘任务
  285. **
  286. ** input parameters:
  287. ** output parameters:
  288. **
  289. ** Returned value           
  290. **
  291. **
  292. ** Created by:
  293. ** Created Date:
  294. **--------------------------------------------------------------------------------------------------------
  295. ** Modified by:
  296. ** Modified date:
  297. **--------------------------------------------------------------------------------------------------------
  298. *********************************************************************************************************/
  299. void TaskKey(void *pdata)
  300. {
  301.     unsigned char   ucKey;
  302.     unsigned char   ucKey0;
  303.     unsigned char   ucFlag = 0;                                         /*  修改状态标志                */
  304.    
  305.     pdata = pdata;                                                      /*  防止编译器警告              */
  306.    
  307.     TargetInit();
  308.     OSTimeDly(OS_TICKS_PER_SEC/10);                                     /*  上电延时,等待zlg7290复位   */
  309.    
  310.     PINSEL1 = (PINSEL1 & ~(0xff << 22))|(  0x05 << 22 );                /*  设置I2C管脚功能             */
  311.     i2cInit(0, "Speed=30000", NULL);                                    /*  初始化I2C0                  */
  312.     SetVICIRQ(9, 2, (int)i2c0IRQ);                                      /*  设置了IRQ中断               */
  313.    
  314.     GmboxRingCtrl = OSMboxCreate((void *)0);                            /*  创建消息邮箱                */
  315.     rtcInit();
  316.     SetVICIRQ(13, 3, (int)RTC_Exception);
  317.    
  318.     OSTaskCreateExt (TaskDisp,
  319.                      (void *)0,
  320.                      &TaskDispStk[TASKDISP_STACK_SIZE - 1],
  321.                      TASKDISP_PRIO,
  322.                      TASKDISP_ID,
  323.                      &TaskDispStk[0],
  324.                      TASKDISP_STACK_SIZE,
  325.                      (void *)0,
  326.                      OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);

  327.                                                                         /*  创建显示任务                */
  328.                                                                         
  329.     OSTaskCreateExt (TaskCtrl,
  330.                      (void *)0,
  331.                      &TaskCtrlStk[TASKCTRL_STACK_SIZE - 1],
  332.                      TASKCTRL_PRIO,
  333.                      TASKCTRL_ID,
  334.                      &TaskCtrlStk[0],
  335.                      TASKCTRL_STACK_SIZE,
  336.                      (void *)0,
  337.                      OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);

  338.                                                                         /*  创建输出控制任务            */
  339.    
  340.     while (1) {
  341.         OSTimeDly(4);                                                   /*  延时                        */
  342.         ucKey = (uint8)ZLG7290GetKey();                                 /*  获取键值                    */
  343.         if ((ucKey == 0) || (ucKey > 16)) {                             /*  没有按键                    */
  344.                 continue;
  345.         }
  346.         if (ucKey<11) {                                                 /*  数字键                      */
  347.                 ucKey0 = 0;
  348.         }
  349.         else {                                                          /*  功能键                      */
  350.                 ucKey0 = (uint8)(ucKey % 10);
  351.         }
  352.            
  353.             /*
  354.              *  键值处理
  355.              */
  356.             OS_ENTER_CRITICAL();                                            /*  进入临界区,原子操作        */
  357.         switch (ucKey0) {
  358.         
  359.         case 0:                                                         /*  数字键                      */
  360.             if (ucFlag) {                                               /*  数字键只有在修改状态下有效  */
  361.                 ucKey %= 10;
  362.                 switch (GuiMode) {
  363.                
  364.                 case 0:                                                 /*  时钟模式                    */
  365.                     if (ucKey <= GucTimeMaxTable[GuiItem][GuiCursor]) {
  366.                         GucTimeDispBuf[GuiItem][GuiCursor] = ucKey;
  367.                         GuiCursor = GucTimeNextCursor[GuiItem][GuiCursor];
  368.                         FromDispBuf();
  369.                         SetTime();
  370.                     }
  371.                     break;
  372.                 case 1:                                                 /*  闹钟模式                    */
  373.                     if (ucKey <= GucAlarmMaxTable[GuiItem][GuiCursor]) {
  374.                         GucAlarmDispBuf[GuiIndex][GuiItem][GuiCursor] = ucKey;
  375.                         GuiCursor = GucAlarmNextCursor[GuiItem][GuiCursor];
  376.                         FromDispBuf();
  377.                     }
  378.                     break;
  379.                 default:
  380.                     break;
  381.                 }
  382.                     
  383.             }
  384.             break;
  385.         case 1:                                                         /*  左移动键                    */
  386.             if (ucFlag) {
  387.                 switch (GuiMode) {
  388.                
  389.                 case 0:
  390.                     GuiCursor = GucTimePreCursor[GuiItem][GuiCursor];
  391.                     break;
  392.                 case 1:
  393.                     GuiCursor = GucAlarmPreCursor[GuiItem][GuiCursor];
  394.                     break;
  395.                 default:
  396.                     break;
  397.                 }
  398.             }
  399.             else {
  400.                 switch (GuiMode) {
  401.                
  402.                 case 0:
  403.                     if (GuiItem > 0) {
  404.                         GuiItem--;
  405.                     }
  406.                     else {
  407.                         GuiItem = 1;
  408.                     }
  409.                     break;
  410.                 case 1:
  411.                     if (GuiItem > 0) {
  412.                         GuiItem--;
  413.                     }
  414.                     else {
  415.                         GuiItem = 5;
  416.                     }
  417.                     break;
  418.                 default:
  419.                     break;
  420.                 }
  421.             }
  422.             break;
  423.         case 2:                                                         /*  右移动键                    */
  424.             if (ucFlag) {
  425.                 switch (GuiMode) {
  426.                
  427.                 case 0:
  428.                     GuiCursor = GucTimeNextCursor[GuiItem][GuiCursor];
  429.                     break;
  430.                 case 1:
  431.                     GuiCursor = GucAlarmNextCursor[GuiItem][GuiCursor];
  432.                     break;
  433.                 default:
  434.                     break;
  435.                 }
  436.             }
  437.             else {
  438.                 switch (GuiMode) {
  439.                
  440.                 case 0:
  441.                     GuiItem = (GuiItem+1)%2;
  442.                     break;
  443.                 case 1:
  444.                     GuiItem = (GuiItem+1)%6;
  445.                     break;
  446.                 default:
  447.                     break;
  448.                 }
  449.             }
  450.             break;
  451.         case 3:                                                         /*  上移动键                    */
  452.             if (ucFlag) {
  453.                 switch (GuiMode) {
  454.                
  455.                 case 0:
  456.                     break;
  457.                 case 1:
  458.                     break;
  459.                 default:
  460.                     break;
  461.                 }
  462.             }
  463.             else {
  464.                 switch (GuiMode) {
  465.                
  466.                 case 0:
  467.                     break;
  468.                 case 1:
  469.                     if(GuiIndex > 0 ) {
  470.                         GuiIndex--;
  471.                     }
  472.                     else {
  473.                         GuiIndex = MAX_ALARM-1;
  474.                     }
  475.                     GuiItem = 0;
  476.                     break;
  477.                 default:
  478.                     break;
  479.                 }
  480.             }
  481.             break;
  482.         case 4:                                                         /*  下移动键                    */
  483.             if (ucFlag) {
  484.                 switch (GuiMode) {
  485.                
  486.                 case 0:
  487.                     break;
  488.                 case 1:
  489.                     break;
  490.                 default:
  491.                     break;
  492.                 }
  493.             }
  494.             else {
  495.                 switch (GuiMode) {
  496.                 case 0:
  497.                     break;
  498.                 case 1:
  499.                     GuiIndex = (GuiIndex+1)%MAX_ALARM;
  500.                     GuiItem = 0;
  501.                     break;
  502.                 }
  503.             }
  504.             break;
  505.         case 5:                                                         /*  模式切换键                  */
  506.             if (!ucFlag) {
  507.                 GuiMode = (GuiMode+1)%2;
  508.                 GuiIndex = 0;
  509.                 GuiItem = 0;
  510.             }
  511.             break;
  512.         case 6:                                                         /*  确定键                      */
  513.             ucFlag = (uint8)(!ucFlag);
  514.             if (ucFlag) {
  515.                 switch (GuiMode) {
  516.                
  517.                 case 0:
  518.                     GuiCursor = GucTimeNextCursor[GuiItem][GuiCursor];
  519.                     break;
  520.                 case 1:
  521.                     GuiCursor = GucAlarmNextCursor[GuiItem][GuiCursor];
  522.                     break;
  523.                 default:
  524.                     break;
  525.                 }
  526.             }
  527.             else {
  528.                 GuiCursor = 8;
  529.             }
  530.             break;
  531.         default:
  532.             break;
  533.         }
  534.         OS_EXIT_CRITICAL();                                             /*  退出临界区                  */
  535.         
  536.         while (1) {                                                     /*  等待按键释放                */
  537.             OSTimeDly(4);                                               /*  延时                        */
  538.             ucKey = (uint8)ZLG7290GetKey();                             /*  获取键值                    */
  539.             if ((ucKey == 0) || (ucKey > 16)) {                         /*  按键释放                    */
  540.                 break;
  541.             }
  542.         }
  543.     }
  544. }
  545.    
  546. /*********************************************************************************************************
  547. ** Function name:           TaskDisp
  548. **
  549. ** Descriptions:            显示任务
  550. **
  551. ** input parameters:
  552. ** output parameters:
  553. **
  554. ** Returned value           
  555. **
  556. **
  557. ** Created by:
  558. ** Created Date:
  559. **--------------------------------------------------------------------------------------------------------
  560. ** Modified by:
  561. ** Modified date:
  562. **--------------------------------------------------------------------------------------------------------
  563. *********************************************************************************************************/
  564. void TaskDisp(void *pdata)
  565. {
  566.     uint8 i;
  567.    
  568.     pdata = pdata;
  569.    
  570.     while (1) {
  571. /*显示任务周期性的输出全局变量的信息(时钟和闹钟),100ms是一个经验值,
  572. 用户会觉得比较舒服,不会产生“系统死机”的感觉。  */  
  573.         OSTimeDly(20);                                                  /*  延时                        */
  574.         
  575.         OS_ENTER_CRITICAL();
  576.         ToDispBuf();                                                    /*  刷新显示缓冲区              */
  577.         OS_EXIT_CRITICAL();
  578.         
  579.         switch (GuiMode) {                                              /*  显示信息                    */
  580.         
  581.         case 0:                                                         /*  时钟模式                    */
  582.             for (i=0; i<8; i++) {
  583.                 ZLG7290ShowChar(i, (uint8)(((i==GuiCursor) ?
  584.                                 0x40 : 0x00) | GucTimeDispBuf[GuiItem][i]));
  585.                 OSTimeDly(1);
  586.             }
  587.             break;
  588.         case 1:                                                         /*  闹钟模式                    */
  589.             for (i=0; i<8; i++) {
  590.                 ZLG7290ShowChar(i, (uint8)(((i==GuiCursor) ?
  591.                                 0x40 : 0x00) | GucAlarmDispBuf[GuiIndex][GuiItem][i]));
  592.                 OSTimeDly(1);
  593.             }
  594.             break;
  595.         default:
  596.             break;
  597.         }
  598.     }
  599. }

  600. /*********************************************************************************************************
  601. ** Function name:           TaskCtrl
  602. **
  603. ** Descriptions:            输出控制任务
  604. **
  605. ** input parameters:
  606. ** output parameters:
  607. **
  608. ** Returned value           
  609. **
  610. **
  611. ** Created by:
  612. ** Created Date:
  613. **--------------------------------------------------------------------------------------------------------
  614. ** Modified by:
  615. ** Modified date:
  616. **--------------------------------------------------------------------------------------------------------
  617. *********************************************************************************************************/
  618. void TaskCtrl(void *pdata)
  619. {
  620.     INT8U   ucErr;
  621.     unsigned short  usLevel[4];                                         /*  4路输出电平控制             */
  622.     unsigned short  usTime[4];                                          /*  4路输出时间控制             */
  623.     unsigned short *pusMsg;
  624.     OS_MBOX_DATA mboxdataMsg;                                           /*  查询消息的结构体            */
  625.     int   i;
  626.    
  627.     pdata = pdata;
  628.     IO1DIR |=  (LED0 | LED1 | LED2 | LED3);                             /*  设置IO方向                  */
  629.     IO1SET = (LED0 | LED1 | LED2 |LED3);                                /*  设置IO初值                  */
  630.    
  631.     while (1) {
  632.         pusMsg = (unsigned short *)OSMboxPend(GmboxRingCtrl, 0, &ucErr);/*  获取消息                    */
  633.         for (i=0; i<4; i++) {                                           /*  解释消息                    */
  634.             usLevel[i] = pusMsg[2*i];
  635.             usTime[i]  = pusMsg[2*i+1];
  636.         }
  637.         if (usLevel[0]==0) {                                            /*  设置输出电平                */
  638.             IO1CLR = LED0;
  639.         }
  640.         if (usLevel[1]==0) {
  641.             IO1CLR = LED1;
  642.         }
  643.         if (usLevel[2]==0) {
  644.             IO1CLR = LED2;
  645.         }
  646.         if (usLevel[3]==0) {
  647.             IO1CLR = LED3;
  648.         }
  649.         while (1) {                                                     /*  输出时间循环                */
  650.             OSTimeDly(OS_TICKS_PER_SEC);
  651.             if (usTime[0] != 0) {                                       /*  0路时间                     */
  652.                 if (--usTime[0] == 0) {
  653.                     IO1SET = LED0;
  654.                 }
  655.             }
  656.             if (usTime[1] != 0) {                                       /*  1路时间                     */
  657.                 if (--usTime[1] == 0) {
  658.                     IO1SET = LED1;
  659.                 }
  660.             }
  661.             if (usTime[2] != 0) {                                       /*  2路时间                     */
  662.                 if (--usTime[2] == 0) {
  663.                     IO1SET = LED2;
  664.                 }
  665.             }
  666.             if (usTime[3] != 0) {                                       /*  3路时间                     */
  667.                 if (--usTime[3] == 0) {
  668.                     IO1SET = LED3;
  669.                 }
  670.             }
  671.             if ((usTime[0] == 0) && (usTime[1] == 0) && (usTime[2] == 0) && (usTime[3] == 0)) {
  672.                 break;
  673.             }
  674.             OSMboxQuery(GmboxRingCtrl, &mboxdataMsg);                   /*  查询是否有新的消息          */
  675.             if (mboxdataMsg.OSMsg != (void *)0) {
  676.                 break;
  677.             }
  678.         }
  679.     }
  680. }

  681. /*********************************************************************************************************
  682. ** Function name:           GetTime
  683. **
  684. ** Descriptions:            获取当前时间
  685. **
  686. ** input parameters:
  687. ** output parameters:
  688. **
  689. ** Returned value           
  690. **
  691. **
  692. ** Created by:
  693. ** Created Date:
  694. **--------------------------------------------------------------------------------------------------------
  695. ** Modified by:
  696. ** Modified date:
  697. **--------------------------------------------------------------------------------------------------------
  698. *********************************************************************************************************/
  699. void GetTime(void)
  700. {
  701.     GtimeCurrentTime.ucHour = (uint8)HOUR;
  702.     GtimeCurrentTime.ucMin  = (uint8)MIN;
  703.     GtimeCurrentTime.ucSec  = (uint8)SEC;
  704.     GtimeCurrentTime.usYear = (uint16)YEAR;
  705.     GtimeCurrentTime.ucMon  = (uint8)MONTH;
  706.     GtimeCurrentTime.ucDay  = (uint8)DOM;
  707.     GtimeCurrentTime.ucWeek = (uint8)DOW;
  708. }

  709. /*********************************************************************************************************
  710. ** Function name:           ToDispBuf
  711. **
  712. ** Descriptions:            更新显示缓冲区
  713. **
  714. ** input parameters:
  715. ** output parameters:
  716. **
  717. ** Returned value           
  718. **
  719. **
  720. ** Created by:
  721. ** Created Date:
  722. **--------------------------------------------------------------------------------------------------------
  723. ** Modified by:
  724. ** Modified date:
  725. **--------------------------------------------------------------------------------------------------------
  726. *********************************************************************************************************/
  727. void ToDispBuf(void)
  728. {
  729.     int i, j;
  730.    
  731.     GucTimeDispBuf[0][0] = (uint8)(GtimeCurrentTime.ucSec % 10);        /*  星期,时分秒                */
  732.     GucTimeDispBuf[0][1] = (uint8)(GtimeCurrentTime.ucSec / 10);
  733.     GucTimeDispBuf[0][2] = (uint8)(GtimeCurrentTime.ucMin % 10);
  734.     GucTimeDispBuf[0][3] = (uint8)(GtimeCurrentTime.ucMin / 10);
  735.     GucTimeDispBuf[0][4] = (uint8)(GtimeCurrentTime.ucHour % 10);
  736.     GucTimeDispBuf[0][5] = (uint8)(GtimeCurrentTime.ucHour / 10);
  737.     GucTimeDispBuf[0][6] = 0x1f;
  738.     GucTimeDispBuf[0][7] = GtimeCurrentTime.ucWeek;
  739.    
  740.     GucTimeDispBuf[1][0] = (uint8)(GtimeCurrentTime.ucDay % 10);        /*  年月日                      */
  741.     GucTimeDispBuf[1][1] = (uint8)(GtimeCurrentTime.ucDay / 10);
  742.     GucTimeDispBuf[1][2] = (uint8)(GtimeCurrentTime.ucMon % 10);
  743.     GucTimeDispBuf[1][3] = (uint8)(GtimeCurrentTime.ucMon / 10);
  744.     GucTimeDispBuf[1][4] = (uint8)(GtimeCurrentTime.usYear % 10);
  745.     GucTimeDispBuf[1][5] = (uint8)(GtimeCurrentTime.usYear % 100 / 10);
  746.     GucTimeDispBuf[1][6] = (uint8)(GtimeCurrentTime.usYear % 1000 / 100);
  747.     GucTimeDispBuf[1][7] = (uint8)(GtimeCurrentTime.usYear / 1000);
  748.    
  749.     for (i=0; i<MAX_ALARM; i++) {
  750.         GucAlarmDispBuf[i][0][0] = (uint8)(GalarmRingTime[i].ucSec % 10);
  751.         GucAlarmDispBuf[i][0][1] = (uint8)(GalarmRingTime[i].ucSec % 100 / 10);
  752.         GucAlarmDispBuf[i][0][2] = (uint8)(GalarmRingTime[i].ucMin % 10);
  753.         GucAlarmDispBuf[i][0][3] = (uint8)(GalarmRingTime[i].ucMin % 100 / 10);
  754.         GucAlarmDispBuf[i][0][4] = (uint8)(GalarmRingTime[i].ucHour % 10);
  755.         GucAlarmDispBuf[i][0][5] = (uint8)(GalarmRingTime[i].ucHour % 100 / 10);
  756.         GucAlarmDispBuf[i][0][6] = (uint8)i;
  757.         GucAlarmDispBuf[i][0][7] = 0x0a;
  758.         
  759.         for (j=0; j<8; j++) {
  760.             GucAlarmDispBuf[i][1][j] = (uint8)((GalarmRingTime[i].ucEnable&(1<<j)) ? 0x0e : 0x0d);
  761.         }
  762.         
  763.         for (j=0; j<4; j++) {
  764.             GucAlarmDispBuf[i][j+2][0] = (uint8)(GalarmRingTime[i].c[j].usTime % 10);
  765.             GucAlarmDispBuf[i][j+2][1] = (uint8)(GalarmRingTime[i].c[j].usTime % 100 / 10);
  766.             GucAlarmDispBuf[i][j+2][2] = (uint8)(GalarmRingTime[i].c[j].usTime % 1000 / 100);
  767.             GucAlarmDispBuf[i][j+2][3] = (uint8)(GalarmRingTime[i].c[j].usTime % 10000 / 1000);
  768.             GucAlarmDispBuf[i][j+2][4] = (uint8)(GalarmRingTime[i].c[j].usLevel ? 0x11 : 0x14);
  769.             GucAlarmDispBuf[i][j+2][5] = 0x1f;
  770.             GucAlarmDispBuf[i][j+2][6] = (uint8)j;
  771.             GucAlarmDispBuf[i][j+2][7] = 0x0c;
  772.         }
  773.     }
  774. }

  775. /*********************************************************************************************************
  776. ** Function name:           FromDispBuf
  777. **
  778. ** Descriptions:            更新当前时间
  779. **
  780. ** input parameters:
  781. ** output parameters:
  782. **
  783. ** Returned value           
  784. **
  785. **
  786. ** Created by:
  787. ** Created Date:
  788. **--------------------------------------------------------------------------------------------------------
  789. ** Modified by:
  790. ** Modified date:
  791. **--------------------------------------------------------------------------------------------------------
  792. *********************************************************************************************************/
  793. void FromDispBuf(void)
  794. {
  795.     int i, j;
  796.    
  797.     GtimeCurrentTime.ucSec  = (uint8)(GucTimeDispBuf[0][1]*10 + GucTimeDispBuf[0][0]);
  798.     GtimeCurrentTime.ucMin  = (uint8)(GucTimeDispBuf[0][3]*10 + GucTimeDispBuf[0][2]);
  799.     GtimeCurrentTime.ucHour = (uint8)(GucTimeDispBuf[0][5]*10 + GucTimeDispBuf[0][4]);
  800.     GtimeCurrentTime.ucWeek = GucTimeDispBuf[0][7];
  801.    
  802.     GtimeCurrentTime.ucDay  = (uint8)(GucTimeDispBuf[1][1]*10 + GucTimeDispBuf[1][0]);
  803.     GtimeCurrentTime.ucMon  = (uint8)(GucTimeDispBuf[1][3]*10 + GucTimeDispBuf[1][2]);
  804.     GtimeCurrentTime.usYear = (uint16)(GucTimeDispBuf[1][7]*1000 + GucTimeDispBuf[1][6]*100
  805.                             + GucTimeDispBuf[1][5]*10 + GucTimeDispBuf[1][4]);
  806.    
  807.     for (i=0; i<MAX_ALARM; i++) {
  808.         GalarmRingTime[i].ucSec = (uint8)(GucAlarmDispBuf[i][0][1]*10 + GucAlarmDispBuf[i][0][0]);
  809.         if (GalarmRingTime[i].ucSec > 59) {
  810.             GalarmRingTime[i].ucSec = 59;
  811.         }
  812.         GalarmRingTime[i].ucMin = (uint8)(GucAlarmDispBuf[i][0][3]*10 + GucAlarmDispBuf[i][0][2]);
  813.         if (GalarmRingTime[i].ucMin > 59) {
  814.             GalarmRingTime[i].ucMin = 59;
  815.         }
  816.         GalarmRingTime[i].ucHour = (uint8)(GucAlarmDispBuf[i][0][5]*10 + GucAlarmDispBuf[i][0][4]);
  817.         if (GalarmRingTime[i].ucHour > 23) {
  818.             GalarmRingTime[i].ucHour = 23;
  819.         }
  820.         
  821.         for (j=0; j<8; j++) {
  822.             if (GucAlarmDispBuf[i][1][j] == 1) {
  823.                 GalarmRingTime[i].ucEnable = (uint8)(GalarmRingTime[i].ucEnable | (1<<j));
  824.             }
  825.             else if (GucAlarmDispBuf[i][1][j] == 0) {
  826.                GalarmRingTime[i].ucEnable = (uint8)(GalarmRingTime[i].ucEnable & (~(1<<j)));
  827.             }
  828.         }
  829.         
  830.         for (j=0; j<4; j++) {
  831.             GalarmRingTime[i].c[j].usTime = (uint16)(GucAlarmDispBuf[i][j+2][3]*1000
  832.                                           + GucAlarmDispBuf[i][j+2][2]*100
  833.                                           + GucAlarmDispBuf[i][j+2][1]*10
  834.                                           + GucAlarmDispBuf[i][j+2][0]);
  835.             if (GucAlarmDispBuf[i][j+2][4] == 1) {
  836.                 GalarmRingTime[i].c[j].usLevel = 1;
  837.             }
  838.             else if (GucAlarmDispBuf[i][j+2][4] == 0) {
  839.                 GalarmRingTime[i].c[j].usLevel = 0;
  840.             }
  841.         }
  842.     }
  843. }

  844. /*********************************************************************************************************
  845. ** Function name:           SetTime
  846. **
  847. ** Descriptions:            设置当前时间
  848. **
  849. ** input parameters:
  850. ** output parameters:
  851. **
  852. ** Returned value           
  853. **
  854. **
  855. ** Created by:
  856. ……………………

  857. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
第四章 电脑自动打铃器设计与实现.rar (305.84 KB, 下载次数: 20)




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表