Page 1 of 1

simple error

PostPosted: Sun May 06, 2012 11:27 pm
by areebee
hi everyone
i need ur help
can u identified where is my mistake?
actually i want to make the robot moving when sw1 pressed and stop when sw2 pressed
i cant identify where is my mistake

here my code:
CODE: SELECT_ALL_CODE
//============================================================================================================
//   Author            :CYTRON   
//   Project            :PR23
//   Project description   :Simple line follow
//  Version          :v1.4
//============================================================================================================

//   include
//============================================================================================================
#include <pic.h>

//   configuration
//============================================================================================================
__CONFIG ( 0x3F32 );

//   define
//============================================================================================================
#define sw1         RB0
#define sw2         RB1
#define motor_ra   RC0
#define motor_rb   RC3
#define motor_la   RC4
#define motor_lb   RC5

/*
#define s_left      RB0
#define s_mleft      RB1
#define s_mright   RB2
#define s_right      RB3

#define buzzer       RE2
*/
#define   rs         RB4 //ubah
#define   e         RB5 //ubah
#define   lcd_data   PORTD

#define SPEEDL      CCPR1L
#define SPEEDR      CCPR2L

#define   CHANNEL0   0b10000001   // AN0 ( Ultrasonic )
#define   CHANNEL1   0b10001001   // AN1 ( Distance sensor )


#define RX_PIN       RC7
#define TX_PIN       RA2
#define BOT_ADD      100
*/


//   global variable
//============================================================================================================
unsigned char data[6] = {0};
//const unsigned char line [] = {"1.LINE FOLLOW"};
const unsigned char US[] = {"2.Ultrasonic"};
//const unsigned char AS[] = {"3.Analog Sensor"};
//const unsigned char XB[] = {"4.SKXBEE"};
//const unsigned char SKPS[] = {"5.SKPS"};
//const unsigned char *mode [5] = {&line[0],&US[0],&AS[0],&XB[0],&SKPS[0]};

unsigned int result;
unsigned int To=0,T=0,TH=0;
unsigned char REC;
unsigned char i=0,raw;

unsigned int us_value (unsigned char mode);

//   function prototype
//============================================================================================================
void init(void);
void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void e_pulse(void);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
void dis_num(unsigned long data);

//void line_follow(void);
void ultrasonic(void);
//void wireless_xbee(void);
//void analog_sen(void);
//void SKPS_PScon(void);

void forward(void);
void stop (void);
void backward (void);
void reverse (void);
void left(void);
void right(void);

//void uart_send(unsigned char data);
//unsigned char uart_rec(void);
//unsigned char skps(unsigned char data);
//void skps_vibrate(unsigned char motor, unsigned char value);
void read_adc(char config);


//   interrupt prototype
//============================================================================================================
/*static void interrupt isr(void)
{
   if (TMR0IF)                        // TMR0 is overflow
   {
      TMR0IF = 0;                   // clear flag bit
      To +=0x100;                     // count number of TMR0 overflow ( make it to 16bit TMR)
   }
      
   if(RBIF)                        // there is change bit on RB4-RB7
   {
      RBIF = 0;                     //                                     ____
      if (RB4)                     // Rb4 is 1 mean is rising form 0  __|         
      {
         TMR0 = 0;                  // clear all counter involved, start new count for period of RB4 high
         To = 0;
      }
                                 //                           ___
      else TH = TMR0 + To;            // RB4 is 0 mean is falling form 1       |_____  // save TH, RB4 high period
   }

   if(RCIF)
   {
      RCIF = 0;                     // clear flag bit

      if (RCREG == 'R') data[i=0]= RCREG;            // check if start byte 'R' is met       
      else if (RCREG == 100) data[i=0]= RCREG;      // check if start byte 'd'(decimal 100) is met
      if ((data[0] == 'R'))data [i++] = RCREG;      // save the data in data array
      if (i>4) i = 4;                           // if the data array reached max, set the index to 4
   }
*/}


//   main function
//============================================================================================================
void main(void)
{

   while (1)
   {   
      if (sw1 == 1)
      {
         ultrasonic();
      
      }
      
      if (!sw2)
      {
         stop();
         break;
      }
   }


//===========================================================================================================
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
   // ADC configuration   
   ADCON1 = 0b10000100;            //set RA0 and RA1 as Analog Input, left justified

   // setup for capture pwm
   RBIE = 1;                     // enable interrupt on change of port B

   // motor PWM configuration
   PR2 = 255;                  // set period register
   T2CON =     0b00000100;         //
   CCP1CON =   0b00001100;         // config for RC1 to generate PWM   ( for more detail refer datasheet section 'capture/compare/pwm')
   CCP2CON =   0b00001100;         // config for RC2 to generate PWM

   // Tris configuration (input or output)
   TRISA = 0b00000101;            //set RA0 and RA2 pin as input,other as output
   TRISB = 0b00000000;            //set RB0-RB4 pin as input, other as output
   TRISC = 0b00000000;            //set PORTC pin as output
   TRISD = 0b00000000;            //set all PORTD pin as output
   TRISE = 0b00000011;

   // TMR 0 configuation
   T0CS = 0;                  
   PSA = 0;                  
   PS2 = 1;                  // prescale 1:32
   PS1 = 1;                  //
   PS0 = 1;                  //
   TMR0IE = 1;                  // TMR0 Interrupt
   TMR0 = 0;

   //setup UART
   SPBRG = 0x81;                  //set baud rate to 9600 for 20Mhz
   BRGH = 1;                     //baud rate high speed option
   TXEN = 1;                     //enable transmission
   TX9 = 0;
   CREN = 1;                     //enable reception
   SPEN = 1;                     //enable serial port
   RX9 = 0;
   RCIE = 1;                     //enable interrupt on eachdata received

   // enable all unmasked interrupt   
   GIE = 1;
   PEIE = 1;

   // LCD configuration
   send_config(0b00000001);      //clear display at lcd
   send_config(0b00000010);      //Lcd Return to home
   send_config(0b00000110);      //entry mode-cursor increase 1
   send_config(0b00001100);      //diplay on, cursor off and cursor blink off
   send_config(0b00111000);      //function

//   TX_PIN = 1;
   b_light = 0;
//   buzzer = 0;
   stop();                                 
}


//============================================================================================================
// Mode 2 : Ultrasonic
// Description: Maintain distance measure using ultrasonic between obsacle and robot
// Can choose data acquiring methode between ADC, PWM and UART
//============================================================================================================
void ultrasonic(void)      
{
   int distance;                                                // variable for distance measuring
//   char n=1;                                                   // index for indicat mode
//   lcd_clr();                                                   // clear lcd
//   send_string("Measure mode");                                    // display string
//   lcd_goto(20);                                                // lcd goto 2nd line
//   send_string("ADC");   




//   lcd_clr();                                                   // clear the lcd
//   send_string("Distance");                                       // display string "Distance"

   while(1)                                                   // loop forever
   {
//      lcd_goto(20);                                             // lcd goto 2nd line
      distance = us_value(n);                                       // disance variable are equal to value return from subroutine us_value
      dis_num(distance);                                          // display the value of distance


       if (distance> 40)                                          // check if distance more than 40
      {
         forward();                                             // then forward with full speed
         SPEEDL = 255;
         SPEEDR = 255;
         lcd_goto(20);                                             // lcd go to 2nd line 1st character
         send_string ("forward  ");
//         buzzer = 0;
      }
      else if (distance> 30)                                       // check if distance more than 40
      {
         forward();                                             // forward with medium speed
         SPEEDL = 230;
         SPEEDR = 230;
         lcd_goto(20);                                             // lcd go to 2nd line 1st character
         send_string ("forward  ");
//         buzzer = 0;
      }
      else if( distance >20)                                       // check if distance more than 40
      {
         stop();   
         lcd_goto(20);                                             // lcd go to 2nd line 1st character
         send_string ("stop  ");                                             // then stop
//         buzzer = 0;
      }
      else                                                    // else, distance less than 20
      {
         backward();                                             // then backward with medium speed and on the buzzer
         SPEEDL = 250;
         SPEEDR = 250;
         lcd_goto(20);                                             // lcd go to 2nd line 1st character
         send_string ("backward");
//         buzzer = 1;
      }
   }

}

//====================================================================================================
// Ultrasonic value
// Description : Retrive data from Ultrsonic. Can choose methode between ADC, PWM and UART
// Parameter : mode      1) using analog
//                  2) using pwm
//                  3) using uart
//====================================================================================================
unsigned int us_value (unsigned char mode)                                 // subroutine for ultrasonic measurement
{
   unsigned int value;
   switch (mode)                                                   // retrive value of measured distane based on the methode selected
   {
      case 1:   read_adc(CHANNEL0);
            value = result;                                          // max vslue 2.55v = 2.55/5 *1024 - 1 =  522, resolution = 10mV/ inch, 10m/5*1024 =~ 2
            break;
      case 2:   value = TH;                                             // each value = 256*4/20mhz = 51.2us, i inch = 147us 
            break;                                                // can change using smaller timer prescale, but resulation fixed 147us / inch
      case 3:   if ( data [0]=='R')   value = (data[1] - 0x30)*100+ (data[2] - 0x30)*10+ (data[3] - 0x30); // 1 = 1 inch
            else
            {   
//               lcd_goto(20);                                       // if stater byte is not 'R', Display 'not connected'
//               send_string("not connected");
               while(1);                                          // loop forever
            }   
      default: ;
   }
   return value;
}   


//===========================================================================================================
// read adc
// Description: subroutine for converting analog value to digital with average 200 samples
// Parameter : config  ( select the channel )
//============================================================================================================
void read_adc(char config)
{
   unsigned short i;
   unsigned long result_temp=0;

   ADCON0 = config;
   delay(10000);               // delay after changing configuration
   for(i=200;i>0;i-=1)            //looping 200 times for getting average value
   {
      ADGO = 1;               //ADGO is the bit 2 of the ADCON0 register
      while(ADGO==1);            //ADC start, ADGO=0 after finish ADC progress
      result=ADRESH;
      result=result<<8;         //shift to left for 8 bit
      result=result|ADRESL;      //10 bit result from ADC

      result_temp+=result;      
   }
   result = result_temp/200;      //getting average value
   ADON = 0;                  //adc module is shut off
}


//===========================================================================================================
// Motor control function
// Description : subroutine to set the robot moving direction
//============================================================================================================


void backward ()
{
   motor_ra = 1;
   motor_rb = 0;
   motor_la = 1;
   motor_lb = 0;
}
void forward ()
{
   motor_ra = 0;
   motor_rb = 1;
   motor_la = 0;
   motor_lb = 1;
}

void left()
{
   motor_la = 1;
   motor_lb = 0;
   motor_ra = 0;
   motor_rb = 1;
}
void right()
{
   motor_la = 0;
   motor_lb = 1;
   motor_ra = 1;
   motor_rb = 0;
}

void stop()
{
   motor_la = 1;
   motor_lb = 1;
   motor_ra = 1;
   motor_rb = 1;
}

void delay(unsigned long data)               //delay function, the delay time
{
   for( ;data>0;data-=1);                  //depend on the given value
}


Re: simple error

PostPosted: Mon May 07, 2012 6:49 am
by ABSF
The problem is not the program, I think you need to understand how the program works first before you start modifying it.

Allen