hello every one
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
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
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.