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.

  • 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.

120khz square wave generation

Status
Not open for further replies.

mischa

New Member
Hi Guys. I need some help with the following. I need to know how to generate a 1ms long burst of 120Khz square wave signal using P1_1,using the pic16f877 and the c8051f320 microcontroller by Silicon Labs. I was initially thinking of using the CCP modules to do this but I found an easier way on the internet:

void squarewave(void) //generates a square wave of 138kHz
unsigned int j;
for(j=0;j<0XFF;j++)
{
P1_1 =~ (P1_1); //Please explain this line of code to me???
}
P1_1 = 0;
}

But how do you know that the above wave would generate a square wave and especially that of a frequency of 138kHz. Please help me. How would I then make certain that it’s also only a burst of 1ms.

Thanks a lot
 

Papabravo

Well-Known Member
That's the trouble with stuff you "find on the internet". No details, no undelying assumptions -- just mystery.

How you do it:
  1. Compute the half period of 138kHz or 120 kHz. You decide which, because I'm not a mind reader.
  2. Turn the output on for 1 half period
  3. Turn the output off for one half period
  4. Repeat 2 & 3 for the number of periods of 120kHz or 138 kHz in 1 millisecond

The line you asked for an explanation on is quite simple. It read the value of P1_1 which should be either a 1 or a zero. It applies the unary operator ~ (tilde) to that value, and it wites the result back to P1_1.

You can go to a 'C' language reference and look up the tilde (~) operator or you can make three guesses as to what it does but the first two guesses don't count.
 

rajbex

Member
The time period of 120KHz square wave is 8.3 us. So you need to send high to an output pin for 4.15 us, and low for the other half time. The timing calculation requires what frequency crystal you are going to use, and what microcontroller you gonna select.
P1_1 =~ (P1_1); means find the complement of P1_1 and assign the new value to P1_1. 1's complement is 0, and 0's complement is 1.

- Raj
Experiments with PIC16F628A
 

mischa

New Member
hey

thanks guys for the help. I have decided to use the pic16f877.

Rajbex, i have included a diagram of the crystals i will be using (7.680Mhz and 32kHz) crystals. pic_timers.jpg Can you tell me how i would calculate a 120khz square wave for 1ms now and what this function would look like with these changes.

Another question i have regarding the code is this: should the P1_1=0; come before the p1_1=~(p1_1) line. Because we dont know what the pin status is to start with. what if is initially a 1?

Another thing, would it not be possible to generate a 120khz pwm output using the ccp1 nd ccp2 modules and then just time it for 1ms using a timer? How can you code this though?
 

rajbex

Member
1. I am sorry, I have never seen two Xtals connected to a PIC like this, so I can't help in this.
2. Of course you have to initialize the value of P1_1 as 0 or 1 first.
3. I would suggest to read some literature on timers and delays with PICs. If you want to go with an easy way, try any high level language compiler, like mikroC; it has a built in function delay_us(); to generate the delay in microseconds. And you don't have to use two XTALs.

- Raj
Experiments with PIC16F628A
 

Pommie

Well-Known Member
Most Helpful Member
You could use the two oscillators to produce your signals but I doubt it is necessary. What are the frequencies that you are trying to generate? You can use 1 of the ccp modules to produce one frequency and then probably timer1 for the other. Actually, thinking about it you could set it up so it was all transparent to the main code.

BTW, you keep posting 8051 code whilst talking about pic chips. The hardware available is not similar and code that runs on a pic will not run on an 8051.

Mike.
 

preeste

New Member
Wont they code i put up work for the pic16f877? How would you go about this if there was only one oscillator-lets say the 7.805mhz?
 

mischa

New Member
Mike and Sarma here is the code I posted. I got it off the internet and it is supposed to generate a 138kHz square wave. I want to know how it does this and how I can modify the code to 120kHz.

void squarewave(void) //generates a square wave of 138kHz
unsigned int j;
for(j=0;j<0XFF;j++)
{
P1_1 =~ (P1_1);
}
P1_1 = 0;
}


I also want to know how I can generate a 120Khz (50% DC) pwm output using the ccp1 module. To put all of this in perspective, I have a switch connected to RB0. When this switch is switched on, a 120khz PWM should be generated onto RC3 pin for only a period of 1ms. How do I do this using c code? The Xtal I use is of the value 7.805Mhz. I was thinking that i should continously generate a 120Khz pwm but output it onto RC3 using Trisc for only 1ms. But how do i do this using c. Please help…..I have been struggling with this for a while now.
 

Pommie

Well-Known Member
Most Helpful Member
It's confusing when you post as two different posters and refer to the other poster as yourself.

Anyway, with your 7.805MHz crystal the closest you can get is to put 14 or 15 in PR2. This will give a frequency of 1/((14+1)*4*1(7.805*10^6)) = 130kHz or 15 in PR2 gives 122kHz. You would put half the PR2 value in CCPRnL/H.

Mike.
 

mvs sarma

Well-Known Member
It's confusing when you post as two different posters and refer to the other poster as yourself.

Anyway, with your 7.805MHz crystal the closest you can get is to put 14 or 15 in PR2. This will give a frequency of 1/((14+1)*4*1(7.805*10^6)) = 130kHz or 15 in PR2 gives 122kHz. You would put half the PR2 value in CCPRnL/H.

Mike.
pommie. I had the same doubt
the old Dr Jeckil and Mr Hide
whether these two users are same person!!
 

mischa

New Member
By the way....two very different people who know each other....but i ended up using preeste's account by mistake.
 

atferrari

Well-Known Member
Stick with one and grasp the basics

Dissociative identity disorder happens with people that have more than one account in the same forum. Stick with one or you will repeat the performance. (You waste bandwith and the time of contributors).

To you both, please decide yourself to:

1 - stick with one micro PIC or whatever.

2 - Read the relevant part in the respective datasheet. Once you grasp the concepts frequency = 1/period of PWM signal and duty cycle (how much of that period you want the output high), you should be prety much able to attempt a test with any PIC fitted with PWM capability.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top