using ttl on innovation first

hello every one :slight_smile:
im using first 18f8520 with edu code for mplab.
im having problams with my code.
i want to control my first (connects with rs323 to pc) with hyper tirminal to be precise i want to control my servo on pwm out pins.
how i write a code for a thing like that?
need yours help please :slight_smile:

I’ve never used the EDU controllers before, but I have a fair amount of experience with PICs in general and the IFI FRC controller. Someone else may be able to give you a better answer, but here’s what I can tell you:

Under the default code (I downloaded the one available on ifirobotics.com), you can use printf(…) to send stuff out on the serial port. If you want to read in from the serial port, try making the following changes:

in ifi_utilities.c: Initialize_Serial_Comms
change USART_RX_INT_OFF to USART_RX_INT_ON

in user_routines_fast.c:InterruptHanderLow
add the following code to the bottom of the function:
if (RXINTF)
{
char data = RCREG;
/* data now holds one received character. it should be placed in a buffer somewhere */
}

Like I said, I haven’t tried this code, but it should work. Post back here if it doesn’t, or if you need help writing a receive buffer or anything. You also said “[your] code isn’t working,” so if you already have some code in the works that’s having issues, you might try posting it so we can give you suggestions.

Good luck,
–Ryan

thanks rayn,
i post the message it helped me.
i have a one more quastion.
when i define to control pwm pins what code i need to write to do this things:
i mean for example i want when i press on hyper terminal the letter A it will send to pwm out 1
0 like:
A= 0;
if a press B it will send to pwm out 1 127.
B= 127;
if a press c it will send to pwm out 1 255.
C= 255;
if you can help me its great im new in all that :slight_smile:

For that, just start interpreting the information that you get in the data variable. As the datatype suggests, the code allows you to receive one character at a time, so what you’re wanting is fairly simple, just select a different behavior based on what letter you receive, like this:

switch(data)
{
case ‘A’:
pwm01 = 0;
break;
case ‘B’:
pwm01 = 127;
break;
case ‘C’:
pwm01 = 255;
break;
}

A couple of WARNINGS to anyone who wants to start using this code for reading from serial ports:

  • Make sure to only read the character from the data variable. Once the RCREG register is read, it is automatically cleared, that’s why we store the character in an intermediary variable
  • Once you change the init flag to USART_RX_INT_ON, make sure to add the interrupt code as well so that the processor can service the interrupt
  • When processing an interrupt handler (in our case, anything that’s inside InterruptHandlerLow), the processor completely stops doing anything else.
    Firstly, this is BAD for IFI controllers (either the EDU or the FRC controller) because they depend on periodic communication between the controller’s two processors. This means that if your code in InterruptHandlerLow takes too long, the controller will auto-reset. If you’re getting unexpected “code errors” after you add code to interact with the serial port, make sure this isn’t happening to you.
    Secondly, if the interrupt is not serviced quickly enough, the processor may miss the next character sent, and you’ll start wondering why you’re only getting every second or third character you’re sending to the robot.

The usual way to avoid the last problem is to store characters into a global circular buffer in the interrupt handler, then add code outside the interrupt handler (like in Process_Data_From_Local_IO) that checks if there are any new characters in the buffer and processes them accordingly.

The advantages of the buffer are:

  • You can process multi-character commands
  • You have have more complex processing of the received data
  • It keeps the serial port receiving smoothly

However, adding a buffer adds a fair bit of complexity to your code, and circular buffers can be easy to mis-code, especially since there’s only a vary limited amount of debugging that can be done with the IFI controllers.

For examples of how to write such a buffer, try Googling for “circular buffer” or “ring buffer.” If you don’t feel like investing in the experience of writing your own buffer, allow me (as I’m often apt to do) to point you to the venerable Kevin Watson’s implementation of serial port code for the IFI controllers. His code was written for the newer IFI FRC controllers (based off the PIC18F8722 instead of the PIC18F8520 like the EDU controllers), and as such may require some massaging in order to get the hardware portion of the code working, but it still has a good example of a circular buffer, regardless.

For the example given of just setting motor values, it should be fine to just put the code given into InterruptHandlerLow because it’s not too computationally intensive (just make sure that you control the rate of characters coming to the robot over the serial link. If the rate gets to be several thousand per second or such, you may start encountering problems).

As always, if you have any other questions or problems, feel free to post back.

Happy coding,
–Ryan

No need to massage any code. Kevin has done remarking and impressive work the PIC controllers from IFI.

All of his EDU Controller Sample code can be found here:
(Bottom half of the page)
http://kevin.org/frc/2005/

Specifically Serial Driver for the EDU 8520.
http://kevin.org/frc/2005/edu_serial_ports_0.3.zip

Thanks for filling in the blank. I haven’t been around FIRST for long enough to have used the EDU controllers, so I was taking some shots in the dark.

–Ryan

thank you very much guy!
you helped me alot especially you rayn.
:slight_smile: