LetAllLinesOfCodeSpeak

/******************************************************
*Author:JYW
*Time:2019_03_31
*Describe:使用T1对Led进行PWM代码学习练习
******************************/
#include<ioCC2530.h>

typedef unsigned char uchar;
typedef unsigned int uint;

#define LED1 P1_0
#define KEY1 P0_1

uchar KeyCount = 0;


void InitLed()
{
    P1DIR |= 0x01;
    LED1 = 1;

}

uchar KeyEvent()
{
    static uchar KeyBefSta = 1;
    static uchar KeyCurSta = 0;
    KeyCurSta = KEY1;
    if(KeyBefSta != KeyCurSta)
    {
        if(1 == KeyBefSta)
        {
            KeyCount++;
            if(KeyCount >= 4)
            {
                Keycount = 0;
            }
        
        }
        KeyBefSta = KeyCurSta;
    }
    return KeyCount;
}


void InitT1(uchar pwm)
{
    CLKCONCMD &= ~0x40;
    while(CLKCONSTA & 0x40);
    CLKCONCMD &= ~0x07;
    CLKCONCMD |= 0x38;
    
    PERCFG |= 0x40;
    P2SEL &= ~0x01;
    P2DIR |= 0xC0;
    

    P1DIR |= 0xff;              
    P1SEL |= 0x01;           
    
    T1CC2H = 0x00;           
    T1CC2L = pwm; 
    
    T1CC0H = 0x00;           
    T1CC0L = 0xff; 
    T1CCTL2 = 0x1c;          
    T1CTL = 0x02;
    
void main(void)
{
    InitLed();                
    InitT1(0x33);                
    while(1)
    {
      switch(KeyEvent())
      {
        case 0:InitT1(0x33);break;//20%
        case 1:InitT1(0x7F);break;//50%
        case 2:InitT1(0xCC);break;//80%
        case 3:InitT1(0xFF);break;//100%
        default: break;
      }
      
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值