完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
大家好。我试图用PIC16F628A@20MHz的TMR2控制伺服电机。我把定时器设置为每隔1ms溢出(预分频器=4;TMR2寄存器=130),然后在中断例程中,伺服管脚被保持在高或低,因为代码显示:我的伺服正在移动大约2度。有些事情是非常错误的,一旦它从0点移动到180点,我会错过什么?
以上来自于百度翻译 以下为原文 Hello guys. I was trying to control a servo motor using TMR2 of a PIC16F628A @20MHz. I set the timer to overflow every 1ms (prescaler = 4; TMR2 register = 130), and then in the interrupt routine the servo pin is held high or low as the code shows: // CONFIG #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR) #pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled) #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) #define _XTAL_FREQ 20000000 #include unsigned char count, pos; void interrupt ISR() { if(TMR2IF) { if(pos >= count) PORTB = 0x04; else PORTB = 0; if (count == 200) count = 0; else count++; } TMR2 = 130; TMR2IF = 0; } void main(void) { TRISB = 0; PORTB = 0; T2CON = 0x20; CCP1CON = 0; INTCON = 0xC0; PIE1 = 0x02; PIR1 = 0; count = 0; TMR2 = 130; TMR2ON = 1; while(1) { for(pos = 10; pos < 20; pos++) __delay_ms(500); for(pos = 20; pos > 10; pos--) __delay_ms(500); } } My servo is moving just around 2 degrees. Something is very wrong, once it should move from 0 to 180º and back. What am I missing? |
|
相关推荐
2个回答
|
|
|
是的,我已经读了这个代码,什么也不懂,这就是为什么我想要另一个借口。它不能只有一种方法来控制伺服系统。我的代码的错误在哪里?我只是希望10个职位开始了解如何做到这一点。
以上来自于百度翻译 以下为原文 Yeah, I already read this code and understood nothing, that's why I want another aproach. It can't have only one method to control servos. And where is the error of my code? I just want 10 positions initially to just understand how to do it. |
|
|
|
|
|
嗯,对于10个位置,你需要能够在1ms和2ms之间产生10个不同的脉冲宽度。所以你需要每111.11us100us中断,在Fosc=20MHz时每555.55个指令周期中断一次。这是不可能的,所以您必须使用支持预分频比的最近整数。假设您仍然决定使用定时器2,那么您将预分频器设置为1:4,PR2设置为138。这会让你每110毫秒中断一次,这应该足够接近了。ISR*必须**不要*重新加载定时器2。然后只需要每次通过ISR,从0到180进行计数,生成一个伺服帧。在0设置针高,并在9和18之间的可变数量(POS)设置低。当递增计数超过180时,将计数重置为0。您可能更喜欢使用一个100us的中断率(PR2=124),计数到199,用pos结束脉冲,总共11个位置,范围从10到20。您需要得到右边的一些细节——pos需要是一个声明为volatile的全局8位值(提示:include
以上来自于百度翻译 以下为原文 Well, for 10 positions, you need to be able to generate 10 different pulse widths between 1ms and 2ms. so you need an interrupt every 111.11us 100us which at Fosc=20MHz is every 555.55 instruction cycles. That's impossible so you have to use the nearest whole number with a supported prescaler ratio. Assuming you are still determined to use Timer 2, you'd set the prescaler to 1:4 and PR2 to 138. That gets you an interrupt every 110.4ms which should be close enough. The ISR *MUST* *NOT* reload Timer 2. Then its just a matter of counting up from 0 to 180 with each pass of the ISR to generate 1 servo frame. At 0 set the pin high, and at a variable number (pos) between 9 and 18 set it back low. When incrementing the count takes it past 180, reset the count to 0. You may prefer to use a 100us interrupt rate (PR2=124), count to 199 and end the pulse with pos in the range 10 to 20 for a total of 11 positions. There are a few details you need to get right - pos needs to be a global 8 bit value declared as volatile, (Hint: #include volatile uint8_t pos; so the compiler knows its accessed from more than one thread, and count should be a static local so it retains its value from interrupt to interrupt, but the main program cannot mess with it. void interrupt ISR(){ static uint8_t count=0; |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
473 浏览 0 评论
5793 浏览 9 评论
2334 浏览 8 评论
2224 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3530 浏览 3 评论
1124浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1095浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
873浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
475浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 05:35 , Processed in 0.535162 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
1871