Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Power control PWM module on PIC18f2431 help

Status
Not open for further replies.

Cantafford

Member
Hello,

I'm trying to control a BLDC motor with PIC18f2431 and control it's speed :D. I'm trying to use the PCPWM module's override function to control each transistor of my inverter bridge and send PWM signals to it in order to control the motor speed. This is the schematic:
2s8qnpf.png

I set transistors to be in independent mode(0&1, 2&3, 4&5) and I calculated the frequency(period) to be 1.5Khz. For starters I want to control the motor with 75% duty cycle to see if it works and then I will modify the speed using a POT. So for now I calculated the PWM duty cycle to be 75% for all transistors(by updating each PDCxL and PDCxH) and tried to turn them on individually using the OVDCONS registers.

This is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"

int HALL;
int HALLvalue[6] = { 0b00000101, 0b00000001, 0b00000011, 0b00000010, 0b00000110, 0b00000100 };

// Capture Interrupt Service Routine

void interrupt CheckHallValue()
{
  IC1IF = 0; IC2QEIF = 0; IC3DRIF = 0;
  HALL = (PORTA >> 2) & 0x7; // read the capture pins to get hall sensor state
  if(HALL == HALLvalue[0]) {OVDCONS = 0b00100100; PDC2H = 0x03; PDC2L = 0x12; PDC1H = 0x03; PDC1L = 0x12;}      // and give
  else if(HALL == HALLvalue[1]) {OVDCONS = 0b00000110; PDC1H = 0x03; PDC1L = 0x12; PDC0H = 0x03; PDC0L = 0x12;} // appropiate phase sequence (duty cycle is supposed to be 75%)
  else if(HALL == HALLvalue[2]) {OVDCONS = 0b00010010; PDC2H = 0x03; PDC2L = 0x12; PDC0H = 0x03; PDC0L = 0x12;}  //
  else if(HALL == HALLvalue[3]) {OVDCONS = 0b00011000; PDC2H = 0x03; PDC2L = 0x12; PDC1H = 0x03; PDC1L = 0x12;} //
  else if(HALL == HALLvalue[4]) {OVDCONS = 0b00001001; PDC1H = 0x03; PDC1L = 0x12; PDC0H = 0x03; PDC0L = 0x12;} //
  else if(HALL == HALLvalue[5]) {OVDCONS = 0b00100001; PDC2H = 0x03; PDC2L = 0x12; PDC0H = 0x03; PDC0L = 0x12;} //
}


void main()
{
 OSCCON = 0b01001111; // frequency is 1Mhz
 ANSEL0 = 0b00000000; // set portA to digital
 TRISA = 0b11111111; // PORTA is input(CAP's + POT)
 TRISB = 0b11000000;
 GIE = 1; // enable global interrupts
 GIEH = 1; // enable high interrupts
 PEIE = 1; // enable pheripheral interrupts

 // Initialize the Input Capture Module
 CAP1CON = 0b00000000; // disable input capture 1 module
 CAP1CON = 0b00001000; // enable input capture 1 module; interrupt on every state change
 CAP2CON = 0b00000000; // disable input capture 2 module
 CAP2CON = 0b00001000; // enable input capture 2 module; interrupt on every state change
 CAP3CON = 0b00000000; // disable input capture 3 module
 CAP3CON = 0b00001000; // enable input capture 3 module; interrupt on every state change

 // Enable Capture Interrupt and configure TMR5
 IC1IE = 1; // enable IC1 interrupt
 IC1IP = 1; // IC1 interrupt on high priority
 IC1IF = 0; // clear IC1 interrupt status flag
 IC2QEIE = 1; // enable IC2 interrupt
 IC2QEIP = 1; // IC2 interrupt on high priority
 IC2QEIF = 0; // clear IC2 interrupt status flag
 IC3DRIE = 1; // enable IC3 interrupt
 IC3DRIP = 1; // IC3 interrupt on high priority
 IC3DRIF = 0; // clear IC3 interrupt status flag

 //Configure PCPWM module
 PTCON0 = 0b00000000; // 1:1 postscale, input timebase FOSC/4(1:1 prescale), PWM in free-running mode
 PTCON1 = 0b11000000; // ??? PWM Time base on, pwm time base counts down
 PWMCON0 = 0b01001111; // PWM 0-5 activated in independent mode
 //PWMCON1
 //DTCON
 OVDCOND = 0b00000000; // Output PWM controlled by corresponding POUT bit(active when set)

 PTPERH = 0x10; // TPWM CALCULATION (PTPER supposed to be 0xA4)
 PTPERL = 0x04; // frequency is 1.5Khz(PTMRPS=1)

 // Configure TMR5 module
 T5CON = 0b00000101;

 PDC2H = 0x03; PDC2L = 0x12; PDC1H = 0x03; PDC1L = 0x12; // kickstart
 OVDCONS = 0b00100100; // the motor

 while(1)
 {
 }

}

The transistors do not change state at all upon running the simulation in proteus. They are just stuck in a certain state.
 
Thank you. I'm guessing that maybe I did something wrong at calculating the period and/or duty cycle. Maybe I have not loaded those registers high and low correctly.
 
Yeah I use that when generating PWM's via CCP module :D. This time I made the calculations by hand but I'm not sure if I loaded the values correctly in the two registers for the period and duty cycle. I have never used a duty cycle that required both PWMxH and PWMxL to be used and not sure if I did it correctly. I'm not sure if you understand what I mean my explanation is a little ambiguous.
 
Anyone can help me with this? I can't figure it out :(
Trouble here is that no-one has driven a BLDC motor from a pic.... I would have to buy all the gear to test anything suggest... I haven't the time at the moment... I have run DC motors but not these star / triangle brushless types..
 
Trouble here is that no-one has driven a BLDC motor from a pic.... .

???
The link I posted in #2 has the source code for different Pic's, including the 18F , but in Assembly, I have the PIC MC LV board and controlled a motor with it.
Max.
 
Trouble here is that no-one has driven a BLDC motor from a pic.... I would have to buy all the gear to test anything suggest... I haven't the time at the moment... I have run DC motors but not these star / triangle brushless types..
An example with a DC motor would be fine I just need to see how to use the module, I know how to give the sequence to the BLDC.
 
???
The link I posted in #2 has the source code for different Pic's, including the 18F , but in Assembly, I have the PIC MC LV board and controlled a motor with it.
Max.
Yes!! I read it... Now put it in C so the OP can understand it.... To advise properly he needs someone who has actually done it... I'll do a simple PWM controlled single motor.... Hopefully he can move on from there...

I use an external motor controller so I just use a single PWM signal and a direction..
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top