Blog Entry




H-Bridge Microchip PIC Microcontroller PWM Motor Controller

January 26, 2009 by , under Microcontroller.




One of the advantages using the Microchip PIC microcontroller Pulse Width Modulation or PWM for short is; this PWM peripheral circuit is designed to control the DC motor using the full bridge mode PWM feature. The PWM peripheral works by supplying the correct signal to the H-Bridge DC motor circuit such as speed controlling and changing the DC motor direction. Therefore on this tutorial we will learn to use this sophisticated feature offered by Microchip PIC PWM. For those with the AVR microcontroller background this is also a good chance to learn the beauty of the different between AVR and PIC microcontroller especially in the PWM peripheral features.

PWM is used in many industrial mostly for controlling the motor speed. How this PWM signal could change the DC motor speed is showed on this following PWM signal timing diagram:

From the PWM timing diagram above we could see that by changing the pulse width we could change the average voltage receipt by the DC motor; the wider the pulse width; the higher the average voltage receipt by the DC motor. The shorter the pulse width, the lower the average voltage receipt by the DC motor. Therefore by varying the pulse width we could vary the DC motor speed. The ratio between the pulse width and the total length of the pulse (time on plus time off) is called duty cycle, so by saying 100% duty cycle means the DC motor is in it’s full speed and 10% duty cycle the DC motor is in it’s 10% of speed. We are going to use this following H-Bridge circuit schema on our project:

The circuit above basically is the H-Bridge transistor circuit which connected to the PIC 16F690 PWM pins through the PIC PWM output ports P1A, P1B, P1C and P1D. For more detail how the H-Bridge circuit works you could refer to the “Using Transistor as a Switch” posted on this blog.

The following is the list of hardware and software used in this tutorial:

1. PICJazz 16F690 learning board from ermicro (the PICJazz 16F690 schema)
2. One 5 to 6 volt DC Motor (in this project I’am using geared DC Motor)
3. Four 2K2 Ohm ¼ watt resistor for the H-Bridge circuit
4. Four BC639 transistors (or equivalent) for the H-Bridge circuit
5. Four 1N4148 diodes for the H-Bridge circuit
6. One 100nF (0.1uF) 16 volt for the H-Bridge circuit
7. JazzMate 2576-5V power board, the 5 volt switching power supply from ermicro for the H-Bridge circuit.
8. Microchip MPLAB IDE v8.0 or higher
9. HITEC PICC-Lite PICC-Lite Version 9.60PL1
10. Microchip PICKit2 Programmer

In this tutorial first we will learn how to use the basic single output PWM mode and later on we will use the full bridge PWM mode for controlling the H-Bridge motor circuit.

1. The PIC16F690 Microcontroller PWM peripheral

The heart of PIC16F690 PWM lays on the TIMER2 peripheral, this timer is used as the basic counter generator used by the PWM peripheral to generating the PWM pulse, the following is the PIC 16F690 simplify PWM peripheral diagram (for complete explanation please refer to the datasheet):

The TMR2 counter register clock is supplied by the prescale circuit which can be selected using the T2CKPS1 and T2CKPS0 bits in the T2CON register, the TMR2 register value is compared to the PR2 register which determine the TOP value of TMR2 counter register. When the TMR2 value is equal to the PR2 value, then the TMR2 counter register will be reset to 0.

At the same time the value of TMR2 counter register is compared to the CCPR1L register value (actually with the CCPR1H register value, but since the CCPR1H equal to the CCPR1L than we could just say CCPR1L), when the TMR2 value equal to the CCPR1L register value than the PWM peripheral circuit will reset the CCP1 output (logical “0“) and when the TMR2 counter register equal to the PR2 register value than it will set the CCP1 output (logical “1“). Therefore the PR2 register determine the PWM period, by changing the PR2 value we could change the PWM period and this mean changing the PWM frequency as well. The PWM period could be calculated using this following formula:

PWM period = (PR2 + 1) x 4 x Tosc x (TMR2 prescale value) second

Where Tosc is the system clock period in second

PWM frequency = 1 / PWM Period Hz

While the PWM pulse width is depend on the CCPR1L register value, therefore by varying the CCPR1L register value, we could change the PWM pulse width. The PWM width can be calculated using this following formula:

PWM width = (CCPR1L:CCP1CON<5:4>) x Tosc x (TMR2 prescale value) second

This following table shows the PWM frequency and resolution with the 8 MHz clock:

The PWM output behavior is controlled by the CCP1CON, PWM1CON and PSTRCON registers, we will talk about these three registers shortly, but the important think to remember that all the TRIS (three states input/output) register for each of the PWM output ports: P1A, P1B, P1C and P1D should be set to the output mode.

 

2. Single Output Mode PWM

With single output PWM mode we could sent the same PWM signal to each of available PWM pins (i.e. P1A, P1B, P1C and P1D) or to all of them at the same time. Our first C program example will show you how this single output PWM mode works. Ok let’s pasting this following code into your Microchip MPLAB IDE:

// ***************************************************************************
//  File Name    : pwm.c
//  Version      : 1.0
//  Description  : Pulse Width Modulation (PWM)
//                 Single Output, Steering Mode
//  Author(s)    : RWB
//  Target(s)    : PICJazz 16F690 Board
//  Compiler     : HITECT PICC-Lite Version 9.60PL1
//  IDE          : Microchip MPLAB IDE v8.00
//  Programmer   : PICKit2
//  Last Updated : 03 Jan 2009
// ***************************************************************************
#include <pic.h>
/*   PIC Configuration Bit:
**   INTIO     - Using Internal RC No Clock
**   WDTDIS    - Wacthdog Timer Disable
**   PWRTEN    - Power Up Timer Enable
**   MCLREN    - Master Clear Enable
**   UNPROTECT - Code Un-Protect
**   UNPROTECT - Data EEPROM Read Un-Protect
**   BORDIS    - Borwn Out Detect Disable
**   IESODIS   - Internal External Switch Over Mode Disable
**   FCMDIS    - Monitor Clock Fail Safe Disable
*/
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT \
  & UNPROTECT & BORDIS & IESODIS & FCMDIS);
// Using Internal Clock of 8 MHz
#define FOSC 8000000L
// Delay Function
#define	_delay_us(x) { unsigned char us; \
		       us = (x)/(12000000/FOSC)|1; \
		       while(--us != 0) continue; }
void _delay_ms(unsigned int ms)
{
  unsigned char i;
  if (ms == 0) return;
  do {
    i = 4;
    do {
      _delay_us(164);
    } while(--i);
  } while(--ms);
}
void main(void)
{
  unsigned int ipwm;
  unsigned char state;
  OSCCON=0x70;         // Select 8 MHz internal clock
  TRISC = 0x00;        // Set All on PORTC as Output
  ANSEL = 0x00;        // Set PORT AN0 to AN7 digital I/O
  ANSELH = 0x00;       // Set PORT AN8 to AN11 as Digital I/O
  PORTC = 0x00;        // Turn Off all PORTC
  /* Init PWM for Single Output */
  CCP1CON=0b00001100;  // Single PWM mode; P1A, P1C active-high; P1B, P1D active-high
  CCPR1L=0;            // Start with zero Duty Cycle
  T2CON=0b00000101;    // Postscale: 1:1, Timer2=On, Prescale = 1:4
  PR2=0x65;            // Frequency: 4.90 kHz
  TMR2=0;              // Start with zero Counter

  PSTRCON=0b00000100;  // Enable Pulse Steering on P1C (RC3)
  state=0;             // Start with state 1
  for(;;) {
    ipwm=0;
    while (ipwm < 255) {
      CCPR1L=++ipwm;
      _delay_ms(5);     // Delay 5 millisecond
    } 	                    

    ipwm=0xff;
    while (ipwm > 0) {
      CCPR1L=--ipwm;
      _delay_ms(5);     // Delay 5 millisecond
    }       

     _delay_ms(100);    // Delay 100 millisecond
    if (state == 0) {
      state=1;
      PSTRCON=0b00001000;  // Enable Pulse Steering on P1D (RC2)
    } else if (state == 1) {
      state=2;
      PSTRCON=0b00001100;  // Enable Pulse Steering on P1C and P1D (RC3 and RC2)
    } else {
      state=0;
      PSTRCON=0b00000100;  // Enable Pulse Steering on P1C (RC3)
    }
  }
}
/* EOF: pwm.c */

This C program basically will use only P1C and P1D PWM output ports, since these ports are attached to the LEDs on the PICJazz 16F690 board. The program will use the PWM pulse steering mode to make these two LEDs to slowly turn to bright and slowly turn to dark by changing the PWM duty cycle.

 

2.1. CCP1CON: Enhance CCP1 Control Register

This register is use to select the PWM output configuration bit and PWM mode.

By setting P1M1=0 and P1M0=0 bits in the CCP1CON register we select the single output PWM; setting the CCP1M3=1, CCP1M2=1, CCP1M1=0 and CCP1M0=0 in the CCP1CON register we select the PWM mode with P1A, P1C active-high; P1B, P1D active-high as this following code:

/* Init PWM for Single Output */
CCP1CON=0b00001100;  // Single PWM mode; P1A, P1C active-high; P1B, P1D active-high
CCPR1L=0;            // Start with zero Duty Cycle

In this tutorial we just set the additional 2 LSB extended bits (DC1B1 and DC1B0) to all zero (logical “0“) for the CCPR1L register (10-bit wide). We start by setting the CCPR1L to zero mean we start with zero duty cycle (no PWM output). The next step is to configure the TIMER2 control register:

This register is used to set the postscale, activate the TIMER2 peripheral and set the prescale clock used by the TMR2 counter register. BY setting the T2CKPS1=0 and T2CKPS0=1 in the T2CON register we select the 1:4 prescale; and by setting the TMR2ON to logical “1” we activate the TIMER2 peripheral.

T2CON=0b00000101;    // Postscale: 1:1, Timer2=On, Prescale = 1:4
PR2=0x65;            // Frequency: 4.90 kHz
TMR2=0;              // Start with zero Counter

On the PWM mode, we are not using the postscaller output therefore we just set all the postscale bits (TOUTPS3, TOUTPS2, TOUTPS1 and TOUTPS0) to all zero. Setting the PR2=0×65 (or 101 decimal) with the 1:4 prescale and system clock of 8 MHz, then we could calculate the PWM output frequency according to the formula above as follow:

PWM period = (PR2 + 1) x 4 x Tosc x (TMR2 prescale value) second

PWM period = (101 + 1) x 4 x (1 / 8000000) x 4 = 0.000204 second
PWM frequency = 1 / PWM period = 1 / 0.000204 = 4901.96 Hz = 4.90 kHz


2.2. PSTRCON: Pulse Steering Control Register

In the single output PWM mode this register is use to control where the PWM ports to be used as the PWM output signal.

By activating the desire steering bit in the PSTRCON register, we could control the PWM output signal whether to use just one port or simultaneously use all the available PWM output ports. On the example program above we use the program state to change these bits. First we activate only the P1C (RC3) port:

PSTRCON=0b00000100;  // Enable Pulse Steering on P1C (RC3)
state=0;             // Start with state 1

Then inside the endless loop we change these bits according to the state status as this following code:

if (state == 0) {
  state=1;
  PSTRCON=0b00001000;  // Enable Pulse Steering on P1D (RC2)
} else if (state == 1) {
  state=2;
  PSTRCON=0b00001100;  // Enable Pulse Steering on P1C and P1D (RC3 and RC2)
} else {
  state=0;
  PSTRCON=0b00000100;  // Enable Pulse Steering on P1C (RC3)
}

The steering sync bit (STRSYNC) in the PSTRCON register is set to zero, means the output steering update occur at the beginning of the instruction cycle.

2.3. Inside the C Program

The C program begins by selecting the 8 MHz internal clock and setting all the I/O ports used on this examples. After doing the PWM and TIMER2 peripherals setup, we go the endless loop (the for(;;) C statement with no argument); inside this loop we use the CCPR1L register to change the PWM duty cycle by assigning the value from 0 to 255 for turning the LEDs slowly from dark to bright and assigning the CCPR1L register value from 255 to 0 for turning the LEDs from bright to dark.

ipwm=0;
while (ipwm < 255) {
  CCPR1L=++ipwm;
  _delay_ms(5);     // Delay 5 millisecond
} 	                    

ipwm=0xff;
while (ipwm > 0) {
  CCPR1L=--ipwm;
  _delay_ms(5);     // Delay 5 millisecond
}

2.4. Downloading the Code

After compiling and simulating your code hook up your PICKit2 programmer to the PICJazz 16F690 board ICSP port turn the PICJazz 16F690 power. From the MPLAB IDE menu select Programmer -> Select Programmer -> Pickit2 it will automatically configure the connection and display it on the PICkit2 tab Output windows:

Now you are ready to down load the code from MPLAB IDE menu select Programmer -> Program; this will down load the HEX code into the PICJazz 16F690 board:

H-Bridge Microchip PIC Microcontroller PWM Motor Controller

3. Full Bridge Mode PWM

After learning the PWM principal by using the single output mode, now we come to the best part; the ability to easily control the H-Bridge DC motor circuit is what makes the PIC microcontrollers unique among the other microcontroller’s types. Ok let’s take a look at the following C program code:

// ***************************************************************************
//  File Name    : pwm2.c
//  Version      : 1.0
//  Description  : Pulse Width Modulation (PWM)
//                 H-Bridge DC Motor Controller
//  Author(s)    : RWB
//  Target(s)    : PICJazz 16F690 Board
//  Compiler     : HITECT PICC-Lite Version 9.60PL1
//  IDE          : Microchip MPLAB IDE v8.00
//  Programmer   : PICKit2
//  Last Updated : 03 Jan 2009
// ***************************************************************************
#include <pic.h>
/*   PIC Configuration Bit:
**   INTIO     - Using Internal RC No Clock
**   WDTDIS    - Wacthdog Timer Disable
**   PWRTEN    - Power Up Timer Enable
**   MCLREN    - Master Clear Enable
**   UNPROTECT - Code Un-Protect
**   UNPROTECT - Data EEPROM Read Un-Protect
**   BORDIS    - Borwn Out Detect Disable
**   IESODIS   - Internal External Switch Over Mode Disable
**   FCMDIS    - Monitor Clock Fail Safe Disable
*/
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT \
  & UNPROTECT & BORDIS & IESODIS & FCMDIS);
// Using Internal Clock of 8 MHz
#define FOSC 8000000L
// Delay Function
#define	_delay_us(x) { unsigned char us; \
		       us = (x)/(12000000/FOSC)|1; \
		       while(--us != 0) continue; }
void _delay_ms(unsigned int ms)
{
  unsigned char i;

  if (ms == 0) return;
  do {
    i = 4;
    do {
      _delay_us(164);
    } while(--i);
  } while(--ms);
}
void main(void)
{
  unsigned int ipwm;
  unsigned char direction;
  OSCCON=0x70;         // Select 8 Mhz internal clock
  TRISC = 0x00;        // Set All on PORTC as Output
  TRISA = 0x03;        // Input for RA0 and RA1
  ANSEL = 0x01;        // Set PORT AN0 to analog input AN1 to AN7 digital I/O
  ANSELH = 0x00;       // Set PORT AN8 to AN11 as Digital I/O
  PORTC = 0x00;        // Turn Off all PORTC
  /* Init PWM for Full Bridge Output */
  CCP1CON=0b01001100;  // Full Bridge Forward; P1A, P1C active-high; P1B, P1D active-high
  CCPR1L=0;            // Start with zero Duty Cycle
  T2CON=0b00000101;    // Postscale: 1:1, Timer2=On, Prescale = 1:4
  PR2=0x65;            // Frequency: 4.90 KHz
  TMR2=0;              // Start with zero Counter
  /* Init ADC */
  ADCON0=0b00000000;   // Select Left justify result. ADC port channel 0
  ADCON1=0b00110000;   // Select the FRC for 8 MHz
  ADON=1;	        // turn on the ADC conversion module
  direction=0;         // Start with Forward Direction
  ipwm=0;
  for(;;) {
    if (RA1 == 0) {           // Change the Motor Direction when pressed
     _delay_ms(1);
     if (RA1 == 0) {          // Read again for simple debounce
       if (direction == 0) {
         direction=1;         // Reverse direction
         P1M1=1;
         P1M0=1;
       } else {
         direction=0;         // Forward direction
         P1M1=0;
         P1M0=1;
       }
     }
    }
    GODONE=1;	              // initiate conversion on the channel 0
    while(GODONE) continue;  // Wait conversion done

    ipwm = ADRESH;           // Get the highest 8 bit MSB result, ignore the 2 bit LSB
    CCPR1L=ipwm;             // Set the Duty Cycle base on the ADC result
    /* Blink the LED attached on RC0 */
    RC0=1;                   // Turn On
    _delay_ms(ipwm);
    RC0=0;                   // Turn Off
    _delay_ms(ipwm);
  }
}
/* EOF: pwm2.c */

The basic PWM setup routines is almost the same with our previous program, but in this program I used the PIC 16F690 ADC peripheral for adjusting the DC motor speed; the motor speed is depend on the voltage value read by the PIC 16F690 ADC peripheral from the PICJazz 16F690 board user’s trimpot that works as the voltage divider circuit. This value will be assigned to the CCPR1L duty cycle register.

I also use the PICJazz 16F690 user’s switch that works as a toggle switch to change the DC motor direction; and to make it more interesting, I use the LED attached to the RC0 port as the program life beacon; this LED will blink according to the value we assigned to the CCPR1L register.

3.1. Full Bridge PWM Mode Setup

The full bridge PWM mode is selected by setting the P1M1 and P1M0 bits in the CCP1CON register, there are 2 available modes for the full bridge PWM mode; the first one is called full bridge output forward, this can be selected by setting the P1M1=0 and P1M0 = 1 in the CCP1CON register. In this mode the P1D pin will be the modulated port; P1A port will active; while P1B, P1C will inactive:

On the forward full bridge PWM mode the TR1 transistor will on saturate and the TR4 transistor will on/off base on the PWM signal supplied by P1D port, while the TR2 and TR3 transistors inactive. The current will flow from TR1 transistor through DC Motor and go to the TR4 transistor, this make the DC motor to turn on the forward direction.

The second full bridge PWM mode is the reverse full bridge PWM mode, this mode can be selected by setting the P1M1=1 and P1M0 = 1 in the CCP1CON register. In this mode the P1B pin will be the modulated port; P1C port will active; while P1A, P1D will inactive:

On the reverse full bridge PWM mode the TR3 transistor will on saturate and the TR2 transistor will on/off base on the PWM signal supplied by P1B port, while the TR1 and TR4 transistors inactive. The current will flow from TR3 transistor through DC Motor and go to the TR2 transistor, this make the DC motor to turn on the reverse direction.

The following is the C program code for the PWM setup we use in this project:

/* Init PWM for Full Bridge Output */
CCP1CON=0b01001100;  // Full Bridge Forward; P1A, P1C active-high; P1B, P1D active-high
CCPR1L=0;            // Start with zero Duty Cycle
T2CON=0b00000101;    // Postscale: 1:1, Timer2=On, Prescale = 1:4
PR2=0x65;            // Frequency: 4.90 KHz
TMR2=0;              // Start with zero Counter

The other register that I mention before is the PWM1CON register (enhanced PWM control register). This register is used in half bridge PWM mode to put a time delay to the actual PWM signal transition from zero to active; we are not using this feature on the full bridge PWM mode.

3.2. The PIC 16F690 ADC Peripheral Setup

The ADC peripheral is used to read the voltage value on the analog input port RA0, this voltage value is supplied by the user’s trimpot on the PICJazz 16F690 board. Adjusting the trimpot means changing the voltage value as well; by converting this voltage value to the digital equivalent value and assign this value to the CCPR1L (duty cycle) register, we could change the PWM duty cycle and this will effecting the DC motor speed.

The complete PIC microcontroller ADC setup explanation could be read on the article “PIC Analog to Digital Converter C Programming” posted on this blog. The following is the C program code for setting the PIC 16F690 ADC peripheral:

/* Init ADC */
ADCON0=0b00000000;   // Select Left justify result. ADC port channel 0
ADCON1=0b00110000;   // Select the FRC for 8 MHz
ADON=1;	             // Turn on the ADC conversion module

By setting the ADFM bit to logical “0” in the ADCON0 register we use the “left justified” result. This mean the higher 8 bits ADC result value will be placed in the ADRESH register and the lower 2 bits ADC result value in the ADRESL register, because we just need the 8 bit value for the CCPR1L register, so the lowest 2 bits value in ADRESL register can be ignored:

ipwm = ADRESH;           // Get the highest 8 bit MSB result, ignore the 2 bit LSB
CCPR1L=ipwm;             // Set the Duty Cycle base on the ADC result

3.3. Inside the C Program

The C program begins by selecting the 8 MHz internal clock and setting all the I/O ports used on this examples. After doing the PWM, TIMER2 and ADC peripherals setup, we go the endless loop (the for(;;) C statement with no argument); inside this loop first we read the user’s switch, this switch is attached to the input port RA1 and work as the toggle switch to change the DC motor direction:

if (RA1 == 0) {            // Change the Motor Direction when pressed
  _delay_ms(1);
  if (RA1 == 0) {          // Read again for simple debounce
    if (direction == 0) {
      direction=1;         // Reverse direction
      P1M1=1;
      P1M0=1;
    } else {
      direction=0;         // Forward direction
      P1M1=0;
      P1M0=1;
    }
  }
}

By setting the P1M1=1 and P1M0=1 bits in the CCP1CON register, we select the reverse full bridge PWM mode; setting the P1M1=0 and P1M0=1 bits in the CCP1CON register, we select the forward full bridge PWM mode.

The PIC ADC peripheral than read the analog input channel 0 (RA0) port and assigned the value to the CCPR1L register and as the _delay_ms() function argument to the life beacon LED attached to the output port RC0.

GODONE=1;	         // initiate conversion on the channel 0
while(GODONE) continue;  // Wait conversion done

ipwm = ADRESH;           // Get the highest 8 bit MSB result, ignore the 2 bit LSB
CCPR1L=ipwm;             // Set the Duty Cycle base on the ADC result
/* Blink the LED attached on RC0 */
RC0=1;                   // Turn On
_delay_ms(ipwm);
RC0=0;                   // Turn Off
_delay_ms(ipwm);

3.4. Downloading the Code

After compiling and simulating your code hook up your PICKit2 programmer to the PICJazz 16F690 board ICSP port turn the PICJazz 16F690 power. From the MPLAB IDE menu select Programmer -> Select Programmer -> Pickit2 it will automatically configure the connection and display it on the PICkit2 tab Output windows:

Now you are ready to down load the code from MPLAB IDE menu select Programmer -> Program; this will down load the HEX code into the PICJazz 16F690 board:

After all the hard work, now it’s time to watch to your PIC microcontroller controlling the H-Bridge DC motor circuit:

Bookmarks and Share


bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark




105 Responses to “H-Bridge Microchip PIC Microcontroller PWM Motor Controller”

12.01.10#1

Comment by newbie999.

for the single pwm out

i got the hex file
but RC2 and RC3 leds did not light up or blink
i wonder why

Thanks

12.01.10#2

Comment by rwb.

Make sure you check your circuit the RC2 and RC3 LED connection and then use simple debugging C code to turn both LED on.

12.01.10#3

Comment by newbie999.

rwb,
thanks. it finally worked.

12.01.10#4

Comment by newbie999.

pal, for the single pwm out

if i want to make PIA and PIB as pwm out
for two external leds

what part of the codes should I change?

Thanks

12.01.10#5

Comment by rwb.

Just set the STRA and STRB bits on the PSTRCON register, see the blog detail above of how to use the PSTRCON register as the PWM channel selector.

13.01.10#6

Comment by newbie999.

Thanks a lot.

I got it.

27.01.10#7

Comment by EricTmz.

Hey,
I’m using pic18 micro-controller would I be able to use the same code or there will be major difference?
Also I’m making r/c car..I decided to use l293d H-bridge…would that work in similarly or should I just follow your circuit and make H-bridge…any help would be appreciated…thanks

27.01.10#8

Comment by rwb.

If you use the Microchip PIC18 microcontroller families there will be a slightly different code especially in the configuration bit section the other is almost the same. You could refer to my article “PIC18 Pulse Width Modulation (PWM) DC Motor Speed Controller with the RPM Counter Project” (http://www.ermicro.com/blog/?p=1461). For the L293D H-Bridge chip you could refer to my article “Building BRAM your first Autonomous Mobile Robot using Microchip PIC Microcontroller – Part 1″ (http://www.ermicro.com/blog/?p=983) and Behavior Based Artificial Intelligent Mobile Robot with Sharp GP2D120 Distance Measuring Sensor – BRAM Part 2 (http://www.ermicro.com/blog/?p=1016).

27.01.10#9

Comment by EricTmz.

Thanks very much for your response.. I’ll update you how things worked out..

22.02.10#10

Comment by shree.

Hiii, I am new here and thank you for the nice work above. I am trying to use 16F690 to generate 14uS pulse + 2uS deadband on P1A and P1B alternatively (Half Bridge mode! Total Half bridge freq 31.25KHz in push pull mode) Any help of yours on how the code would look like would be highly appriciated. I am really stuck with this.
Thanks in advance
Shree
shreevs@gmail.com

23.02.10#11

Comment by rwb.

For calculating the PR2 with 31.25kHz or 32us PWM period use this formula:

PWM period = (PR2 + 1) x 4 x Tosc x (TMR2 prescale value) second.

If we used 8 mHz clock, TMR2 prescale 1:1 and PR2 = 63

PWM Period = (63 + 1) x 4 x 0.000000125 = 32 us

For the 4us pulse, means the duty cycle will be 4/32 = 12.5 %, therefore you could set the CCPR1L register to 32

For the 2us deadband delay use the lower 7 bits of PWM1CON register (PDC0 to PDC6) to set the number of 4 x Tosc (PIC16F690 instruction cycle)for the deadband delay. This mean with 8 mHz these bits value will be:

deadband delay = n x 4 x Tosc = n x 0.0000005 second

Therefore for 2us deadband delay; n = 4.

For other clock frequency you could use the same principal to calculate all the required value.

23.02.10#12

Comment by shree.

Thanks for that! A code snippet is here. please let me know if thats ok/enough. BTW its written in boostC.
#include
#pragma CLOCK_FREQ 8000000
#pragma DATA _CONFIG, _WDT_OFF & _INTOSCIO & _CP_OFF & _PWRTE_ON & _IESO_OFF & _FCMEN_OFF & _CPD_OFF & _MCLRE_OFF & _BOR_ON

void main()
{
//Setting up internal oscillator of 8MHz
osccon.IRCF2=1;
osccon.IRCF1=1;
osccon.IRCF0=1;
osccon.OSTS=0;
osccon.SCS=0;

trisc=0;
portc=0;
pr2=63;
ccpr1l=31;
ccp1con=0x8c;
pwm1con=0×84;
t2con=4;

while(1){
}
}

23.02.10#13

Comment by rwb.

It seems your code is Ok.

23.03.10#14

Comment by EricTmz.

Comment by EricTmz.

Hey, I’m working on project and I need bit help with it.
I am getting value between 0 to 5 and I need to set the PWM signal according to it so the motor can run at respective speed. I am getting value OK, the motor is running OK with H-bridge…following is the code for PWM..would you verify if it make sense!!
Need 2 pwm output control 2 dc-motors(for steering and speed of RC), ..just look at one case for now.. I have calculated the period, duty cycle value properly, that should be OK
———————————————–
TRISC=0b11111001; // Set C1 and C2 as PWM output.
LATC= 0×00; // Initialize the PORTC.

OpenPWM1(159);
//inside some where..after directions working OK
//for speed
if(z > 0 & z 2 & z < 4)
{
SetDCPWM1(360); //Medium
}
else
{
SetDCPWM1(640); //Fast 100%duty cycle
}

//at the end
ClosePWM1();
//similar code for PWM2 as well
————————————————-

I am wondering if this code will work OK?
let me know if you need more info..
thanks very much..

Please remove above post, it has mistake in code..
–Eric

23.03.10#15

Comment by rwb.

What kind of PIC C compiler that you use? I also assume that you use the z variable to control the DC motor speed (z > 0 and z < 4) and OpenPWM1(), SetDCPWM1() and ClosePWM1() functions is used to initialized, set and close the PWM. I think the C code flow is seems Ok.

24.03.10#16

Comment by EricTmz.

I am using MPlab, programing in c18 with pickit2 on pic18 board. Yes, z is used to control the speed. Thanks I’ll run it today and let you know if any problem shows up…thanks
-Eric

29.03.10#17

Comment by lawrence.

I am using the pic16f877a and MPlab with a bootloader connected via rs232 , so i need clarity on a few things:
1. can I use the code on the 16f877 ( same family assumption) of course using a hitech complier.
2.I am only used to assembly language i am not clued up with c but if you have the in assembly (*.asm ) i would be a happy man.
3. I see you have two code so do you put both of them under the source code on mplab or is there another method.

lastly I must say a student studying micro controllers this is great stuff keep it up

29.03.10#18

Comment by rwb.

Yes you could use the code with some changes because the PIC16F877 don’t have the internal oscillator and not support the enhanced PWM mode such as the PIC16F699. You could use newer PIC microcontroller such as PIC16F887 (replacement for PIC16F877) where it has the internal oscillator and enhanced PWM mode.

The C and assembler use similar principle when we directly manipulate the PIC register to program the PWM and ADC peripherals. Therefore if you use PIC assembler, you could use similar approach as described on this blog.

The two C code (pwm.c and pwm2.c) are used in separate project, so I could clearly demonstrate the PIC16F699 enhanced PWM mode features (i.e. steering mode and full H-Bridge mode).

13.05.10#19

Comment by tanv3er.

hello iam doing a project for which i need to generate two pwm pulses with a dead band iam stucked up to get two pwm pulses can u please go through the code and help me out whats the mistakes iam doing sir

#include
void pwm_int()
{
PR2=249;
CCPR1L=100;
CCP1CON=0×8d;
ECCPAS=0×00;
PWM1CON=0×00;
PSTRCON=0×03;
}
void main()
{
TRISC=0×00;
TRISD=0×00;
pwm_init();
T2CON=0×04;
while(1);
}

iam using mplabide v8.43 compiler and pic16f887 microcontroller any help done is very much thankfull

sorry some changes so reposted

13.05.10#20

Comment by rwb.

The PIC16F887 only supported one PWM using TIMER2 peripheral, therefore you could not generate two independent PWM. The alternative way is to generate the PWM with other TIMER peripheral. You could read more about this on my posted blog “Building your own Simple Laser Projector using the Microchip PIC12F683 Microcontroller” http://www.ermicro.com/blog/?p=1622

16.05.10#21

Comment by tanv3er.

it is clearly mentioned in the data sheets that in enhanced pwm we can generate upto four pwm in pia pib pic and pid which refers to portc2 portd5 portd6 and portd7 respectively …. can u help me

16.05.10#22

Comment by rwb.

Yes in the “Single Output Mode PWM” part in this blog I give the example of how this four PWM output work using the Pulse Steering Control Register PSTRCON (please read it!); But all of the PWM signals are come from single PWM peripheral base on the TIMER2. Therefore you could not have two independent PWM signal with two separate duty cycle from one PWM generator peripheral. The alternative way is to use two TIMER peripheral to generate it as shown on my posted blog “Building your own Simple Laser Projector using the Microchip PIC12F683 Microcontroller”

19.06.10#23

Comment by din.gulu.er.

Hi rwb,

I am new to your blog, its realy ausome can you please suggest what should be the right frequency PWM .

We can vary the duty cycle. But how to chose the right frequency for the PWM or what is the right value for PWM frequency ?

Your early reply will be appreciable.

BR,

ABC

19.06.10#24

Comment by rwb.

There are no right values for the PWM frequency, each motor has their own characteristic, you need to experiment with your own DC motor; but you could start with 1kHz to 5 kHz PWM frequency range because most DC motor will work on this range. For high efficient DC motor you could experiment with the frequency above the human hearing of 20kHz.

09.07.10#25

Comment by kansairobot.

Hi
First of all I want to say your blog is great! I am having troubles understanding PWM and although I use C18 lite, this post has helped me a lot. Today I am going to try it.

I have several questions though:

1) I think you are using a 5V motor. What happens if I want to use a 12V motor (PICs only output 5V right?) how can I do this?

2) I am using a p18f2550. I understand you used 8MHz as internal clock. do you know what is the default freq? I am assuming 1MHz…

that is it for now. Thank you in advance for your reply and thanks for all the work

kansai

09.07.10#26

Comment by rwb.

1) Yes you could use 12 volt, that’s why you need the interface circuit such as transistor in order to drive 12 volt load. You could read more about microcontroller interfacing in my previous posted blog Using Transistor as a Switch

2) From the PIC18F2550 datasheet, the internal oscillator is 8 MHz (OSCCON<6:4> = 111) with prescaller you could get as low as 31 kHz (OSCCON<6:4> = 000)

21.07.10#27

Comment by kansairobot.

(From the other post: I thought it is more appropriate here)

>When using the Toshiba TA7291S bridge all you need is to supply the >correct standard logic voltage to the IN1 and IN2 input pins; the >integrated circuit inside TA7291S will make sure you get the saturate >transistors condition on its output (average 0.9 volt). The input current >on the IN1 and IN2 pins is very low (about 3 to 10 uA with Vin = 3.5 >volt).

The current is 3 to 10 uA???? does that mean that if i use the TA7291S I should put resistors between the iputs and the output of the PIC???

Also, in this post you showed about PWM being applied to one transistor(P1B or P1D) in the bridge and high in the other. (P1A or P1C). In case of using the TA7291S, how does this work??? We don’t directly control the transistors, do we?

Thank you always for your useful replies. I can’t believe how much I have learned from the tutorials

Kansai

21.07.10#28

Comment by rwb.

As I said before as long as you supply the appropriate “standard logic” voltage (HIGH or LOW) to the IN1 and IN2 input pins the TA7291S will work just find. The 3 to 10 uA is the sink current when we supply the HIGH logic (i.e. 3.5 volt) to the IN1 and IN2 input pins according to the datasheet (electrical characteristic – page 4); this mean you don’t have to put any resistor as long as you use “standard logic” voltage to the input pins (i.e. 0 to 5 volt).

For the TA7291S you should refer to the datasheet (the function table – page 3), for example when you apply HIGH on IN1 and LOW on IN2 then the OUT1 will be logical HIGH and OUT2 will be logical LOW, this mean the current will flow from OUT1 -> DC MOTOR -> OUT2 and vice versa.

You could read similar principal about using the H-Bridge Integrated Circuit on these following posted blog:

Building BRAM your first Autonomous Mobile Robot using Microchip PIC Microcontroller – Part 1

Behavior Based Artificial Intelligent Mobile Robot with Sharp GP2D120 Distance Measuring Sensor – BRAM Part 2

Build Your Own Microcontroller Based PID Control Line Follower Robot (LFR) – Second Part

02.12.10#29

Comment by Dakota.

hi i was wondering if you had a schematic for this whole project because i have standard pic kit 2 demo board and i wanted to put this whole project on a bread board including the pic16 chip. i tried to look at the picjazz board schematic and i couldn’t find out what pins the potentiometer and push button are connected to. if could point me in the right direction or post a schematic it would be greatly appreciated.

-Dakota

02.12.10#30

Comment by rwb.

The PICjazz board schematic could be found on the link on the list of hardware and software used in this project (http://www.ermicro.com/blog/?p=15). The user push button switch (S2) is connected to RA1 port through input enable jumper 2 (IENB2 – J4) and the trimport (VR) is connected to RA0 port through input enable jumper 1 (IENB1 – J5). For more information about the PICJazz 20PIN Learning and Development board, you could read this article at the link address shown above.

24.03.11#31

Comment by pichaha.

Hi, I am now trying to use mcu to control dc motor…
I have try to use a 2n2222 npn transistor to interface between my mcu and dc motor…the motor is able to rotate smoothly wheN i

24.03.11#32

Comment by pichaha.

sorry i haven’t finish yet…the motor is able to rotate smoothly when I use a 12V dc motor…but when i change the motor to a 3V tamiya car dc motor…the motor is not moving anymore, can I know what is the problem???
The voltage coming out from my mcu pin is around 5V.

24.03.11#33

Comment by rwb.

Try to connect the base terminal of the 2N2222 transistor with 1K resistor directly to Vcc (disconnect it from the Microcontroller output pin) and check whether the DC motor is working. If it not then you should recheck your circuit! Remember the Tamiya racing car DC motor usually consume more than 1A (especially when it stall) and it could easily exceed the 2N2222 maximum Collector current (Ic) specification.

27.03.11#34

Comment by pichaha.

Hi,
I have a question here…
If I am using a pair of 9V battery to drive my racing car actuated by two tamiya dc motor, how long can my supply maintain constant voltage without dropping too much??
Can it stand for around 5 minutes??
Thanks a lot…

27.03.11#35

Comment by rwb.

A typical 9 Volt battery (also called PP3) has 500 to 600 mA-h. This mean if your DC motor draws about 100 mA then theoretically the PP3 battery will operate about 5 hours but in practical it will be much less than that.

30.03.11#36

Comment by pichaha.

Hi,
I would like to ask, what type of battery did you use for your line following robot???
I have tried to use 9V battery to drive my racing car(there are two tamiya dc motors)and it consumed my battery very very fast…
Can you recommend any battery which is suitable for this setup???
Thanks a lot…

30.03.11#37

Comment by rwb.

I used 3xAA Alkaline battery for all of my line follower robot presented in this blog. You could try to use 6xAA battery for driving your motor.

01.04.11#38

Comment by pichaha.

Hi,
did you ever use L293d motor driver before??

Actually, I am using a 9V battery to power up my mcu, ir sensor and motor driver,

And I use another four AA 1.5V batteries and connect the batteries to pin 8 of the motor driver to drive the motors, is it ok??

Actually, now I have a +3.7V, 3700mAh lipo battery, I am thinking of using this to substitute the AA batteries, but to drive the motor driver and motor I have to share the same common(ground) for the two power supplies, I am afraid it is not appropriate to share the same common between two different types of power supply…can you please give some suggestion thanks a lot…

01.04.11#39

Comment by rwb.

Yes I used L293D motor driver in these following projects “Build Your Own Microcontroller Based PID Control Line Follower Robot (LFR) – Second Part” and “Building BRAM your first Autonomous Mobile Robot using Microchip PIC Microcontroller – Part 1“. Yes you could share the common ground with two or more power supplies but you need to drive the L293D motor drive with a minimum 4.5 volt in order to make it work properly (i.e. within the specification).

01.04.11#40

Comment by pichaha.

How about pin number eight?? Can I connect 4 AA 1.5V batteries to pin number eight to supply power to the motor??
Or instead of using AA batteries, can I use 3.7V lipo battery connect to pin 8, but I think maybe cannot since the required voltage in pin 8 is between 4.5V to 36V.

01.04.11#41

Comment by rwb.

Yes you could connect L293D pin 8 with 4 x AA (total 6 volt) to drive the DC motor.

07.05.11#42

Comment by p4n4k4ts.

Hi!!!
I am wondering how can we control 2 full H-bridges with the pic 16f690? I mean is it possible to use the 4 pwm channels (RC2, RC3, RC4, RC5) to modulate the low-side transistors and 4 other pins to to supply low or high signal to the up-side transistors?

Thanks for your time and your effort to help us!!!
Regards

07.05.11#43

Comment by rwb.

In order to control two full H-Bridge motors, you need two independent PWM peripheral for each H-Bridges. Unfortunately the Microchip PIC16F690 microcontroller only have one PWM peripheral known as ECCP (Enhanced Capture/Compare/PWM) module. The ECCP use the 16-bit TIMER1 to generate the PWM signal designed for driving the full H-Bridges motor. You could read these following articles of how to drive two full H-Bridges motors with PIC16F690 microcontroller:

Building BRAM your first Autonomous Mobile Robot using Microchip PIC Microcontroller – Part 1

Behavior Based Artificial Intelligent Mobile Robot with Sharp GP2D120 Distance Measuring Sensor – BRAM Part 2

You could also read the following article of how to generate a software based PWM signal:

Building your own Simple Laser Projector using the Microchip PIC12F683 Microcontroller

08.05.11#44

Comment by p4n4k4ts.

If I try to use the single PWM output (P1M1=0, P1M0=0) , will I be able to control the P1A, P1B, P1C, P1D separately?

ps. I didn’t have time to read the links you proposed above, which I am going to read right now!

Thanks again

08.05.11#45

Comment by rwb.

Because you only have one source PWM module on Microchip PIC16F690 microcontroller, therefore even though you use a single output PWM mode (P1M1=0 and P1M0=0) and activate all the PWM output ports (i.e. P1A, P1B, P1C, and P1D) through pulse steering control register (PSTRCON); all the ports will always give you the same PWM duty cycle output.

19.05.11#46

Comment by kicsikg.

I have a little problem with this. :( I could use some help. When I put the 12V on my breadboard the DC motor starts to circle and I even can slow it down, but when I try to give it some speed it doesn’t work anymore, and the reset switch doesn’t work anyway… Any ideas?

20.05.11#47

Comment by rwb.

There are many factors that make your circuit didn’t work as expected, you need to re-check your circuit and debug it one by one i.e. digital switch reading, analog trimpot reading, and digital PWM output.

06.09.11#48

Comment by chandra.

Hi,
I assembled the project.It is working fine.But I dont want the single press switch I need to use single pole 2way switch for the forwd and revesrse purpose.When the RA1 is 0 means revesr direction and RA1 is at 1 means +5v forwd direction.
I tried to debug the code..But not worked..
How can u tell me the code changes.
Thanks a lot.

06.09.11#49

Comment by rwb.

You just need to check if the RA1=0, then the motor direction is reverse (P1M1=1 and P1M0=1) and when the RA1=1, then the motor direction is forward (P1M1=0 and P1M0=1).

10.09.11#50

Comment by chandra.

Thanks for your kind reply
Ok dear……
But I need stop position also at start up,even if the control is turned on.Any possiblities with software.
Thanks for your kind reply

10.09.11#51

Comment by rwb.

Yes, just use state machine algorithm; on the first power up the state is 0 and the DC motor is Off. any change in the RA1 input condition (RA1=0 or RA1=1) will change the state to 1, which mean the DC motor is On.

29.09.11#52

Comment by pichaha.

Hi rwb,

I would like to ask if there is any motor driver or motor driver circuit that can receive analogue +-12V input voltage and produce relevant output voltage (with current amplified). Please advice, thank you very much.

Regards

29.09.11#53

Comment by rwb.

Yes is possible, basically the circuit has to shift the input voltage to the positive level and trim it to 0-5 volt variation so it can be processed by the microcontroller.

04.10.11#54

Comment by pichaha.

Hi, thanks for the reply.

I am sorry I forget to mention the target processor I am using is not a microcontroller, it is a microbox which can receive and produce voltage of up to 12V. So, I just want to know if there is any motor driver or motor driver circuit which can handle this voltage range (because if I am not wrong, the L293D can only sustain voltage variation of not more than 5V, right?) Please advice, thank you very much.

Regards

04.10.11#55

Comment by rwb.

The L293D maximum logic level input (Vi) and enable (Ve) voltage is 7 volt, the logic supply voltage is 36 volt, and the motor supply voltage is 36 volt, therefore you just need to use the voltage divider circuit to trim the microbox output (i.e. 12 volt) down bellow 7 volt.

05.10.11#56

Comment by pichaha.

Hi thanks for the reply.

What is the logic supply voltage for, is it the supply voltage for the chip? Previously, I use this motor driver to access digital input and output via microcontroller. But this time I am using it for analogue input and output. What is the changes that I should make. The output of the microbox should go to enable pin or input pin?? Thank you very much.

Regards

05.10.11#57

Comment by rwb.

Yes, the Vss pin is used to supply the motor controller logic inside the chip and the Vs pin is used for supply the motor. All the logical input is not an analog, they are all digital. Usually the enable pins (i.e. ENABLE1 and ENABLE2) are used to control the motor speed by supplying the PWM signal on it and the other logical pins (i.e. IN1,IN2,IN3, and IN4) are used to control the motor direction. If the analog is used to control the motor speed, then you have to convert the analog signal to the PWM signal in order to used it with L293D.

06.10.11#58

Comment by pichaha.

Hi, thanks for the reply.

I have a doubt. If the microbox I am using can only produce one anologue output to control one DC motor, how am I going to control the rotating direction of the motor using normal motor driver chips? What is the difference between supplying a +12V signal and -12V signal to the motor. Will the motor direction be reversed via this way without inverting the terminal connection of the motor?

I am asking this because the microbox I am using will be working with the matlab simulink in PC and the project I am doing is a closed loop control system. In the closed loop control, if the set point is less than the process variable, the manipulated signal will become negative, so as to the output signal.

Please advice. Your assistance is much appreciated. Thank you very much.

Regards

06.10.11#59

Comment by rwb.

If you plan to use +12V and -12V supply then you could try to use the half bridge DC motor driver circuit.

06.10.11#60

Comment by pichaha.

Hi thanks for the reply.

I am sorry, I have made a mistake here. The maximum analogue voltage that can be generated from the microbox is +-10V instead of 12V, so as to the maximum analogue input voltage that can be sustained by the microbox.

Apart from this, can I know what is the difference between supplying +10V and -10V voltage to the DC motor without inverting its terminal. Thank you very much. (For the half bridge circuit that you have suggested, I will try to study later.)

Regards

06.10.11#61

Comment by rwb.

You need to study the half bridge circuit! Basically this circuit required a symmetric power supply (i.e. +V, GND, and -V) in order to change the DC motor direction (i.e. forward and reverse).

13.10.11#62

Comment by chandra.

Hi….I am again here asking some doughts…..
The circuit and the program is working well…
This program at stat up if we make to use state machine algorithm; on the first power up the state is 0 and the DC motor is Off. any change in the RA1 input condition (RA1=0 or RA1=1) will change the state to 1, which mean the DC motor is On.
At running I want Stop possition….
How can it Possible?
So I want use RA1===0 for forwd…
and Ra2== 0 for revese….
and both at ===1 is stop…
Can u tell the code modifications…..
Thanks..

13.10.11#63

Comment by chandra.

Now writing clearly….
RA1 == 0 and RA2 == 1 is forwd..
RA1 == 1 and RA2 == 0 is reverse….
RA1 == 1 and RA2 == 1 is stop.
RA1 == 0 and RA2 == 0 is stop.

Can u say the code modicications….
Thanks..

14.10.11#64

Comment by rwb.

You just need to use a simple if condition in order to achieve what you need. Here the pseudo code:

SW_RA1 = read_RA1()
SW_RA2 = read_RA2()

if (SW_RA1 <> SW_RA2) then
if (SW_RA1 = 0 AND SW_RA2 = 1) then
direction=forward
end

if (SW_RA1 = 1 AND SW_RA2 = 0) then
direction=reverse
end
else
direction=stop
end

motor_move(direction)

15.10.11#65

Comment by chandra.

Hi….I tried the code at MPLAB it giving many error messages….Can u Provide the full code……
Thanks…..

15.10.11#66

Comment by rwb.

All the codes provides in this blog has been tested, make sure you install the Microchip MPLAB IDE and Microchip HITECH C Compiler correctly

15.10.11#67

Comment by chandra.

Hi….I tested that code is working…..But…
As per your advice,I try to edit and compile the code This….

SW_RA1 = read_RA1()
SW_RA2 = read_RA2()

if (SW_RA1 SW_RA2) then
if (SW_RA1 = 0 AND SW_RA2 = 1) then
direction=forward
end

if (SW_RA1 = 1 AND SW_RA2 = 0) then
direction=reverse
end
else
direction=stop
end

motor_move(direction)

When I am trying this…Giving error message..So can u edit and post the full code for this function.
Thanks…

15.10.11#68

Comment by rwb.

The code is called “pseudo code”, which mean its only show you the program logic not a real C code. You have to make your own code when you want to modify this project C code.

16.10.11#69

Comment by chandra.

Hi…Thanks for your kind reply……
I define as follow…
#define forward;
P1M1=0;
P1M0=1;

#define reverse;
P1M1=1;
P1M0=1;

How can I define the stop……

I need the stop mode….
Thanks…

16.10.11#70

Comment by rwb.

You could simply assign the duty cycle on CCPR1L register to 0 to stop the motor.

17.10.11#71

Comment by chandra.

I am confusing this stage.Really…. when the control at level of full speed…. when change the switch to Stop possition…I need stop….
Please explain abt stop ..
How han I define…

#define forward;
P1M1=0;
P1M0=1;

#define reverse;
P1M1=1;
P1M0=1;

Write about me stop….

Thanks…

18.10.11#72

Comment by rwb.

The stop condition could be achieve by set the PWM duty cycle to 0%

18.12.11#73

Comment by olam.

I have this project working ok. But when I pressed the button it changed the direction only once, and never return, also when ajust the pot it’s freeze the LED and stuck there.
Could you point out the error I have made? I use PCkit3 programmer, A Low pin count demo-board
From Microchip. Where or how could I purchase your Demo and learning board?

Thank you

18.12.11#74

Comment by rwb.

Check your program and circuit again. The AVRJazz 20PIN demo board could be purchased from http://www.ermicro.com

22.12.11#75

Comment by olam.

Hello! Mr. Besinga.
Thank you so much to reply my questions.
I got this worked ok now.
Merry Christmas and Happy New year
To you and your family !!!

22.12.11#76

Comment by rwb.

Olam, Merry Christmas and Happy New Year, GBU

25.02.12#77

Comment by aredhel_vlsi.

Hello man, thank you very much for this tutorial !!
It’s usefull to advise some code while studying. I’m a beginner and I use the power control module. Have you done anything with this ,or only with CCP?

Thanx again

26.02.12#78

Comment by rwb.

What did you mean by using the “power control module” on Microchip PIC16F690 Microcontroller?

29.03.12#79

Comment by aredhel_vlsi.

Excuse me, there is a power control module at PIC 18F4331… I didn’t notice that you use PIC16F.. But your logic helped me a lot. So I guess you haven’t ever used 18F family?

07.04.12#80

Comment by Izanur.

Hi, Thanks to you for your great-tutorial !!

Using this 16F690 I want to take three analog value using RA0,RA1,RA2. Now, if RA0>RA1 the voltage will gradually increase from 60-80%. Again if RA0=RA0,RA1 or RA2<=RA0,RA1 the voltage will be 50%.
What will be the program? This will works as charger. Please help me….. I am Newer..

07.04.12#81

Comment by Izanur.

Analog value will be taken as input.By PWM the percentage will vary. Which i did not mentioned in the previous..
Thanks

10.04.12#82

Comment by Izanur.

Pls. give the Answer

11.04.12#83

Comment by rwb.

You could use the C language if condition to change the PWM duty cycle (CCPR1L register) value based on the RA0, RA1, and RA2 analog input. This article show you how to read multiple analog input on PIC16F690 and how to use the analog value to control a servo Basic Servo Motor Controlling with Microchip PIC Microcontroller.

17.04.12#84

Comment by Izanur.

I want to take voltage(6v) & current(10mA) value as analog input, according to this analog value I want to change the PWM duty cycle. Please help me, how can i do it….?????

thanks.. Please know me….

17.04.12#85

Comment by rwb.

The PIC16F690 microcontroller analog input only could handle voltage level start from 0 to 5 volt. Therefore for 6 volt voltage level you need to use the voltage divider circuit and for the current you need to use a current to voltage transducer such as LEM FHS 40-P/SP600. Next based on the ADC reading on both these voltage inputs, you could adjust the PWM duty cycle using the CCPR1L register.

18.04.12#86

Comment by Izanur.

Hi, i want to do it using timer 2. But i am not clear, what will be the C programme code of the project. Actually i want to make a charger, after taking analog input it will creates different PWM based on the analog input value. say if AN0 value>3 & AN1 value<6, then any pin of C-port will create 3V
pwm signal again if AN0 value6 then same pin of C-port will create 8V pwm.

Some of code given below….
Please complete the code according to my requirement above.
I am new in programming.
Thanks, You are Great….
[Code Truncated...]

18.04.12#87

Comment by Izanur.

Please, Give me the answer…..

19.04.12#88

Comment by rwb.

You need to read more about the PWM principle on the Microchip PIC16F690 microcontroller in order to understand how it really work. Remember the PIC16F690 PWM output port will not create 3 volt nor the 8 volt output but it will create a pulse signal from 0 to 5 volt with a duty cycle (HIGH vs LOW) vary from 0 to 100%.

19.05.12#89

Comment by GeorgeAE.

It is anyway you can provide me the C code, so i can take a look . Thank you.

20.05.12#90

Comment by rwb.

You could read the article above for the complete C code for this project.

28.05.12#91

Comment by Jolly.

Hi Mr.rwb
Thanks you so very much for your tutorials. I got a lot of knowledge from your blog. Excume me, can you tell me, how to know 2 dc motors position in rectangular coordinates and pls guide me which articles and books I should read. Thank you.

03.06.12#92

Comment by rwb.

You could use the DC motor with the encoder.

22.06.12#93

Comment by Izanur.

What is difference between PORTC & TRISC. To show output, have i need to set both(PORTC & TRISC) to Zero(0) or Please tell me details about PORTC & TRISC.
Thank You.

23.06.12#94

Comment by rwb.

Because the PORTC is the bidirectional port (i.e. Input and Output), therefore we need to configure it whether to use it as input or output port. To configure PORTC as the input port you need to set the TRISC (Port C Tri-State) register to 0xFF, while assigning the value of 0×00 to the TRISC register will configure the PORTC as the output port.

26.07.12#95

Comment by harish.

Hi i am trying to build Dual Dc Motor Speed control using PIC Controller. I have to use an analog joystick to control the speed and direction. Its going to be used on wheel chairs for disabled. Can you plaese provide me with any information .thank you

30.07.12#96

Comment by rwb.

Basically the joystick has two potentiometers. These two potentiometers will be used two analog ports in PIC microcontroller and use the ADC peripheral to read the analog value. Next use these values to control motor speed (PWM) and direction.

02.08.12#97

Comment by harish.

thanks a lot for the reply. I have 2 more questions.
1. I am going to use an arduino (atmega328) controller and i have to make sure that i program it and put it on my board is it possible?? i dont want to have 2 seperate boards.
2. Also is it possible for you to provide me with a circuit diagram please??

03.08.12#98

Comment by harish.

Hi, I wanted to know if there are any H BRIDGE with 20A as mine is a 300W and 24V DC MOTOR??

03.08.12#99

Comment by rwb.

Yes you could use arduino board to control the motor (you need a motor driver circuit) and you could use the arduino board analog input to read the potentiometer to control both speed and direction. For the circuit first you need to find a suitable motor driver (arduino motor shield if possible) for your needs, next use two analog (e.g. A0 and A1) input on the arduino board and connect it to the potentiometer (there are a plenty example in the arduino playground website)

03.08.12#100

Comment by rwb.

For this particular need you have to build your own H-Bridge motor driver, try to look at the Open Source Motor Control (OSMC) project

09.08.12#101

Comment by sam781.

Very good tutorial indeed. I need a little more info regarding single output mode. In the single output mode, all the output pins (P1A, P1B, P1C & P1D) will have the same waveform. I need output like – for the first cycle only A and C will be enabled and in the next cycle only B and C will be enabled. This is for full bridge converter.

13.08.12#102

Comment by rwb.

In single output mode, you could use the PSTRCON register to control the P1A, P1B, P1C, and P1D output by enabling the corresponding STRA, STRB, STRC, and STRD bits.

23.08.12#103

Comment by wall e.

I already have a pic 16F877A micro chip can I use it for this

23.08.12#104

Comment by wall e.

please can you provide easy and medium cost project like the 1st project you provide to make line following robot by transistor basis,but it should be more accurate than transistor base one.
I have poor knowledge in pic’s and programming so I’m mostly appreciate projects without pics

23.08.12#105

Comment by rwb.

Yes you could but you need to modify the C program. For the robotics project you could try the “The LM324 Quad Op-Amp Line Follower Robot with Pulse Width Modulation” project.