N76E003 学习三 按键控制LED亮灭

#include <stdio.h>
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"

#define  Key1 P01 // STOP 按键  
#define  Key2 P10 // UP 按键  
#define  Key3 P10 // DOWN 按键 

#define  Out1 P30  // 1
#define  Out2 P05  // A
#define  Out3 P02  // F

/********************************************************
* 函数名称:GPIO_init
* 输    入:无
* 输    出:无
* 功能描述: i/o口初始化
********************************************************/
void GPIO_init()            // 端口初始化
{

       P01_Quasi_Mode;       // STOP 设置为准双向模式
       P10_Quasi_Mode;       // UP 设置为准双向模式
       P04_Quasi_Mode;       // DOWN 设置为准双向模式
       P30_PushPull_Mode;   // 1 设置为输出推挽模式
       P05_PushPull_Mode;   // A 设置为输出推挽模式
       P02_PushPull_Mode;   // F 设置为输出推挽模式




/********************************************************
* 函数名称:Timer0_Delay1ms
* 输    入:无
* 输    出:无
* 功能描述: 延时1MS
********************************************************/
void Timer0_Delay1ms(UINT32 u32CNT)
{
    clr_T0M;                                                       //T0M=0, Timer0 Clock = Fsys/12 定时器0 12分频
    TMOD |= 0x01;                                            //Timer0 is 16-bit mode  16位定器/计数器
    set_TR0;                                                     //Start Timer0  开启
    while (u32CNT != 0)
    {
        TL0 = LOBYTE(TIMER_DIV12_VALUE_1ms);                 //Find  define in "Function_define.h" "TIMER VALUE"
        TH0 = HIBYTE(TIMER_DIV12_VALUE_1ms);                 //高8位和低8位设置相应值
        while (TF0 != 1);                                                        //Check Timer0 Time-Out Flag  等待溢出
        clr_TF0;                                                                    //清除溢出
        u32CNT --;
    }
    clr_TR0;                                                //Stop Timer0  定时器0停止
}

/********************************************************
* 函数名称:MAIN
* 输    入:无
* 输    出:无
* 功能描述: 按键控制IO口输出
********************************************************/
void main (void) 
{
        GPIO_init();            // IO端口初始化 

        Out1 = 1;
        Out2 = 1;        
        Out3 = 1;               //输高低电平  LED全灭


       while(1)
      {
          if (Key1==0)       // STOP 按键  按下
       {                    Timer0_Delay1ms(100) ; // 延时消抖
           if (Key1==0)          
           {
               Out1 = 1;
               Out2 = 1;        
               Out3 = 1;           //输出高电平  LED全灭
              }           }
          if (Key2==0)         // UP 按键  按下
         {   
             Timer0_Delay1ms(100) ; // 延时消抖
              if (Key2==0) 
                   {         
                         Out1 = 1;
                         Out2 = 1;        
                         Out3 = 0;       //OUT3输出低电平  LED亮    
                    }
             }
          if (Key3==0)         // DOWN 按键  按下
          {
              Timer0_Delay1ms(100) ; // 延时消抖
               if (Key3==0)          
                {
                   Out1 = 1;
                   Out2 = 0;          //OUT2输出低电平  LED亮  
                   Out3 = 1; 
             }
          }
      }                        
}

新手刚学习,按键控制LED亮灭。走了点弯路,终于点亮了,分享给有需要的朋友

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值