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

help in writing in C language for a pic18f4331
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
wewe



Joined: 17 Apr 2010
Posts: 31

View user's profile Send private message

help in writing in C language for a pic18f4331
PostPosted: Sat Apr 17, 2010 10:19 am     Reply with quote

This is a program that is supposed to convert an analog to digital value and output it on port B. It's not working. I tried to simulate it in Proteus.
Code:

void main()
{
int16 value;
int1 done;

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0|ADC_CONT_A|ADC_WHE N_INT0|ADC_INT_EVERY_OTHER);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

// TODO: USER CODE!!

set_tris_a(0xff);
setup_adc_ports (all_analog);
set_adc_channel (0);
setup_adc(ADC_CLOCK_INTERNAL);
read_adc(ADC_START_AND_READ);


done = adc_done();

while(!done) {
done=adc_done();
}

value = read_adc();
OUTPUT_B(value);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 17, 2010 11:46 am     Reply with quote

Look at this example:
http://www.ccsinfo.com/forum/viewtopic.php?t=42090&start=1
You can substitute
Code:

output_b(adc_value);

for the printf statement.
wewe



Joined: 17 Apr 2010
Posts: 31

View user's profile Send private message

PostPosted: Sat Apr 17, 2010 12:36 pm     Reply with quote

PCM programmer wrote:
Look at this example:
http://www.ccsinfo.com/forum/viewtopic.php?t=42090&start=1
You can substitute
Code:

output_b(adc_value);

for the printf statement.

Can i ask a couple of questions ???
I am new to CCS , today is my first day of writing codes for pic in c language.
First of all, how can you guys simulate your codes ??? I use Proteus but it seems like the "cof" file generated with CCS cannot be used with Proteus (or I am just unable to do it)
And output_b outputs a number on an entire port while printf is only suitable for a LCD (or I think so).
Second, is there any program here that was made before, that is suitable to control a dc motor ??
I need something that generates a pwm with a variable duty cycle (1 to 99% ) from a potentiometer and it must use the power pwms (2 complementary pwms). Because I need to have a dead time, I cannot risk any short circuit.
I was trying to adapt to this C language by first successfully making an analog to digital conversion and then move on to make a pwm but it seems like i am stuck on analog to digital. I understood your program but still can't figure out what's wrong with mine. Really without any simulation available, no one can be so sure.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 17, 2010 1:39 pm     Reply with quote

Quote:
how can you guys simulate your codes

I do it in hardware. I build a board, and write the program, and test it.

Quote:

Second, is there any program here that was made before, that is suitable
to control a dc motor ??

This program runs an external H-Bridge chip (LMD18200) to control
a DC motor:
http://www.ccsinfo.com/forum/viewtopic.php?t=40467&start=1

This program changes the PWM duty cycle by turning a trimpot:
http://www.ccsinfo.com/forum/viewtopic.php?t=40222&start=1
wewe



Joined: 17 Apr 2010
Posts: 31

View user's profile Send private message

PostPosted: Sat Apr 17, 2010 2:16 pm     Reply with quote

PCM programmer wrote:
Quote:
how can you guys simulate your codes

I do it in hardware. I build a board, and write the program, and test it.

Quote:

Second, is there any program here that was made before, that is suitable
to control a dc motor ??

This program runs an external H-Bridge chip (LMD18200) to control
a DC motor:
http://www.ccsinfo.com/forum/viewtopic.php?t=40467&start=1

This program changes the PWM duty cycle by turning a trimpot:
http://www.ccsinfo.com/forum/viewtopic.php?t=40222&start=1

Aha I understand now, but I cannot simulate in hardware.
As for the program, what I need is to control a DC motor with an H bridge of MOSFETs and not with an H-bridge chip. That's why controlling a DC motor with a PIC other than an 18f with the complementary pwms is impossible, because the H-bridge chip takes a pwm as an input and generates a direct output for the motor.
Anyway, if I wrote a code for pic18f, are you capable of simulating it with hardware?? Do you have the equipment (mosfet, oscilloscope..) ??
and it will probably take too much of your time anyway ....

First of all my program have to be with a PIC like 18f4331 or 18f2331
and then the result of the analog to digital conversion must generate a pwm and its complementary pwm. The frequency is stable (10khz) and the deadtime is optional (I would go for 0.5 to 1 micro seconds).
I think that it's easy to write the code in CCS, but how easy it is to test it LOL, that's the question.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 17, 2010 2:20 pm     Reply with quote

I don't want to sign up to be a testing lab. You need to build your own
testing environment.
wewe



Joined: 17 Apr 2010
Posts: 31

View user's profile Send private message

PostPosted: Sat Apr 17, 2010 2:28 pm     Reply with quote

PCM programmer wrote:
I don't want to sign up to be a testing lab. You need to build your own
testing environment.


Hehe, ok, I wasn't expecting that.
I thought that you have a previously made circuit like this, that's why I described it.
I was looking for a way to simulate my codes by software anyway.
I have to run a 1.5 KW DC motor, with a 280 v input voltage. I cannot risk anything going wrong, so I need a software simulation.
First of all I was happy with Proteus, but then mplab programing is hard, then I was happy with C language, but it turned out that the "cof" files generated do not work with Proteus (apparently).
I think it's a dead end for me.
Btw, when I compile or build, is there any files with assembly language that's generated??
Maybe I can use that in mplab, then compile and build that and use the "cof" output for Proteus. Maybe that can work.
wewe



Joined: 17 Apr 2010
Posts: 31

View user's profile Send private message

PostPosted: Sun Apr 18, 2010 12:56 am     Reply with quote

Btw if I choose the pic18f4331 along with the the internal oscillator, what's the frequency of that oscillator ???
I need a formula to calculate the period if I want to use one of the power pwms in pic18f4331.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Apr 18, 2010 6:15 am     Reply with quote

Quote:
As for the program, what I need is to control a DC motor with an H bridge of MOSFETs and not with an H-bridge chip. That's why controlling a DC motor with a PIC other than an 18f with the complementary pwms is impossible, because the H-bridge chip takes a pwm as an input and generates a direct output for the motor.


People have been using PICs to control H bridges since long before the 18f series was ever dreamed of. If you are controlling a 1.5kW motor from 280V you are going to need some serious driver hardware between your PIC and the FETs. Usually dead time to prevent shoot through is built into the driver electronics. It is built to turn on slow and shut off fast, usually with a RC network with a diode across it.

You need to study how FET drivers are built, or team up with an analog power engineer who knows.

Also try building your circuit for 12VDC first before shooting for 280V. At 12V the shrapnel from exploding components is much less painful.
_________________
The search for better is endless. Instead simply find very good and get the job done.
wewe



Joined: 17 Apr 2010
Posts: 31

View user's profile Send private message

PostPosted: Sun Apr 18, 2010 8:20 am     Reply with quote

SherpaDoug wrote:
Quote:
As for the program, what I need is to control a DC motor with an H bridge of MOSFETs and not with an H-bridge chip. That's why controlling a DC motor with a PIC other than an 18f with the complementary pwms is impossible, because the H-bridge chip takes a pwm as an input and generates a direct output for the motor.


People have been using PICs to control H bridges since long before the 18f series was ever dreamed of. If you are controlling a 1.5kW motor from 280V you are going to need some serious driver hardware between your PIC and the FETs. Usually dead time to prevent shoot through is built into the driver electronics. It is built to turn on slow and shut off fast, usually with a RC network with a diode across it.

You need to study how FET drivers are built, or team up with an analog power engineer who knows.

Also try building your circuit for 12VDC first before shooting for 280V. At 12V the shrapnel from exploding components is much less painful.

I chose a driver, it's the tlp25. I will not connect my pic directly to the mosfet.
But this driver cannot help me with my request for a dead time for the pwms since the pic16f877 doesn't really have a mottor control pwm generator, that's my problem.
Can you explain me the "RC" network with a diode across it method ??
If I have a PWM output, I can use an inverter to have a complementary pwm, so now I have 2 pwms, without any dead time.
How can I make the RC circuit across one of these ???
Please help, with this I can now completely drop the pic18f , which is a big plus because I don't have time to learn it anymore (in assembly) and I really don't need all the options in the pic18f. If this dead time can be generated by hardware then it's for my best anyway.
And yes I will start with a 12Vdc motor, but first I need to generate the right command circuit to control the mosfets.
You can help by naming some of the ICs that I can use. My problem is that I am not in USA or in an advanced country and if the IC is new to the market I will not find it in the market, so I will need to look for a relatively old IC that does the job.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Apr 18, 2010 12:54 pm     Reply with quote

I wish there was a decent way to post a schematic here...

Consider a resistor between your PIC and the driver chip. Add a capacitor from the driver input to ground. This slows the action of the driver by about the RC time constant. Put a diode in parallel with the resistor, cathode to the PIC. Now the rising edge from the PIC is slowed by the RC but the the falling edge is only slowed by the smaller resistance of the diode. Your FETs shut off fast and turn on slower. This avoids shoot through.

You can also do it between the driver and the FET, but the dynamic capacitance of the FET gate makes predicting the behavior tougher.
_________________
The search for better is endless. Instead simply find very good and get the job done.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Apr 18, 2010 3:09 pm     Reply with quote

wewe wrote:
Btw if I choose the pic18f4331 along with the the internal oscillator, what's the frequency of that oscillator ???



It's on the datasheet...

Always make sure to look at the datasheet...

The datasheet is your friend.

Have I mentioned how important the datasheet is?

:D

-Ben

p.s. I believe the built in oscillator is 8MHz.
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Apr 18, 2010 4:12 pm     Reply with quote

Section 2.6 on Page 26 of the data sheet.

http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010288
_________________
Google and Forum Search are some of your best tools!!!!
wewe



Joined: 17 Apr 2010
Posts: 31

View user's profile Send private message

PostPosted: Mon Apr 19, 2010 10:58 am     Reply with quote

bkamen wrote:
wewe wrote:
Btw if I choose the pic18f4331 along with the the internal oscillator, what's the frequency of that oscillator ???



It's on the datasheet...

Always make sure to look at the datasheet...

The datasheet is your friend.

Have I mentioned how important the datasheet is?

:D

-Ben

p.s. I believe the built in oscillator is 8MHz.

Believe me I have read the datasheet and the only thing that I understood is that I can use one of 8 frequencies available for the internal oscillator.
But my problem with the datasheet is that they are never clear.
For example you can't find a sentence like this " this pic is equipped with an internal oscillator with a 8 MHz frequency".
They always have this fancy way of displaying data, quite long, boring, too much technical and in the end I don't get anything :D

SherpaDoug wrote:
I wish there was a decent way to post a schematic here...

Consider a resistor between your PIC and the driver chip. Add a capacitor from the driver input to ground. This slows the action of the driver by about the RC time constant. Put a diode in parallel with the resistor, cathode to the PIC. Now the rising edge from the PIC is slowed by the RC but the the falling edge is only slowed by the smaller resistance of the diode. Your FETs shut off fast and turn on slower. This avoids shoot through.

You can also do it between the driver and the FET, but the dynamic capacitance of the FET gate makes predicting the behavior tougher.

thx for the help Smile
Now I want to ask a couple of questions
1- The capacitor comes between the PIC output and the ground, and after this node comes a resistor and a diode in parallel ????
2- Didn't you mean anode to the PIC???
Since the diode doesn't open unless the positive voltage is on the anode...
I am not smarter than you in electronics but I believe that you may have meant this.
3- Can you remind me how to calculate the delay on the rising edge ??
4- I will be operating on a frequency of 10KHZ, is this method of delay applicable on this frequency and is there a specific kind of capacitor I should use ??
thx for the help
MikeP



Joined: 07 Sep 2003
Posts: 49

View user's profile Send private message

PostPosted: Mon Apr 19, 2010 1:35 pm     Reply with quote

What version of Proteus are you using ? The cof file work fine here. I use version 7.7 SP1.
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, 3, 4  Next
Page 1 of 4

 
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