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

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


--Boundary-00=_7gPHFiS7TAMXIRA
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


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 
be easy to update to more than one channel.

Functions:

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 
resolution.


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

Mike


-- 
KIWACAN 
Electronics Design
Concept to Production

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


--Boundary-00=_7gPHFiS7TAMXIRA
Content-Type: text/x-csrc;
  charset="iso-8859-1";
  name="rcpulse.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="rcpulse.c"

/**********************************************
*              rcpulse.h
*
* 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 
*
* Free to use in Non Commercial private projects.
* For Commercial licencing contact the author.
***********************************************
*              Version Info
***********************************************
* V1.0 - 29 September 2006
* First Draft
**********************************************/
#include "rcpulse.h"

int RC_Pulse_Done;

/**************************************
*           RCPulse_Init
*
* Setup Port pin, Timers and interrupt
*
**************************************/
void RCPulse_Init(void)
{
 T1CON=0x0000;   /* Internal OSC, Prescale=1:1 */
 RCLat=0;        /* Pin low to start with */
 RCTris=0;       /* Pin to Output */

 IPC0bits.T1IP=4;  /* Priority of 4 */
 IFS0bits.T1IF=0;   /* Clear Flag */
 IEC0bits.T1IE=1;   /* Enable Interrupt */

 RC_Pulse_Done=1;
}

/**************************************
*           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)
{
 unsigned int Load;

 /* Check if already outputing a pulse */
 if(RC_Pulse_Done==0)return(0);

 /* Check Value input Limit */
 if(Pulse > RCMax) Pulse=RCMax;

 /* Calculate Timer compare value */ 
 Load=(RCBase * RCMax) + (RCBase * Pulse);

 /* Clear the Timer */
 TMR1=0;

 /* Load the compare value */
 PR1=Load;

 /* Start the Pulse */
 RCLat=1;
 
 /* Start the Timer */
 T1CONbits.TON=1;

 /* Indicate that we are outputting a pulse */
 RC_Pulse_Done=0;  /* Output is active! */

 return(1);
}

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

/*************************************
*           _T1Interrupt
*
* Interrupt to end the pulse
*************************************/
void _ISRFAST _T1Interrupt(void)
{
 RCLat=0; /* Turn Pin Off */
 T1CONbits.TON=0;
 IFS0bits.T1IF=0;
 RC_Pulse_Done=1;
}




--Boundary-00=_7gPHFiS7TAMXIRA
Content-Type: text/x-chdr;
  charset="iso-8859-1";
  name="rcpulse.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="rcpulse.h"

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

#define RCLat    _LATB0
#define RCTris   _TRISB0


/**************************************
* 8 Bit Values for 30MIP Clocking 
* Value = 0   = 0.9945ms
* Value = 255 = 1.989ms
*
* RCBase and RCMax can be changed to 
* increase or decrease the control 
* resolution.
* 
* Calculation:
* RCMax = Max Resolution value  required
*
* RCBase = 1ms/RCMax * (1/30,000,000)
*
* Note: RCMax * RCBase * 2 must be <= 65535
*************************************/
#define RCBase   117   // Number of clocks in 1/256 of a ms @ 30MIP
#define RCMax    255   // Bigest number allowed

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



#endif


--Boundary-00=_7gPHFiS7TAMXIRA--