Go to Post I'm sure you can come up with something, with a bit of warped thought! ... and if not, warped thinking is what your team's Engineers are for! :) - kmcclary [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 15-10-2005, 22:32
sciguy125 sciguy125 is offline
Electrical Engineer
AKA: Phil Baltar
FRC #1351
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Sunnyvale, CA
Posts: 519
sciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond repute
Send a message via AIM to sciguy125 Send a message via MSN to sciguy125 Send a message via Yahoo to sciguy125
reading hobby PWM

Ok, I've been working on this for a week and I haven't managed to fix any problems, just find ways around them. It's time to get some help. I've been working on a speed controller based on a PIC 12F683. The reason I chose it is that it has hardware PWM. I set the frequency and duty cycle (period and high time, rather) and I have a nice signal to send to my h-bridge. I managed to get this PWM output working correctly by reading a pot. I decided to move on and try to read a servo pulse. (To avoid confusion between the motor control PWM and hobby PWM, I'm going to start calling the latter a servo pulse or the signal, the former will be called the PWM output)

In my early attempts, I was polling for the signal. This worked, but my decoding algorithm still need some fine tuning. After rooting around the datasheet, I discovered that I actually did have free interrupts (apparently all the pins can be set as interrupts) and also found a 16-bit timer (Timer1). I decided that it would be more elegant to use these. This is my current scheme:

-when an interrupt is triggered (state change on input), check the signal's state
-if it's high, clear the timer and start it
-if it's low, stop the timer and decode it

In the interrupt handler, I set an output so that I know it made it inside. Essentially, this output follows the servo pulse. (If the signal is high, the output goes high) This is how I know it's working properly. Now, however, I'm having some trouble with the timer.

For some reason, the time it measures is too short. I'm sure there's some bugs in my decoding algorithm, so I bypassed it by setting the duty cycle of the PWM output to the lower byte of what the timer measured. I measured the PWM output with a scope. Assuming I did all the math correctly, the timer byte is about 50. I did the same with the upper byte and it's 0. It stays the same no matter what the input signal is. If I don't clear the timer bytes, however, it keeps counting and overflows. This tells me that it's counting something. I've gone over all the code that initializes the timer and checked the datasheet many many times already, but I can't spot the problem.


As a separate problem, for some reason, I can't set any pins to be inputs. I set the appropriate bits in TRISIO, but it doesn't work. The only input that works is GP3, but that's because it works only as an input and can't be changed. I can't remember if it reads the "inputs" as high or low, but whichever it is, it never changes no matter what I apply to the pin.

GP4 does something strange, however. No matter what I set TRISIO<4> to be, GP4 outputs some strange signal. It's almost square, but it oscillates between transitions (when it is supposed to be a flat high or low). ...Actually, I take that back, the header I'm using has some bad numbers. It's setting GP4 to output the clock. But that doesn't explain why the others don't work.

Of course, I've attached code. You can ignore my signal decoding routine (procsig). I'm sure there are still bugs in it and there aren't many comments (I did a lot of it on paper, so I copied the code, but not the comments). The interrupts are handled in gpio_change (called from my interrupt service routine at the top). This is where the timer is stopped and started.
Attached Files
File Type: txt sc_servo.asm.txt (4.7 KB, 136 views)
__________________

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GE/S/P a-- e y-- r-- s:++ d+ h! X+++
t++ C+ P+ L++ E W++ w M-- V? PS+ PE+
5- R-- tv+ b+ DI+++ D- G
------END GEEK CODE BLOCK------
  #2   Spotlight this post!  
Unread 16-10-2005, 13:57
sciguy125 sciguy125 is offline
Electrical Engineer
AKA: Phil Baltar
FRC #1351
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Sunnyvale, CA
Posts: 519
sciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond repute
Send a message via AIM to sciguy125 Send a message via MSN to sciguy125 Send a message via Yahoo to sciguy125
Re: reading hobby PWM

Well, I still haven't figured out the timer problem, but I figured out why the inputs don't work. When some of the pins are set to inputs, there's still a choice between analog and digital input. By default, they're analog. When they are analog, you can't read them as digital inputs.

I should have learned my lesson from last time something like this happened to me... I was working with a comparator a few years ago and couldn't figure out why the output never went high. After looking at the schematic, I noticed that it was open collector. This was never mentioned in the text, however. After getting extraorinarily frustrated with this PIC, I decided to do the same. I looked at the schematic of the pins. After backtracking and trying to figure out what needs to be done in order to read the pin, I noticed "Analog Input Mode" on one of the gates. I checked the A/D converter info and discovered that analog is the default mode...

Learn from my mistakes: when in doubt, check the schematic.
__________________

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GE/S/P a-- e y-- r-- s:++ d+ h! X+++
t++ C+ P+ L++ E W++ w M-- V? PS+ PE+
5- R-- tv+ b+ DI+++ D- G
------END GEEK CODE BLOCK------
  #3   Spotlight this post!  
Unread 17-10-2005, 20:47
sciguy125 sciguy125 is offline
Electrical Engineer
AKA: Phil Baltar
FRC #1351
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Sunnyvale, CA
Posts: 519
sciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond repute
Send a message via AIM to sciguy125 Send a message via MSN to sciguy125 Send a message via Yahoo to sciguy125
Re: reading hobby PWM

Lesson #2: Read very carefully.

Quote:
Originally Posted by PIC12F683 datasheet
The user, in the Interrupt Service Routine, clears the interrupt by:
a) Any read or write of GPIO. This will end the
mismatch condition, then
b) Clear the flag bit GPIF.
A mismatch condition will continue to set flag bit GPIF.
Reading GPIO will end the mismatch condition and
allow flag bit GPIF to be cleared.
The key word in that is then. "Any read or write of GPIO. This will end the mismatch condition, then Clear the flag bit GPIF." That does not mean that it will be cleared automatically when you read from GPIO. That means that GPIF needs to be cleared manually.

Now, when my batteries are recharged, I can test my decoding algorithm...
__________________

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GE/S/P a-- e y-- r-- s:++ d+ h! X+++
t++ C+ P+ L++ E W++ w M-- V? PS+ PE+
5- R-- tv+ b+ DI+++ D- G
------END GEEK CODE BLOCK------
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Failed Teh Mike Programming 7 09-02-2005 13:27
FYI about using PWM 13-16 with interrupts cabbagekid2 Programming 6 22-01-2005 00:54
Using an Operator Interface with the 2004 EDU RC wirelessly Dave Flowerday Robotics Education and Curriculum 34 19-04-2004 19:06
pwm 13-15 wayne 05 Programming 2 04-10-2003 12:08
PWM and burning out motors patrickrd Technical Discussion 7 19-06-2003 15:30


All times are GMT -5. The time now is 22:38.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi