|
#include
#include"TA_PWM.h"
unsigned int bright=0;
void chushi()
{
// P1DIR|=BIT0;
// P1OUT|=BIT0;
P1REN|=BIT3;
P1OUT|=BIT3;
P1DIR&=~BIT3;
P1IES|=BIT3;////下降沿
P1IE|=BIT3;
}
void chuli()
{
//static unsigned int bright=0;
bright=bright+50;
if(bright>500)
bright=0;
TA_PWM_setperill(1,bright);
// }
}
void jiance()
{
unsigned int Push_Key=0;
Push_Key=P1IFG&(~P1DIR);
_delay_cycles(10000);
if((P1IN&Push_Key)==0)
{
switch(Push_Key)
{
case BIT3: chuli(); break;
default: break;
}
}
}
/*
* main.c
*/
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
TA_PWM_init('A',1,'F',0);
TA_PWM_setperiod(500);
chushi();
_enable_interrupts();
while(1);
}
#pragma vector=PORT1_VECTOR
__interrupt void port1_isr(void)
{
jiance();
P1IFG=0;
}
/*
* TA_PWM.c
*
* Created on: 2014-8-10
* Author: Administrator
*/
#include
#define DEADTIME 20
#define TA01_SEL P1SEL|=BIT6; P1DIR|=BIT6
#define TA01_SET P3SEL|=BIT0; P3DIR|=BIT0
#define TA11_SEL P2SEL|=BIT2; P2DIR|=BIT2
#define TA12_SEL P2SEL|=BIT4; P2DIR|=BIT4
#define TA01_OFF P1SEL&=~BIT6
#define TA02_OFF P3SEL&=~BIT0
#define TA11_OFF P2SEL&=~BIT2
#define TA12_OFF P2SEL&=~BIT4
char TA_PWM_init(char clk,char div,char mode1,char mode2)
{
TA0CTL=0;//////清楚以前设置
switch(clk)
{
case'A':case'a': TA0CTL|=TASSEL_1;break;////aclk
case's':case'S': TA0CTL|=TASSEL_2;break;////smclk
case'E': TA0CTL|=TASSEL_0;break;////weibu taclk
case'e': TA0CTL|=TASSEL_3;break;
default: return(0);
}
switch(div)
{
case 1:TA0CTL|=ID_0;break;
case 2:TA0CTL|=ID_1;break;
case 4:TA0CTL|=ID_2;break;
case 8:TA0CTL|=ID_3;break;
default: return(0);
}
switch(mode1)
{
case'F':case'f':case'b':case'B': TA0CTL|=MC_1;break;
case'D':case'd': TA0CTL|=MC_3;break;
default: return(0);
}
switch(mode1)
{
case'f':case'F': TA0CCTL1=OUTMOD_7;TA01_SEL; break;
case'b':case'B': TA0CCTL1=OUTMOD_3;TA01_SEL; break;
case'd':case'D': TA0CCTL1=OUTMOD_6;TA01_SEL; break;
case'0':case 0: TA01_OFF; break;
default : return(0);
}
switch(mode2)
{
case'F':case'f': TA0CCTL2=OUTMOD_7;TA01_SEL; break;
case'B':case'b': TA0CCTL2=OUTMOD_3;TA01_SEL; break;
case'D':case'd': TA0CCTL2=OUTMOD_6;TA01_SEL; break;
case'0': case 0: TA02_OFF; break;
default : return(0);
}
return (1);
}
char TA_PWM_setperiod(unsigned int period)
{
if(period>65535)
return(0);
TA0CCR0=period;
return (1);
}
char TA_PWM_setperill(char channel,unsigned duty)
{//1
unsigned char mod=0;
unsigned int deadperill=0;
unsigned long int percent=0;
percent=duty;
deadperill=((DEADTIME*1000)/TACCR0);
switch(channel)
{
case 1:
mod=(TA0CCTL1&0x00e0)>>5;break;
case 2:
mod=(TA0CCTL2&0x00e0)>>5;break;
default: return(0);
}
switch(mod)
{
case 2:case 6:
{
if((1000-2*percent)<=deadperill)
percent=(1000-deadperill)/2;
TA0CCR1=percent*TA0CCR0/1000;
TA0CCR2=TA0CCR0-TA0CCR1;
break;
}
case 7:
{
if(percent>1000)
percent=1000;
if(channel==1)
TA0CCR1=percent*TA0CCR0/1000;
if(channel==2)
TA0CCR2=percent*TA0CCR0/1000;
break;
}
case 3:
{
if(percent>1000)
percent=1000;
if(channel==1)
TA0CCR1=TA0CCR0-percent*TA0CCR0;
if(channel==2)
TA0CCR2=TA0CCR0-percent*TA0CCR0;
break;
}
default :return(0);
}
return (1);
}
|
|