PID-filter routine (2nd try).


Results 1 to 40 of 132

Threaded View

  1. #28
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: PID-filter routine (2nd try).

    If anyone is interested, here is what I used to run the simulation (my controller has an LCD)

    Code:
    #CONFIG
     __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _LVP_OFF & _CP_OFF 
    #ENDCONFIG
    
    DEFINE OSC 8  'LETS PBP KNOW THE OSCILLATOR IS RUNNING AT 8MHz
    DEFINE NO_CLEARWDT 1 'NO WATCHDOG TIMER - FOR NOW - WILL DO OUR OWN CLRWDT
    
    
    OSCCON = %01110001 '8 MHz INTERNAL OSCILLATOR, INTERNAL OSCILLATOR IS USED FOR SYSTEM CLOCK
    
    
    DISABLE 'NO PBP INTERRUPTS NO PBP DEBUG
    
    ' Set LCD Data port
    DEFINE LCD_DREG PORTB
    ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_DBIT 0
    ' Set LCD Register Select port
    DEFINE LCD_RSREG PORTB
    ' Set LCD Register Select bit
    DEFINE LCD_RSBIT 4
    ' Set LCD Enable port
    DEFINE LCD_EREG PORTB
    ' Set LCD Enable bit
    DEFINE LCD_EBIT 5
    ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_BITS 4
    ' Set number of lines on LCD
    DEFINE LCD_LINES 2
    ' Set command delay time in us
    DEFINE LCD_COMMANDUS 1500
    ' Set data delay time in us
    DEFINE LCD_DATAUS 44
    
    
    
      '// Set up hardware registers, ADC etc here (not shown) //
    
      ADValue VAR WORD                     '<---This is your variable.
      SetPoint VAR WORD                    '<---This is your variable.
      COUNTER VAR BYTE
      RUN_IND VAR BYTE
    
    
    
    
      INCLUDE "incPID.pbp"                 'Include the PID routine.
    
        'These variables are declared by the incPID routine but
        'the user needs to assign values to them.
        pid_Kp = $0700                     'Set Kp to 7.0 GAIN
        pid_Ki = $0080                     'Set Ki to 0.5 RESET
        pid_Kd = $0001                     'Set Kd to 0 (Derivative not necessary)
        pid_Ti = 8                         'Update I-term every 8th call to PID
        pid_I_Clamp = 100                  'Clamp I-term to max ±100
        pid_Out_Clamp = 255                'Clamp the final output to 0 to 255
    	Setpoint = 230		               '<---Set desired position.
    	RUN_IND = 1
    
    START:
       	FOR ADVALUE = 225 TO 235 'TEST A BUNCH OF VALUES
    		FOR COUNTER = 1 TO 10
       			pid_Error = Setpoint - ADValue     'Calculate the error
    			Gosub PID                          'Result returned in pid_Drive
       			if pid_Out.15 then pid_out = 0     'IF NEGATIVE THEN SHUT OFF FAN
       			pid_Out = ABS pid_Out              'Convert from two's comp. to absolute
    '  			HPWM 1, pid_Out, 10000             'Set PWM output
    
    			LCDOUT $FE, 1, "AD=", DEC ADVALUE, " PO=", SDEC PID_OUT
    			LCDOUT $FE, $C0, "RUN #", DEC COUNTER, " SETP ", DEC SETPOINT
    			Pause 1000                           'Wait....
    		NEXT COUNTER
    	NEXT ADVALUE
    	IF RUN_IND = 1 THEN
    		RUN_IND = 0 
    		GOTO START 'DO IT AGAIN SO THAT ITS AS IF THE TEMPERATURE DROPPED 
    	ENDIF
    	LCDOUT $FE, 1, "DONE!"
    	DO
    	LOOP 'ENDLESS LOOP
    	
    END
    Last edited by andywpg; - 17th April 2013 at 14:51.
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

Similar Threads

  1. Darrel's latest 16 bit averaging routine?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 17th October 2009, 01:57
  2. 2nd order Low-pass passive RC filter on PWM
    By munromh in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th January 2009, 19:03
  3. Atod Digital Filter
    By GeoJoe in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd April 2008, 17:04
  4. PID controller in 16F737
    By joeri in forum mel PIC BASIC
    Replies: 8
    Last Post: - 24th June 2006, 11:39
  5. 2nd Order Digital Filter for 24-bit
    By sefayil in forum mel PIC BASIC
    Replies: 0
    Last Post: - 2nd December 2005, 21:55

Members who have read this thread : 2

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

Tags for this Thread

Posting Permissions

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