[chbot] The next steps for my bot and control board.

Michael Pearce chchrobotics@lists.linuxnut.co.nz
Fri, 29 Sep 2006 23:19:22 +1200


Hi Carl,=20

Code attached that should work for the Servo Pulse.

It compiles but I have not tested it yet.

It is setup for one output pin only, but if it works it will=20
be easy to update to more than one channel.

=46unctions:

RCPulse_Init(); //Sets everything up.

RCPulse(value); // Outputs a pulse.
 Returns 0 if a pulse is active or 1 if started a new pulse.

RCDone(); // Indicates Pulse state
 returns 0 if pulse is active or 1 if done.

You can change defines in rcpulse.h for port settings and=20
resolution.


P.S. Let me know if it works as I may not have time to test=20
it for a while.

Mike

=46iles have been pasted as attachments dont always get=20
through to the list.


#ifndef _RCPULSE_H
#define _RCPULSE_H
/**********************************************
* =A0 =A0 =A0 =A0 =A0 =A0 =A0rcpulse.h
*
* Author: Michael Pearce <mike@kiwacan.co.nz>
*
* Started: 29 September 2006
*
* Copyright 2006 Michael Pearce All Rights reserved=20
*
* Free to use in Non Commercial private projects.
* For Commercial licencing contact the author.
***********************************************
* =A0 =A0 =A0 =A0 =A0 =A0 =A0Version Info
***********************************************
* V1.0 - 29 September 2006
* First Draft
**********************************************/
#include <p30fxxxx.h>

#define RCLat =A0 =A0_LATB0
#define RCTris =A0 _TRISB0


/*********************************************
* 8 Bit Values for 30MIP Clocking=20
* Value =3D 0 =A0 =3D 0.9945ms
* Value =3D 255 =3D 1.989ms
*
* RCBase and RCMax can be changed to=20
* increase or decrease the control=20
* resolution.
*=20
* Timer Prescale int RCPulse_Init may need
* to be changed for other clock frequencies.
*
* Calculation:
* RCMax =3D Max Resolution value =A0required
*
* RCBase =3D 1ms/RCMax / (1/30,000,000)
*        =3D  0.001/255 / 33.33333e-9
*        =3D 117.64
*
* Note: RCMax * RCBase * 2 must be <=3D 65535
**********************************************/
#define RCBase 117 // Number of clocks in 1/256ms @ 30MIP
#define RCMax =A0255 // Biggest number allowed (Resolution)

extern void RCPulse_Init(void);
extern int RCPulse(unsigned int Pulse);
extern int RCDone(void);

#endif



/**********************************************
* =A0 =A0 =A0 =A0 =A0 =A0 =A0rcpulse.c
*
* Simple interrupt driven RC Servo pulse generator
*
* Author: Michael Pearce <mike@kiwacan.co.nz>
*
* Started: 29 September 2006
*
* Copyright 2006 Michael Pearce All Rights reserved=20
*
* Free to use in Non Commercial private projects.
* For Commercial licencing contact the author.
***********************************************
* =A0 =A0 =A0 =A0 =A0 =A0 =A0Version Info
***********************************************
* V1.0 - 29 September 2006
* First Draft
**********************************************/
#include "rcpulse.h"

int RC_Pulse_Done;

/**************************************
* =A0 =A0 =A0 =A0 =A0 RCPulse_Init
*
* Setup Port pin, Timers and interrupt
*
**************************************/
void RCPulse_Init(void)
{
=A0T1CON=3D0x0000; =A0 /* Internal OSC, Prescale=3D1:1 */
=A0RCLat=3D0; =A0 =A0 =A0 =A0/* Pin low to start with */
=A0RCTris=3D0; =A0 =A0 =A0 /* Pin to Output */

=A0IPC0bits.T1IP=3D4; =A0/* Priority of 4 */
=A0IFS0bits.T1IF=3D0; =A0 /* Clear Flag */
=A0IEC0bits.T1IE=3D1; =A0 /* Enable Interrupt */

=A0RC_Pulse_Done=3D1;
}

/**************************************
* =A0 =A0 =A0 =A0 =A0 RCPulse
*
* Start a RC Pulse
*
* Returns 0 if a pulse is currently active
* Returns 1 if sucessfuly started a pulse
**************************************/
int RCPulse(unsigned int Pulse)
{
=A0unsigned int Load;

=A0/* Check if already outputing a pulse */
=A0if(RC_Pulse_Done=3D=3D0)return(0);

=A0/* Check Value input Limit */
=A0if(Pulse > RCMax) Pulse=3DRCMax;

=A0/* Calculate Timer compare value */=20
=A0Load=3D(RCBase * RCMax) + (RCBase * Pulse);

=A0TMR1=3D0;          /* Clear the Timer */
=A0PR1=3DLoad;        /* Load the compare value */
=A0RCLat=3D1;         /* Start the Pulse */
 T1CONbits.TON=3D1; /* Start the Timer */

=A0/* Indicate that we are outputting a pulse */
=A0RC_Pulse_Done=3D0; =A0/* Output is active! */

=A0return(1);
}

/*************************************
* =A0 =A0 =A0 =A0 =A0 RCDone
*
* Indicates Pulse State
*
* Returns 1 for Done, 0 for active
*************************************/
int RCDone(void)
{
=A0return(RC_Pulse_Done);
}

/*************************************
* =A0 =A0 =A0 =A0 =A0 _T1Interrupt
*
* Interrupt to end the pulse
*************************************/
void _ISRFAST _T1Interrupt(void)
{
=A0RCLat=3D0;         /* End Pulse           */
=A0T1CONbits.TON=3D0; /* Turn Timer Off      */
=A0IFS0bits.T1IF=3D0; /* Clear Int Flag      */
=A0RC_Pulse_Done=3D1; /* Indicate Pulse Done */
}






=2D-=20
KIWACAN=20
Electronics Design
Concept to Production

Mobile:  +64-21-249-5674
Phone:   +64-3-347-3311
=46ax:     +64-3-347-2123
Website: http://www.kiwacan.co.nz