完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
我想用两个定时器驱动一个伺服电机。time1,16位,管理20毫秒周期,并将输出PORT.B0设置为1.Time0,8位,管理1毫秒的持续时间,并设置输出端口B0至0。我的问题是:伺服不动。我断开了伺服电机。我连接了一个范围。当我用范围内的光标来测量时间的时候,我检查了这个周期T持续时间大约是40毫秒,而不是20毫秒2x。当我用范围内的光标测量时间的时候,thTaTon持续时间大约是2毫秒,而不是1毫秒2x。在这些条件中,我理解了为什么伺服。马达不动。我检查了所有的计算,以便设置良好的值TMR0、TMR1L和TMR1H寄存器。但是在代码下://PIC16F87// 8 MHz / /Time1 / /预分频器1:1//TMR1预加载=25536 / /中断时间:20 MsHar CNT=0;空隙IITTIMER 1){T1CONN=0X01;//T1CON寄存器>TMR1ON位=1>Time1。TMR1IFN1比特=0;//PIR1寄存器>TMR1没有溢出。1H=0x63;TMR1LY=0xC0;TMR1IELY比特=1;//PIE寄存器>TMR1溢出中断。ITCON=0xC0;//ITCCON寄存器>GIE位=1和PEE比特=1。}//Time0//预分频器1:1/TMR0预加载=56//中断时间:100个US(0,1 ms)空隙IITTIMER 0(){OpthyReg=0x88;/PSA比特=1和G。Prescaler被分配给WDT & GT;PosiClar=1:1//RBPU位=1;GTCO0=0xA0;}空隙中断(){{(TMR1IF1比特=1){TMR1IF=56;TMR1H=0x63;TMR1L=0xC0;PORT.B0=1;TMR0IFN1比特=0;}(TMR0IF1比特=1){CNT++;TMR0IFN1比特=0;TMR。t;0=56;} }安塞尔=0;ANSELH=0;C1ONYBIT=0;C2ONYBIT=0;TrISD=0;//端口D COMPURE EN SORTI.PORTD=0;//初始化端口DD 0 0 .IITIN Time1();{IF(CNT= 10){PORT.B0= 0;CNT= 0;},同时(1);}
以上来自于百度翻译 以下为原文 I want to drive a servomotor with two timers.Timer1, 16 bits, manage the 20 mS period, and set output portd.b0 to 1.Timer0, 8 bits, manage the 1 mS Ton duration, and set output portd.b0 to 0.My problem :Servo doesn't move. I disconnected the servomotor. I connected a scope.When measuring time with cursors on the scope I checked that period T duration is about 40 mS, instead of 20 mS. 2x more.When measuring time with cursors on the scope I checked that Ton duration is about 2 mS, instead of 1 mS. 2x more. In these conditions I understand why the servomotor doesn't move. I checked all calculations in order to set with good values TMR0, TMR1L and TMR1H registers. But in vain. Here under the code :// PIC 16F887 // 8 MHz// Timer1 // Prescaler 1:1 // TMR1 preload = 25536 // Interrupt time : 20 mSchar cnt = 0;void Init_Timer1() { T1CON = 0x01; // T1CON register > TMR1ON bit = 1 > Timer1 is enabled. TMR1IF_bit = 0; // PIR1 register > TMR1 has not overflowed. TMR1H = 0x63; TMR1L = 0xC0; TMR1IE_bit = 1; // PIE register > TMR1 overflow interrupt is enabled. INTCON = 0xC0; // INTCON register > GIE bit = 1 and PEIE bit = 1. }// Timer0 // Prescaler 1:1 // TMR0 Preload = 56 // Interrupt time : 100 uS (0,1 mS)void Init_Timer0() { OPTION_REG = 0x88; // PSA bit = 1 > Prescaler is assigned to WDT > Prescaler = 1:1 // RBPU bit = 1 > PORTB pull-up resistors are disabled. TMR0 = 56; INTCON = 0xA0; }void Interrupt() { if (TMR1IF_bit = 1) { TMR1IF_bit = 0; TMR1H = 0x63; TMR1L = 0xC0; portd.b0 = 1; TMR0IF_bit = 0; } if (TMR0IF_bit = 1) { cnt ++; TMR0IF_bit = 0; TMR0 = 56; } }void main() { ANSEL = 0; ANSELH = 0; C1ON_bit = 0; C2ON_bit = 0; TRISD = 0; // PORT D configuré en sortie. PORTD = 0; // Initialisation du PORT D à 0. Init_Timer1(); Init_Timer0(); do { if (cnt = 10) { portd.b0 = 0; cnt = 0; } } while(1); } |
|
相关推荐
19个回答
|
|
|
使用2定时器有何特殊原因?这是最糟糕的做法。PIC作为3个PWM外围设备(2xCCP+1XECCP),一个可以产生完整的控制信号。使用2个定时器的第一个问题是使它们保持同步。只是我的2分钱…最好。
以上来自于百度翻译 以下为原文 Hi Any special reason to use 2 timers? IMHO its the worst way of doing it. That PIC as 3 PWM peripherals (2xCCP + 1xECCP), a single one can generate the full control signal. Your first problem in using 2 timers is keeping them in synch. Just my 2 cents... Best regards Jorge |
|
|
|
|
|
因为您没有在OSCCon寄存器中设置IrCF位,所以您的内部时钟将以默认速度运行,这发生在PIC中4 MHz,而不是您可能用来确定定时器设置的8 MHz。这样你就得到了两倍的时间。我同意豪尔赫的观点,这是很难做到的。你不会把两个计时器保持同步。如果你要使用两个定时器,那么你必须至少启动Time0当你开始Time1,所以他们保持在一个步骤。
以上来自于百度翻译 以下为原文 Because you do not set the IRCF bits in the OSCCON register your internal clock will operate at the default speed which happens to 4 Mhz in this pic, NOT the 8 Mhz you probably used to determine your timer settings. Thus you get twice the period. And I agree with Jorge, this is the hard way to do this. You will not keep the two timers synchronized the way you are going about it. IF you are going to use two timers then you must at least start timer0 when you start timer1 so they stay in step. |
|
|
|
|
|
谢谢杰克的建议。但是:我使用的是一个8 MHz的外部石英钟(我不需要设置任何一个OSCCon寄存器……)。在In TimeTime1()和InIdTimeServer()中,在主(埃里克)开头,同时运行两个定时器。
以上来自于百度翻译 以下为原文 Thank you Jack for your advise. But : I m using an external quartz clock of 8 MHz (I should not need to set any bit of OSCCON register ...). And with lines Init_Timer1(); and Init_Timer0(); at the beginning of main() I run both timers at the same time. Eric |
|
|
|
|
|
因为你没有显示你的配置设置,没有办法知道你使用的是内部的OSC还是外部的水晶。从我所看到的,你的In定时器在DO循环之外,所以它只做一次,之后定时器自由运行,而没有它们之间的任何同步。也许我没有正确地理解你想要做的……伺服控制通常不是这样做的。
以上来自于百度翻译 以下为原文 Because you did not show your configuration settings there is no way to know if you are using the internal osc or an external crystal. From what I see your init_timers is outside the do loop so it is only done once, after that the timers free-run without any synchronization between them. Maybe I am not correctly understanding what you are trying to do.....servo control is not usually done quite this way. |
|
|
|
|
|
Merci Jack。今晚我将尝试,移动这两条线。我使用MikRelekrutika的Mikro PROG程序员。在编写ROM中的十六进制文件之前,我在消息框中设置了xt(xt模式,外部振荡器高达8 MHz)的振荡器字段。一个非常简单的问题是:为了检查我的范围内的8 MHz时钟,我只需将我的范围的信道1或信道2与Quart并行地连接。Z?(没问题,因为没有地面参考?)因为有两个陶瓷33 PF电容器连接到石英和2个引脚的PIC。在尝试PWM之前,您建议我,我真的想运行这个代码后添加代码,以改变吨从1毫秒到1,5毫秒,和2毫秒,以便转动伺服。埃里克。
以上来自于百度翻译 以下为原文 Merci Jack. I will try tonight, with moving these two lines. I use MikroProg programmer of MikroElektronika. Before writing the hex file in the ROM I set the Ocillator field with XT (XT mode, external oscillator up to 8 MHz) in the message box. One very simple question : In order to check the 8 MHz clock with my scope, I just have to connect the channel 1 or channel 2 of my scope in parallel with the quartz ? (No problem because no ground reference ?). Because there are the two ceramic 33 pF capacitor connected to quartz and 2 pins of PIC. Before trying with pwm as you recommended me, I really would like to run this code with after add code in order to change Ton from 1 mS to 1,5 mS, and 2 mS, in order to turn servo. Eric |
|
|
|
|
|
这是一种危险的做法。你最终会忘记。最好是将设置嵌入到你的代码中,所以它总是在那里。确保你的探针被设置为“10:1”。它将在1:1模式中加载太多的信号。将其触摸到PIC的PIN 14(“OSC2/CKOUT”)。它将停止振荡器,如果你触摸它到引脚13(“OSC1/CKIN”)。你的探测地应该连接到PIC的地面。
以上来自于百度翻译 以下为原文 That is a dangerous way to do it. You will eventually forget. It's much better to embed the settings into your code, so it's always there. Make sure your probe is set to "10:1". It will load the signal too much in the 1:1 mode. Touch it to pin 14 of the PIC ("OSC2/CLKOUT'). It will stop the oscillator if you touch it to pin 13 ("OSC1/CLKIN"). Your probe ground should be connected to the PIC's ground. |
|
|
|
|
|
|
|
|
|
|
|
豪尔赫,谢谢你的建议。我也会尝试PWM。但我有点沮丧,因为我没有在网上找到简单的例子。我不想在了解Mikroelektronika之前使用PWM子程序并编写我自己的子程序。肠系膜的
以上来自于百度翻译 以下为原文 Merci Jorge for your advise. I will also try with pwm. But I m a little bit frustrated because I haven't found simple examples on the web. And I don't want to use pwm subroutine of Mikroelektronika before understand them and write my own subroutine. Cordialement Eric |
|
|
|
|
|
谢谢杰克。我写SCSYBIT=0;为了设置指示外部振荡器被用作时钟源。但是,iNa代码行我可以向MCU指示XT模式,它是用于外部振荡器模式的。我不知道哪个寄存器。如果没有,我必须学习数据表。埃里克。
以上来自于百度翻译 以下为原文 Thanks Jack. I wrote SCS_bit = 0; in order to set indicate that external oscillator is used as a clock source. But how inna code line I can indicate to the MCU the XT mode, which is for external oscillator mode. I don't have any idea of which register. If not I have to study the datasheet. Eric |
|
|
|
|
|
XT模式(和其他OSC模式)由PIC、FoSC位的配置字来选择。
以上来自于百度翻译 以下为原文 The XT mode (and other osc modes) are selected by the configuration words of the pic, FOSC bits usually. |
|
|
|
|
|
谢谢你的建议。因此,我插入了代码SCSYBIT=0;为了选择外部源振荡器。我把我的范围和石英连接起来。我期待着看到一个完美的正方形信号,比如555,但我和其他东西混淆了。我检查了窦性信号,但正负部分的表面不相等。但是我用光标检查了124,5ns=8,032,122赫兹=8兆赫。对MCU来说,这足够了吗?我期待检查一个完美的周期为125纳秒,与两个init计时器程序内的DO循环…什么都没有发生。当我把他们放回赛道之前,我有一个一吨和Toff的时期。但是T=114,而不是20毫秒!!!!最好的问候
以上来自于百度翻译 以下为原文 Merci qhb for your advice. So I inserted the code SCS_bit = 0; in order select external source for oscillator. I connected my scope in parallel with quartz. I was expecting to watch a perfect square signal like with a 555, but I made confusion with something else. I checked a sinus signal but with not equal surface of the positive part and the negative part. But I checked with cursor a period of 124,5 nS = 8 032 122 Hz = ~ 8 MHz. Is that enough précisé for the MCU ? I was expecting to check a perfect period of 125 nS. With the two Init timer procedure inside the do loop ... nothing happend. When I putted them again back before the loop I have a period with a Ton and Toff. But T = 114 uS instead of 20 mS !!!! Best Regards Eric |
|
|
|
|
|
|
|
|
|
|
|
当你把示波器上的探头放在晶体上时,你会改变振荡器的频率,甚至在极端情况下也会停止。它不是测量PIC时钟频率的好方法,在振荡电路中的石英晶体上的信号很少是“完美的正方形信号”,它不是数字信号。
以上来自于百度翻译 以下为原文 When you place a probe from the oscilloscope across the crystal you will change the frequency of the oscillator, maybe even stop it in extreme cases. It is not a good way to measure the pic clock frequency. The signal across a quartz crystal in an oscillator circuit is seldom a "perfect square signal", it is not a digital signal. |
|
|
|
|
|
在这些论坛上有关于同一主题的线索。你可以搜索“Futaba伺服”来找到它们。这是一个带有示例代码的线程:HTTP://www. McCHIP.COM/FuMss/FordPase/957 606问候,Mysil。
以上来自于百度翻译 以下为原文 There have been threads about the same subject in these forum. You may search for 'Futaba servo' to find them. This is one thread with example code: http://www.microchip.com/forums/FindPost/957606 Regards, Mysil |
|
|
|
|
|
亲爱的,最后,在重新检查每一行的每一条指令的语法之后,我意识到在使用if时,我错过了写一个双“=”。现在,范围内的PWM信号完全符合代码,但是…现在,我想知道如何在MIN()中编写代码,以便将吨时间从1毫秒切换到1,5毫秒,每1秒切换到2毫秒,以便转动伺服电机。我试了几行,但没用。有人有主意吗?谢谢!埃里克
以上来自于百度翻译 以下为原文 Dear all, Finally after re checking syntax of each instruction in each line, I realized that I missed to write a double "=" when using if ... ! Now, pwm signal on the scope is totally according with the code. But ... now, I have any idea how to write the code in main() in order to switch Ton duration from 1 mS, to 1,5 mS, to 2 mS every 1 second in order to turn servomotor. I tried some lines but in vain. Some one has an idea ? Merci beaucoup ! Eric |
|
|
|
|
|
事实上,阻止这种情况的一个常见的方法是反转Tooi。如果你省略了第二个“=”,就会出现一个错误。
以上来自于百度翻译 以下为原文 Indeed. A common technique to stop yourself doing this is to reverse the two i.e. change if (TMR1IF_bit == 1) to if (1 == TMR1IF_bit) as that will give an error if you omit the second "=". |
|
|
|
|
|
你用什么来确定吨值?你有一个电位器连接到一个引脚,你会阅读与A/D?或者类似的东西?在你的主()循环中,无论哪种方式都得到值,按需要缩放它,然后加载适当的寄存器来设置时间。
以上来自于百度翻译 以下为原文 What are you using to determine the Ton value? DO you have a potentiometer attached to a pin that you will read with the A/D? Or something similar? Either way in your main() loop get the value, scale it as needed and then load the appropriate registers to set the times. |
|
|
|
|
|
关于伺服电机的驱动,Ton只能取以下值:1毫秒(90度),1,5毫秒(0度)和2毫秒(+ 90度右)?或者我可以从1毫秒逐渐增加到2毫秒?谢谢,埃里克
以上来自于百度翻译 以下为原文 Concerning the drive of a servomotor, Ton can only take the following values : 1 mS (- 90 degrees left), 1,5 mS (0 degree) and 2 mS ( + 90 degrees right) ? Or I can increase gradually from 1 mS to 2 mS ? Thanks. Eric |
|
|
|
|
|
嗨,你可以有任何中间位置,如果你能产生适当的脉冲长度,并且可以通过逐步增加脉冲长度在小步进中使平滑运动。此外,伺服致动器可以使用小于1毫秒的信号,或大于2毫秒,以达到扩展的运动范围,B。UT注意伺服不能对硬端停止工作。遥控发射器来控制这样的装置,通常有调整中点位置的脉冲长度的修整器,并且还可以有用于端部脉冲长度的修整器。迈西尔
以上来自于百度翻译 以下为原文 Hi, You may have any intermediate position, if you can generate the appropriate pulse length, and may make smooth movement by gradually increasing the pulse length in small steps. Also, servo actuators, may work with signals shorter than 1 ms, or longer than 2 ms to reach extended range of movement, but take care that the servo do not work against hard end stops. Remote control transmitters to control such devices, usually have trimmers that will adjust the pulse length for the midpoint position, and may also have trimmers for end of range pulse length. Mysil |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
473 浏览 0 评论
5793 浏览 9 评论
2334 浏览 8 评论
2224 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3530 浏览 3 评论
1122浏览 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 00:40 , Processed in 2.499055 second(s), Total 112, Slave 94 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
419