完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include
***it PWM1 = P2^0;//PWM?????? ***it PWM2 = P2^1; ***it motor_control_1 = P2^7;//???? ***it motor_control_2 = P2^6;// ***it motor_control_3 = P2^5; ***it motor_control_4 = P2^4;// unsigned int PWMCnt1 = 0; unsigned int cntPWM1 = 60; unsigned int PWMCnt2 = 0; unsigned int cntPWM2 = 60; unsigned char bluetoothdata; void initial_myself(); void initial_interrupt(); void usart_service(void); void delay_long(unsigned int time); void go_forward(void);// void stop(); void turn_right(); void turn_left(); void back(); void turn_right1(); void turn_left1(); #define uint unsigned int #define uchar unsigned char void DelayMs(uchar ms); void init_PWM(); void PWM0_set(uchar empty); void DelayMs(uchar ms) //????????,?????,????? { uint i; while(ms--) { for(i = 0; i < 850; i++); } } void init_PWM() { CCON=0X00; CH=0; CL=0; CMOD=0X02; //0X00:?????/12 ????,0X02:????/2,0x08:???? PWM0_set(0Xec);//?????,0XC0:????25%,0X80:????50%,0X40:????75% // PCA_PWM0=0x00; //??????????0 CCAPM0=0X42; //0X42:8?PWM P1.3??,???;0X53:8?PWM??,???????;0X63:???????;0X73:??????? CR=1; //??????? } void PWM0_set(uchar empty) //??????? { CCAP0L=empty; CCAP0H=empty; } void main() { init_PWM(); initial_myself(); delay_long(100); initial_interrupt(); stop(); while(1) { usart_service(); } } void usart_service() { switch(bluetoothdata) { case 'f':go_forward(); delay_long(100); SBUF = 'f';//??????? bluetoothdata = 'a'; break; case 's':stop(); delay_long(100); SBUF = 's'; bluetoothdata = 'a'; break; case 'r':turn_right(); delay_long(100); SBUF = 'r'; bluetoothdata = 'a'; break; case 'l':turn_left(); delay_long(100); SBUF = 'l'; bluetoothdata = 'a'; break; case 'x':turn_right1(); delay_long(100); SBUF = 'r'; bluetoothdata = 'a'; break; case 'y':turn_left1(); delay_long(100); SBUF = 'l'; bluetoothdata = 'a'; break; case 'b':back(); delay_long(100); SBUF = 'b'; bluetoothdata = 'a'; break; case '1':cntPWM1 = 60; cntPWM2 = 60; delay_long(100); SBUF = '1'; bluetoothdata = 'a'; break; case '2':cntPWM1 = 100; cntPWM2 = 100; delay_long(100); SBUF = '2'; bluetoothdata = 'a'; break; case '3':cntPWM1 = 140; cntPWM2 = 140; delay_long(100); SBUF = '3'; bluetoothdata = 'a'; break; } } void usart_receive(void) interrupt 4 //?????? { if(RI == 1)//???? { RI = 0;//???? bluetoothdata = SBUF;//???? } if(TI == 1)//???? { TI = 0;//?? } } void T0_time() interrupt 1 //???1???? { PWMCnt1++; PWMCnt2++; if(PWMCnt1 >= 200) { PWMCnt1 = 1; } if(PWMCnt1 <= cntPWM1) //230 { PWM1 = 1; } else { PWM1 = 0; } if(PWMCnt2 >= 200) { PWMCnt2 = 1; } if(PWMCnt1 <= cntPWM2) //230 { PWM2 = 1; } else { PWM2 = 0; } TH0 = (65536 - 50) / 256; TL0 = (65536 - 50) % 256; } void initial_myself() { TMOD = 0x01;// TH0 = (65536 - 50) / 256;//??50?? TL0 = (65536 - 50) % 256; SCON = 0x50; TMOD = 0x21; TH1 = TL1 = 0xfd;//?????9600 IP = 0x10;//??????????? } void initial_interrupt() { EA = 1;// ???? ES = 1;// ????? ET0 = 1;//????0??? TR0 = 1;//????0 TR1 = 1;// ????1 } void delay_long(unsigned int time) { unsigned int i; unsigned int j; for(i = 0; i< time;i++) { for(j = 0; j < 500; j++); } } void go_forward() { PWM0_set(0xec); motor_control_1 = 0; motor_control_2 = 1; motor_control_3 = 0; motor_control_4 = 1; } void stop() { PWM0_set(0Xec); motor_control_1 = 0; motor_control_2 = 0; motor_control_3 = 0; motor_control_4 = 0; } void turn_left() { PWM0_set(0Xe7); motor_control_1 = 0; motor_control_2 = 1; motor_control_3 = 0; motor_control_4 = 1; } void turn_right() { PWM0_set(0Xf0); motor_control_1 = 0; motor_control_2 = 1; motor_control_3 = 0; motor_control_4 = 1; } void back() { PWM0_set(0xec); motor_control_1 = 1; motor_control_2 = 0; motor_control_3 = 1; motor_control_4 = 0; } void turn_left1() { PWM0_set(0Xe7); motor_control_1 = 1; motor_control_2 = 0; motor_control_3 = 1; motor_control_4 = 0; } void turn_right1() { PWM0_set(0Xf0); motor_control_1 = 1; motor_control_2 = 0; motor_control_3 = 1; motor_control_4 = 0; } |
|
相关推荐
5个回答
|
|
首先确定发送的消息确实收到了,其次是你的消息是否被正确处理
|
|
|
|
@曲终人散@ 发表于 2016-10-28 10:42 你好,我前期的程序可以用手机控制前后和档位,说明蓝牙通信成功, 后来加入了这块芯片的内部产生的pwm波,去控制舵机, 然后再试验就错了,输入字符就没反应了。(程序 0 error) |
|
|
|
回帖奖励 +1 分积分Hey__Jude 发表于 2016-10-28 11:09 那就检查检查PWM输出是否正常。 |
|
|
|
Hey__Jude 发表于 2016-10-28 11:09 你先别用蓝牙去控制舵机,先事先实现你想要的功能,看能不能实现,然后再和一起 |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
求解外围电路实现的是4脚给持续低电平复位并正常工作,高电平不工作的原因
2087 浏览 1 评论
3633 浏览 3 评论
PIC1946程序有一个变量在运行过程中恢复初始值其他变量保持不变
2337 浏览 2 评论
2764 浏览 0 评论
PIC16F1825的RC5引脚,在主程序中操作无效,在中断中可以改变是为什么?
4033 浏览 5 评论
980浏览 0评论
用XC8编译PIC18F25K80时提示下面Error,求怎么解决这个问题
6366浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-5 11:13 , Processed in 0.675767 second(s), Total 87, Slave 69 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号