Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 26th October 2009, 09:30 PM   #1
Default 120khz square wave generation

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
mischa is offline  
Old 26th October 2009, 11:15 PM   #2
Default

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.
__________________
We never have time to do it right; but we always have time to do it over.
Papabravo is offline  
Old 27th October 2009, 02:31 AM   #3
Default

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
rajbex is offline  
Old 27th October 2009, 08:58 AM   #4
Default 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. 120khz square wave generation-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?
mischa is offline  
Old 27th October 2009, 02:38 PM   #5
Default

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
rajbex is offline  
Old 27th October 2009, 02:56 PM   #6
Default

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.
Pommie is online now  
Old 27th October 2009, 05:03 PM   #7
Default

How would you do that?
preeste is offline  
Old 27th October 2009, 05:11 PM   #8
Default

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?
preeste is offline  
Old 27th October 2009, 05:16 PM   #9
Default

Quote:
Originally Posted by preeste View Post
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?
Maybe I missed something. Where did you post any code?

Mike.
Pommie is online now  
Old 27th October 2009, 06:37 PM   #10
Default

The code is posted within the very first post i made.
preeste is offline  
Old 28th October 2009, 04:33 AM   #11
Default

Quote:
Originally Posted by preeste View Post
The code is posted within the very first post i made.
Nope, just had another look and you haven't posted any code in this thread.

Mike.
Pommie is online now  
Old 28th October 2009, 07:27 AM   #12
Default

Quote:
Originally Posted by preeste View Post
The code is posted within the very first post i made.
I too can't locate any such, except few lines by Mischa
why not paste a copy of the file here
__________________
Regards,
Sarma.
mvs sarma is offline  
Old 28th October 2009, 09:19 AM   #13
Default

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.
mischa is offline  
Old 28th October 2009, 09:53 AM   #14
Default

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 is online now  
Old 28th October 2009, 10:25 AM   #15
Default

Quote:
Originally Posted by Pommie View Post
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!!
__________________
Regards,
Sarma.
mvs sarma is offline  
Reply

Tags
120khz, generation, square, wave

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Square wave generation using CCP Modules on 16f747 sp_vibes Micro Controllers 1 22nd February 2009 03:08 PM
Converting a square wave into sine wave using a D/A converte khalifemazen Electronic Projects Design/Ideas/Reviews 6 21st May 2008 11:21 PM
reg: square wave to sine wave converter deepika97 Electronic Projects Design/Ideas/Reviews 4 20th May 2008 08:10 PM
circuit help: square and sin wave input = square wave output spyghost Electronic Projects Design/Ideas/Reviews 2 28th April 2004 02:09 AM
LC filter- filtering a square wave to sine wave kken6248 General Electronics Chat 3 22nd April 2004 05:32 AM



All times are GMT. The time now is 02:05 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker