help: coding for line follower robot using microcontroller PIC18F4550

This is a discussion on help: coding for line follower robot using microcontroller PIC18F4550 within the C Programming forums, part of the General Programming Boards category; Code: #include <htc.h> __CONFIG(1,0x0F24); __CONFIG(2,0X0000); #define _XTAL_FREQ 8000000 //using internal osc #define UART_BAUD 9600 #define LEFT RA0 #define M_LEFT1 RA1 ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    help: coding for line follower robot using microcontroller PIC18F4550

    Code:
    #include <htc.h>
    
    __CONFIG(1,0x0F24);
    __CONFIG(2,0X0000);
    
    #define    _XTAL_FREQ        8000000        //using internal osc
    
    #define UART_BAUD        9600
    
    
    
    
    #define LEFT            RA0
    #define M_LEFT1            RA1
    #define M_LEFT2            RA2
    #define MIDDLE_L        RA3
    #define MIDDLE_R        RA4
    #define M_RIGHT2        RA5
    #define M_RIGHT1        RE0
    #define RIGHT            RE1
    
    
    void fast_line_follow(void);    
    void adc_init(void);
    unsigned int ui_adc_read(void);
    void pwm_init(void);
    void set_pwmr(unsigned char uc_duty_cycle);
    void set_pwml(unsigned char uc_duty_cycle);
    void delay_ms(unsigned int ui_value);
    void beep(unsigned char uc_count);
    void motor(unsigned char uc_left_motor_speed,unsigned char uc_right_motor_speed);
    
    int main(void)
    {
        
        // Initialize the I/O port direction.
        TRISA = 0b11111111;
        TRISB = 0b00000011;
        TRISC = 0b10011000;
        TRISD = 0;
        TRISE = 0b11111111;    
        
        // Initialize ADC.
        adc_init();
        
        // Initialize PWM.
        pwm_init();
        
        delay_ms(1500);            //wait for 1.5 second
        fast_line_follow();    
    }
    
    void delay_ms(unsigned int ui_value)
    {
        while (ui_value-- > 0) {
            __delay_ms(1);
        }    
    }    
    
    void fast_line_follow(void)
    
    {
        
        while(1)
        {
              PORTA = 0B00000000;
             PORTB = 0B01000100;
             PORTC = 0B00000110;
             PORTE = 0B11111100;     
        
        if((LEFT == 0)&&(M_LEFT1 == 1)&&(M_LEFT2 == 1)&&(MIDDLE_L == 1)&&(MIDDLE_R == 1)&&(M_RIGHT2 == 1)&&(M_RIGHT1 == 0)) //check middle, middle left and middle right sensor
                                                            //assuming black line, dark ON
            {        
                motor(85,85);    // robot move straight with both left and right motor moving at same speed
                //motor(uc_left_motor_speed, uc_right_motor_speed)            
            }
            
            else if((LEFT == 1)&&(M_LEFT1 == 1)&&(M_LEFT2 == 1)&&(MIDDLE_L == 1)&&(MIDDLE_R == 1)&&(M_RIGHT2 == 1)&&(M_RIGHT1 == 0)) // robot has move to left a little bit
            {            
                motor(80,85);    // robot turning to left
                //motor(120,200);
            }        
            
            else if((LEFT == 0)&&(M_LEFT1 == 1)&&(M_LEFT2 == 1)&&(MIDDLE_L == 1)&&(MIDDLE_R == 1)&&(M_RIGHT2 == 1)&&(M_RIGHT1 == 1)) // robot has move to right a little bit
            {            
                motor(85,80);    // robot turning to right
                //motor(200,120);
            }
            
            else if((LEFT == 1)&&(M_LEFT1 == 1)&&(M_LEFT2 == 1)&&(MIDDLE_L == 1)&&(MIDDLE_R == 1)&&(M_RIGHT2 == 0))    // robot has move to left
            {            
                motor(20,80);    // robot turning to Left, hard
                //motor(55,140);
            }
                    
            else if((M_LEFT1 == 1)&&(M_LEFT2 == 1)&&(MIDDLE_L == 1)&&(MIDDLE_R == 1)&&(M_RIGHT2 == 1)&&(M_RIGHT1 == 1)&&(RIGHT == 1))// robot has move to right
            {        
                motor(80,20);    // robot turning to right, hard
                //motor(140,55);
            }    
            else if((LEFT == 1)&&(M_LEFT1 == 1)&&(M_LEFT2 == 0))    // robot has move to the most left side
            {            
                motor(0,80);    // robot turning to Left, hard
                //motor(0,150);
            }
                    
            else if((M_RIGHT2 == 0)&&(M_RIGHT1 == 1)&&(RIGHT == 1))// robot has move to the most right site
            {        
                motor(80,0);    // robot turning to right, hard
                //motor(150,0);
            }    
        }//while(1)
        
    }    
    
    void adc_init(void)
    {
        // A/D Conversion Clock = FOSC/32.
        ADCS1 = 1;
        ADCS0 = 0;
        
        // Set AN0 as analog input only, the rest is digital I/O.
    
        // Configure Vref (-) to Vss
        VCFG1 = 0;
    
        // Configure Vref (+) to Vdd
        VCFG0 = 0;
    
        // Configure the result to be right justified.
        ADFM = 1;
        
        // Turn OFF ADC by default.
        ADON = 0;
    }    
    
    extern unsigned int ui_adc_read(void)
    {
        unsigned int temp = 0;
        // Select the ADC channel.
        CHS3 = 0;    //select channel 0
        CHS2 = 0;
        CHS1 = 0;
        CHS0 = 0;
        
        // Delay 5mS to fully charge the holding capacitor in the ADC module.
        __delay_ms(5);
        
        // Start the conversion and wait for it to complete.
        GODONE = 1;
        while (GODONE == 1);
        
        // Return the ADC result.
        temp = ADRESH << 8;
        temp = temp + ADRESL;
        return temp;
    }    
    
    void pwm_init(void)
    {
        // Setting PWM frequency = 4.90KHz at 8MHz OSC Freq
        PR2 = 0x65;
        T2CKPS1 = 0;
        T2CKPS0 = 1;    // Timer 2 prescale = 4.
        
        CCPR1L = 0;        // Duty cycle = 0;
        TMR2ON = 1;        // Turn on Timer 2.    
        
        //configuration for CCP1CON
        P1M1 = 0;        //CCP1, P1A as single output
        P1M0 = 0;
    
        DC1B1 = 0;        // 2 LSB of 10 PWM, make it 00
        DC1B0 = 0;
        
        CCP1M3 = 1;        // Configure CCP1 module to operate in PWM mode.
        CCP1M2 = 1;        
        CCP1M1 = 0;
        CCP1M0 = 0;    
        
        //configuration for CCP2CON
    
    
        CCP2M3 = 1;        // Configure CCP1 module to operate in PWM mode.
        CCP2M2 = 1;        
        CCP2M1 = 0;
        CCP2M0 = 0;    
    }    
    
    void set_pwmr(unsigned char uc_duty_cycle)
    {
        CCPR1L = uc_duty_cycle;
    }    
    
    void set_pwml(unsigned char uc_duty_cycle)
    {
        CCPR2L = uc_duty_cycle;
    }    
    
    void motor(unsigned char uc_left_motor_speed,unsigned char uc_right_motor_speed)
    {    
        //set the speed for left and right motor
        set_pwmr(uc_right_motor_speed);
        set_pwml(uc_left_motor_speed);    
    }
    this coding for pwm is not function. ccp1 always in high low (blinking). please help me..thanks

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    125
    You've got a better chance of getting an answer if you narrow
    down where you problem is and be a bit more specific on what the problem is.
    If it is more PIC specific, you may have better luck at this forum:
    16-Bit Core (instructions, reset, WDT, specifications...) PIC17Cxx, PIC18Fxxx

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    Code:
    void pwm_init(void){
        // Setting PWM frequency = 4.90KHz at 8MHz OSC Freq
        PR2 = 0x65;
        T2CKPS1 = 0;
        T2CKPS0 = 1;    // Timer 2 prescale = 4.
         
        CCPR1L = 0;        // Duty cycle = 0;
        TMR2ON = 1;        // Turn on Timer 2.    
         
        //configuration for CCP1CON
        P1M1 = 0;        //CCP1, P1A as single output
        P1M0 = 0;
     
        DC1B1 = 0;        // 2 LSB of 10 PWM, make it 00
        DC1B0 = 0;
         
        CCP1M3 = 1;        // Configure CCP1 module to operate in PWM mode.
        CCP1M2 = 1;        
        CCP1M1 = 0;
        CCP1M0 = 0;    
         
        //configuration for CCP2CON
     
     
        CCP2M3 = 1;        // Configure CCP1 module to operate in PWM mode.
        CCP2M2 = 1;        
        CCP2M1 = 0;
        CCP2M0 = 0;    
    }    
     
    void set_pwmr(unsigned char uc_duty_cycle)
    {
        CCPR1L = uc_duty_cycle;
    }    
     
    void set_pwml(unsigned char uc_duty_cycle)
    {
        CCPR2L = uc_duty_cycle;
    }    
     
    void motor(unsigned char uc_left_motor_speed,unsigned char uc_right_motor_speed)
    {    
        //set the speed for left and right motor
        set_pwmr(uc_right_motor_speed);
        set_pwml(uc_left_motor_speed);    
    }
    our main problem are on the pwm part of the coding~ the motor simulation did not work properly~ only 1 motor moving~ so we thought there's something wrong with the coding above but we do not know which part~
    Last edited by Salem; 02-22-2012 at 11:46 AM. Reason: removed crappy formatting from the code - remember, paste as TEXT ONLY!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    Does any motor move at all?

    Before rushing headlong into a complete program, did you prototype just moving ONE motor. You know, real simple things like
    - turn it on
    - turn it off
    - turn through say 90' (in both directions).

    When you're really comfortable with moving 1 motor, try controlling 2 at once.

    Remember, we don't have your hardware or your motors or anything, so we can't do anything to TEST what you post (that's your job).

    Providing as much detail as possible (like what you tried, what worked, what didn't), might allow us to join the gaps and suggest what to do.

    But "here's the code, it doesn't work and I've no idea" is not likely to give you the answer you seek.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    It'd help if you provide hardware diagrams or schematics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. programming a robot
    By sauoon in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2010, 08:39 AM
  2. Help! Robot Programming
    By Gizzle in forum C Programming
    Replies: 23
    Last Post: 09-28-2006, 08:32 AM
  3. I, Robot -- It's a trick, and I'm mad.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-15-2004, 02:57 PM
  4. Robot Programming?
    By Yaj in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-19-2002, 12:42 PM
  5. Robot programming
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 06-05-2002, 12:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21