![]() |
這是我畫的仿真電路和程序,現(xiàn)在這個程序只能讓電機同速轉(zhuǎn)動。但我需要當我按下SW1時,電機能停止,而且蜂鳴器能報警 |
參與人數(shù) 1 | 黑幣 +20 | 收起 理由 |
---|---|---|
![]() | + 20 | 回帖助人的獎勵! |
#include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit out1=P0^0; sbit out2=P0^1; sbit out3=P0^2; sbit out4=P0^3; bit pwm0;bit pwm1; bit o1,o2,o3,o4; uchar G_pwm_num0=50;//(占空比為50%),這兩個參數(shù)的最大值為100,因為下面count<=0 uchar G_pwm_num1=50; uchar count; void time_init() { TMOD=0x21; EA=1; TH1=156; TL1=156; TR1=1; ET1=1; ET0=1; TH0=0; TL0=0; } void time1() interrupt 3 { count++; if(count<G_pwm_num0) pwm0=1; else pwm0=0; if(count<G_pwm_num1) pwm1=1; else pwm1=0; if(count==100) //這里的占空比是這樣算的(G_pwm_num0/100,G_pwm_num1/100) { count=0; } else _nop_(); out1=o1&&pwm0;//left+ out2=o2&&pwm0; out3=o3&&pwm1;//right+ out4=o4&&pwm1; } void main() { time_init(); G_pwm_num0=50;//占空比可自行定義大小 o1=1; o2=0;//當兩者都為0時,停止 G_pwm_num1=50; o3=1; o4=0; while(1); } |
Powered by 單片機教程網(wǎng)