PDA

View Full Version : Control Feedback & RS-232



nate
- 25th June 2005, 22:28
Background: Need a PIC to complete a feedback loop using a tachometer. PIC will vary a PWM signal based on Tach input.

I need a RS-232 link to a PC but I haven't quite figured out how I want to structure my program due to the limitations of ucontrollers. I've chosen the 16F876 PIC and the PBP compiler.

The PC will send a START/STOP and RPM command via RS232. The motor will then start up and tightly regulate the RPM on the motor as the load changes.

My question is how do I effectively maintain a tight loop on my feedback loop but maintain RS-232 comm with the PC? Do i need to use an interupt? I would like to stream RPM data back to the PC for analysis.

Thanks!

- Nate

Dave
- 26th June 2005, 12:34
Nate, I would make the serial communications interrupt driven, that way you can spend the other bulk of the time doing the speed control loop.

Dave Purola,
N8NTA

nate
- 30th June 2005, 00:56
Made my very first cut at the code and haven't even tried to compile as my compiler hasn't arrived yet.

You guys mind reviewing it to see if I'm doing anything stupid?

getdata:HSERIN [B0] ' Wait start/stop/RPM data on serial port
IF B0 = rpm10 THEN spd2kp = 12
IF B0 = rpm20 THEN spd2kp = 24
IF B0 = rpm30 THEN spd2kp = 36
IF B0 = rpm40 THEN spd2kp = 48
ELSE getdata
ENDIF

loop: COUNT portX.X,100,CRNTSPD ' Get speed from wheel encoder
IF (crntspd < spd2kp) THEN pw = 1000 ' If not up to speed apply full power
IF (crntspd >= spd2kp) THEN pw = 750 ' If to fast slow down
ELSE pw = 500 ' something is wrong shut down
PULSOUT PORTX.X,pw ' put pulse out to the speed controller
PAUSE 15 ' setup the pwm frequency
HSEROUT crntspd ' send speed data to computer
HSERIN 5,loop,[B0] ' Wait 5ms for stop command
IF B0.8 = 0 THEN getdata ' If the first bit is 0 then stop and wait for start again
ELSE loop ' No stop command keep going
ENDIF
GOTO loop
END

As you can see I tried to steer clear of the interupts as I have control of the pc. I'm hoping this will work. I am interfacing to a standard RC speed controller which should explain the pulseout / pause commands.

Any comments or suggestions greatly appreciated as I'm on a very very short time schedule for development.

- Nate

p.s. I couldn't figure out how to properly format my code for easy readability:(