MPXA4115A picbasic code


Closed Thread
Results 1 to 40 of 104

Hybrid View

  1. #1
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Default digital oscilosope may will clear things out!

    Sure there is a timing (delay) problem.
    You gave a more sophisticating explanation.
    You gave me an idea.May be we I will hook the oscilloscope to see the exact pulse..

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Hi Alexandros

    I had committed that from what you wrote ...

    Code:
    '****************************************************************************
    ' (c)2009 Alexandros Zahariadis (E.A.TH.) Greece *
    ' Electric Glider electronic switch to be used in F3J-GR competitions.
    ' The device will cut-off motor if 200m ALT reached or 30seconds passed
    ' whichever comes first!
    ' There is also a status led option which idicates if you have reached 200m
    ' or not (may be because of luck of power!)
    ' steady led -> 200m ALT reached , blinking led -> 30 secs passed.
    ' NOTE: after a lot of experiments and because of code execution speed
    ' I use direct ADC raw sample values without converting them to meters first!
    '    :: thanks to Acetronics (Alain) and picbasic forum for codding ideas ::
    '****************************************************************************
    
    '@ DEVICE FCMEN_OFF
    '@ DEVICE IESO_OFF
    '@ DEVICE BOD_ON
    '@ DEVICE CPD_OFF
    '@ DEVICE PROTECT_OFF
    '@ DEVICE MCLR_OFF
    '@ DEVICE PWRT_OFF
    '@ DEVICE WDT_OFF
    '@ DEVICE INTRC_OSC
    
    @    __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _BOD_ON
    
    INCLUDE "modedefs.bas"
    
    clear
    DEFINE OSC 4
    define PULSIN_MAX 250
    ;DEFINE DEBUG_REG GPIO
    ;DEFINE DEBUG_BIT 2
    ;DEFINE DEBUG_BAUD 9600
    ;DEFINE DEBUG_MODE 1
    
    OSCCON = $60           ' $60:4 Mhz $70:8 Mhz int.
    CMCON0 = 7              'TURN COMPARATORS OFF
    TRISIO = %00001010      'Set GPIO1 and GPIO3 0 INPUTS, others to OUTPUT
    ANSEL =  %00000100      'Set AN2 , Fosc/32
    ADCON0 = %00000000      'AN2 select & Right Justify for 10-bit
    ;---------------------------------------------------------------------------   
    THROTTLE    VAR GPIO.0  'Outputs to throttle servo 
    RADIO       VAR GPIO.1  'Input from receiver
    LED         var GPIO.2  'status indicator
    ADC_DATA	var	GPIO.3	' adc serial data
    ADC_SCLK    var	GPIO.5	' adc serial clock
    ADC_CS		var	GPIO.4	' adc chip select
    ;--------------------------------------------------------------------------- 
                           
    advalue 	var	word    ' value read from adc
    groundALT   var word
    difalt      var word
    P           VAR WORD    ' This stores the PULSIN value
    x           var byte    ' general var
    y           var byte    ' general var
    SEC_COUNT   VAR BYTE    ' seconds counter
    O_FLOWS     VAR BYTE    ' holds the number of timer1 overflows
    BRAKE_PULSE var word    ' servo pulse to STOP (brake on) THR servo
    BRAKE_PULSE = 100
    
    '-= vars for averaging routine =-
    AvgCount	CON  6		' = Number of samples to average
    FAspread	CON  1000	' = Fast Average threshold +/-
    Value		VAR  WORD
    '-= end of vars for averaging routine =-
    
    '******************************************************************************
    ' TIMER1 Cycle MUST be longer than a R/C frame duration to be sure not to miss any ticks.
    ' 50 ms is a good value for timing precision.
    ' PL = 15543 @ 4Mhz ( reload = 7 cycle )
    '******************************************************************************
    
    T1LO CON $49            'timer1 preload values
    T1HI CON $C3            '$C349 -> -15543
    low throttle
    
       INTCON  = 0          ' disable interrupts
       T1CON = %00010000    ' 1:2 prescale (1uS per tick), timer1 off (max 1:8!)
                            ' 11 = 1:8 ,10 = 1:4, 01 = 1:2, 00 = 1:1 Prescale Value
       TMR1H = T1HI         ' 65,536-15536 = 50.000
       TMR1L = T1LO         ' 50.000 ticks * 1uS =50.000usec.=100msec
                            ' 1 second= 100*10
                            ' every 10 timer1 overflows = 1 second   1uS=
    '---------------------------------------------------------------------------
    pause 3000
    
    for x=1 to 4
    	high led
    	pause 200
    	low led
    	pause 100
    next x
    
    
    
    '******************************************************************************
    CAL_BP: ' CALIBRATING Brake_Pulse ( P )
    '******************************************************************************
    
    	Pause 50
    	LOW led
     	P = 0
     
    	For x = 1 TO 16
    
    		PulsIn radio,1,P
    		
    		IF ( P < 80 ) OR ( P > 220 ) Then
    			Led = 1
    			GoTo CAL_BP
    		EndIF
    		
    	
    		IF P = $00 Then prems
    	
    		IF ( P < (Brake_Pulse -1)) OR ( P > (Brake_Pulse +1)) THEN
    			Led = 1
    			GoTo CAL_BP
    		ENDIF
    
    prems:	Brake_Pulse = P
    	
    	Next x
    
    LOW Throttle
    
    ''******************************************************************************
    'calibrate:             'check for valid signal
    '
    'For x = 1 TO 2
    '
    '	PulsIn radio,1,P
    '	pauseus 10
    '	IF ( P < 80 ) OR ( P > 200 ) Then calibrate
    '	next x
    '	
    'for x=1 to 3
    '	PulsIn radio,1,P
    '	brake_pulse = P
    'next x
    '
    'low led
    '
    'low throttle
    
    '*****************************************************************************
    precheck:            'leave some seconds to do a motor test
    						'then motor brake is ON for some seconds
    for y=1 to 3
             
    	for x=1 to 255
    		PULSIN RADIO,1,P
    				
    		IF ( P < 80 ) OR ( P > 220 ) Then
    			Led = 1
    			GoTo Precheck
    		EndIF
    		
    		Led = 0
    		LOW Throttle            
    		pULSOUT THROTTLE, p
    		
    	next x
    	
    next y
    
    for x=1 to 160
    
    	LOW Throttle
    	PULSOUT THROTTLE, brake_pulse   
    	pause 18
    	
    next x
    
    low THROTTLE
    
    GOSUB GetAdc         						'Get ground ALT
    groundalt=value
    
    '******************************************************************************
    armcheck:									' Now, we can launch the beast ...
                
    PULSIN RADIO,1,P            
    	IF ( P < 80 ) OR ( P > 220 ) Then
    		Led = 1								' Show Problem !!!		
    		LOW Throttle
    		GoTo Armcheck
    	ENDIF
    	
    Led = 0		
    low throttle
    PULSOUT THROTTLE, p
    
    if difalt < 2 OR difalt > 10000 then Armcheck 'Arm if>=5mALT (in adc samples) reached!!
    
    
    '******************************************************************************
    '******************************************************************************
    start:                  				' We are flying , now ... preset timer1
     
       SEC_COUNT=28         				' load for 30 to 0 seconds countdown
       O_FLOWS=0            				' clear overflow count
       PIR1.0=0             				' clear timer1 overflow flag
       T1CON.0=1            				' start timer1
       
    low throttle							' to be sure ...
    
    '******************************************************************************
    main:					
        
    gosub GetADC
    
    PULSIN RADIO,1,P            
    pauseUS 10
    
    pULSOUT THROTTLE, p
    
    ' Note: Timer1 overflows every 0.050 seconds
    
       IF PIR1.0 THEN           			' if timer1 overflow then
          O_FLOWS = O_FLOWS + 1     		' inc oveflow counter
          
          IF O_FLOWS = 20 THEN    			' if 1 second elapsed 
    	      SEC_COUNT = SEC_COUNT - 1 	' decrement seconds count
    	      O_FLOWS=0             		' reset overflow counter
          ENDIF
          
          PIR1.0=0              			' clear timer1 overflow flag
          T1CON.0 = 0           			' stop timer1
          
          TMR1H = T1HI 						' Reload values 
          TMR1L = T1LO
    
          T1CON.0=1              			' re-start timer1
         
       ENDIF
       
    if difalt>=89 and difalt<10000 then KILL_MOTOR_ALT 'Check if 100mALT
                                                       '(in adc samples) reached!!
    if sec_count<=0 then KILL_MOTOR_TMR
    
    goto main
    
    
    '                                SUBROUTINES
    '******************************************************************************
    '******************************************************************************
    GetADC:									'Read MAX187 12bit ADC value
    
    low ADC_CS
    PAUSEUS 10 
    shiftin ADC_DATA,ADC_SCLK,msbpost,[advalue\12]
    high ADC_CS
    
    
    Average:' -=-=-=-=-=-=  Average 6 X Analog values -=-=-=-=-=-=-=-=-=-=
        IF Value = advalue Then NoChange
        
        IF ABS (Value - advalue) > FAspread OR Value < AvgCount Then FastAvg
        IF ABS (Value - advalue) < AvgCount Then RealClose
        
        advalue = advalue - (advalue/AvgCount)
        advalue = advalue + (Value/AvgCount)
        GoTo AVGok
        
    FastAvg:
        advalue = Value
        GoTo AVGok
        
    RealClose:'If averaging more than 8 samples,change to (AvgCount/4)
    
        advalue = advalue - (advalue/(AvgCount/2)) 
        advalue = advalue + (Value/(AvgCount/2))
        
    AVGok:
        Value = advalue					' Put Average back into Value
        
    NoChange:' -=-=-=-=-=-=  End of Averageing -=-=-=-=-=-=-=-=-=-=
    
    difalt=value-groundalt
    
    return
    END
    
    '******************************************************************************   
    KILL_MOTOR_ALT:
    low throttle
    high led
    
    LOOP1:
    PULSOUT throttle, brake_pulse   ; Kill motor - adjust this value. 4Mhz ->> 10uSec
    pause 18                        
    goto LOOP1
    
    END
    '******************************************************************************
    KILL_MOTOR_TMR:
    x = 0
    
    LOOP2:
    
    low throttle
    PULSOUT throttle, brake_pulse   ; Kill motor - adjust this value. 4Mhz ->> 10uSec
    
    IF X = 0  THEN led = led ^ 1
    x = ( x+1 ) // 18							' Counter for led Blinking @ 1.5 Hz
    
    pause 18                      
    goto LOOP2
    
    END
    '******************************************************************************
    '******************************************************************************
    ...

    But did not try it ...


    have fun

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Hey Bitmaniac,

    You could try using a 20ms interrupt, and start your pulsout on interrupt. That would make things more consistent, and probably keep your motor controller happy.

  4. #4
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Hey Bitmaniac,

    I was thinking this use of interrupts would probably keep things happy as far as motor controllers. It may need some more work though, and I still have to test it on a motor control.

    http://www.picbasic.co.uk/forum/show...3834#post83834

    Walter

  5. #5
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Default maybe interrupt is the way to go

    Well, Scalerobotics , Alain and others

    May be we could give this aproach a try and see...

    So let's sum up what I have tried so far:

    1st just 12f683 -> 10bit ADC cannot do any MPX4115 useful calculations!

    2nd 12f683 + LM358 op-amp for 'window-range' method -> clever idea but I think we may have erratic behavour as op-amp temp drift!

    3rd 12f683 DT 14 bit oversampling method very good idea and increased resolution but delay issues with servo pass-through routines.

    4th 12f683 + MAX187 12bit ADC (increased resolution-not as bad as internal 10bit and not as good as oversampling method) and interrupts for servo 'pass-trhough' LET's try it

    Now I ended up in using a 12f683 internal osc (4MHz) + MAX187 so we have free interrupt sources and speed to try other methods.Also note that I have tried also with internal and external crystals and keep in-mind that I have already TMR1 running for the moment to keep track of 30sec. passing time.

    Maybe I think somehow wrong / or weird for any of the above ideas ,please feel free to express your method / comments

    Alain (Acetronics) some time ago , already gave a great idea for a completely different approach to the whole project, using an external DAC (NOT ADC!) to set the Vref of the pic internal 10bit adc so we 'narrow' the measurements of MPX to a resonable amount
    This has also to be tried!!!


    (P.S:For the moment I have a PIC ethernet project open.... in the same time)
    Last edited by bitmaniac; - 29th January 2010 at 12:58.

  6. #6
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by bitmaniac View Post
    Also note that I have tried also with internal and external crystals and keep in-mind that I have already TMR1 running for the moment to keep track of 30sec. passing time.

    Alain (Acetronics) some time ago , already gave a great idea for a completely different approach to the whole project, using an external DAC (NOT ADC!) to set the Vref of the pic internal 10bit adc so we 'narrow' the measurements of MPX to a resonable amount
    This has also to be tried!!!
    Hey Alexandros,

    Certainly lots of methods to try. I am sure that Alain's method would work just fine. I just like the idea of interrupts, and using fewer components. And besides, I am learning a lot about interrupts and timers in the process, so that is good!

    And for your 30 seconds: Since it is using a time base of 20ms on timer2, you could easily add a counter in that interrupt handler. And when it reached 1500, that is 30 seconds.

    I will try adding the ADC oversample into it, and let you know how it goes. But my motor controller is taking kindly to it, and accepting it's pulses.

  7. #7
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Default

    Well a little bit of experiment with my last hardware : 12f683 , max187 12bit ADC , 4 Mhz Int osc. Servo attached instead of SC working ok (a little bit jerky -but acceptable) BUT when I connect SC it works only once at the begining when you plug the battery in.
    So, after a little notice THE OBVIOUS THING CAME IN MY MIND!!

    Of course there is a problem meanwhile with the SC , as soon as I 'break' for the first time just to to jump to my motor pre-check routine afterwards SC accepts no pulses at all for a period of time. So when I come back to re-activate the motor last position SC does not arm . It must go to arm procedure first (throttle low) and then it accepts signals!

    So, the only way we can go is WITH interrupts just to keep the pulsout routine alive!

    So obvious ......

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Unhappy

    Hi, Alex

    Interrupts ... ok, nice idea, ... but it will surely interfere with your INPUT Pulsin ....

    hé,hé ... you said " Aaaargh !!! " ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. sample code for AT45DB642D in Picbasic Pro
    By itsssyam in forum General
    Replies: 0
    Last Post: - 10th March 2010, 07:01
  2. 16f887 44 pin demo board code problem?
    By jessey in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 7th December 2008, 15:17
  3. How to configure SPI in PICBASIC PRO?
    By moogle in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 15th April 2007, 19:31
  4. PicBasic code problems with a 16F84A
    By Lauren Barta in forum mel PIC BASIC
    Replies: 3
    Last Post: - 30th May 2006, 23:50
  5. PicBasic Fundamentals
    By Billyc in forum General
    Replies: 9
    Last Post: - 4th May 2004, 11:04

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts