/******************************************************
*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;
}
}
}