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

PWM for ultrasonic
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hobby_85



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

PostPosted: Tue Aug 18, 2009 10:02 pm     Reply with quote

Hey guys,

Just wanted to say that the pwm signal worked. Thanks for all the help, especially mkuang, languer and bungee. Very much appreciated.

For those who want to know the final code, its the previous code I posted with the changes suggested afterward. So remove xt, nolvp fuses and swap include to have #include <16f690.h>

Thanks heaps!
klavdij



Joined: 20 Sep 2009
Posts: 5

View user's profile Send private message

PostPosted: Sun Sep 20, 2009 12:15 pm     Reply with quote

hello !!

I have a similar problem so no use in opening a new topic i guess ... below is some explanation but if you're not in a reading mood just jump to the bottom of the post - at the end of the QUOTE area ;)

Quote:
I'm trying to make an ultrasonic range meter based on some web designs i found .... the US transmitter is powered by PWM and the receiver sends data on ADC
the circuit is on this link ... not that it matters but just in cse if anybody is interested Smile
http://www.micro-examples.com/pics/090-ULTRASONIC-RANGER-schematic.png

i allready made the whole thing but for some reason the receiver doesn't pick up any signal so im stuck in a while loop that waits for ADC result to bo not zero ...

If i connect the PWM directly to ADC the loop breaks so i guess that the problem can be in either
- one of the sensors not working or
- the frequency of PWN is not 40kHz so the receiver doesn't pick anything up

i dont have any real equipment to check for these things so i'm kinda stuck


I'm using PIC18F4450 and data for PWM i'm using is

PR2 = 74
CCPR1L = 150
CCP1CON = 101100
and prescaler : 4

the whole C port is made an output, im also using another pin from C to power up a led display

could u please tell me if this produces 40kHz and 50% duty cycle

and also can u tell me what do the functions so:
Code:

setup_ccp1(CCP_PWM);          // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1,24,1);     // 40000 Hz     
set_pwm1_duty(62);

since i dont have the library's - i dont mean in words like they set the duty cycle etc. but to what registers they write etc ... best to simply copy their code ...


ow and i need this for a college seminar and am kinda in a hurry - million thanks Smile
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Sun Sep 20, 2009 12:55 pm     Reply with quote

What is the frequency of your oscillator. Without that it is impossible to check the code.
klavdij



Joined: 20 Sep 2009
Posts: 5

View user's profile Send private message

PostPosted: Sun Sep 20, 2009 1:18 pm     Reply with quote

hm... I'm using PIC18f4450 and made no changes and am using no external oscillator.... so I'm guessing it should be 48MHz but I could be wrong ...

The datasheet for the pic is on this location:
http://ww1.microchip.com/downloads/en/DeviceDoc/39760d.pdf

Quote:
anyhow it says:
Operating Frequency: DC – 48 MHz

and also
Flexible Oscillator Structure:
• Four Crystal modes, including High-Precision PLL
for USB
• Two External Clock modes, up to 48 MHz
• Internal 31 kHz Oscillator
• Secondary Oscillator using Timer1 @ 32 kHz
• Dual Oscillator Options allow Microcontroller and
USB module to Run at Different Clock Speeds
• Fail-Safe Clock Monitor:
- Allows for safe shutdown if any clock stops


ow and I forgot before the function that I'm using to set PWM is
Code:

void PWM_Init_Manual(int pr,int duty,int control){
   CCP1CON = control;  //PWM mode
   PR2 = pr;           //Load Period Register
   CCPR1L = duty;      //Set 50% duty cycle
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 20, 2009 1:27 pm     Reply with quote

Quote:

since i dont have the library's - i dont mean in words like they set the
duty cycle etc. but to what registers they write etc ... best to simply copy
their code ...

You don't have the CCS compiler.
klavdij



Joined: 20 Sep 2009
Posts: 5

View user's profile Send private message

PostPosted: Sun Sep 20, 2009 3:10 pm     Reply with quote

Don't know about that... I'm pretty new at this so I'm just writing everything by hand and using the PIC datasheet ... for programing I'm using pickit2 and software that - its mplab ide.
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Mon Sep 21, 2009 1:35 am     Reply with quote

klavdij wrote:
Don't know about that... I'm pretty new at this so I'm just writing everything by hand and using the PIC datasheet ... for programing I'm using pickit2 and software that - its mplab ide.
You should try Microchip's forum Wink
klavdij



Joined: 20 Sep 2009
Posts: 5

View user's profile Send private message

PostPosted: Mon Sep 21, 2009 3:19 am     Reply with quote

Cool

yea well didnt realise that this was such a tight crowd ... i just found the topic with google and since the values are the same no matter what u use ....
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Mon Sep 21, 2009 4:12 am     Reply with quote

klavdij wrote:
Cool

yea well didnt realise that this was such a tight crowd ... i just found the topic with google and since the values are the same no matter what u use ....
well it is hard to help you on ccs forum if you do not have ccs.

In CCS it is trivial to setup pwm and you do not need any library for that.

At 48MHz code would look like that:
Code:

   setup_timer_2(T2_DIV_BY_4,74,1);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(512);
klavdij



Joined: 20 Sep 2009
Posts: 5

View user's profile Send private message

PostPosted: Mon Sep 21, 2009 5:59 am     Reply with quote

thanks .. that is basically what i needed ... i just asked for the functions cause i am curious - i see now that they are a compiler thing (found some for mplab but nothing that usefull)

just needed to know if the values im using are correct...

setup_timer_2(T2_DIV_BY_4,74,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(512);


would mean duty - 512 and period - 75 or if i put it by registers
PR2=74
duty bits (10 bits - CCP1CON (6,5 - lsb) and CCP1R) form 512
and prescaler is 4 - so last two bits of T2CON are 01


i'll let u know if it works and post some code if anybody is interested Smile
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 Previous  1, 2
Page 2 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