|
|
View previous topic :: View next topic |
Author |
Message |
jim Guest
|
You've got to be kidding me |
Posted: Tue May 09, 2006 3:56 pm |
|
|
I just up graded my compiler since there was "supposed" to be a fix for setup_power_pwm(). Now when I compile the same program setup_oscillator( OSC_8MHZ ); complains "identifier expected"
WHAT!!! ????
if I take that line out, it compiles but each time I hit reset on my target board the PWM duty cycle changes sizes..
what is going on here? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 09, 2006 4:05 pm |
|
|
You didn't provide a test program so I had to make one, and it
compiles with no errors with PCH vs. 3.249.
Code: |
#include <18F4331.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOLVP
#use delay(clock=8000000)
//==============================
void main()
{
setup_oscillator(OSC_8MHZ);
while(1);
} |
-------
Edited to add:
I looked at your previous post. You've got the setup_oscillator()
statement stuck up above main(). It's not part of the program.
You can't do that. Previous versions of the compiler had a bug
wherein they wouldn't show an error if you stuck built-in functions
above main(). Current versions of the compiler properly show
an error. I think I emailed them about this a while ago and
apparently they fixed it.
Last edited by PCM programmer on Tue May 09, 2006 4:09 pm; edited 1 time in total |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 09, 2006 4:08 pm |
|
|
Quote: | You've got to be kidding me | With a subject title like this I assumed it to be a link to a Russian bride website. I'm disappointed.... (and lonely). |
|
|
jim Guest
|
hi |
Posted: Tue May 09, 2006 4:11 pm |
|
|
I've got to be going insane...
I had
#include<18f4331.h>
#device adc=10
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP, NOBROWNOUT
#use delay(clock=8000000) //8Mhz
#use rs232(baud=9600,xmit=PIN_E0,rcv=PIN_E1,parity=n,bits=8,INVERT)
setup_oscillator( OSC_8MHZ);
commented it out.. copied your and all works fine.. Long day. thanks for the help |
|
|
Jim Guest
|
Still Insane.. |
Posted: Tue May 09, 2006 4:27 pm |
|
|
each time I get hit reset..I get a differn't pulse size (x2) I'm using version 3.236
#include <18F4331.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600,xmit=PIN_E0,rcv=PIN_E1,parity=n,bits=8,INVERT)
#define FORWARD 1
#define BACKWARD 0
#define OFF 0
//////////////// functions ////////////////////////
void move(int left_speed,int left_direction, int right_speed, int right_direction);
int16 liLeftDuty;
int16 liRightDuty;
void main(void)
{
int period=199;
setup_oscillator(OSC_8MHZ);
/***********************************
freq= Fosc / (4*(period + 1) * postscale)
or
period = (Fosc /( 4 * freq * postscale)) -1
if we want 10khz
period =( 8mhz /(4 * 10khz * 1) -1
peroid = 199;
*****************************************/
setup_power_pwm_pins(PWM_ODD_ON,PWM_ODD_ON,PWM_OFF,PWM_OFF);
setup_power_pwm(PWM_FREE_RUN,1,0,period,0,1,0);
//forward
output_B(0b01010000);
while(true)
{
delay_ms(1000);
// half speed
move(5,FORWARD,5,FORWARD);
set_power_pwm0_duty(liLeftDuty);
set_power_pwm2_duty(liRightDuty);
// printf("\n\n\rhalf");
//full speed
delay_ms(1000);
// printf("\n\n\rFull");
move(10,FORWARD,10,FORWARD);
set_power_pwm0_duty(4000);
// set_power_pwm0_duty(liLeftDuty);
set_power_pwm2_duty(liRightDuty);
}//while true
}//main()
////////////////////////////////////////////////////////////////////////////
//void move(int left_speed,int left_direction,
// int right_speed, int right_direction)
// this is used to set the speed and direction of the bot
// speed is a value for 0-10
// since this uses PWM for the motors and anything below ~40% duty is useless
// setting the speed to 1 will be ~40% duty and 0 will be off
//
// example: move(10,FORWARD,5,FORWARD);
void move(int left_speed,int left_direction, int right_speed, int right_direction)
{
//4000 will be full on
int direction;
int16 baseline = 1600; // ~40%
int16 factor = 240; // ~ (16232 - 6600 )/ 10
if(left_speed > 10)
left_speed = 10;
if(right_speed > 10)
right_speed = 10;
//output_b(0b00000000); //all off
//calc speed first and set pwm
if(left_speed == 0)
liLeftDuty = left_speed;
else
liLeftDuty = (left_speed * factor) + baseline;
if(right_speed == 0)
liRightDuty = right_speed;
else
liRIghtDuty = (right_speed * factor) + baseline;
printf("\n\r Left %ld",liLeftDuty);
printf("\n\r Right %ld",liRightDuty);
}//move |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 09, 2006 5:43 pm |
|
|
Quote: | each time I get hit reset..I get a differn't pulse size (x2) I'm using version 3.236 |
Can you reduce the size of the test program to just a few lines of
code in main(), and still show the problem ? I don't have your
hardware, so I can't test it or look at signals with an oscilloscope.
If you reduce the code to a few lines, I can look at the .LST file. |
|
|
Jim Guest
|
smaller |
Posted: Tue May 09, 2006 5:54 pm |
|
|
#include <18F4331.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOLVP
#use delay(clock=8000000)
void main(void)
{
int period=199;
setup_oscillator(OSC_8MHZ);
/***********************************
freq= Fosc / (4*(period + 1) * postscale)
or
period = (Fosc /( 4 * freq * postscale)) -1
if we want 10khz
period =( 8mhz /(4 * 10khz * 1) -1
peroid = 199;
*****************************************/
setup_power_pwm_pins(PWM_ODD_ON,PWM_ODD_ON,PWM_OFF,PWM_OFF);
setup_power_pwm(PWM_FREE_RUN,1,0,period,0,1,0);
while(true)
{
delay_ms(1000);
// half speed
set_power_pwm0_duty(500);
//full speed
delay_ms(1000);
set_power_pwm0_duty(4000);
}//while true
}//main() |
|
|
Ttelmah Guest
|
|
Posted: Wed May 10, 2006 3:00 am |
|
|
Big problem here, is that the maths is being done in an int8, not an int16.
If you take this line:
freq= Fosc / (4*(period + 1) * postscale)
The arithmetic will evaluate '199+1' = 200. Then multiply by 4, which will give 32!... (it wraps round).
The problem is that the compiler defaults to int (8 bit), unless _you_ ensure that a variable/constant is an int16. Try:
freq =Fosc / (4L*(period+1)) * postscale)
Notice the 'L', which forces this to be a 'long', and will then force the arithmetic to use int16. You should also do the same with your defines for postscale and Fosc (though provided the latter is over 255, it will automatically be forced to the larger type). Depending on the value used for 'postscale', you may even have to force the arithmetic up to int32 (you do this by 'casting'), so:
freq= Fosc / (4L*(period + 1) * (int32)postscale)
Which will force postscale to be an int32 value, and the result here will then be an int32.
Best Wishes |
|
|
Jim Guest
|
in the comment |
Posted: Wed May 10, 2006 5:48 am |
|
|
That was just a comment to explain how I came up with the 199. I did the math on my calculator. |
|
|
Jim Guest
|
The hardware |
Posted: Wed May 10, 2006 7:33 am |
|
|
They hardware I'm using is an nothing more then the 18f4331 on a circuit board with the Pic's pins going to jumper pins. Aside from that I have a MCLR reset switch and ICP pings with a 5v reg and a capacitor to smooth out the signal .. I built this board for testing and prototyping so there is nothing fancy that would alter the signal. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 10, 2006 11:07 am |
|
|
I compared the .LST file from your version of the compiler to vs. 3.249
and they're essentially the same. There is a Microchip Appnote, AN900,
which gives a detailed procedure on how to configure the PCPWM module.
http://ww1.microchip.com/downloads/en/AppNotes/00900a.pdf
I didn't compare it to CCS's code because it would take a while to do.
Quote: |
Each time I get hit reset..I get a different pulse size (x2).
I'm using version 3.236 |
1. Does this problem still occur with the shorter program that you posted ?
2. Have you tried using the Ex_power_pwm.c example file ?
Does it work ? You should now have that file, because it came out
with vs. 3.232. Look in this folder: c:\Program Files\Picc\Examples
3. Here are some possible reasons why you're seeing the problem:
A. Some register that should be configured for PWM mode is not
being configured properly and upon power-on reset, it comes up in a
random state.
B. There could be a bug in the PWM module for the Free Running mode.
The latest errata on your PIC doesn't mention any bugs for that mode
but there are several bugs for other modes. You could have found
a new bug.
C. Possibly there is a problem with the internal oscillator. It may not
be starting up at 8 MHz every time.
D. The CCS generated code may be incorrect.
To work on this problem further, I would have to buy a Power PWM PIC,
probably the 18F4431 instead of your PIC, because it's more common.
I might do that, but it would take several days before I could give you
a response. For all I know, the problem might turn out to be some
power-up problem that you're having, such as the oscillator speed
mentioned above. |
|
|
Jim Guest
|
CCS support found my problem |
Posted: Wed May 10, 2006 12:52 pm |
|
|
"..Make period an int16. The PWM time base register PTPERH and PTPERL contain 12 bits of data for the period. An 8 bit period will throw the calculations off...."
Thanks for all your help guys.. |
|
|
Mit Guest
|
|
Posted: Wed Feb 07, 2007 6:14 am |
|
|
excuse me but I need some help.
i use those code those code from Jim last post with PIC18F4431 and 4MHz Xtal
I already change those include and INTRC_IO to 18F4431.h and XT
i got nothing from output pin
but if i get "set_power_pwm0_duty(500); set_power_pwm0_duty(500); "
out from while loop i get pulse from output pin
can anyone give me some advise |
|
|
|
|
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
|