CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Control motor brushless ccs c

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Charles Francês



Joined: 29 Feb 2020
Posts: 7

View user's profile Send private message

Control motor brushless ccs c
PostPosted: Sat Feb 29, 2020 3:01 pm     Reply with quote

Hello guys from CCS C How are you? I'm studying brushless motors and I'm having trouble understanding this code. Could someone explain to me how this code works on bldc motors with hall sensor? and if possible I would like to control your vocation! Laughing Rolling Eyes

I have a hard time understanding because I'm new.
Code:

// Controle de motor BLDC detectado por CD-ROM com código CIC PIC16F877A CCS
//

#include <16F877A.h>
 #fuses SH, NOWDT, NOPROTECT, NOLVP
 #device ADC = 10
 #use  atraso (relógio = 8000000)
 #use fast_io (B)
 #use fast_io (D)

int8 hall;
int8 MoveTable[8] = {0, 33, 6, 36, 24, 9, 18, 0};
int16 value;
#INT_RB                                       // RB port interrupt on change
void rb_isr(void){
  hall = (input_b() >> 4) & 7;         
  output_d(MoveTable[hall]);
  clear_interrupt(INT_RB);
}
void main(){
  output_b(0);                                // PORTB initial state
  set_tris_b(0xF3);
  port_b_pullups(TRUE);                       // Enable PORTB internal pull-ups
  output_d(0);
  set_tris_d(0);
  setup_adc(ADC_CLOCK_DIV_16);                // Set ADC conversion time to 16Tosc
  setup_adc_ports(AN0);                       // Configure AN0 as analog 
  set_adc_channel(0);                         // Select channel 0 input
  setup_timer_2(T2_DIV_BY_16, 250, 1);        // Set PWM frequency to 500Hz
  setup_ccp1(CCP_OFF);                        // CCP1 OFF
  enable_interrupts(GLOBAL);               
  delay_ms(100);                              // Wait 100ms
  while(TRUE){
    if(!input(PIN_B0)){                       // If RB0 button pressed
      if(input(PIN_B2)){                      // Check if motor is already running
        disable_interrupts(INT_RB);
        output_d(0);
        setup_ccp1(CCP_OFF);                  // CCP1 OFF
        output_low(PIN_B2);                   // RB2 LED OFF
      }
    }
    if(!input(PIN_B1)){                       // If RB1 button pressed
      if(!input(PIN_B2)){                     // Check if motor is already running
        output_high(PIN_B2);                  // RB2 LED ON
        clear_interrupt(INT_RB);              // Clear RB IOC flag bit
        enable_interrupts(INT_RB);            // Enable PORTB IOC
        setup_ccp1(CCP_PWM);                  // Configure CCP1 as a PWM
        hall = (input_b() >> 4) & 7;
        output_d (MoveTable [hall]);
      }
    }
    if (input (PIN_B2)) {
      read_adc (ADC_START_ONLY);
      while (! adc_done ());                     // Aguarde até a conversão concluir o
      valor = read_adc (ADC_READ_ONLY);        // Leia o valor ADC
      set_pwm1_duty (value);                   // Definir ciclo de trabalho PWM
    }
  }
}
temtronic



Joined: 01 Jul 2010
Posts: 9176
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 29, 2020 5:23 pm     Reply with quote

this is a problem...
valor = read_adc (ADC_READ_ONLY); // Leia o valor ADC
set_pwm1_duty (value); // Definir ciclo de trabalho PWM

I think 'valor' is 'vaue' in another language ?

I believe that's a 'stepper' motor that is being controlled by port D of the PIC. You need to decode which port pins are controlling the 4 winding and hopefully there is a transistor buffer between the PIC and the motor !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Feb 29, 2020 7:52 pm     Reply with quote

I believe this is the website that he got the code from:
https://pic-microcontroller.com/sensored-brushless-dc-bldc-motor-control-pic16f877a-microcontroller/
It has a schematic. It's not great, it doesn't zoom in. But you can see
that PWM comes out on CCP1, which is what the code is using.

The website explains how it works. I have provided a link below
that translates the website into Portuguese so he can easily read it:
https://translate.google.com/translate?hl=en&tab=wT&sl=auto&tl=pt&u=https%3A%2F%2Fpic-microcontroller.com%2Fsensored-brushless-dc-bldc-motor-control-pic16f877a-microcontroller%2F
Ttelmah



Joined: 11 Mar 2010
Posts: 19375

View user's profile Send private message

PostPosted: Sun Mar 01, 2020 12:53 am     Reply with quote

and (of course), a 'stepper' motor, and a BLDC motor are very similar things.
The BLDC is analogous to a 3 phase stepper motor, where the PWM being
fed to the phases is modulated to give a smooth motion instead of moving
in 'steps'. They are optimised for smooth motion, instead of step 'accuracy'.
The waveforms required are very similar. You can actually drive a
BLDC like a stepper, and a stepper like a BLDC. However efficiency will
plummet.....
Charles Francês



Joined: 29 Feb 2020
Posts: 7

View user's profile Send private message

PostPosted: Sun Mar 01, 2020 10:07 am     Reply with quote

Thanks! Charles
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group