Other Parts Discussed in Thread: CC2530
I am working on CC2530 PWM output to cotrol the brightness of a LED indicator. I don't get the result when I press the buton K1 or K2. I debug the code , it can response the K1 or K1 to run TIMER34_SET_PWM_PULSE_LENGTH(4,luminance). The following is my code:
//#include <ioCC2530.h>
#include "hal.h"
*****************************************/
BYTE counter = 0;
/*****************************************
//T4 c
*****************************************/
#define TIMER34_SET_CLOCK_DIVIDE(timer,val) \
do{ \
T##timer##CTL &= ~0XE0; \
(val==2) ? (T##timer##CTL|=0X20): \
(val==4) ? (T##timer##CTL|=0x40): \
(val==8) ? (T##timer##CTL|=0X60): \
(val==16)? (T##timer##CTL|=0x80): \
(val==32)? (T##timer##CTL|=0xa0): \
(val==64) ? (T##timer##CTL|=0xc0): \
(val==128) ? (T##timer##CTL|=0XE0): \
(T##timer##CTL|=0X00); /* 1 */ \
}while(0)
//Macro for setting the mode of timer3 or 4
#define TIMER34_SET_MODE(timer,val) \
do{ \
T##timer##CTL &= ~0X03; \
(val==1)?(T##timer##CTL|=0X01): /*DOWN */ \
(val==2)?(T##timer##CTL|=0X02): /*Modulo */ \
(val==3)?(T##timer##CTL|=0X03): /*UP / DOWN */ \
(T##timer##CTL|=0X00); /*free runing */ \
}while(0)
#define K1 P0_7
#define K2 P0_6
#define S1 0X01
#define S2 0x02
#define NOKEY 0xff
/*****************************************
//
*****************************************/
void InitKey(void)
{
P0SEL &= ~0X30;
P0DIR &= ~0X30;
P0INP &= ~0X30;
P2INP |= 0X20;
}
/*****************************************
//
*****************************************/
void delay(UINT16 val)
{
UINT16 jj;
for(jj=0;jj<val;jj++);
for(jj=0;jj<val;jj++);
for(jj=0;jj<val;jj++);
for(jj=0;jj<val;jj++);
for(jj=0;jj<val;jj++);
}
/*****************************************
//
*****************************************/
void Init_T4_AND_LED(void)
{
IO_FUNC_PORT_PIN(1,0,IO_FUNC_GIO);
IO_DIR_PORT_PIN(1,0,IO_OUT);
IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH);
IO_PER_LOC_TIMER4_AT_PORT1_PIN01();
TIMER34_INIT(4); //
TIMER34_SET_MODE(4,3);
TIMER34_PWM_CONFIG(4);//T4CCTL1 |= 0X24;
//TIMER34_SET_CLOCK_DIVIDE(4,2); //
TIMER34_SET_PWM_PULSE_LENGTH(4,250); //
TIMER34_SET_MODE(4,3); //up&down
TIMER4_RUN(1);
};
/*********************************************************************
*********************************************************************/
BYTE KeyScan(void)
{
if(K1 == 0)
{
delay(200);
if(K1 == 0)return(S1);
}
if(K2 ==0)
{
delay(200);
if(K2 == 0)return(S2);
}
return(NOKEY);
}
void main(void)
{
BYTE keyvalue;
BYTE luminance=200;
InitKey();
Init_T4_AND_LED(); //
while(1) //
{
keyvalue=KeyScan();
if(keyvalue==S1)
{
if(luminance<=200)luminance+=50;
TIMER34_SET_PWM_PULSE_LENGTH(4,luminance);
}
if(keyvalue==S2)
{
if(luminance>=50)luminance-=50;
TIMER34_SET_PWM_PULSE_LENGTH(4,luminance);
}
delay(20000);
}
}
I don't know why I can adjust the LED brightness.Can someone help me,please.
Thanks for reply