Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Controlling DC motor using PIC18 - C codes

Status
Not open for further replies.

msab2006

Newbie level 4
Joined
Nov 21, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,337
I'm currently working on a project the uses an IR sensor to control the start and stop of a DC motor. The idea is to receive the input from the IR sensor through 1 port when the sensor sense an object, and output to start the motor through another port. I'm having trouble with the c codes. Can any of you help me with the codes from this? Need it asap. FYI, i'm using a PIC18F452. Thanks.
 

your ir sensor sent ON OFF signal?

The IR sensor is a SHARP GP2XX series. Basically i need to use the ADC of the microcontroller since the sensor reads the the distance in analog form. It's takes a continuous distance reading with a range between 10-80cm. But i just want it to trigger at a certain range, say about 50cm.

---------- Post added at 18:28 ---------- Previous post was at 18:27 ----------

The IR sensor is a SHARP GP2XX series. Basically i need to use the ADC of the microcontroller since the sensor reads the the distance in analog form. It's takes a continuous distance reading with a range between 10-80cm. But i just want it to trigger at a certain range, say about 50cm.

oh, and can find the link here: Sharp GP2D12 Infrared Sensor
 

Hi,
I'm assuming you've successfully received the signal from the sensor. Therefore, you now need to use PWM - pulse width modulation - to control the motor. It is very easy. Read the datasheet of the PIC and the reference manual. If you don't understand, then post again, and I'll try to post a sample code that may help you.

Take a look at this project for reference:
PWM DC Motor Controller

Hope this helps.
Tahmid.
 

Is the motor Brushless or Brushed? How about the power?

At the moment I've chosen a geared motor. It's a brush motor I suppose. The specs are here: http://www.cytron.com.my/usr_attachment/SPG30 Spec.pdf As for the power, I will be using 12v DC supply from an rechargeable sealed acid lead battery.

---------- Post added at 11:33 ---------- Previous post was at 11:24 ----------

Hi,
I'm assuming you've successfully received the signal from the sensor. Therefore, you now need to use PWM - pulse width modulation - to control the motor. It is very easy. Read the datasheet of the PIC and the reference manual. If you don't understand, then post again, and I'll try to post a sample code that may help you.

Take a look at this project for reference:
PWM DC Motor Controller

Hope this helps.
Tahmid.

Thanks. Just a question, since I need to use the PWM, where does the ADC part come in? Is it possible for you to post a simple code for me to refer to? Would be of great help.

---------- Post added at 11:39 ---------- Previous post was at 11:33 ----------

Would it be easier if I used the MPLAB IDE compiler as opposed to the MikroC compiler?
 

Would it be easier if I used the MPLAB IDE compiler as opposed to the MikroC compiler?
If you think so, but I think mikroC would be much easier.
Thanks. Just a question, since I need to use the PWM, where does the ADC part come in? Is it possible for you to post a simple code for me to refer to? Would be of great help.
You could send the signal to the controller through IR, so you wouldn't need ADC. But ADC is usually here to be used for reading a value off a pot and then adjusting the PWM duty cycle.
Code:
//Program to generate 25kHz output at RC2(CCP1) pin
//Microcontroller: Microchip PIC18452
//Language: C
//Compiler: mikroC v8.20
//Programmer: Tahmid

void main (void){
     TRISC = 0;
     PORTC = 0;
     ADCON1 = 7;
     T2CON = 0;
     TMR2 = 0;
     PWM1_Init(25000); //25kHz
     PWM1_Change_Duty(128); //50% duty cycle
// Choose Duty cycle as such:
// PWM_Change_Duty(x);
// x = ( (Duty Cycle in %) / 100) * 255
// This should be read from the IR signal
// This would control the speed of the motor
     PWM1_Start(); //Start PWM
     while (1){ //Loop forever
// Whatever else might be needed to be done while PWM is running
     }
}

If you just need to turn on/off the motor then you don't need this. PWM is needed only for variable duty cycle, ie, adjusting the motor speed. For simple on/off, you could use a port pin and a transistor/MOSFET and send 1 to the pin to turn motor on or 0 to turn motor off.

Hope this helps.
Tahmid.
 

If you think so, but I think mikroC would be much easier.

You could send the signal to the controller through IR, so you wouldn't need ADC. But ADC is usually here to be used for reading a value off a pot and then adjusting the PWM duty cycle.
Code:
//Program to generate 25kHz output at RC2(CCP1) pin
//Microcontroller: Microchip PIC18452
//Language: C
//Compiler: mikroC v8.20
//Programmer: Tahmid

void main (void){
     TRISC = 0;
     PORTC = 0;
     ADCON1 = 7;
     T2CON = 0;
     TMR2 = 0;
     PWM1_Init(25000); //25kHz
     PWM1_Change_Duty(128); //50% duty cycle
// Choose Duty cycle as such:
// PWM_Change_Duty(x);
// x = ( (Duty Cycle in %) / 100) * 255
// This should be read from the IR signal
// This would control the speed of the motor
     PWM1_Start(); //Start PWM
     while (1){ //Loop forever
// Whatever else might be needed to be done while PWM is running
     }
}

If you just need to turn on/off the motor then you don't need this. PWM is needed only for variable duty cycle, ie, adjusting the motor speed. For simple on/off, you could use a port pin and a transistor/MOSFET and send 1 to the pin to turn motor on or 0 to turn motor off.

Hope this helps.
Tahmid.

Hey, thanks. I'll try it out and see if it works.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top