L298N Motor Driver using pwm code not working..


Results 1 to 15 of 15

Threaded View

  1. #9
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default I can not make it stay on the line

    Hi, I'm back...I got the HPWM working and also the timer. But I can never make the robot stay on track/line . Probably my setup is not right or something...kindly check my codes, hoping you can help me out spot what I'm doing wrong.
    Code:
    DEFINE OSC 4  
             
    ANSEL = %00000000 ' Make AN0-AN7 Digital
    ANSELH= %00000000 ' Make AN8-AN13 digital
    ;--------- Setup Interrupt-------------------------- 
    INCLUDE "DT_INTS-14.bas"     ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,  _CheckSensors,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    ;--------------------------------------------------
    T1CON = %00100001                ; 00110001  Prescaler = 8, TMR1ON
    @ INT_ENABLE  TMR1_INT     ; enable Timer 1 interrupts
    
    ;--Setting HPWM
    TRISC.1 = 0 'Set CPP2 to PWM output
    TRISC.2 = 0 'Set CPP1 to PWM output
    CCP1CON = %0001100 ' Set CCP1 to PWM, Active High
    CCP2CON = %0001100 ' Set CCP1 to PWM, Active High
    
    TRISA  = %00111111 ' did not anymore use this for now
    ADCON1 = %00001001 
    TRISC = 0
    TRISD = 0
    TRISB = %10000111 ' 0-2 for sensors
    PortB = %00000000 ' Reset PortB
    OPTION_REG = $7f
    ;--Init variables
    i var Byte
    i = 0 ; 200
    ;-- wheel on forward
    PORTD.0 = 1 ' In1 
    PORTD.1 = 0 ' In2
    PORTD.2 = 1 ' In3 
    PORTD.3 = 0 ' In4
    ;-------------------Speed of motor--
    Gear var byte[10]
    
    ;--Set allowable speed---
    Gear[1] = 0 
    Gear[2] = 75 ' start slow roll
    Gear[3] = 80
    
    pause 2000
    i = 2
    
    MainLoop:  
     ;pause 100 
     
    ; Start rolling
    HPWM 1 ,Gear[i],10
    HPWM 2 ,Gear[i],10
    
    GOTO Mainloop               ' Do it forever
    
    END
    
    '---[TMR1 - interrupt handler]--------------------------------------------------
    CheckSensors:
    
    if PortB.0 = 1 and PortB.2 = 0 then ' Left sensor
       HPWM 1 ,Gear[3],10
       HPWM 2 ,Gear[2],10
       pause 20          
       ;Serout2 PORTB.6, 84, [$D,"Right           "]
    ELSE
    if PortB.1 = 1 and PortB.2 = 0 then ' Right sensor
       
       HPWM 1 ,Gear[2],10
       HPWM 2 ,Gear[3],10  
       ;Serout2 PORTB.6, 84, [$D,"            Left"]
       pause 20
    ELSE
    if PortB.2 = 1 and PortB.0 = 0 and PortB.1 = 0  then  ' Center sensor
       i = i + 1
       if i > 2 then
       i = 3
       ENdif
       ;Serout2 PORTB.6, 84, [$D,"    Center      "]
    ELSE
    ;-------------------More than 1 sensors detected
    if PortB.0 = 1 and PortB.2 = 1 then ' Left and Center sensor
       
       HPWM 1 ,Gear[3],10
       HPWM 2 ,Gear[3],10 
       ;Serout2 PORTB.6, 84, [$D,"Right-Center    "]
    Else
    if PortB.1 = 1 and PortB.2 = 1 then ' Right and Center sensor    
       
       HPWM 1 ,Gear[3],10
       HPWM 2 ,Gear[3],10  
       Serout2 PORTB.6, 84, [$D,"Center-Left     "]
    Else
    if PortB.0 = 0 and PortB.2 = 0 and PortB.1 = 0 then ' Can't see no line
       ;HPWM 1 ,Gear[0],10
       ;HPWM 2 ,Gear[1],10 
       Serout2 PORTB.6, 84, [$D,"No more line?   "]
       pause 50
    ELSE
       Serout2 PORTB.6, 84, [$D,"Error???         "]  ' Others
    ENdif:ENDIF:ENDIF:ENDIF:ENDIF:ENDIF 
      
    @ INT_RETURN
    Thank you in advance,
    tacbanon
    Last edited by tacbanon; - 29th September 2012 at 01:52.

Similar Threads

  1. Replies: 2
    Last Post: - 7th August 2012, 16:16
  2. Motor driver problem w/ HPWM
    By Amoque in forum General
    Replies: 2
    Last Post: - 21st May 2011, 20:51
  3. L293 motor driver options?
    By Ron Marcus in forum Off Topic
    Replies: 0
    Last Post: - 5th December 2006, 15:39
  4. Stepping motor Driver
    By mesamune80 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 8th August 2006, 04:36
  5. Driver code for 14 segment LED display
    By Durward Searcy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th December 2004, 20:08

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