![]() |
其實只要這樣就是占空比50%了 while(1) { P1=~P1; DELAYUS(100); } |
我在上面的解答,仍有個bug: void Time1(void) interrupt 3 using 0 //“interrupt” 應(yīng)改正為: void Time0(void) interrupt 3 using 0 //“interrupt” { WO =~ WO //輸出引腳電平取反 TH0=(65536-50000)/256; TL0=(65536-50000)%256; } |
人人學(xué)會單片機,PWM程序 http://www.zg4o1577.cn/bbs/dpj-200820-1.html |
Kxuan163 發(fā)表于 2021-4-15 16:16 好的謝謝 |
我在上面寫的 “ T0中斷服務(wù)函數(shù)” 漏了個花括號,應(yīng)為: // T0中斷服務(wù)函數(shù): void Time1(void) interrupt 3 using 0 //“interrupt” { WO =~ WO //輸出引腳電平取反 TH0=(65536-50000)/256; TL0=(65536-50000)%256; } |
參與人數(shù) 1 | 黑幣 +20 | 收起 理由 |
---|---|---|
![]() | + 20 | 回帖助人的獎勵! |
建議試用用定時器中斷方式: //主函數(shù): void main(void) { EA=1; ET0=1; TMOD=0x01; //使用定時器T0的模式1 TH0=(65536-50000)/256; TL0=(65536-50000)%256; TR0=1; while(1) //無限循環(huán)等待中斷 ; } // T0中斷服務(wù)函數(shù): void Time1(void) interrupt 3 using 0 //“interrupt” WO =~ WO //輸出引腳電平取反 TH0=(65536-50000)/256; TL0=(65536-50000)%256; } |
參與人數(shù) 1 | 黑幣 +20 | 收起 理由 |
---|---|---|
![]() | + 20 | 回帖助人的獎勵! |
本帖最后由 daemondong 于 2021-4-15 14:48 編輯 這里改成 if(dang==1){count1++;if(count1<=2){w0=1;}if(count1>2){w0=0;}if(count1==4){count1=0;}} |
參與人數(shù) 1 | 黑幣 +20 | 收起 理由 |
---|---|---|
![]() | + 20 | 回帖助人的獎勵! |
[code]void timer0() interrupt 1{ TH0=(65536-50000)/256; TL0=(65536-50000)%256; if(dang==0){w0=0;} if(dang==1){count1++;if(count1<=1){w0=1;}if(count1>1){w0=0;}if(count1==4){count1=0;}} if(dang==2){w0=1;} 我是這樣寫的 |
程序呢? |
這種情況就是產(chǎn)生PWM的兩個參數(shù):周期和脈寬的參數(shù)設(shè)置的問題,仔細查一下看看。 |
參與人數(shù) 1 | 黑幣 +20 | 收起 理由 |
---|---|---|
![]() | + 20 | 回帖助人的獎勵! |
Powered by 單片機教程網(wǎng)