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