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 CCS Technical Support

PWM1 AND PWM2 with 18f46k22[SOLVED]
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PWM1 AND PWM2 with 18f46k22[SOLVED]
PostPosted: Thu Sep 17, 2020 6:06 am     Reply with quote

Hello to everyone.
I'm trying to write some code for the H bridge.
I design the H bridge myself. I control 4 IRLZ44NS at 12V by giving 12 volts to the GATE. I'm checking the Gate with 4 mmbt2222 npn transistors. When I start normally, the engine is controlled.
Code:

//for up
      void UP(void){
      output_high(M2c2);
      output_low(M1c3);
      output_low(M4c0);
      output_high(M3c1);
      }
//for down
      void down(void){
      output_high(M1c3);
      output_low(M2c2);
      output_low(M3c1);
       output_high(M4c0);
      }   

But when I trigger it with pwm, only my DOWN Function works fine.
But in my UP Function; power supply switches to high current fault. Do you think why?

Code:

#include <18f46k22.h>
#INCLUDE <stdlib.h>
#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPUT,NOWRT,NODEBUG, NOMCLR, NOPROTECT, NOWDT, NOLVP, PLLEN
#use delay(clock=64000000)                                                       //osilatör frekansı 4Mhz'e ayarlandı
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#define M4c0 pin_c0
#define M3c1 pin_c1
#define M2c2 pin_c2
#define M1c3 pin_c3
void down(void);
void Up(void);
void main ( )

output_c(0x0C);
delay_ms(1000);

   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   
   setup_timer_2(T2_DIV_BY_16,100,16);
   
   set_pwm1_duty(25); // M4C0
   set_pwm2_duty(25); // M3C1

   while(true)
   {
      UP();

    }
}

      void UP(void){     // it is not work
      output_high(M2c2);
      output_low(M1c3);
      // output_low(M4c0);
      output_high(M3c1);
      set_pwm2_duty(0); // M3C1
      set_pwm1_duty(25); // M4C0
      }
      void down(void){ it is work
      output_high(M1c3);
      output_low(M2c2);
      //output_low(M3c1);
       output_high(M4c0);
      set_pwm1_duty(0); // M4C0
      set_pwm2_duty(25); // M3C1
      }   


Last edited by ressas on Thu Oct 08, 2020 10:49 pm; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 6:13 am     Reply with quote

Could be a simple wiring mistake, or turning on the wrong drivers.....

Instead of ...

#define M4c0 pin_c0
#define M3c1 pin_c1
#define M2c2 pin_c2
#define M1c3 pin_c3

I'd code so the defines 'show' what leg or arm of the Hbridge they control.
say 'UL' for Upper Left, 'UR' for Upper Right, 'LL' for Lower Left, and 'LR' for Lower Right'.
This way you can see what gets turned on and off.
If you have more than one motor,...M1UL = Motor number 1, Upper Left.

Jay
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 6:15 am     Reply with quote

temtronic wrote:
Could be a simple wiring mistake, or turning on the wrong drivers.....

Instead of ...

#define M4c0 pin_c0
#define M3c1 pin_c1
#define M2c2 pin_c2
#define M1c3 pin_c3

I'd code so the defines 'show' what leg or arm of the Hbridge they control.
say 'UL' for Upper Left, 'UR' for Upper Right, 'LL' for Lower Left, and 'LR' for Lower Right'.
This way you can see what gets turned on and off.
If you have more than one motor,...M1UL = Motor number 1, Upper Left.

Jay


nice suggestion thanks


#define DR pin_c0
#define DL pin_c1
#define UR pin_c2
#define UL pin_c3
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 7:14 am     Reply with quote

I don't see in what you post, any sign that you are setting the second PWM to
use that pin. You need the fuse CCP2C1. It may well be going to B3.... Sad
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 7:39 am     Reply with quote

Ttelmah wrote:
I don't see in what you post, any sign that you are setting the second PWM to
use that pin. You need the fuse CCP2C1. It may well be going to B3.... Sad


I add this fuse but; not work
I think the problem is not caused by cc2 but by ccp1.
My problem isn't just that pwm isn't working.
Also, when I use "set_pwm1_duty", high current is also drawn.
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 7:59 am     Reply with quote

AND
Not because the motor draws high current.
I did not connect the motor and continued to give high current warning I guess I can't really trigger the mosfets.
AND why?
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 8:40 am     Reply with quote

Maybe post a schematic, might be a wiring problem
temtronic



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

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 9:44 am     Reply with quote

Have to ask WHAT gives you the 'high current' warning ?
also
If it is drawing current without the motor, that suggests both upper and low MOSFETs are on, effectively a power supply short.....
Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Sep 17, 2020 10:33 am     Reply with quote

Two things:

This line:

setup_timer_2(T2_DIV_BY_16,100,16);

Sets the PWM to 9.9KHz. I guess you want 10K?. If so you need to use
99 for PR2. Remember the division is PR2+1.

Post what MOSFET is connected where?. When you are operating the
circuit 'yourself', you turn on C0 and C3 or C2 and C3, but with the PWM
you are turning on C2 and C1. Very different....
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Fri Sep 18, 2020 5:55 am     Reply with quote

Ttelmah wrote:
Two things:

Post what MOSFET is connected where?. When you are operating the
circuit 'yourself', you turn on C0 and C3 or C2 and C3, but with the PWM
you are turning on C2 and C1. Very different....

Yes Ttelmah. you are very different.
Thank you.
This solved my problem.
However, I used an outside interrupt to stop the engine.
After using this interrupt I got an error like this:
interrupts disabled during call to prevent reentrancy (@ADDF)

Actually, as this is a separate problem, you may not be able to answer that. I think I should investigate some things myself.

However, there is something that surprises me. On page 21 of the datasheet, pwm1 property for c0 and c2 appears.
Why can't I use it in C0?

You can view the scheme of the H bridge here:

https://ibb.co/Y381cX3

AND
Quote:

Have to ask WHAT gives you the 'high current' warning ?
also
If it is drawing current without the motor, that suggests both upper and low MOSFETs are on, effectively a power supply short.....
Jay


Yes Temtronic. You are right. That was exactly the problem
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri Sep 18, 2020 7:05 am     Reply with quote

Two separate things here.
The first is down to the mode you run the PWM.

In standard mode (CCP_PWM), it only generates the single signal on PWMx.
In it's enhanced mode, you have four PWM outputs from each PWM, and these
go to P1A, P1B, P1C & P1D. The signals from these can be 'steered' to
different pins, or used as half bridge or full bridge pairs. The pulse steering
is done using the settings CCP_PULSE_STEERING_x in the CCP setup. Where
'x' is the output you want to use (A, B, C or D).
However the second part here is an error in the data sheet. C0, is P2B,
which is the 'B' output from PWM2, not from PWM1. The title is right in
the pin names, but wrong in the description. So you can't use this for PWM1...
Sad
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Fri Sep 18, 2020 8:11 am     Reply with quote

Ttelmah wrote:

Sad

Get it.
Don't be sad
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri Sep 18, 2020 10:59 am     Reply with quote

I always hate it when a data sheet is wrong... Very Happy

On the interrupts disabled warning, this means you are using the same
function both inside and outside the interrupt. To avoid this being
therefore being called 'inside itself', the version outside the interrupt
gets the interrupts disabled around it, and you get this warning.
For anything reasonably 'quick', don't worry about it.
jeremiah



Joined: 20 Jul 2010
Posts: 1346

View user's profile Send private message

PostPosted: Fri Sep 18, 2020 5:47 pm     Reply with quote

Is the F in ADDF "float" or is that some sort of bank notation? I'm used to PIC24's and don't know the PIC18's. If it is for floats, then I would recommend not doing float math in an ISR.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Sep 19, 2020 12:10 am     Reply with quote

Don't know.
On my PIC24 code, the float addition is called ADDFF & ADDFF64 (for the
double version). I don't get an ADDF. Have looked through half a dozen
programs. Would agree wholeheartedly. It might be being triggered
accidentally if a variable type is float, and it is used for something quite
#innocent' like array indexing.... Sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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