CC2530¶¨Ê±Æ÷1ÔÚÊä³ö±È½Ïģʽ£¬ÓëͨµÀÏà¹ØµÄI/OÒý½ÅÉèÖÃΪÊä³ö¡£ÔÚ¶¨Ê±Æ÷Æô¶¯Ö®ºó£¬½«±È½Ï¼ÆÊýÆ÷ºÍͨµÀ±È½Ï¼Ä´æÆ÷T1CCnH:T1CCnLµÄÄÚÈÝ¡£Èç¹û±È½Ï¼Ä´æÆ÷µÈÓÚ¼ÆÊýÆ÷µÄÄÚÈÝ£¬Êä³öÒý½Å¸ù¾Ý±È½ÏÊä³öģʽT1CCTLn.CMPµÄÉèÖýøÐÐÉèÖᢸ´Î»»òÇл»¡£×¢ÒâÊä³öÒý½ÅÔËÐÐÔÚÒ»¸ö¸ø¶¨Êä³ö±È½ÏģʽÏÂʱ£¬ËüÉÏÃæµÄËùÓбßÑض¼ÊÇÎÞ¹ÊÕÏÔËÐеġ£Ð´Èë±È½Ï¼Ä´æÆ÷ T1CCnL½«±»»º³å£¬ÕâÑùдÈëµ½ T1CCnLµÄÖµ²»Æð×÷Óã¬Ö±µ½ÏàÓ¦µÄ¸ßλ¼Ä´æÆ÷T1CCnH±»Ð´È롣дÈë±È½Ï¼Ä´æÆ÷T1CCnH:T1CCnL¶ÔÓÚÊä³ö±È½ÏÖµ²»Æð×÷Óã¬Ö±µ½¶¨Ê±Æ÷´ïµ½0x00¡£
×¢ÒâͨµÀ0µÄÊä³ö±È½Ïģʽ½ÏÉÙ£¬ÒòΪT1CC0H:T1CC0LÔÚģʽ6ºÍ7ÓÐÒ»¸öÌØÊ⹦ÄÜ£¬ÕâÒâζ×ÅÕâЩģ
ʽ¶ÔÓÚͨµÀ0ÊDz»ÄÜʹÓõġ£
µ±·¢ÉúÒ»¸ö±È½Ïʱ£¬ÉèÖÃIRCON.T1IF±êÖ¾ºÍ¸ÃͨµÀµÄÖжϱêÖ¾ T1STAT.CHnIF£¨ nÊÇͨµÀºÅÂ룩¡£Èç¹û·Ö±ðÉèÖÃÁËÏàÓ¦µÄÖжÏÆÁ±ÎλT1CCTLn.IM£¬ÒÔ¼°IEN1.T1EN£¬½«²úÉúÒ»¸öÖжÏÇëÇó¡£²»Í¬¶¨Ê±Æ÷ģʽÏÂÊä³ö±È½ÏģʽµÄÀý×Ó¸ø¶¨ÔÚÒÔÏÂͼÖС£
±ßÑضÔÆ룺 PWMÊä³öÐźſÉÒÔʹÓö¨Ê±Æ÷ÔÚ×ÔÓÉÔËÐÐģʽÏ£¬Í¨µÀ1ºÍ2ÔÚÊä³ö±È½Ïģʽ6»ò7ÏÂÉú³É£¨ÓÉT1CCTLn.CMP붨Ò壬ÆäÖÐnÊÇ1»ò2£©£¬Èçͼ2.8Ëùʾ¡£ PWMÐźŵÄÖÜÆÚͨ¹ýÉèÖÃT1CC0È·¶¨£¬Í¨µÀÊä³öµÄÕ¼¿Õ±ÈÓÉT1CCnÈ·¶¨£¬ÆäÖÐnÊÇPWMͨµÀ1»ò2¡£Ò²¿ÉÒÔʹÓö¨Ê±Æ÷×ÔÓÉÔËÐÐģʽ¡£ÔÚÕâÖÖÇé¿öÏ£¬ T1CTL.DIVλÖеÄCLKCON.TICKSPDºÍ·ÖƵÆ÷ÖµÉèÖÃPWMÐźŵÄÖÜÆÚ¡£ PWMÐźŵļ«ÐÔÓÉʹÓõÄÊÇÊä³ö±È½Ïģʽ6»¹ÊÇ7È·¶¨¡£PWMÊä³öÐźŻ¹¿ÉÒÔʹÓÃͼ2.8ËùʾµÄÊä³ö±È½Ïģʽ4ºÍ5£¬»òͨ¹ýʹÓÃͼ2.9ËùʾµÄģģʽÉú³É¡£¶ÔÓÚ¼òµ¥µÄPWM£¬×îºÃʹÓÃʹÓÃÊä³ö±È½Ïģʽ4ºÍ 5À´Éú³É¡£
ÖÐÐĶÔÆ룺 PWMÊä³ö¿ÉÒÔͨ¹ýÑ¡Ôñ¶¨Ê±Æ÷Õý¼ÆÊý/µ¹¼ÆÊýģʽÉú³É¡£¸ù¾ÝPWMÐźÅËùÐèµÄ¼«ÐÔÑ¡ÔñͨµÀÊä³ö±È½Ïģʽ 4»ò 5£¨ÓÉ T1CCTLn.CMP붨Ò壬ÆäÖÐ nÊÇ1»ò 2£©¡£ PWMÐźŵÄÖÜÆÚÓÉ T1CC0È·¶¨£¬Í¨µÀÊä³öµÄÕ¼¿Õ±ÈÓÉT1CCnÈ·¶¨£¬ÆäÖÐnÊÇPWMͨµÀ1»ò2¡£Ä³Ð©ÀàÐ͵ĵç»úÇý¶¯Ó¦ÓóÌÐò»áÐèÒªÖÐÐĶÔÆëµÄPWMģʽ£¬Ò»°ãµØÕâ±È±ßÑضÔÆëµÄPWMģʽ²úÉúµÄÔëÒô¸üÉÙ£¬ÒòΪI/OÒý½Å´«Êä²»¼¯ÖÐÔÚͬһ¸öʱÖÓ±ßÑØÉÏ
#include <iocc2530.h>
//LED½Ó¿Ú ¸ßµçƽÓÐЧ
#define LED_R P1_1
#define LED_G P1_0
#define LED_B P1_2
void delay(unsigned int t)
{
while (t)
{
t--;
}
}
/* ÉèÖÃRGB_LED PWMÕ¼¿Õ±È */
void LED_set_pwm(unsigned char red_pwm, unsigned char green_pwm, unsigned char blue_pwm)
{
unsigned short temp;
if (red_pwm > 100 || green_pwm > 100 || blue_pwm > 100)
{
return;
}
/* ÉèÖÃÀ¶É«LEDÕ¼¿Õ±È */
if (blue_pwm)
{
T1CCTL0 |= 1<<2; //ʹÄÜCH0±È½Ïģʽ
temp = (unsigned short)(blue_pwm * (655.35 * (blue_pwm/100.0)));
T1CC0L = temp & 0xff; //³õʼ»¯CH0 Õ¼¿Õ±È
T1CC0H = temp >> 8;
}
else
{
T1CCTL0 &= ~(1<<2); //¹Ø±ÕCH0±È½Ïģʽ
}
/* ÉèÖúìÉ«LEDÕ¼¿Õ±È */
if (red_pwm)
{
T1CCTL1 |= 1<<2; //ʹÄÜCH1±È½Ïģʽ
temp = (unsigned short)(red_pwm * (655.35 * (red_pwm/100.0)));
T1CC1L = temp & 0xff; //³õʼ»¯CH1 Õ¼¿Õ±È
T1CC1H = temp >> 8;
}
else
{
T1CCTL1 &= ~(1<<2); //¹Ø±ÕCH1±È½Ïģʽ
}
/* ÉèÖÃÂÌÉ«LEDÕ¼¿Õ±È */
if (green_pwm)
{
T1CCTL2 |= 1<<2; //ʹÄÜCH2±È½Ïģʽ
temp = (unsigned short)(green_pwm * (655.35 * (green_pwm/100.0)));
T1CC2L = temp & 0xff; //³õʼ»¯CH2 Õ¼¿Õ±È
T1CC2H = temp >> 8;
}
else
{
T1CCTL2 &= ~(1<<2); //¹Ø±ÕCH2±È½Ïģʽ
}
}
/*
*³õʼ»¯¶¨Ê±Æ÷1£¬Í¨µÀ0/1/2£¬²úÉúPWM
*PWMÖÜÆÚ 2048us
*PWMÕ¼¿Õ±È£¬ÓÉ T1CCn µÄÖµ¾ö¶¨
**/
void init_timer1_pwm()
{
PERCFG |= 1<<6;//TIM1 ±¸ÓÃλÖÃ2
P1SEL |= 7; // P10 P11 P12 Ñ¡ÔñÍâÉ蹦ÄÜ
T1CTL = 0;
T1CTL &= ~(3<<2);//1·ÖƵ ¼ÆÊýÖÜÆÚ 1/32us
//TIM1_CH0 LED_BLUE
T1CCTL0 &= ~(7<<3);
T1CCTL0 |= 4<<3; //PWMģʽ
//TIM1_CH1 LED_RED
T1CCTL1 &= ~(7<<3);
T1CCTL1 |= 4<<3; //PWMģʽ
//TIM1_CH2 LED_GREEN
T1CCTL2 &= ~(7<<3);
T1CCTL2 |= 4<<3; //PWMģʽ
T1CTL |= 1<<0; //Æô¶¯¼ÆÊýÆ÷£¬×ÔÓÉÔËÐÐģʽ 0-0xffff
LED_set_pwm(0,0,0); //ÉèÖÃPWM Õ¼¿Õ±ÈΪ0£¬Ï¨ÃðLED
}
void init_system()
{
CLKCONCMD = 0;
while (CLKCONCMD != 0);//ÉèÖÃϵͳʱÖÓΪ32M
}
int main()
{
char duty_cycle = 100, dir = 1;
init_system();
init_timer1_pwm();
while (1)
{
LED_set_pwm(duty_cycle, 0, 0);
if (dir)
{
duty_cycle++;
if (duty_cycle > 100)
{
dir = !dir;
}
}
else
{
duty_cycle--;
if (duty_cycle ==0)
{
dir = !dir;
}
}
delay(30000);
}
}
¸´ÖÆ´úÂë