有小车循迹
有红外遥控
有声控小车
有超声波避障
电路原理图如下:
单片机源程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit pwm=P2^7; //P2.7为PWM脚。
- uchar count,angle; //计数和角度。
- void delay_100us() //100us延时,相当于0.1ms
- {
- uchar j;
- for(j=0;j<31;j++)
- {
- ;
- }
- }
- void toji()
- {
- while(count<200) //count少于200时,无限循环,相当于20ms为一个周期
- {
- if(count<angle) //count少于角度。
- {
- pwm=1; //pwm输出高电平。
-
-
- }
-
- else //其它情况
- {
- pwm=0; //pwm输出低电平。
- }
- delay_100us(); //延时100us,相当于0.1ms
- count++; //count自加1。
- }
- count=0; //当count经历200次后,count归0
- }
- void main()
- {
- count=0;
- while(1)
- {
- angle=5; //角度为5ms
- toji(); //调用舵机子程序。
-
- }
- }
复制代码
所有资料51hei提供下载:
03 小车程序与电路.rar
(3.23 MB, 下载次数: 53)
|