--_003_4EA7253A236992B011B9brentbrownclearnetnz_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable On 25 Oct 2011 at 22:45, Yigit Turgut wrote: > Hi all, >=20 > As some of you may have noticed recently, I was trying to implement a > custom algorithm in Spartan 3E development board - which can be > assumed to be completed now. FPGA sends an analog signal (X) to PIC > 18F, PIC rotates a motor back or forward depending on the content. > This trigger is both variable in amplitude and frequency (0-5V / > 0-50HZ). I am not getting into details of this not to mix things up. > This is a very small part of the project and yet can't move on to > further parts because this is not completed yet. >=20 > There is a critical level in the range of X that PIC decides to > rotate the motor or stay still. This critical level is saved to eeprom > and from then on PIC reads analog signal and compares to the value > stored in eeprom. There is a tiny metal stick embedded to motor, > rotating right - left depending on the choice. This metal stick spans > approximately 45 degrees where there are active low push buttons at > both ends (right - left) of the stick so that PIC will know when to > stop rotation. >=20 > Sounds like a piece of cake right ? >=20 > Read AN0 constantly; > if button is pressed save the current AN0 value (critical_level) to eepro= m > if AN0 >> critical_level rotate motor to right until pushbutton_right is = low > if AN0 << critical_level rotate motor to left until pushbutton_left is lo= w >=20 > That's it. Reading AN0 has no problem but that button and motor > rotation... I can't find words that will express my anger. It's such > a simple thing but yet not doable, at least for me. PCB is in perfect > condition, paths are checked a dozen times and components are brand > new. To save some time I test with > http://www.semifluid.com/PIC18F2550_usb_hid_oscilloscope/Full-Circuit.gif > as the skeleton, without rs232 and usb connections. First I tried to > rotate the motor back and forth with the push of the button. A very > simple test consists of ; >=20 > sbit MOTR at RB0_bit; > sbit MOTL at RB1_bit; > sbit BTN3 at RB4_bit; >=20 > void main() { > TRISB =3D 00010000; > PORTB =3D 00010000; > do { >=20 > if(BTN3 !=3D 1){ > MOTL =3D 0; > MOTR =3D 1;} > else > MOTL =3D 1; > } > while(1); > } A few things to ponder: - RB4 is also an analog input on your PIC, you probably need to disbale the= analog=20 function to use it as a digital input. - You set RB4 as an input (TRISB=3D00010000), then write 1 to RB4 output (P= ORTB=20 =3D 00010000). Shouldn't affect anything input but it doesn't make any sens= e. - What compiler are you using and what units does it default to, eg. dec/he= x/bin?=20 This would make it more definitive: TRISB =3D 0b00010000; PORTB =3D 0b00010000; - In the code snippet above it looks like you have forgotten MOTR =3D 0 in = the else=20 section. Motor will go one way then stop. Based on your previous emails tha= t have=20 said motor is connected directly between the two pins. > With the press of BTN3 motor is expected to rotate to right and with > the release rotate to left. System doesn't provide a reliable > response with this code, it rotates to left even if BTN3 is not > pressed. On the other hand ; >=20 > sbit MOTR at RB0_bit; > sbit MOTL at RB1_bit; > sbit BTN3 at RB4_bit; >=20 > void main() { > TRISB =3D 00010000; > PORTB =3D 00010000; > do { > MOTR =3D 1; > delay_ms(500); > MOTR =3D 0; > MOTL =3D 1; > delay_ms(500); > MOTL =3D 0; > } > while(1); > } >=20 > this function is independent of the BTN3 but when I run the board > with this basic firmware, initially there is no motor movement (stays > still). But when I press BTN3, it starts to rotate to right and when I > press it for the second time it stops or rotates to left > (unpredictable responses). This is a very unexpected behavior IMHO and > I tried the same with 4 other PIC's to make sure the ones I use are > not corrupted. Same motor is perfectly driven with the same algorithm > on Atmega328p. No comments on your second code example. I will only add that instead of wi= th a=20 motor you might like to try it with back to back LEDs (and a resistor). Thi= s would=20 rule out any potential issues with driving the motor as previously discusse= d. =20 > Either I am doing something fundamentally wrong and I can't spot it or > this is a bug or something; >=20 > What can I possibly be doing wrong ? It's a simple I/O operation and > is driving me crazy. I have been trying to get over this for more than > 40 hours and couldn't succeed. Seeking for serious guidance here, I am > sure most of you implemented much more complex stuff thousands of > times. It's not uncommon to get stuck on something seemingly trivial. Hang in ther= e, be=20 methodical, keep your assumptions in check, break it down piece by piece...= then=20 put it all back together! =20 > Thank you and sorry for the long post. --=20 Brent Brown, Electronic Design Solutions 16 English Street, St Andrews, Hamilton 3200, New Zealand Ph: +64 7 849 0069 Fax: +64 7 849 0071 Cell: +64 27 433 4069 eMail: brent.brown@clear.net.nz --_003_4EA7253A236992B011B9brentbrownclearnetnz_ Content-Type: application/octet-stream; name="WPM$0L7A.PM$" Content-Description: Mail message body.PM$ Content-Disposition: attachment; filename="WPM$0L7A.PM$"; size=5405; creation-date="Tue, 25 Oct 2011 14:14:55 GMT"; modification-date="Tue, 25 Oct 2011 14:14:55 GMT" Content-Transfer-Encoding: base64 T24gMjUgT2N0IDIwMTEgYXQgMjI6NDUsIFlpZ2l0IFR1cmd1dCB3cm90ZToNCg0KPiBIaSBhbGws DQo+IA0KPiBBcyBzb21lIG9mIHlvdSBtYXkgaGF2ZSBub3RpY2VkIHJlY2VudGx5LCBJIHdhcyB0 cnlpbmcgdG8gaW1wbGVtZW50IGENCj4gY3VzdG9tIGFsZ29yaXRobSBpbiBTcGFydGFuIDNFIGRl dmVsb3BtZW50IGJvYXJkIC0gd2hpY2ggY2FuIGJlDQo+IGFzc3VtZWQgdG8gYmUgY29tcGxldGVk IG5vdy4gRlBHQSAgc2VuZHMgYW4gYW5hbG9nIHNpZ25hbCAoWCkgdG8gUElDDQo+IDE4RiwgUElD IHJvdGF0ZXMgYSBtb3RvciBiYWNrIG9yIGZvcndhcmQgZGVwZW5kaW5nIG9uIHRoZSBjb250ZW50 Lg0KPiBUaGlzIHRyaWdnZXIgaXMgYm90aCB2YXJpYWJsZSBpbiBhbXBsaXR1ZGUgYW5kIGZyZXF1 ZW5jeSAoMC01ViAvDQo+IDAtNTBIWikuIEkgYW0gbm90IGdldHRpbmcgaW50byBkZXRhaWxzIG9m IHRoaXMgbm90IHRvIG1peCB0aGluZ3MgdXAuDQo+IFRoaXMgaXMgYSB2ZXJ5IHNtYWxsIHBhcnQg b2YgdGhlIHByb2plY3QgYW5kIHlldCBjYW4ndCBtb3ZlIG9uIHRvDQo+IGZ1cnRoZXIgcGFydHMg YmVjYXVzZSB0aGlzIGlzIG5vdCBjb21wbGV0ZWQgeWV0Lg0KPiANCj4gVGhlcmUgaXMgYSBjcml0 aWNhbCBsZXZlbCBpbiB0aGUgcmFuZ2Ugb2YgIFggdGhhdCBQSUMgZGVjaWRlc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yB3 cm9uZyA/IEl0J3MgYSBzaW1wbGUgSS9PIG9wZXJhdGlvbiBhbmQNCj4gaXMgZHJpdmluZyBtZSBj cmF6eS4gSSBoYXZlIGJlZW4gdHJ5aW5nIHRvIGdldCBvdmVyIHRoaXMgZm9yIG1vcmUgdGhhbg0K PiA0MCBob3VycyBhbmQgY291bGRuJ3Qgc3VjY2VlZC4gU2Vla2luZyBmb3Igc2VyaW91cyBndWlk YW5jZSBoZXJlLCBJIGFtDQo+IHN1cmUgbW9zdCBvZiB5b3UgaW1wbGVtZW50ZWQgbXVjaCBtb3Jl IGNvbXBsZXggc3R1ZmYgdGhvdXNhbmRzIG9mDQo+IHRpbWVzLg0KDQpJdCdzIG5vdCB1bmNvbW1v biB0byBnZXQgc3R1Y2sgb24gc29tZXRoaW5nIHNlZW1pbmdseSB0cml2aWFsLiBIYW5nIGluIHRo ZXJlLCBiZSANCm1ldGhvZGljYWwsIGtlZXAgeW91ciBhc3N1bXB0aW9ucyBpbiBjaGVjaywgYnJl YWsgaXQgZG93biBwaWVjZSBieSBwaWVjZS4uLiB0aGVuIA0KcHV0IGl0IGFsbCBiYWNrIHRvZ2V0 aGVyIQ0KIA0KPiBUaGFuayB5b3UgYW5kIHNvcnJ5IGZvciB0aGUgbG9uZyBwb3N0Lg0KPiAtLSAN Cj4gaHR0cDovL3d3dy5waWNsaXN0LmNvbSBQSUMvU1ggRkFRICYgbGlzdCBhcmNoaXZlDQo+IFZp ZXcvY2hhbmdlIHlvdXIgbWVtYmVyc2hpcCBvcHRpb25zIGF0DQo+IGh0dHA6Ly9tYWlsbWFuLm1p dC5lZHUvbWFpbG1hbi9saXN0aW5mby9waWNsaXN0DQo+IA0KDQoNCg== --_003_4EA7253A236992B011B9brentbrownclearnetnz_ Content-Type: text/plain; name="ATT00001.txt" Content-Description: ATT00001.txt Content-Disposition: attachment; filename="ATT00001.txt"; size=208; creation-date="Tue, 25 Oct 2011 14:14:55 GMT"; modification-date="Tue, 25 Oct 2011 14:14:55 GMT" Content-Transfer-Encoding: base64 LS0gDQpodHRwOi8vd3d3LnBpY2xpc3QuY29tIFBJQy9TWCBGQVEgJiBsaXN0IGFyY2hpdmUNClZp ZXcvY2hhbmdlIHlvdXIgbWVtYmVyc2hpcCBvcHRpb25zIGF0DQpodHRwOi8vbWFpbG1hbi5taXQu ZWR1L21haWxtYW4vbGlzdGluZm8vcGljbGlzdA0K --_003_4EA7253A236992B011B9brentbrownclearnetnz_-- .