![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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 | |
| |
| | #2 |
|
That's the trouble with stuff you "find on the internet". No details, no undelying assumptions -- just mystery. How you do it:
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. | |
| |
| | #3 |
|
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 | |
| |
| | #4 |
|
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. 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? | |
| |
| | #5 |
|
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 | |
| |
| | #6 |
|
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. | |
| |
| | #7 |
|
How would you do that?
| |
| |
| | #8 |
|
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?
| |
| |
| | #9 |
| | |
| |
| | #10 |
|
The code is posted within the very first post i made.
| |
| |
| | #11 |
| | |
| |
| | #12 |
| I too can't locate any such, except few lines by Mischa why not paste a copy of the file here
__________________ Regards, Sarma. | |
| |
| | #13 |
|
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. | |
| |
| | #14 |
|
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. | |
| |
| | #15 | |
| Quote:
the old Dr Jeckil and Mr Hide whether these two users are same person!!
__________________ Regards, Sarma. | ||
| |
|
| 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 |