完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
舵机Servo 示意图
舵机控制原理 使用芯片:Atmega 2560 晶振: 16MHz 产品: freenove_Carwling_Robot 目标:用16位的四个定时器1,3,4,5分别产生PWM控制12个舵机。由于舵机的控制并不是PWM的硬件连接,所以这里要使用软件中断来控制 程序: #include #include #define F_CPU 16000000UL #include // ******************************************************************************** // Interrupt Routines // ******************************************************************************** #define scale 180 #define RF_wrist_def 170*182 #define RF_elbow_def 110*182 //minus up #define RF_shoulder_def 127*182 #define LF_wrist_def 100*182 // inc dn #define LF_elbow_def 150*182 //inc up #define LF_shoulder_def 130*182 //inc cw #define RR_wrist_def 170*182 #define RR_elbow_def 110*182 #define RR_shoulder_def 90*182 void timer_init(void) { // timer1 init TIMSK1= (1< TIMSK3= (1< } ISR(TIMER1_OVF_vect) { // timer1 overflow PORTA |= 0b00000111; //PA0 <--> D22 // } ISR(TIMER1_COMPA_vect) { // timer1 match PORTA &= ~0x01; } ISR(TIMER1_COMPB_vect) { PORTA &= ~0x02; } ISR(TIMER1_COMPC_vect) { PORTA &= ~0x04; } void RF_wrist(unsigned char angle) { unsigned int temp = angle*scale; OCR1A = temp; } void RF_elbow(unsigned char angle) { unsigned int temp = angle*scale; OCR1B = temp; } void RF_shoulder(unsigned char angle) { unsigned int temp = angle*scale; OCR1C = temp; } ISR(TIMER3_OVF_vect) { // timer1 overflow PORTC |= 1<<0; //37<-->PC0 PORTD |= 1<<7; //38<-->PD7 PORTG |= 1<<2; //39<-->PG2 } ISR(TIMER3_COMPA_vect) { // timer3 match PORTC &= ~(1<<0); //37<-->PC0 } ISR(TIMER3_COMPB_vect) { PORTD &= ~(1<<7); //38<-->PD7 } ISR(TIMER3_COMPC_vect) { PORTG &= ~(1<<2); //39<-->PG2 } void LF_wrist(unsigned char angle) { unsigned int temp = angle*scale; OCR3C = temp; } void LF_elbow(unsigned char angle) { unsigned int temp = angle*scale; OCR3B = temp; } void LF_shoulder(unsigned char angle) { unsigned int temp = angle*scale; OCR3A= temp; } unsigned char RF_wrist_cur = 0; unsigned char RF_elbow_cur = 0; unsigned char RF_shoulder_cur = 0; unsigned char LF_wrist_cur = 0; unsigned char LF_elbow_cur = 0; unsigned char LF_shoulder_cur = 0; unsigned char RR_wrist_cur = 0; unsigned char RR_elbow_cur = 0; unsigned char RR_shoulder_cur = 0; unsigned char LR_wrist_cur = 0; unsigned char LR_elbow_cur = 0; unsigned char LR_shoulder_cur = 0; void shape_def(void) { PORTK |= (1<<7) | (1<<5); OCR1A = RF_wrist_def; OCR1B = RF_elbow_def; OCR1C = RF_shoulder_def; RF_wrist_cur = RF_wrist_def/scale; RF_elbow_cur = RF_elbow_def/scale; RF_shoulder_cur = RF_shoulder_def/scale; OCR3A = LF_shoulder_def; OCR3B = LF_elbow_def; OCR3C = LF_wrist_def; LF_wrist_cur = LF_wrist_def/scale; LF_elbow_cur = LF_elbow_def/scale; LF_shoulder_cur = LF_shoulder_def/scale; _delay_ms(1000); PORTK &= ~((1<<7) | (1<<5)); } void Action1(void){ PORTK |= (1<<7) | (1<<5); for(unsigned char i=0;i<22;i++) { RF_elbow_cur-=2; RF_elbow(RF_elbow_cur); RF_wrist_cur-=4; RF_wrist(RF_wrist_cur); LF_elbow_cur+=2; LF_elbow(LF_elbow_cur); LF_wrist_cur+=4; LF_wrist(LF_wrist_cur); _delay_ms(40); } for(unsigned char i=0;i<15;i++) { RF_shoulder_cur-=2; RF_shoulder(RF_shoulder_cur); LF_shoulder_cur+=2; LF_shoulder(LF_shoulder_cur); _delay_ms(20); } for (unsigned char j=0;j<3;j++) { for(unsigned char i=0;i<30;i++) { RF_shoulder_cur+=2; RF_shoulder(RF_shoulder_cur); LF_shoulder_cur-=2; LF_shoulder(LF_shoulder_cur); _delay_ms(20); } for(unsigned char i=0;i<30;i++) { RF_shoulder_cur-=2; RF_shoulder(RF_shoulder_cur); LF_shoulder_cur+=2; LF_shoulder(LF_shoulder_cur); _delay_ms(20); } } PORTK &= ~((1<<7) | (1<<5)); } int main( void ) { DDRA = 0xFF; DDRK = 0xFF; DDRC = 0xFF; DDRD = 0xFF; DDRG = 0xFF; timer_init(); sei(); shape_def(); Action1(); shape_def(); while(true) { } } |
|
|
|
只有小组成员才能发言,加入小组>>
3309 浏览 9 评论
2988 浏览 16 评论
3490 浏览 1 评论
9050 浏览 16 评论
4085 浏览 18 评论
1171浏览 3评论
602浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
594浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2332浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1894浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-20 19:33 , Processed in 1.911770 second(s), Total 80, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号