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

Carl Ranson chchrobotics@lists.linuxnut.co.nz
Sat, 30 Sep 2006 18:02:10 +1200


I've tried the same basic technique from within my RTOS framework and 
can't get it going right.

Using it to drive a servo it jumps around randomly (freaks out). At 
first i thought it was a power supply noise problem, so i rigged up a 
seperate supply for the servo. No difference.

Its entirely possible that ive got the timing all wrong. Im running a 
10MHz crystal with PLLx4 enabled (10MHz effective instruction clock).

With a timer prescale of 1:1 i would expect that setting the period 
register to 15000 to give me a 1.5ms pulse.

There are enough things i could have done wrong that I'd like to measure 
whats actually going on.

Is there anyone on the west side that wouldn't mind giving me 30 mins of 
scope time?
Cheers
CR

Michael Pearce wrote:

>Sorry - Corrected Program code and Hex attached.
>
>I had setup the oscillator and soft delay incorrectly so was 
>running at 32KHz x 16 rather than 7.5Mhz x 16  (Divide 
>result by 4 to get instruction time)
>
>Mike
>
>  
>
>------------------------------------------------------------------------
>
>/**********************************************
>*              rcpulse.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 
>*
>* 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);
> 
> TMR1=0;          /* Clear the Timer */
> PR1=Load;        /* Load the compare value */
> RCLat=1;         /* Start the Pulse */
> T1CONbits.TON=1; /* Start the Timer */
>
> RC_Pulse_Done=0;  /* Indicate Output is active! */
>
> return(1);
>}
>
>/*************************************
>*           RCDone
>*
>* Indicates Pulse State
>*
>* Returns 1 for Done
>* Returns 0 for pulse 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;   /* Turn Timer Off */
> IFS0bits.T1IF=0;   /* Clear Interrupt flag */
> RC_Pulse_Done=1;   /* Indicate pulse is complete */
>}
>
>
>
>  
>
>------------------------------------------------------------------------
>
>#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
>
>  
>
>------------------------------------------------------------------------
>
>:020000040000fa
>:080000000001040000000000f3
>:020000040000fa
>:100200002f80200080bf2000000188000000000037
>:10021000050007000c0007001c020200000000009f
>:100220000040da000000fe004440a9000000200069
>:100230000000e0000300320000002000a001880060
>:100240004440a80000000600a02920000100200072
>:100250001600370062004000e080480091018800ed
>:100260009001ba0062004000e080480091018800df
>:100270009002ba0062004000e08048000002eb00fb
>:100280006028e10005003a008301e900070033001f
>:10029000038009000059eb00040037006128e100e9
>:1002a000010032000082eb00050007009101880088
>:1002b0001001ba000200e000e6ff3a00000005006d
>:1002c000910188008002780062004000e0804800d0
>:1002d0003559ba008301e900080032002559ba00f7
>:1002e0008301e900050032000400e000f4ff320061
>:1002f00015d9ba008301e900f1ff3a0000000600b9
>:100300000000fa000000eb0020088800ca02a900e3
>:10031000c602a9005109200091407800f0c8b3003e
>:1003200000c0600001c4b3000140700095e0b70058
>:100330008460a9008c60a8001000200000408800a4
>:100340000080fa00000006000600fa00000f7800a6
>:100350000008e20003003a000000eb0020079800cc
>:1003600015003700f00f20009e0f1000020036002d
>:10037000f00f2000000f78009e007800500720004a
>:100380000008b800b248270002004000100798009b
>:100390000000eb00000888001e0090001008880094
>:1003a000ca02a80005e1a8000000eb000040880098
>:1003b00012002000220798002e0090000080fa0012
>:1003c000000006000000fa00004080000080fa00f3
>:1003d0000000060000a0fe000000fa00ca02a9000a
>:1003e00005e1a9008460a9001000200000408800f9
>:1003f0000080fa000080fe00004006000400fa00c1
>:10040000000f78001e00e000090034008038210051
>:10041000100798001e0090000000e90010079800e7
>:100420000000e000fbff3a001e0fe900f5ff370077
>:100430000080fa00000006000200fa000080eb00d5
>:1004400040158800aeff07000000eb00000f7800a9
>:10045000f00f20009e0f100009003e001e007800e3
>:10046000b9ff0700d7ff07000000e000fdff3200e2
>:1004700040012000e1ff07001e0fe800f4ff3700f5
>:1004800040062000ddff0700f00f2000000f78007d
>:100490001e00e0000a0032001e007800aaff0700dc
>:1004a000c8ff07000000e000fdff3200400120000f
>:1004b000d2ff07001e007800650f5000f4ff3700e0
>:1004c00040062000cdff07000000eb00000f780081
>:1004d000f00720009e0f100013003e0001082000ce
>:1004e0001e80400098ff0700b6ff07000000e000f4
>:1004f000fdff320020032000c0ff0700000820009d
>:100500001e00500090ff0700aeff07000000e00053
>:10051000fdff320020032000b8ff07001e00780016
>:10052000680f4000eaff370040062000b3ff0700d5
>:04053000c5ff3700cc
>:020000040000fa
>:0200000401f009
>:04000000e3ff00001a
>:020000040000fa
>:0200000401f009
>:04000400ff7f00007a
>:020000040000fa
>:0200000401f009
>:040008007fff000076
>:020000040000fa
>:0200000401f009
>:04001400ffff0000ea
>:020000040000fa
>:10000800a2020000a2020000a2020000a202000058
>:10001800a2020000a2020000a2020000a202000048
>:10002800a2020000a2020000a2020000ea010000f1
>:10003800a2020000a2020000a2020000a202000028
>:10004800a2020000a2020000a2020000a202000018
>:10005800a2020000a2020000a2020000a202000008
>:10006800a2020000a2020000a2020000a2020000f8
>:10007800a2020000a2020000a2020000a2020000e8
>:10008800a2020000a2020000a2020000a2020000d8
>:10009800a2020000a2020000a2020000a2020000c8
>:1000a800a2020000a2020000a2020000a2020000b8
>:1000b800a2020000a2020000a2020000a2020000a8
>:1000c800a2020000a2020000a2020000a202000098
>:1000d800a2020000a2020000a2020000a202000088
>:1000e800a2020000a2020000a2020000a202000078
>:0800f800a2020000a2020000b8
>:020000040000fa
>:10010800a2020000a2020000a2020000a202000057
>:10011800a2020000a2020000a2020000a202000047
>:10012800a2020000a2020000a2020000ea010000f0
>:10013800a2020000a2020000a2020000a202000027
>:10014800a2020000a2020000a2020000a202000017
>:10015800a2020000a2020000a2020000a202000007
>:10016800a2020000a2020000a2020000a2020000f7
>:10017800a2020000a2020000a2020000a2020000e7
>:10018800a2020000a2020000a2020000a2020000d7
>:10019800a2020000a2020000a2020000a2020000c7
>:1001a800a2020000a2020000a2020000a2020000b7
>:1001b800a2020000a2020000a2020000a2020000a7
>:1001c800a2020000a2020000a2020000a202000097
>:1001d800a2020000a2020000a2020000a202000087
>:1001e800a2020000a2020000a2020000a202000077
>:0801f800a2020000a2020000b7
>:020000040000fa
>:1005340000080000020000000000000000000000ad
>:020000040000fa
>:040544000000fe00b5
>:00000001FF
>  
>
>------------------------------------------------------------------------
>
>/***************************
>* rctest.c
>*
>* Test code for RCPulse code
>****************************/
>#include <p30fxxxx.h>
>#include "rcpulse.h"
>
>_FOSC(CSW_FSCM_OFF & FRC_PLL16);    /* Internal Osc PLLx16 = 30MIP */
>_FWDT(WDT_OFF);                     /* Watchdog Off */
>_FBORPOR(PBOR_OFF & MCLR_EN);       /* Brownout Disabled - MCLR Enabled */
>_FGS(CODE_PROT_OFF);                /* Code Protect OFF */
>
>void DelayMs(int dly)
>{
> unsigned int x;
>
> while(dly >0)
> {
>  x=5000;  
>  while(--x)continue;  
>  dly--;
> }
>}
>
>int main(void)
>{
> unsigned int pos;
>  
> ADPCFG=0xFFFF; /* All to Digital pins! */
> 
> RCPulse_Init();
> 
> while(1)
> {
>  for(pos=0;pos<256;pos++)
>  {
>   RCPulse(pos);
>   while(RCDone()==0);
>   DelayMs(20);   /* 20mS delay min between pulses */
>  }
>
>  DelayMs(100);
>  
>
>  for(pos=255;pos>0;pos-=5)
>  {
>   RCPulse(pos);
>   while(RCDone()==0);
>   DelayMs(20);   /* 20mS delay min between pulses */
>  }
>
>  DelayMs(100);
>
>  /* Bounce between extreams and eventually end up in the middle*/
>  for(pos=0;pos<128;pos+=8)
>  {
>   RCPulse(pos+128);
>   while(RCDone()==0);
>   DelayMs(50);  
>
>   RCPulse(128-pos);
>   while(RCDone()==0);
>   DelayMs(50);  
>  }
>
>  DelayMs(100);
> }
>  
> return(0);	
>}
>  
>