Cortex-M3 (NXP LPC1788)之PWM
1,PWM使用公共的PCLK,因此要配置系統(tǒng)時(shí)鐘和外設(shè)時(shí)鐘。之前的文章中有具體的時(shí)鐘配置過(guò)程。
本文引用地址:http://cafeforensic.com/article/201611/318448.htm2,使能PWM模塊。配置外設(shè)功率配置寄存器PCONP,使能PWM0的時(shí)鐘控制位。
3,PWM0.1的輸出管腳和P1_2管腳復(fù)用,因此要配置IOCON_P1_02寄存器,將其設(shè)置成PWM0.1的輸出。
4,設(shè)置PWM的脈沖寬度,基本的原理就是比較PWM定時(shí)器計(jì)數(shù)器TC和匹配寄存器MR中的值,如果匹配我們可以通過(guò)匹配控制寄存器MCR選擇操作,如產(chǎn)生一個(gè)中斷,復(fù)位TC,停止TC和預(yù)分頻計(jì)數(shù)器PC且停止計(jì)數(shù)。匹配寄存器MR0通過(guò)在匹配是將計(jì)數(shù)器TC復(fù)位來(lái)控制PWM的周期頻率。另一個(gè)匹配寄存器控制PWM沿的位置。如PWM0.1的輸出,將使用MR0控制PWM的周期頻率,MR1控制邊沿的位置。
5,最后是對(duì)于PWM的具體控制,配置PWM預(yù)分頻寄存器PWMPR,該32位寄存器規(guī)定了PWM預(yù)分頻計(jì)數(shù)的最大值,PWM預(yù)分頻計(jì)數(shù)器寄存器PWMPC在每個(gè)PCLK上遞增一次,當(dāng)PWMPC和PWMPR值相等時(shí),PWMTC的值會(huì)遞增,而PWMPR在系一個(gè)PCLK周期被復(fù)位。這樣,當(dāng)PWMPR=0時(shí),PWMTC會(huì)在每個(gè)PCLK上遞增,而當(dāng)PWMPR=1時(shí),在每2個(gè)PCLK上遞增。匹配寄存器PWMMR中的值和PWMTC的值比較,如果相等則觸發(fā)在PWMMCR中配置的操作。當(dāng)MR0和TC相等時(shí),我們進(jìn)行復(fù)位TC從新計(jì)數(shù)從而固定了PWM的周期頻率。當(dāng)定時(shí)器處于PWM模式時(shí),軟件對(duì)PWM匹配寄存器MR的寫操作,寫入值實(shí)際上被保存在一個(gè)映像寄存器中,不會(huì)被立即使用。所以在我們需要操作PWM鎖存使能寄存器PWMLER,典型序列為:將新值寫入MR,寫PWMLER中相應(yīng)的位,更改的MR值將在下一次定時(shí)器復(fù)位時(shí)生效。
在下面的程序中,將給MR1中寫入不同的匹配值,來(lái)控制PWM的占空比。為了方便使用LED燈進(jìn)行示意。
- #defineCCLK120000000
- #definePCLK60000000
- #definerFIO1DIR(*(volatileunsigned*)(0x20098020))
- #definerFIO1MASK(*(volatileunsigned*)(0x20098030))
- #definerFIO1PIN(*(volatileunsigned*)(0x20098034))
- #definerFIO1SET(*(volatileunsigned*)(0x20098038))
- #definerFIO1CLR(*(volatileunsigned*)(0x2009803c))
- #definerCLKSRCSEL(*(volatileunsigned*)(0x400FC10C))//時(shí)鐘源選擇寄存器
- #definerPLL0CON(*(volatileunsigned*)(0x400FC080))//PLL0控制寄存器
- #definerPLL0CFG(*(volatileunsigned*)(0x400FC084))//PLL0配置寄存器
- #definerPLL0STAT(*(volatileunsigned*)(0x400FC088))//PLL0狀態(tài)寄存器
- #definerPLL0FEED(*(volatileunsigned*)(0x400FC08C))//PLL0饋送寄存器
- #definerPLL1CON(*(volatileunsigned*)(0x400FC0A0))
- #definerPLL1CFG(*(volatileunsigned*)(0x400FC0A4))
- #definerPLL1STAT(*(volatileunsigned*)(0x400FC0A8))
- #definerPLL1FEED(*(volatileunsigned*)(0x400FC0AC))
- #definerCCLKSEL(*(volatileunsigned*)(0x400FC104))//CPU時(shí)鐘選擇寄存器
- #definerUSBCLKSEL(*(volatileunsigned*)(0x400FC108))//USB時(shí)鐘選擇寄存器
- #definerPCLKSEL(*(volatileunsigned*)(0x400FC1A8))//外設(shè)時(shí)鐘寄存器
- #definerPCON(*(volatileunsigned*)(0x400FC0C0))
- #definerPXCONP(*(volatileunsigned*)(0x400FC0C4))
- #definerSCS(*(volatileunsigned*)(0x400FC1A0))//系統(tǒng)控制和狀態(tài)寄存器
- #definerCLKOUTCFG(*(volatileunsigned*)(0x400FC1C8))
- #definerIOCON_P1_02(*(volatileunsigned*)(0x4002C088))
- #definerPCONP(*(volatileunsigned*)(0x400FC0C4))
- #definerPWM0IR(*(volatileunsigned*)(0x40014000))
- #definerPWM0TCR(*(volatileunsigned*)(0x40014004))
- #definerPWM0TC(*(volatileunsigned*)(0x40014008))
- #definerPWM0PR(*(volatileunsigned*)(0x4001400C))
- #definerPWM0CTCR(*(volatileunsigned*)(0x40014070))
- #definerPWM0MCR(*(volatileunsigned*)(0x40014014))
- #definerPWM0MR0(*(volatileunsigned*)(0x40014018))
- #definerPWM0MR1(*(volatileunsigned*)(0x4001401C))
- #definerPWM0CCR(*(volatileunsigned*)(0x40014028))
- #definerPWM0PCR(*(volatileunsigned*)(0x4001404C))
- #definerPWM0LER(*(volatileunsigned*)(0x40014050))
- #definerISER1(*(volatileunsigned*)(0xE000E104))
- #definerCER1(*(volatileunsigned*)(0xE000E184))
- unsignedintduty=10;
- unsignedcharmatch_cnt=0;
- voidPWM0_IRQHandler(void)
- {
- if(rPWM0IR&0x1)
- {
- rFIO1PIN|=(1<<18);
- match_cnt++;
- rPWM0IR|=0x1;//MR0中斷復(fù)位
- }
- if(rPWM0IR&(0x1<<1))
- {
- rFIO1PIN&=~(1<<18);
- rPWM0IR|=0x1<<1;//MR1中斷復(fù)位
- }
- }
- voidSystemInit()
- {
- rSCS&=~(0x1<<4);//頻率12M
- rSCS|=(0x1<<5);//使能主振蕩器
- while(0==(rSCS&(0x1<<6)));//等待主振蕩器穩(wěn)定
- rCLKSRCSEL=0x1;
- rPLL0CFG=0x9;//配置CCLK=120M
- rPLL0CON=0x01;
- rPLL0FEED=0xAA;
- rPLL0FEED=0x55;
- while(0==(rPLL0STAT&(0x1<<10)));
- rCCLKSEL=(0x1|(0x1<<8));
- rPCLKSEL=0x2;//配置PCLK=60M
- rCLKOUTCFG=0x0|(0xb<<4)|(0x1<<8);
- }
- voidPWMInit()
- {
- rIOCON_P1_02&=~0x7;
- rIOCON_P1_02|=0x3;//P1.02配置成PWM0[1]
- rPCONP|=0x1<<5;//使能PWM0外設(shè)
- rPWM0IR=0x73F;//初始化PWM相關(guān)控制寄存器
- rPWM0TCR=0;
- rPWM0CTCR=0;
- rPWM0MCR=0;
- rPWM0CCR=0;
- rPWM0PCR=0;
- rPWM0LER=0;
- rPWM0PR=0x1<<20;//每0x1<<20+1個(gè)PLCK上升沿,TC遞增
- rPWM0TCR|=0x1<<1;//復(fù)位TC和PC
- rPWM0TCR&=~(0x1<<1);
- rPWM0MR0=100;
- rPWM0LER|=0x1;
- rPWM0MCR|=0x1<<1|0x1;//MR0和TC匹配時(shí)復(fù)位TC和PC.并且產(chǎn)生中斷
- rPWM0MR1=duty;
- rPWM0LER|=0x1<<1;
- rPWM0MCR|=0x1<<3;//MR1和TC匹配時(shí)產(chǎn)生中斷
- }
- intmain()
- {
- PWMInit();
- rFIO1DIR|=(0x1<<18);
- rISER1|=0x1<<7;//PWM0中斷使能
- rPWM0TCR|=0x1<<1;//復(fù)位TC和PC
- rPWM0TCR&=~(0x1<<1);
- rPWM0TCR|=0x1;//PC和TC計(jì)數(shù)使能
- rPWM0TCR|=0x1<<3;//PWM模式使能
- while(1)
- {
- if(match_cnt>=1)
- {
- match_cnt=0;
- duty=duty+10;
- if(duty>=100)
- {
- duty=0;
- }
- rPWM0MR1=duty;
- rPWM0LER|=0x1<<1;
- rPWM0MCR|=0x1<<3;
- }
- }
- return1;
- }
LPC1788的PWM可以進(jìn)行雙邊沿的控制。如PWM0.2可以用MR0控制PWM的周期頻率,用MR1和MR2控制PWM0.2的邊沿。
評(píng)論