SK40C + MD10C + DC Geared Motor + Visual Basic

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

Re: SK40C + MD10C + DC Geared Motor + Visual Basic

Postby Abdullah » Sun Dec 09, 2012 3:41 pm

Hai everyone,

Here is my latest code:
CODE: SELECT_ALL_CODE
//
//   Author               : Muhammad Abdullah bin Rosdi
//   Project               : Assignment Deposit Machine
//   Project description      : Interface SK40C with MD10C to control 2 DC motor
//                       in Sign-magnitude mode using PIC16F877A
//   Compiler            :
//   Notes               :
//   
//Connection Summary:
//   RC0-connected to DIR of right motor
//   RC1-connected to PWM of right motor
//   RC2-connected to PWM of left motor
//   RC3-connected to DIR of left motor
//==============================================================================================

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

//   define
//==============================================================================================
// Oscillator Frequency.
//#define   _XTAL_FREQ      20000000

// UART baud rate
//#define UART_BAUD   9600

//   SK40C switches
#define   SW1         RB0         
#define   SW2         RB1   

//   MD10C ports   
#define MD10C_R_DIR   RC0
#define SPEEDR      CCPR2L
#define SPEEDF      CCPR1L
#define MD10C_F_DIR   RC3            

//   SK40C LED
#define   LED1      RB6         
#define   LED2      RB7   

/*******************************************************************************
* DEVICE CONFIGURATION WORDS                                                   *
*******************************************************************************/
//   configuration
//==========================================================================
__CONFIG ( 0x3F32 );            //configuration for the  microcontroller

//   function prototype
//===========================================================================================
void delay(unsigned long data); //delay function

unsigned char uart_rec(void);        //uart function (receive)
void uart_send(unsigned char data);  //uart function (send)
void uart_str(const char *s);       //uart function(string)

//   main function
//===========================================================================================
void main(void)
{
   //unsigned int received;
   unsigned int send;
   unsigned int a;
   unsigned int b=0;
   unsigned int c=1;
   
   //set I/O input output
   TRISB = 0b00000011;               //configure PORTB I/O direction
   TRISC = 0b10000000;               //configure PORTC I/O direction
   TRISD = 0b00000000;               //configure PORTD I/O direction

   //setup ADC
   ADCON1 = 0b00000110;            //set ADx pin digital I/O

   //initialize UART

   SPBRG = 129;      // Configure the baud rate. Set to 9600
   BRGH=1;            //baud rate high speed option
   TXEN=1;            //enable transmission
   TX9 =0;            //8-bit transmission
   RX9 =0;            //8-bit reception   
   CREN=1;            //enable reception
   SPEN=1;            //enable serial port

   // initialize PWM

   // Setup PWM
   // Setting PWM frequency = 4.88khz
   // using PIC16F877A with 20MHz Crystal

   PR2 = 0xFF;
   T2CKPS1 = 0;
   T2CKPS0 = 1;   // Timer 2 prescale = 4.
   
   SPEEDF = 0;
   SPEEDR = 0;
   TMR2ON = 1;      // Turn on Timer 2.
   
   // Configure CCP1 module to operate in PWM mode.
   CCP1M3 = 1;
   CCP1M2 = 1;      
   // Configure CCP2 module to operate in PWM mode.
   CCP2M3 = 1;
   CCP2M2 = 1;   

   LED1=0;
   LED2=0;

   while(1)
   {   
   //led1 for open, led2 for close
   //sw1 for stop motor in open, sw2 for stop motor in close.
      a = uart_rec();
   
      if (SW1==1)
      {
         if (a == '0')//close front door
           {
             MD10C_F_DIR = 0;
               SPEEDF = 220;
               LED2 =1;
         }
            else if (a =='1') //open front door02
            {         
               MD10C_F_DIR = 1;
               SPEEDF = 220;
               LED1 = 1;     
            }
 
         else //if (a=='2')//open back door
           {
            MD10C_R_DIR = 1;
            SPEEDR = 220;
            LED1 = 1;
            LED2 = 0;
            delay(900000);
            MD10C_R_DIR =0;
            SPEEDR = 220;
            LED1 = 0;
            LED2 = 1;
            delay(900000);
            SPEEDR = 0;
           }
      }
      else if (SW1==0)    //stop motor
       {    
         SPEEDF = 0;
         LED1=0;
         LED2=0;
         uart_send(c);
        }      
    
   }while(1) continue;
}

// Simple delay function
//=============================================================================================
void delay(unsigned long data)
{
   for( ;data>0;data-=1);
}


// UART send function
//=============================================================================================
void uart_send(unsigned char data)   //function to send out a byte via uart
{   
   while(TXIF==0);         //wait for previous data to finish send out
   TXREG=data;         //send new data
}

// UART receive function
//=============================================================================================

unsigned char uart_rec(void)         //receive uart value
{
   unsigned char rec_data;
   while(RCIF==0);                  //wait for data
   rec_data = RCREG;            
   return rec_data;               //return the data received
}

// UART string function
//=============================================================================================
void uart_str(const char *s)
{
   while(*s)uart_send(*s++);
}


The task I want to do is simple, but yet I can't get it done. Using the above code, I can get the motor running when I send "1", "2" or "0", but whenver I press SW1, the motor won't stop.

I'm suspecting the problem is with my code. Can anyone suggest me on what to do?
Abdullah
Newbie
 
Posts: 8
Joined: Wed Dec 05, 2012 1:23 pm

Re: SK40C + MD10C + DC Geared Motor + Visual Basic

Postby zhenning » Sun Dec 09, 2012 3:51 pm

One thing, you need switch debouncing :)
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: SK40C + MD10C + DC Geared Motor + Visual Basic

Postby Abdullah » Sun Dec 09, 2012 4:08 pm

zhenning WROTE:One thing, you need switch debouncing :)


Will look on it. Thank you zhenning.
Abdullah
Newbie
 
Posts: 8
Joined: Wed Dec 05, 2012 1:23 pm

Previous

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 15 guests