危機處理-呼吸燈
阿阿阿,去夏威夷爽了一個禮拜來補個上禮拜作業
主程式不難,比較麻煩的部分是PWM的設定
主程式:
while(1){
int n;
key=Scankey();
DrvSYS_Delay(15000);
//if(key!=0)break;
if(key==1&&n!=1){
light1(1);
n=1;
}
if(key==2&&n!=2){
light2(1);
n=2;
}
if(key==3&&n!=3){
light3(1);
n=3;
}
if(key==4&&n!=4){
light4(1);
n=4;
}
}
light 函式:
原理就是此函式指定的燈會漸亮(for迴圈)
同時其他任何有亮的燈都會漸暗( if(Duty_Cycle<100) )
void light1(int x){
//if(x==0)K=-K;
for(int i=0; i<10; i++){
Duty_CycleA=Duty_CycleA-K*10;
CMRA = (Duty_CycleA*CNRA)/100 ; //Duty Cycle(Unit:%)
PWMA->CMR0=CMRA;
if(Duty_CycleB<100){
Duty_CycleB=Duty_CycleB+K*10;
CMRB = (Duty_CycleB*CNRB)/100 ; //Duty Cycle(Unit:%)
PWMA->CMR1=CMRB;
}
if(Duty_CycleC<100){
Duty_CycleC=Duty_CycleC+K*10;
CMRC = (Duty_CycleC*CNRC)/100 ; //Duty Cycle(Unit:%)
PWMA->CMR2=CMRC;
}
if(Duty_CycleD<100){
Duty_CycleD=Duty_CycleD+K*10;
CMRD = (Duty_CycleD*CNRD)/100 ; //Duty Cycle(Unit:%)
PWMA->CMR3=CMRD;
}
DrvSYS_Delay(200000);
}
}
PWM設定:
void InitPWM(void)
{
/* 1.Setup clock source divider select register (CSR) */
SYSCLK->APBCLK.PWM01_EN = 1; //Enable PWM clock
/*改這邊*/SYSCLK->CLKSEL1.PWM01_S = 0; //00=external(12M), 01=32.768k, 10=HCLK, 11=22.1184M
/*改這邊*/PWMA->CSR.CSR0=0; //000=2, 001=4, 010=8, 011=16, 100=1
SYSCLK->APBCLK.PWM23_EN = 1;
/*改這邊*/SYSCLK->CLKSEL1.PWM01_S = 0;
/*改這邊*/PWMA->CSR.CSR1=0;
SYSCLK->APBCLK.PWM45_EN = 1;
/*改這邊*/SYSCLK->CLKSEL1.PWM23_S = 0;
/*改這邊*/PWMA->CSR.CSR2=0;
SYSCLK->APBCLK.PWM67_EN = 1;
/*改這邊*/SYSCLK->CLKSEL1.PWM23_S = 0;
/*改這邊*/PWMA->CSR.CSR3=0;
/* 2.Setup pre-scaler (PPR) */
/*改這邊*/PWMA->PPR.CP01=1; //Prescaler 0~255, Setting 0 to stop output clock
/*改這邊*/PWMA->PPR.CP23=1;
/*改這邊*/PWMA->PPR.DZI01=1;
/*改這邊*/PWMA->PPR.DZI23=1;
/* 3.Setup inverter on/off,
/* 3.Setup inverter on/off, dead zone generator on/off, auto-reload/ one-shot mode and Stop PWM-timer (PCR)*/
PWMA->PCR.CH0MOD=1; //0:One-shot mode, 1:Auto-load mode
PWMA->PCR.CH0INV=0; //Inverter->0:off, 1:on
PWMA->PCR.CH1MOD=1;
PWMA->PCR.CH1INV=0;
PWMA->PCR.CH2MOD=1;
PWMA->PCR.CH2INV=0;
PWMA->PCR.CH3MOD=1;
PWMA->PCR.CH3INV=0;
/* 5.Setup PWM down-counter register (CNR) for setting PWM period. */
/*改這邊*/PWMA->CNR0=60000; //Counter Register 0~65535
/*改這邊*/PWMA->CNR1=60000;
/*改這邊*/PWMA->CNR2=60000;
/*改這邊*/PWMA->CNR3=60000;
/* 4.Setup comparator register (CMR) for setting PWM duty. */
PWMA->CMR0=60000; //Compare Register 0~65535
PWMA->CMR1=60000;
PWMA->CMR2=60000;
PWMA->CMR3=60000;
//Frequency = clock source/[(prescale+1)*(clock divider)*(CNR+1)];
//Duty ratio = (CMR+1)/(CNR+1).
/* 7.Setup corresponding GPIO pins as PWM function (enable POE and disable CAPENR) for the corresponding PWM channel. */
SYS->GPAMFP.PWM0_AD13=1;
PWMA->POE.PWM0=1;//Output to pin->0:Diasble, 1:Enable
SYS->GPAMFP.PWM1_AD14=1;
PWMA->POE.PWM1=1;
SYS->GPAMFP.PWM2_AD15=1;
PWMA->POE.PWM2=1;
SYS->GPAMFP.PWM3_I2SMCLK=1;
PWMA->POE.PWM3=1;
//PWMA->POE.SPI0_MISO0_I2SDI=1;
/* 8.Enable PWM timer start running (Set CHxEN = 1 in PCR) */
PWMA->PCR.CH0EN=1; //PWM function->0:Disable, 1:Enable
PWMA->PCR.CH1EN=1;
PWMA->PCR.CH2EN=1;
PWMA->PCR.CH3EN=1;
}
就先這樣囉,有空再更新細部說明
掰噗!
留言
張貼留言