小车使用PWM调节频率使得能够进行直线行驶并且可以采用蓝牙进行调控- #include <REGX52.H>
- sbit IN1 = P1^1;
- sbit IN2 = P1^2;
- sbit IN3 = P1^3;
- sbit IN4 = P1^4;
- sbit ENA = P1^0;
- sbit ENB = P1^5;
- sbit LED1 = P1^6;
- unsigned char counter,compare = 0;
- void car_go()
- {
- IN1 = 1;
- IN2 = 0;
- IN3 = 0;
- IN4 = 1;
- }
- void Timer0_Init() //100微秒@11.0592MHz
- {
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0X01; //设置定时器模式
- TL0 = 0xA4; //设置定时初始值
- TH0 = 0xFF; //设置定时初始值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0 = 1;
- EA = 1;
- PT0 = 0;
- }
- void Timer_Init() interrupt 1
- {
- TL0 = 0xA4; //设置定时初始值
- TH0 = 0xFF; //设置定时初始值
-
- counter = 32;
- compare ++ ;
-
- if(compare <= counter)
- {
- IN1 = 0;
- IN2 = 1;
- }
- else
- {
- IN1 = 1;
- IN2 = 0;
- }
- if(compare == 100)
- {
- compare = 0;
- }
- }
- void main()
- {
- Timer0_Init();
-
- ENA = 1;
- ENB = 1;
-
- while(1)
- {
- car_go();
- }
- }
复制代码
|