板子型号:MSP-EXP430G2553
学校的电子赛要求两个:1.让红灯由亮到暗再到亮循环
2.让红灯根据温度显示出不同的亮度,温度高时亮,温度低时暗。
我现在刚刚会用CCS把程序烧进板子,知道头文件中BIT6是表示绿灯,请问红灯怎么表示,还有亮度怎么控制?
本帖最后由 monrother 于 2014-4-6 15:30 编辑
灯的话看你的硬件连接呢,亮度可以由PWM控制呢。。。
人有的时候真是奇怪,选择了会后悔,放弃了会遗憾,完美只能是一种理想,不是一种存在。
我知道BIT0是控制红灯了,现在要解决的是怎么学习PWM。大神能教下吗?留个QQ啥的
哪里有教程或者例程?找了半天没有找到。还有温度值怎么获取?
这个要求只是初筛,过了的才能参加培训。过完节就要交了
温度(内部)采集参照那上面的ADC10例程呢,里面有呢。。。根据温度输出不同占空比的波形就行了呢....
人有的时候真是奇怪,选择了会后悔,放弃了会遗憾,完美只能是一种理想,不是一种存在。
你在TI的网站就可以下到你的单片机对应的示例程序,里面有一个读取温度的示例和一个PWM的示例,你参照例子就可以做你的程序,相关资料你还可以百度一下430的呼吸灯,应该也有相应的范例
这个竞赛的题目蛮简单的(以前一直以为竞赛难度高),呵呵,循环PWM调光和温度检测控制LED亮暗
//******************************************************************************
// MSP430G2xx3 Demo - Timer_A, PWM TA1, Up/Down Mode, 32kHz ACLK
//
// Description: This program generates one PWM output on P1.2 using
// Timer_A configured for up/down mode. The value in CCR0, 128, defines the
// PWM period/2 and the value in CCR1 the PWM duty cycle. Using
// 32kHz ACLK as TACLK, the timer period is 7.8ms with a 75% duty cycle on
// P1.2. Normal operating mode is LPM3.
// ACLK = TACLK = LFXT1 = 32768Hz, MCLK = default DCO
// //* External watch crystal installed on XIN XOUT is required for ACLK *//
//
// MSP430G2xx3
// -----------------
// /|\| XIN|-
// | | | 32kHz
// --|RST XOUT|-
// | |
// | P1.2/TA1|--> CCR1 - 75% PWM
//
// D. Dang
// Texas Instruments, Inc
// December 2010
// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
//******************************************************************************
#include
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x0C; // P1.2 and P1.3 output
P1SEL |= 0x0C; // P1.2 and P1.3 TA1/2 otions
CCR0 = 128; // PWM Period/2
CCTL1 = OUTMOD_6; // CCR1 toggle/set
CCR1 = 32; // CCR1 PWM duty cycle
TACTL = TASSEL_1 + MC_3; // ACLK, up-down mode
_BIS_SR(LPM3_bits); // Enter LPM3
}
这是TI的官方PWM代码,但看规格书P1.0好象不能使用PWM。
下面这个是以前一个供你参考,对错不知道 。
#include "MSP430G2202.h"
#define PWM_P 1000
#define PWM_S 400
#define PWM_L 750
#define PWM_T 10
#define KEY1 BIT1
#define LED1 BIT2 //P1.2|TA0.1 P2.1=TA1.1
#define KEY2 BIT3
#define LED2 BIT4 //P1.4|TA0.2 P2.4=TA1.2
void POWER_ON(unsigned char);
void POWER_OFF(unsigned char);
void LED_DIM(unsigned char);
void DIM_UP(unsigned char);
void DIM_DOWN(unsigned char);
void Delay(int);
unsigned char POWER_STU1=0;
//unsigned char POWER_STU2=0;
unsigned char COUNT_UP=0;
/////////////////////////
//void POWER_ON(void);
//void POWER_OFF(void);
//void LED_DIM(void);
//void Delay(int);
//void DIM_UP(void);
//void DIM_DOWN(void);
//unsigned char POWER_STU=0;
//unsigned char COUNT_UP=1;
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
DCOCTL|= MOD0+DCO0;//+DCO1;//+DCO2 // Set DCO Frequence select
BCSCTL1|=RSEL2;//+RSEL0;// RSEL3++RSEL1; // Set DCO Resistor
// P1SEL |= LED1+LED2; // P1.6 TA1/2 options
//P1SEL2 |=LED2;
P1OUT &= ~(LED1+LED2);
P1DIR |= (LED1+LED2);// // P1.0 and P1.6 output
CCR0 = PWM_P; // PWM Period
CCTL1 = OUTMOD_7; // CCR1 reset/set
CCTL2 = OUTMOD_7; // CCR2 reset/set
//TACTL = TASSEL_2 + MC_1; // SMCLK, up mode
P1IE |= BIT3; // P1.3 interrupt enabled
P1IES|= BIT3; // P1.3 H/L Hiedge
P1IFG &= ~(BIT3); // P1.3 IFG cleared
_EINT();
_BIS_SR(LPM0_bits); // Enter LPM0
}
/*********** P1 port 中断程序 ***************/
#pragma vector=PORT1_VECTOR
__interrupt void Port_11(void)
{ switch(P1IFG&(KEY1+KEY2))
{
case KEY1:
Delay(20);//延迟,防误判
if((P1IN&KEY1)==0) //再次确认键已经按下;
{ Delay(500);//延时0.5S
if((P1IN & KEY1)!=0)
{
if ((POWER_STU1&LED1)==0)
{
POWER_ON(LED1);
POWER_STU1|=LED1;
}
else
{
POWER_OFF(LED1);
POWER_STU1&=~LED1;
}
}
Delay(1000);
LED_DIM(LED1);
}
P1IFG &=~(KEY1);
break;
case KEY2:
Delay(20);//延迟,防误判
if((P1IN&KEY2)==0) //再次确认键已经按下;
{ Delay(500);//延时0.5S
if((P1IN & KEY2)!=0)
{
if ((POWER_STU1&LED2)==0)
{
POWER_ON(LED2);
POWER_STU1|=LED2;
}
else
{
POWER_OFF(LED2);
POWER_STU1&=~LED2;
}
}
Delay(1000);
LED_DIM(LED2);
}
P1IFG &=~(KEY2);
break;
}
}
//LED渐变调光
void LED_DIM(unsigned char LEDx)
{ unsigned char KEYx;
if (LEDx==LED1)
{KEYx=KEY1;}
else
{
if(LEDx==LED2)
{
KEYx=KEY2;
}
else
{
KEYx=(KEY1+KEY2);
}
}
while(POWER_STU1&&((P1IN&KEYx)==0)) //P1.3为LOW,开始自动调光
{ Delay(10);
if(COUNT_UP)
{
DIM_UP(LEDx);
}
else
{
DIM_DOWN(LEDx);
}
}
P1IFG &=~(KEYx);
}
//POWER ON OFF
void POWER_ON(unsigned char LED_x)
{
POWER_STU1|=LED_x;
P1DIR|=LED_x;
P1SEL|=LED_x;
P2SEL|=(LED_x&LED2);
CCR0 = PWM_P; // PWM Period
CCTL2=CCTL1 = OUTMOD_7;
switch(LED_x)
{
case LED1+LED2:
CCR2=CCR1=5; // CCR1 reset/set
for (; CCR1<700; CCR1+=10)
{
CCR2=CCR1;
Delay(10);
TACTL = TASSEL_2 + MC_1;
}
break;
case LED1:
CCR1=5; // CCR1 reset/set
for (; CCR1<700; CCR1+=10)
{
Delay(10);
TACTL = TASSEL_2 + MC_1;
}
break;
case LED2:
CCR1=5; // CCR1 reset/set
for (; CCR2<700; CCR2+=10)
{
Delay(10);
TACTL = TASSEL_2 + MC_1;
}
break;
}
}
void POWER_OFF(unsigned char LED_y)
{ int i;
if(CCR1>CCR2)
{i=CCR1;}
else{i=CCR2;}
switch(LED_y)
{
case LED1+LED2:
while(i>15)
{
Delay(10);
CCR1-=10;
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
break;
//////
case LED1:
while(CCR1>15)
{
Delay(10);
CCR1-=10;
TACTL = TASSEL_2 + MC_1;
}
break;
///////
case LED2:
while(CCR2>15)
{
Delay(10);
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
break;
P1OUT&=~(LED_y);
P1DIR&=~(LED_y);
POWER_STU1&=~(LED_y);
}
//
void DIM_UP(unsigned char LED_n)
{
int i;
if(CCR1>CCR2)
{i=CCR1;}
else
{i=CCR2;}
switch (LED_n)
{
case LED1+LED2:
if(i<1000)
{ Delay(10);
CCR1+=10;
CCR2+=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=0;}
break;
///
case LED1:
if(CCR1<1000)
{ Delay(10);
CCR1+=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=0;}
break;
////
case LED2:
if(CCR2<1000)
{ Delay(10);
CCR2+=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=0;}
break;
}
}
//
void DIM_DOWN(unsigned char LED_n)
{
int i;
if(CCR1
{i=CCR1;}
else{i=CCR2;}
switch (LED_n)
{
case LED1+LED2:
if(i>15)
{ Delay(10);
CCR1-=10;
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=1;}
break;
///
case LED1:
if(CCR1>15)
{ Delay(10);
CCR1-=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=1;}
break;
////
case LED2:
if(CCR2>15)
{ Delay(10);
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=1;}
break;
}
//
//Delay function
void Delay(int i){}
第一个程序我是这么写的……
#include
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased, trap CPU!!
}
// Configure Basic Clock
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
BCSCTL3 |= LFXT1S_2; // Set LFXT1
P1DIR = BIT0; // P1.6 output (green LED)
P1OUT = 0; // LED off
IFG1 &= ~OFIFG; // Clear OSCFault flag
BCSCTL2 |=SELM_1 + DIVM_0; // Set MCLK
for(;;)
{
int i;
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(1000);
P1OUT = 0;
_delay_cycles(5000-1000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(2000);
P1OUT = 0;
_delay_cycles(5000-2000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(3000);
P1OUT = 0;
_delay_cycles(5000-3000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(4000);
P1OUT = 0;
_delay_cycles(5000-4000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(5000);
P1OUT = 0;
_delay_cycles(5000-5000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(5000);
P1OUT = 0;
_delay_cycles(5000-5000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(4000);
P1OUT = 0;
_delay_cycles(5000-4000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(3000);
P1OUT = 0;
_delay_cycles(5000-3000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(2000);
P1OUT = 0;
_delay_cycles(5000-2000);}
_delay_cycles(500000);
for( i=0;i<100;i++)
{P1OUT = BIT0;
_delay_cycles(1000);
P1OUT = 0;
_delay_cycles(5000-1000);}
_delay_cycles(500000);
}
}
表示其实就三句话,只不过delay不能接变量,谁告诉我怎么精简一下最好