找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM8单键长短按操作

[复制链接]
跳转到指定楼层
楼主
ID:94349 发表于 2015-11-2 14:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM8单键长短按操作带液晶显示
  1. #include "stm8s.h"
  2. #include "ht1621.h"
  3. #include "ht1621.c"
  4. unsigned char const smg_number[10]={0xaf,0x06,0xcb,0x4f,0x66,0x6d,0xed,0x07,0xef,0x6f};//0123456789
  5. unsigned char Hour2,Hour1,Min1,Min2,Sec2,Sec1;
  6. unsigned char Hour,Min,Sec,ST_Sec,ST_flag;
  7. unsigned char Half_SecFlag;
  8. unsigned char Key_Count,Key_flag,Key_long_flag;
  9. extern unsigned int count2;
  10. void Data_process(void)
  11. {
  12.   Hour2=Hour/10;
  13.   Hour1=Hour%10;
  14.   Min2=Min/10;
  15.   Min1=Min%10;
  16.   Sec2=Sec/10;
  17.   Sec1=Sec%10;
  18. }
  19. void Display(void)
  20. {
  21.   Ht1621WrOneNum(9,smg_number[Sec2]);Ht1621WrOneNum(12,smg_number[Sec1]);
  22.   Ht1621WrOneNum(4,smg_number[Min2]);Ht1621WrOneNum(6,smg_number[Min1]);
  23.   if((Half_SecFlag!=0))
  24.      {
  25.         Ht1621WrOneData(8,0x04);
  26.      }
  27.    else
  28.      {
  29.        Ht1621WrOneData(8,0x00);
  30.      }
  31.   if(ST_flag)
  32.   {
  33.    Ht1621WrCmd(LCDOFF);ST_flag=0;halt();
  34.   }
  35. }
  36. void GPIO_int(void)
  37. {
  38.   GPIO_Init(GPIOB, GPIO_PIN_HNIB, GPIO_MODE_OUT_PP_HIGH_FAST);
  39.   GPIO_Init(GPIOB, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
  40.   GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
  41.   GPIO_Init(GPIOC, GPIO_PIN_LNIB, GPIO_MODE_IN_PU_IT);
  42.   EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);
  43. }
  44. void TIM4_int(void)
  45. {
  46.   TIM4_TimeBaseInit(TIM4_PRESCALER_128, 77);//5ms
  47.   TIM4_ClearFlag(TIM4_FLAG_UPDATE);
  48.   TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  49.   TIM4_Cmd(ENABLE);
  50. }
  51. void TIM1_int(void)
  52. {
  53.   TIM1_TimeBaseInit(19, TIM1_COUNTERMODE_UP, 50000, 0);//500ms
  54.   TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
  55.   TIM1_GenerateEvent(TIM1_EVENTSOURCE_UPDATE);
  56.   //TIM1_Cmd(ENABLE);
  57. }
  58. void Beep_int(void)
  59. {
  60.   BEEP_DeInit();
  61.   BEEP_LSICalibrationConfig(128000);
  62.   BEEP_Init(BEEP_FREQUENCY_2KHZ);
  63. }
  64. void main(void)
  65. {
  66.   /* Infinite loop */

  67.   GPIO_int();
  68.   Ht1621_Init();
  69.   Ht1621_all_off();
  70.   GPIO_WriteHigh(GPIOB, BackLight);
  71.   Ht1621WrOneData(8,0x04);//col

  72.   TIM4_int();
  73.   TIM1_int();
  74.   Beep_int();

  75.   rim();

  76.   while (1)
  77.    
  78.   {
  79.     Data_process();
  80.     Display();
  81.    
  82.     if(Key_flag==1)
  83.     {
  84.       TIM1_Cmd(ENABLE);ST_Sec=0;count2=0;
  85.     }
  86.     if(Key_flag==2)
  87.     {
  88.       TIM1_Cmd(DISABLE);
  89.       Key_flag=0; ST_Sec=0;
  90.     }
  91.     if(Key_long_flag==1)
  92.     {
  93.       Min=0;Sec=0;Key_long_flag=0;
  94.     }
  95.     /*if (GPIO_ReadInputPin(GPIOC, GPIO_PIN_2) == RESET)
  96.     {
  97.       Delay(0xffff);
  98.       if (GPIO_ReadInputPin(GPIOC, GPIO_PIN_2) == RESET)
  99.       {
  100.         //GPIO_WriteReverse(GPIOB, GPIO_PIN_3);
  101.         //TIM4_Cmd(DISABLE);
  102.         Key_Flag=1;
  103.         Key_Count++;
  104.         if(Key_Count==1)
  105.         {TIM4_Cmd(ENABLE);}
  106.         if(Key_Count==2)
  107.         {TIM4_Cmd(DISABLE);}
  108.         if(Key_Count==3)
  109.         {Min=0;Sec=0;Key_Count=0;}
  110.        }
  111.       while(!GPIO_ReadInputPin(GPIOC, GPIO_PIN_2));//BEEP_Cmd(DISABLE);
  112.     }
  113.     if (GPIO_ReadInputPin(GPIOC, GPIO_PIN_1) == RESET)
  114.     {
  115.       Delay(0xffff);//BEEP_Cmd(ENABLE);
  116.       if (GPIO_ReadInputPin(GPIOC, GPIO_PIN_1) == RESET)
  117.       {
  118.         //TIM4_Cmd(ENABLE);
  119.         Min++;Key_Flag2=1;
  120.         if(Min==60)
  121.          {
  122.            Min=0;
  123.          }
  124.       }
  125.       while(!GPIO_ReadInputPin(GPIOC, GPIO_PIN_1));//BEEP_Cmd(DISABLE);
  126.       Key_Flag2=0;
  127.     }*/
  128.    
  129.   }

  130. }
  131. #ifdef USE_FULL_ASSERT
  132. /**
  133.   * @brief  Reports the name of the source file and the source line number
  134.   *   where the assert_param error has occurred.
  135.   * @param file: pointer to the source file name
  136.   * @param line: assert_param error line source number
  137.   * @retval : None
  138.   */
  139. void assert_failed(u8* file, u32 line)
  140. {
  141.   /* User can add his own implementation to report the file name and line number,
  142.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  143.   /* Infinite loop */
  144.   while (1)
  145.   {
  146.   }
  147. }
  148. #endif

  149. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  150. unsigned int count,count2;
  151.   unsigned char BSec;
  152.   extern unsigned char Hour,Min,Sec,Sec_4,Min_4,Half_SecFlag;  
  153. INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
  154. {
  155.   /* In order to detect unexpected events during development,
  156.      it is recommended to set a breakpoint on the following instruction.
  157.   */
  158.     count++;
  159.     TIM1_ClearFlag(TIM1_FLAG_UPDATE);
  160.     if(count==1)
  161.     {
  162.       BSec++;count=0;Half_SecFlag=~Half_SecFlag;
  163.       if(BSec==2)
  164.       {Sec++;BSec=0;
  165.         if(Sec==60)
  166.         {Sec=0;Min++;
  167.           if(Min==60)
  168.           {
  169.             Min=0;
  170.           }
  171.         }
  172.       }
  173.     }
  174. }
  175.   //unsigned int count=0;unsigned char BSec;
  176.   extern unsigned char SecFlag,Key_Flag,Key_Flag2,Key_long_flag;
  177.   extern unsigned char Hour,Min,Sec,ST_Sec,ST_flag,Key_flag;
  178.   unsigned int Key_cnt,Key_mode;
  179. INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  180. {
  181.    TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
  182.     if (GPIO_ReadInputPin(GPIOC, GPIO_PIN_1)== RESET)
  183.      {
  184.         Key_cnt++;
  185.         if(Key_cnt==4)//20ms
  186.         {
  187.           Key_mode++;
  188.           if(Key_mode==1)
  189.           {
  190.             Key_flag=0;ST_flag=0;Key_flag=1;
  191.           }
  192.           if(Key_mode==2)
  193.           {
  194.             Key_mode=0;ST_flag=0;Key_flag=0;
  195.             Key_flag=2;
  196.           }
  197.         }
  198.         if(Key_cnt>=3*200)//4s
  199.         {
  200.           if(Key_cnt>800)
  201.             Key_long_flag=1;
  202.         }
  203.       }
  204.     else
  205.     {Key_cnt=0;Key_long_flag=0;}
  206.     if(Key_flag==0)
  207.     {
  208.       count2++;
  209.       if(count2==200)
  210.       {
  211.         count2=0;ST_Sec++;
  212.         if(ST_Sec==10)
  213.         {
  214.           ST_Sec=0;ST_flag=1;
  215.         }
  216.       }
  217.    
  218.     }
  219.    
  220.    
  221. }
复制代码


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

使用道具 举报

沙发
ID:59609 发表于 2017-1-18 13:21 | 只看该作者
居然没有说明  孟工
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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