How to program 5 MD10C with sk40c with pic16f877a using skp?

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

How to program 5 MD10C with sk40c with pic16f877a using skp?

Postby samcheong90 » Thu Mar 15, 2012 2:45 pm

hi, so how to program 5 MD10c with pic16f877a? in MD10C the logic is when PWM is high then the motor which connected to MD10C will move in either direction depends on the DIR. so can i just send 1 to PWM in pic to move the motor and controlled by skps? PW3 to PW5 is not working when i press the corresponding button. my code is like:
CODE: SELECT_ALL_CODE
#define M1_DIR      RC3      //MD10C (1) Enable
#define M1_PWM      CCPR1L   //MD10C (1) PWM register

#define M2_DIR      RC0      //MD10C (2) Enable
#define M2_PWM      CCPR2L   //MD1OC (2) PWM register

#define PW3_DIR     RC4
#define PW3_PWM       RA5     //Power Window (3)

#define PW4_DIR     RA0
#define PW4_PWM       RA1     //Power Window (4)

#define PW5_DIR     RA2
#define PW5_PWM       RA3     //Power Window (5)

//left and right max speed
#define left_max_speed   255
#define right_max_speed   255


while(1)
   {
      //get data from joystick
      joystick_rightup=SKPS(p_joy_ru);               //get joystick_rightup value from p_joy_ru value
          joystick_rightdown=SKPS(p_joy_rd);              //get joystick_rightdown value from p_joy_rd value
          joystick_rightleft=SKPS(p_joy_rl);              //get joystick_rightleft value from p_joy_rl value
        joystick_rightright=SKPS(p_joy_rr);             //get joystick_rightright value from p_joy_rr value
      joystick_leftup=SKPS(p_joy_lu);               //get joystick_leftup value from p_joy_lu value
          joystick_leftdown=SKPS(p_joy_ld);              //get joystick_leftdown value from p_joy_ld value
          joystick_leftleft=SKPS(p_joy_ll);              //get joystick_leftleft value from p_joy_ll value
        joystick_leftright=SKPS(p_joy_lr);             //get joystick_leftright value from p_joy_lr value
   

      
      //calculate left_wheel and right_wheel register from all 4 joystick value
      left_wheel=   joystick_leftleft*left_max_speed/100
               -joystick_leftright*left_max_speed/100;
      right_wheel=   joystick_rightleft*right_max_speed/100
                  -joystick_rightright*right_max_speed/100;
      
      //limit left wheel and right wheel register to the maximum value
      if(left_wheel>left_max_speed)left_wheel=left_max_speed;
      else if(left_wheel<-left_max_speed)left_wheel=-left_max_speed;
      
      if(right_wheel>right_max_speed)right_wheel=right_max_speed;
      else if(right_wheel<-right_max_speed)right_wheel=-right_max_speed;   



   //calculate left_wheel and right_wheel register from all 4 joystick value
      fleft_wheel=   joystick_leftup*left_max_speed/100
               -joystick_leftdown*left_max_speed/100
               -joystick_rightdown*left_max_speed/100
               +joystick_rightup*left_max_speed/100;
      fright_wheel=   joystick_leftup*left_max_speed/100
               -joystick_leftdown*left_max_speed/100
               -joystick_rightdown*left_max_speed/100
               +joystick_rightup*left_max_speed/100;
      
      //limit left wheel and right wheel register to the maximum value
      if(fleft_wheel>left_max_speed)fleft_wheel=left_max_speed;
      else if(fleft_wheel<-left_max_speed)fleft_wheel=-left_max_speed;
      
      if(fright_wheel>right_max_speed)fright_wheel=right_max_speed;
      else if(fright_wheel<-right_max_speed)fright_wheel=-right_max_speed;   
      
   
   
      //brake both motor if PS2 controller not connected
      if(SKPS(p_con_status)==0)
      {
         left_wheel=0;
         right_wheel=0;
         fleft_wheel=0;
         fright_wheel=0;
      }
         
      //control motor driver based on left_wheel and right_wheel value
      if(left_wheel>0)      //left forward
      {
         M1_DIR=0;
         M1_PWM=(unsigned char)left_wheel;
      }
      else if(left_wheel<0)   //left reverse
      {
         M1_DIR=1;
         M1_PWM=(unsigned char)(-left_wheel);
      }
      else               //left brake
      {
         M1_PWM=0;
      }
      
      if(right_wheel>0)      //right forward
      {
         M2_DIR=1;
         M2_PWM=(unsigned char)right_wheel;
      }
      else if(right_wheel<0)   //right forward
      {
         M2_DIR=0;
         M2_PWM=(unsigned char)(-right_wheel);
      }
      else               //right brake
      {
         M2_PWM=0;
      }   
      

      //control motor driver based on left_wheel and right_wheel value
      if(fleft_wheel>0)      //left forward
      {
         M1_DIR=0;
         M1_PWM=(unsigned char)fleft_wheel;
      }
      else if(fleft_wheel<0)   //left reverse
      {
         M1_DIR=1;
         M1_PWM=(unsigned char)(-fleft_wheel);
      }
      else               //left brake
      {
         M1_PWM=0;
      }
      
      if(fright_wheel>0)      //right forward
      {
         M2_DIR=1;
         M2_PWM=(unsigned char)fright_wheel;
      }
      else if(fright_wheel<0)   //right forward
      {
         M2_DIR=0;
         M2_PWM=(unsigned char)(-fright_wheel);
      }
      else               //right brake
      {
         M2_PWM=0;
      }   


      //control up,down,in,out,gripper motion
      if(SKPS(p_up)==0)
      {
         PW3_PWM=1;
         PW3_DIR=0;
      }
      else if(SKPS(p_down)==0)
      {
         PW3_PWM=1;
         PW3_DIR=1;
      }
      else
      {
         PW3_PWM=0;
      }
      if(SKPS(p_left)==0)
      {
         PW4_PWM=1;
         PW4_DIR=0;
      }
      else if(SKPS(p_right)==0)
      {
         PW4_PWM=1;
         PW4_DIR=1;
      }
      else
      {
         PW4_PWM=0;
      }
      if(SKPS(p_triangle)==0)
      {
         PW5_PWM=1;
         PW5_DIR=0;
      }
      else if(SKPS(p_cross)==0)
      {
         PW5_PWM=1;
         PW5_DIR=1;
      }
      else
      {
         PW5_PWM=0;
      }         


did my code had any problem? or is it i cannot just send the signal 1 to PWM to run the motor?PW3 to PW5 i don't want speed control i just want to control the motor to move when skps's button is push.
samcheong90
Newbie
 
Posts: 10
Joined: Wed Mar 14, 2012 11:50 am

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby robosang » Thu Mar 15, 2012 10:31 pm

Why don try it and let us know. No one will will build a hardware and try the code for you :mrgreen: We can only take a look.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby ABSF » Fri Mar 16, 2012 7:04 am

samcheong90 WROTE:hi, so how to program 5 MD10c with pic16f877a? in MD10C the logic is when PWM is high then the motor which connected to MD10C will move in either direction depends on the DIR. so can i just send 1 to PWM in pic to move the motor and controlled by skps? PW3 to PW5 is not working when i press the corresponding button. my code is like:

-----clip off -------

does my code have any problem? or i cannot just send the signal 1 to PWM to run the motor?PW3 to PW5 i don't want speed control i just want to control the motor to move when skps's button is push.


Your code is large and incomplete and has many errors. I tried to compile but failed. :roll:

What test have you done on SK40C, MD10C and SKPS so far? Have you tried to drive one MD10C without the SKPS and does it work? You cannot just build a car from scratch without understanding how each indivisual part like engine, gearbox, steering or brake works.

Try to do it step by step from simple then to complicated and finally the complete project. Is your deadline approaching soon?

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby samcheong90 » Fri Mar 16, 2012 8:30 am

i did try to compile it and run a motor. it is success to build and able to run 2 motor which is the M1 and M2 which the DIR is connect to RC3 and RC0 respectively and PWM connect to RC2 and RC1 and having speed control. the problem now i facing is cannot run the other 3 motor which is PW3 to PW5. in this 3 motor i just wanted to run it without speed control but with direction control by pressing buttons on the ps2 controller. so i wondering is it any problem in the code below:

CODE: SELECT_ALL_CODE
 if(SKPS(p_up)==0)
      {
         PW3_PWM=1;
         PW3_DIR=0;
      }
      else if(SKPS(p_down)==0)
      {
         PW3_PWM=1;
         PW3_DIR=1;
      }
      else
      {
         PW3_PWM=0;
      }
      if(SKPS(p_left)==0)
      {
         PW4_PWM=1;
         PW4_DIR=0;
      }
      else if(SKPS(p_right)==0)
      {
         PW4_PWM=1;
         PW4_DIR=1;
      }
      else
      {
         PW4_PWM=0;
      }
      if(SKPS(p_triangle)==0)
      {
         PW5_PWM=1;
         PW5_DIR=0;
      }
      else if(SKPS(p_cross)==0)
      {
         PW5_PWM=1;
         PW5_DIR=1;
      }
      else
      {
         PW5_PWM=0;
      }         


by the way, i did not attach the whole code out. the full code is as below:

CODE: SELECT_ALL_CODE
//device definition header file
#include <htc.h>

//device configuration bit
__CONFIG(0x3F32);

//   Define
//===============================================================
//compiler define
#define _XTAL_FREQ   20000000

//hardware define
#define LED1      RB6      //SK40C on-board red LED
#define LED2      RB7      //SK40C on-board yellow LED

#define   SW1         RB0      //SK40C on-board SW1
#define SW2         RB1      //SK40C   on-board SW2

#define   LCD_data   PORTD   //SK40C LCD 8-bit data byte
#define   LCD_RS      RB4      //SK40C LCD RS
#define LCD_E      RB5      //SK40C LCD E


#define M1_DIR      RC3      //MD10C (1) Enable
#define M1_PWM      CCPR1L   //MD10C (1) PWM register


#define M2_DIR      RC0      //MD10C (2) Enable
#define M2_PWM      CCPR2L   //MD1OC (2) PWM register

#define PW3_DIR     RC4
#define PW3_PWM       RA5     //Power Window (3)

#define PW4_DIR     RA0
#define PW4_PWM       RA1     //Power Window (4)

#define PW5_DIR     RA2
#define PW5_PWM       RA3     //Power Window (5)

#define SKPS_Reset   RB2      //SKPS RESET Pin

//SKPS protocol constant variable define
#define   p_select      0
#define p_joyl         1
#define p_joyr         2
#define p_start         3
#define p_up         4
#define p_right         5
#define p_down         6
#define p_left         7
#define   p_l2         8
#define   p_r2         9
#define p_l1         10
#define p_r1         11
#define p_triangle      12
#define p_circle      13
#define p_cross         14
#define   p_square      15
#define p_joy_lx      16
#define   p_joy_ly      17
#define p_joy_rx      18
#define p_joy_ry      19
#define p_joy_lu      20
#define p_joy_ld      21
#define p_joy_ll      22
#define p_joy_lr      23
#define p_joy_ru      24
#define p_joy_rd      25
#define p_joy_rl      26
#define p_joy_rr      27
#define   p_con_status   28
#define p_motor1      29
#define p_motor2      30

//left and right max speed
#define left_max_speed   255
#define right_max_speed   255
#define left_max_speed_button_1      100
#define right_max_speed_button_1   100
#define left_max_speed_button_2      180
#define right_max_speed_button_2   180

//function prototype
//================================================================
void LCD_char(unsigned char data);
void LCD_string(const unsigned char *s);
void LCD_config(unsigned char data);
void LCD_init(void);
void LCD_clear(void);
void LCD_goto(unsigned char row, unsigned char column);
void LCD_number(unsigned char data);

unsigned char uart_read(void);
void uart_write(unsigned char data);
unsigned char SKPS(unsigned char data);
void SKPS_vibrate(unsigned char motor, unsigned char value);

//   main function
//================================================================
void main(void)
{
   //variable
   unsigned char joystick_rightup=0, joystick_rightdown=0, joystick_rightleft=0, joystick_rightright=0, joystick_leftup=0, joystick_leftdown=0, joystick_leftleft=0, joystick_leftright=0;
   int left_wheel=0, right_wheel=0, fleft_wheel=0, fright_wheel=0;
   
   //TRIS configuration
   RB6=0;         //LED1
   TRISB6=0;   
   RB7=0;         //LED2
   TRISB7=0;   
   TRISB0=1;      //SW1
   TRISB1=1;      //SW2
   PORTD=0x00;      //LCD_data
   TRISD=0x00;   
   RB4=0;         //LCD_RS
   TRISB4=0;   
   RB5=0;         //LCD_E
   TRISB5=0;   
   RC3=0;         //M1_DIR
   TRISC3=0;
   RC0=0;         //M2_DIR
   TRISC0=0;
   RC4=0;         //PW3_DIR
   TRISC4=0;
   RA5=0;         //PW3_PWM
   TRISA5=0;
   RA0=0;         //PW4_DIR
   TRISA0=0;
   RA1=0;         //PW4_PWM
   TRISA1=0;
   RA2=0;         //PW5_DIR
   TRISA2=0;
   RA3=0;         //PW5_PWM
   TRISA3=0;      
   RB2=0;      //SKPS_Reset
   TRISB2=0;
   
   //UART
   TRISC6=0;   //UART tx
   TRISC7=1;   //UART rx
   TX9=0;      //select 8 transmission
   TXEN=1;      //Transmit enable
   SYNC=0;      //USART asynchronous
   BRGH=1;      //Baud Rate high speed
   SPEN=1;      //Serial Port enable
   RX9=0;      //select 8 reception
   CREN=1;      //enable continuous receive
   SPBRG=10;   //high speed   SPBRG = Fosc/(16*Baudrate) - 1
   TXIE=0;      //Transmit Interrupt disable
   RCIE=0;      //Receive Interrupt disable
   RCREG;      //clear receive buffer
   RCREG;      //clear receive buffer
   
   //PWM
   TRISC1=0;   //PWM2
   TRISC2=0;   //PWM1
   PR2=0xff;   //Timer 2 Module's Period Register
   TOUTPS3=0;   //Timer 2 Postscale
   TOUTPS2=0;   //0000=1:1 0001=1:2
   TOUTPS1=0;   //....
   TOUTPS0=0;   //1111=1:16
   TMR2ON=1;   //Timer 2 off/on
   T2CKPS1=0;   //Timer 2 Prescaler
   T2CKPS0=0;   //00=1 01=4 1x=16
   CCP1X=0;   //PWM 1 Lease Significant bits
   CCP1Y=0;   //PWM 1 Lease Significant bits
   CCP1M3=1;   //PWM 1 PWM Mode
   CCP1M2=1;
   CCP1M1=0;
   CCP1M0=0;
   CCPR1L=0;
   CCP2X=0;   //PWM 2 Lease Significant bits
   CCP2Y=0;   //PWM 2 Lease Significant bits
   CCP2M3=1;   //PWM 2 PWM Mode
   CCP2M2=1;
   CCP2M1=0;
   CCP2M0=0;
   CCPR2L=0;
   
   //LCD initialization
   LCD_init();         //call init function to intialize LCD
   
   LCD_config(0x40);   //add a CG graphic to location 0
                  //location 0x00
   LCD_char(0x00);      //   |     |
   LCD_char(0x00);      //   |     |   
   LCD_char(0x00);      //   |     |
   LCD_char(0x00);      //   |     |
   LCD_char(0x00);      //   |     |
   LCD_char(0x11);      //   |@   @|
   LCD_char(0x0a);      //   | @ @ |
   LCD_char(0x04);      //   |  @  |
                  //location 0x01
   LCD_char(0x00);      //   |     |
   LCD_char(0x00);      //   |     |   
   LCD_char(0x00);      //   |     |
   LCD_char(0x0e);      //   | @@@ |
   LCD_char(0x0e);      //   | @@@ |
   LCD_char(0x00);      //   |     |
   LCD_char(0x00);      //   |     |
   LCD_char(0x00);      //   |     |
   
   LCD_clear();      //clear LCD
   LCD_goto(0,0);      //set cursor to location 0,0

   //initially enable and brake both motor
   M1_PWM=0;
   M2_PWM=0;   
   
   //reset SKPS
   SKPS_Reset=1;
   __delay_ms(50);
   SKPS_Reset=0;
   __delay_ms(100);
   
   //Display startup text   
   LCD_goto(0,0);               //first line
   LCD_string("SKPS + SK40C ->");
   LCD_goto(1,5);               //second line
   LCD_string("MD10C");
   __delay_ms(800);            //800ms delay
   LCD_clear();               //clear the LCD
   
   //infinity loop
   while(1)
   {
      //get data from joystick
      joystick_rightup=SKPS(p_joy_ru);               //get joystick_rightup value from p_joy_ru value
          joystick_rightdown=SKPS(p_joy_rd);              //get joystick_rightdown value from p_joy_rd value
          joystick_rightleft=SKPS(p_joy_rl);              //get joystick_rightleft value from p_joy_rl value
        joystick_rightright=SKPS(p_joy_rr);             //get joystick_rightright value from p_joy_rr value
      joystick_leftup=SKPS(p_joy_lu);               //get joystick_leftup value from p_joy_lu value
          joystick_leftdown=SKPS(p_joy_ld);              //get joystick_leftdown value from p_joy_ld value
          joystick_leftleft=SKPS(p_joy_ll);              //get joystick_leftleft value from p_joy_ll value
        joystick_leftright=SKPS(p_joy_lr);             //get joystick_leftright value from p_joy_lr value
   

      
      //calculate left_wheel and right_wheel register from all 4 joystick value
      left_wheel=   joystick_leftleft*left_max_speed/100
               -joystick_leftright*left_max_speed/100;
      right_wheel=   joystick_rightleft*right_max_speed/100
                  -joystick_rightright*right_max_speed/100;
      
      //limit left wheel and right wheel register to the maximum value
      if(left_wheel>left_max_speed)left_wheel=left_max_speed;
      else if(left_wheel<-left_max_speed)left_wheel=-left_max_speed;
      
      if(right_wheel>right_max_speed)right_wheel=right_max_speed;
      else if(right_wheel<-right_max_speed)right_wheel=-right_max_speed;   



   //calculate left_wheel and right_wheel register from all 4 joystick value
      fleft_wheel=   joystick_leftup*left_max_speed/100
               -joystick_leftdown*left_max_speed/100
               -joystick_rightdown*left_max_speed/100
               +joystick_rightup*left_max_speed/100;
      fright_wheel=   joystick_leftup*left_max_speed/100
               -joystick_leftdown*left_max_speed/100
               -joystick_rightdown*left_max_speed/100
               +joystick_rightup*left_max_speed/100;
      
      //limit left wheel and right wheel register to the maximum value
      if(fleft_wheel>left_max_speed)fleft_wheel=left_max_speed;
      else if(fleft_wheel<-left_max_speed)fleft_wheel=-left_max_speed;
      
      if(fright_wheel>right_max_speed)fright_wheel=right_max_speed;
      else if(fright_wheel<-right_max_speed)fright_wheel=-right_max_speed;   
      
   
   
      //brake both motor if PS2 controller not connected
      if(SKPS(p_con_status)==0)
      {
         left_wheel=0;
         right_wheel=0;
         fleft_wheel=0;
         fright_wheel=0;
      }
         
      //control motor driver based on left_wheel and right_wheel value
      if(left_wheel>0)      //left forward
      {
         M1_DIR=0;
         M1_PWM=(unsigned char)left_wheel;
      }
      else if(left_wheel<0)   //left reverse
      {
         M1_DIR=1;
         M1_PWM=(unsigned char)(-left_wheel);
      }
      else               //left brake
      {
         M1_PWM=0;
      }
      
      if(right_wheel>0)      //right forward
      {
         M2_DIR=1;
         M2_PWM=(unsigned char)right_wheel;
      }
      else if(right_wheel<0)   //right forward
      {
         M2_DIR=0;
         M2_PWM=(unsigned char)(-right_wheel);
      }
      else               //right brake
      {
         M2_PWM=0;
      }   
      

      //control motor driver based on left_wheel and right_wheel value
      if(fleft_wheel>0)      //left forward
      {
         M1_DIR=0;
         M1_PWM=(unsigned char)fleft_wheel;
      }
      else if(fleft_wheel<0)   //left reverse
      {
         M1_DIR=1;
         M1_PWM=(unsigned char)(-fleft_wheel);
      }
      else               //left brake
      {
         M1_PWM=0;
      }
      
      if(fright_wheel>0)      //right forward
      {
         M2_DIR=1;
         M2_PWM=(unsigned char)fright_wheel;
      }
      else if(fright_wheel<0)   //right forward
      {
         M2_DIR=0;
         M2_PWM=(unsigned char)(-fright_wheel);
      }
      else               //right brake
      {
         M2_PWM=0;
      }   


      //control up,down,in,out,gripper motion
      if(SKPS(p_up)==0)
      {
         PW3_PWM=1;
         PW3_DIR=0;
      }
      else if(SKPS(p_down)==0)
      {
         PW3_PWM=1;
         PW3_DIR=1;
      }
      else
      {
         PW3_PWM=0;
      }
      if(SKPS(p_left)==0)
      {
         PW4_PWM=1;
         PW4_DIR=0;
      }
      else if(SKPS(p_right)==0)
      {
         PW4_PWM=1;
         PW4_DIR=1;
      }
      else
      {
         PW4_PWM=0;
      }
      if(SKPS(p_triangle)==0)
      {
         PW5_PWM=1;
         PW5_DIR=0;
      }
      else if(SKPS(p_cross)==0)
      {
         PW5_PWM=1;
         PW5_DIR=1;
      }
      else
      {
         PW5_PWM=0;
      }         
      
      
      //display PS status
      LCD_goto(0,0);
      if(SKPS(p_con_status)==0)LCD_string("PS2:X");   //X to show PS2 not connected
      else LCD_string("PS2:O");                  //O to show PS2 connected
      
      //display PS joystick
      LCD_goto(0,6);
      LCD_string("J:");
      if(joystick_leftup>0)
      {
         LCD_char('^');            //symbol up
         LCD_number(joystick_leftup);   //joystick up value
      }
      else if(joystick_leftdown>0)
      {
         LCD_char(0x00);            //symbol down (CG 0x00)
         LCD_number(joystick_leftdown);   //joystick down value
      }
      else
      {
         LCD_char(0x01);            //symbol center (CG 0x01)
         LCD_number(0);            //zero
      }
      if(joystick_leftleft>0)
      {
         LCD_char('<');            //symbol left
         LCD_number(joystick_leftleft);   //joystick left value
      }
      else if(joystick_leftright>0)
      {
         LCD_char('>');            //symbol right
         LCD_number(joystick_leftright);   //joystick right value
      }
      else
      {
         LCD_char(0x01);            //symbol center (CG 0x01)
         LCD_number(0);            //zero
      }         
               
      //display buttons
      LCD_goto(1,0);
      LCD_string("B:");
      if((SKPS(p_up)==0)&&(SKPS(p_down)==1))
      {
         LCD_char('^');            //symbol up
      }
      else if((SKPS(p_up)==1)&&(SKPS(p_down)==0))
      {
         LCD_char(0x00);            //symbol down (CG 0x00)
      }
      else
      {
         LCD_char(0x01);            //symbol center (CG 0x01)
      }      
      if((SKPS(p_left)==0)&&(SKPS(p_right)==1))
      {
         LCD_char('<');            //symbol left
      }
      else if((SKPS(p_left)==1)&&(SKPS(p_right)==0))
      {
         LCD_char('>');            //symbol right
      }
      else
      {
         LCD_char(0x01);            //symbol center (CG0 0x01)
      }      
      
      //display wheel speed
      LCD_goto(1,5);
      LCD_string("W:");
      if(left_wheel>0)
      {
         LCD_char('^');            //symbol up
         LCD_number(left_wheel);      //left wheel speed
      }
      else if(left_wheel<0)
      {
         LCD_char(0x00);            //symbol down (CG 0x01)
         LCD_number(-left_wheel);   //right wheel speed
      }
      else
      {
         LCD_char(0x01);            //symbol center (CG0 0x01)
         LCD_number(0);            //zero
      }         
      LCD_string("-");
      if(right_wheel>0)
      {
         LCD_number(right_wheel);   //right wheel speed   
         LCD_char('^');            //symbol up
      }
      else if(right_wheel<0)
      {
         LCD_number(-right_wheel);   //right wheel speed      
         LCD_char(0x00);            //symbol down (CG 0x01)
      }
      else
      {
         LCD_number(0);         
         LCD_char(0x01);            //symbol center (CG0 0x01)
      }         

   }   
}

//    LCD Functions
//================================================================

//function to put a character to LCD
void LCD_char(unsigned char data)
{
   LCD_RS=1;      //RS pin to 1 to be in character mode
   LCD_data=data;   //put 8-bit data to LCD_data port
   __delay_us(1);   //short delay
   LCD_E=1;      //set E pin
   __delay_us(1);   //short delay
   LCD_E=0;      //clear E pin
   __delay_us(50);   //50us delay for character
}

//function to write a string to LDC
void LCD_string(const unsigned char *s)
{         
     while (s && *s)LCD_char (*s++);
}

//function to configure LCD
void LCD_config(unsigned char data)
{
   LCD_RS=0;      //RS pin to 0 to be in configuration mode
   LCD_data=data;   //put 8-bit data to LCD_data port
   __delay_us(1);   //short delay
   LCD_E=1;      //set E pin
   __delay_us(1);   //short delay
   LCD_E=0;      //clear E pin
   __delay_ms(3);   //3ms delay for configuration
}

//function to initiate LCD
void LCD_init(void)
{
   LCD_config(0b00000110);      //character entry mode increment, display shift OFF
   LCD_config(0b00001100);      //display ON, cursor underline OFF, cursor blink OFF
   LCD_config(0b00010000);      //cursor move ON
   LCD_config(0b00111000);      //8-bit mode, 2 line mode, 5x7 dot format
   LCD_config(0b00000010);      //display and cursor home
   LCD_config(0b00000001);      //clear display
}
   
//function to clear LCD
void LCD_clear(void)
{
   LCD_config(0x01);      //send 0x01 config to LCD to clear display
}      

//function to set the cursor of LCD to specific row and column
void LCD_goto(unsigned char row, unsigned char column)
{
   if(row>=4)return;      //exit if row is not valid
   if(column>=40)return;   //exit if column is not valid
   
   switch(row)
   {
      case 0:      //row 0            
         LCD_config(0x80+column);
         break;
      case 1:      //row 1
         LCD_config(0x80+0x40+column);
         break;
      case 2:      //row 2
         LCD_config(0x80+0x10+column);   
         break;
      case 3:      //row 3
         LCD_config(0x80+0x50+column);
         break;   
   }   
}

//function to write a byte number to LCD
void LCD_number(unsigned char data)
{
   LCD_char(0x30+(data/100));   //write the 1st number
   data=data%100;
   LCD_char(0x30+(data/10));   //write the 2nd number
   data=data%10;
   LCD_char(0x30+data);      //write the last number
}
   
//   UART function
//================================================================

//function to read a byte from UART RX, wait timeout 100ms
unsigned char UART_read(void)
{
   //create a counter variable
   unsigned int counter=0;
   
   //wait for the data
   while(RCIF==0)
   {
      counter+=1;               //increase counter every 10us
      __delay_us(10);
      if(counter>=10000)return 0;   //exit the function and return 0 when counter reach 10000
   }   
   return RCREG;               //return the data from UART
}

//function to write a byte out to UART TX
void UART_write(unsigned char data)
{
   while(TXIF==0);               //wait for the previous UART transmission to complete
   TXREG=data;                  //write data for transmission
}

//   SKPS function
//================================================================

//function to read button and joystick information on ps controller
unsigned char SKPS(unsigned char data)         
{                                 
   UART_write(data);
   return UART_read();
}

//function to control the vibrator motor on ps controller
void SKPS_vibrate(unsigned char motor, unsigned char value)
{                                    
   UART_write(motor);   
   __delay_ms(1);            
   UART_write(value);   
}


and thanks for the reply. and to ABSF my deadline is approaching in a week and i only left the part that move the 3 MD10C i mentioned above.
samcheong90
Newbie
 
Posts: 10
Joined: Wed Mar 14, 2012 11:50 am

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby ober » Fri Mar 16, 2012 9:22 am

I din go through the code carefully as it is quite long. But my guess would be the initialize of analog pin to digital pin at PORTA.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby ABSF » Fri Mar 16, 2012 9:38 am

Yes Ober is right. You're using PORT A as digital port to control PW3-PW5, right? But when I scan throgh the whole program there's nothing done on ADCON1 register. Go to the 16F877 datasheet and stduy the usage of this register.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby samcheong90 » Fri Mar 16, 2012 10:06 pm

oh i see it is the problem. thanks...anyway, after i browse through the datasheet i still not really understand how to initialize the PORTA pin all the digital output.is it should be like this:?

PORTA = 0;
ADCON1 = 0x06;
TRISA = 0xCF;

i not really know how to write it because it is my first time to program a pic.
hope can help me solve this problem.

thanks for helping.
samcheong90
Newbie
 
Posts: 10
Joined: Wed Mar 14, 2012 11:50 am

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby robosang » Sat Mar 17, 2012 11:00 am

Try it!
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby ABSF » Sat Mar 17, 2012 11:13 am

samcheong90 WROTE:oh i see it is the problem. thanks...anyway, after i browse through the datasheet i still not really understand how to initialize the PORTA pin all the digital output.is it should be like this:?

PORTA = 0;
ADCON1 = 0x06;
TRISA = 0xCF;

i not really know how to write it because it is my first time to program a pic.
hope can help me solve this problem.

thanks for helping.


May be you're reading the wrong part of the datasheet. Are you reading chapter 3.1 on port A? You should be reading Chapter 11 ADC module especially on "REGISTER 11-2" chart.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: How to program 5 MD10C with sk40c with pic16f877a using

Postby samcheong90 » Sat Mar 17, 2012 1:29 pm

yes i did read chapter 11 and if i just want to configure my whole RA pin to digital I/O, is it i just need to write the program on ADCON1 to configure it?

PORTA = 0x00;
ADCON1 = 0b00000110;
CMCON = 0x07;
TRISA = 0x00;

or

PORTA = 0x00;
ADCON1 = 0x06;
CMCON = 0x07;
TRISA = 0x00;

is it be alright to write like this to configure all the port A pin as digital output?
samcheong90
Newbie
 
Posts: 10
Joined: Wed Mar 14, 2012 11:50 am

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests