Generate a PWM to start a DC Motor

Thread Starter

Helljoke

Joined Nov 2, 2017
13
Hello guys! :)
I need help to drive a DC Motor (by a DMOS Full-Bridge Motor Driver) with PIC18F4585.
I'm trying to generate a PWM signal like EN input for Motor Driver.
I don't know where i wrong... My object is only to start the motor.

Thank you so much in advance!

I'm using MPLAB X and my code is this:


C:
#include <stdio.h>
#include <stdlib.h>
#include "Config_Bits.h"


#define _XTAL_FREQ 40000000 // Set the frequency of quartz used by PIC

void main(void)
{
   CMCON = 7;                                    // Disable comparators (affect dip_switch reading)
    ADCON1= 0x0f;                                // Set PortA (5 and 3:0) as Digital Inputs
    LATC  = 0xff;                                // Cleanup output ports
    LATD  = 0x7f;               // It sets DOUT16=RD7=0 to reach V_OPT=5V and enable outputs
    TRISC = 0;                                    // Enable output ports (C+D)                                              
    TRISD = 0;                                  
    LATD  = 0xff;                                // ... HALT signal



      PR2 = 255;        /* Load period value */
      CCPR1L = 80;    /* load duty cycle value */
      T2CON = 0;        /* No pre-scalar, timer2 is off */
      CCP1CON = 0x0C;    /* Set PWM mode and no decimal for PWM */
      TMR2 = 0;        /* Clear Timer2 initially */
      TMR2ON = 1;        /* Timer ON for start counting*/

    while(1);
Moderators note : used code tags
 
Last edited by a moderator:

wayneh

Joined Sep 9, 2010
17,498
I don't know C but don't you need a closing "}"?

I also wonder how much actual time does your 255 call for? It may be so brief that nothing happens.

As I said, I don't understand the syntax of a C loop. But I don't see how your "while" statement gets is done.
 

Thread Starter

Helljoke

Joined Nov 2, 2017
13
I don't know C but don't you need a closing "}"?

I also wonder how much actual time does your 255 call for? It may be so brief that nothing happens.

As I said, I don't understand the syntax of a C loop. But I don't see how your "while" statement gets is done.
Thank you much wayneh. It's a good observation, but it's a writing mistake on thread.
The code is correct and I solved the problem few minutes ago. At the output of the PIC I had connected an optocoupler between RC2 and a BJT and this did not allow me to see the PWM output, so disconnecting this I solved.
Anyway thanks again for your answer.
Goodbye
 
Top