• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Question about Hz and programming

Status
Not open for further replies.

psecody

Member
So last night I finally sat down and started working on the basic functions of a robot I'm building in my spare time. I don't actually have any servos with me at the moment to test the program but I started trying to figure out some of the basics anyway. So I'm using MikroC which has a PWM set of functions built into it. MikroC Manual (Page 303 is what I'm talking about)

In the manual the it details using Pwm_Init, Pwm_Start, Pwm_Change_Duty, and Pwm_Stop.

So in my program I first initialized it using the Pwm_Init where in parenthesis you're supposed to put the frequency. As I understand this the frequency is basically the period of the pwm, correct? Well I'm wanting about a 20mS period so this should be 50Hz (1000/20). When I go to try to compile the code it gives me an error saying that number is out of range or something similar to that. I typed it in exactly like Pwm_Init (50); I assume that the frequency is supposed to be written in Hz because the example uses Pwm_Init (5000); and says its 5KHz. So 50 would be right, right?

Also I was wondering if the oscillator that I'm using could be part of the problem (thats what I suspect) I'm using an 8MHz crystal but I also have a few 20MHz crystals laying around. Can someone also kind of explain to me how to figure out what speed of crystal you need for your projects? I've always wondered that.

Thanks I appreciate all the help.
 

psecody

Member
I was going to use it to control servos. I've never been able to quite 100% figure out how to do PWM, every time I get close I find something new that confuses me more than the last thing I was stumbling over.

The reason I used 50Hz was that I looked up what the frequency for a generic servo is (since I don't have the specific one's I'll use picked out yet).

I figured 50Hz was to slow for the 8MHz crystal thats why I tried it first as opposed to the 20MHz but what I don't understand about that is how you're supposed to figure out what crystal will work with that. I'm using the CCP modules on a PIC18F2450 by the way I forgot to mention that.
 

psecody

Member
Ok so I'm just going to kind of try to write a program on here to move a servo or something and if ya'll could kind of correct me on what I'm doing wrong I'd appreciate it.

@blueroomelectronics - I remember now after you said that I looked up the CCP module and am going to try using an interrupt and a special event trigger.

I think I kind of grasp it but I'm not 100% sure yet.
Thanks for the help guys.
 

Al1970

New Member
Hi:

"Also I was wondering if the oscillator that I'm using could be part of the problem (thats what I suspect) I'm using an 8MHz crystal but I also have a few 20MHz crystals laying around. Can someone also kind of explain to me how to figure out what speed of crystal you need for your projects? I've always wondered that.
Thanks I appreciate all the help. "

Almost always use the highest speed crystal that your chip can take if you are going to use a crystal. You can change the speed if you need to with the chip timers. The reason I said "Almost always" is in a case where you may have a project that you don't need much computer power and want to use the lowest power you can. Low clock speed lower power.

Al
 

be80be

Well-Known Member
I don't know what happen to the link I fixed it It was the one you did Mike
 

be80be

Well-Known Member
Hi:

"Also I was wondering if the oscillator that I'm using could be part of the problem (thats what I suspect) I'm using an 8MHz crystal but I also have a few 20MHz crystals laying around. Can someone also kind of explain to me how to figure out what speed of crystal you need for your projects? I've always wondered that.
Thanks I appreciate all the help. "

Almost always use the highest speed crystal that your chip can take if you are going to use a crystal. You can change the speed if you need to with the chip timers. The reason I said "Almost always" is in a case where you may have a project that you don't need much computer power and want to use the lowest power you can. Low clock speed lower power.

Al
Faster oscillator is good for things like high speed uart and Usb

To fast osc and you can't run pwm in low hz

Most things run fine with 4 to 8 mhz osc

low power you need to look it up in the datasheet what to use
 

Al1970

New Member
Hi

"To fast osc and you can't run pwm in low hz"

Sure you can. I can use a 20M Hz clock and do PWM of less than 1 hz !

Al
 

be80be

Well-Known Member
Hi

"To fast osc and you can't run pwm in low hz"

Sure you can. I can use a 20M Hz clock and do PWM of less than 1 hz !

Al
Hardware pwm is based on clock if your clock is fast you can't get 1hz

you can bit bang pwm and do it
 

Pommie

Well-Known Member
Most Helpful Member
Burt,

You can use the hardware to produce slow PWM, you just have to use the CCP module in a different way.

Mike.
 

be80be

Well-Known Member
I use the pwm calculator. Doing it the normal way with a 31khz clock as slow as I can get it was 18 hz

I would sure like to see your way Pommie .

And it wouldn't hurt to see how Al1970 makes his work.
 
Last edited:

psecody

Member
Ok so I haven't had an internet connection and have been having to tether my iphone just to use any internet and I haven't been doing that much because of at&t's limited 'unlimited' data plan. Anyway I looked at this code:

#include <p18f1320.h>
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO2
#define ServoPin LATBbits.LATB3
#define ServoTris TRISBbits.TRISB3

void main(void){
int ServoPos;
OSCCON=0x70; //Osc=8MHz
ADCON0=0b00000101; //A2D on and select AN1
ADCON1=0x7d; //A1 = analogue
ADCON2=0b10110101; //Right justify - Fosc/16
ServoTris=0; //make servo pin output
ServoPin=0; //Servo output off
CCP1CON=0b00001011; //Special event trigger
T1CON=0b10010001; //Timer 1 on with Pre=2
ServoPos=1500; //set servo to mid position
CCPR1=ServoPos; //set CCP initial value
while(1){
while(!PIR1bits.CCP1IF); //wait for CCP interrupt bit
ServoPin=0; //end pulse
CCPR1=20000-ServoPos; //Off time = 20mS - Servo Time
PIR1bits.CCP1IF=0; //clear int flag
ADCON0bits.GO=1; //start conversion
while(ADCON0bits.GO); //Wait for it to complete
ServoPos=ADRES+1000; //Pos will be 1mS to 2.023mS
while(!PIR1bits.CCP1IF); //wait for int flag
ServoPin=1; //start pulse
CCPR1=ServoPos; //Servo time in uS
PIR1bits.CCP1IF=0; //clear int flag
}
}
As I'm just wanting to hard code certain servo movements I wouldn't need to mess with all the A/D stuff right? I mean I could just turn all A/D stuff off and it shouldn't effect my code right? Also on Timer1 the prescaler is set to two, what does that mean? How did you come up with 2? Other than that I think I actually am (slowly) starting to understand more about this... it all just seems to be falling into place here and there. I'm going to try and write some code up that'll just start one servo moving a certain amount or something and post it up and see if I figured it out or if it's back to the drawing board. Thanks guys.
 

Pommie

Well-Known Member
Most Helpful Member
You can turn the A2D stuff off and just set ServoPos to a value between 1000 and 2000 (1mS-2mS). The prescaler value of 2 was chosen so that the timer counts uS. So a count of 1000 is equal to 1mS.

Mike.
 

Al1970

New Member
Hi be80be:

I do PWM in software. If you want to control servos, the best way is to do it is in software. Then the only limit to how many servos a chip can control is the number of I/O pins the chip has.

Al
 

Mr RB

Well-Known Member
You could get one of these;
Official homepage of TalkBotBrain.com

it is a ready made PCB with open source firmware that uses code in MikroC for a PIC 16F628. It automatically drives 8 servos, all you do is write to 2 bytes for a servo (MSB and LSB) and the 8 servos are auto positioned for you (to a 0.2uS resolution).

And it talks! You can put sounds in as well so your robot can talk (up to 256 sounds) and have up to 8 servos. I have a couple little bots here running around with these little brains in, they're pretty cool for a tiny brain.
 

Mike - K8LH

Well-Known Member
I'd like to dispel the myth that the PWM module is poorly suited for low frequency PWM periods (again). The PWM module along with a small low overhead interrupt driven "helper" works exceedingly well at producing jitter free extremely high resolution pulses and very low frequency PWM periods. There's an example here.

Regards, Mike
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top