+ Reply to Thread
Page 2 of 2
First 1 2
Results 16 to 24 of 24

Thread: 120khz square wave generation

  1. #16
    mischa Newbie
    Join Date
    Sep 2009
    Posts
    29

    Default

    But how would i code this in c? And what about the 1ms part?


  2. #17
    mischa Newbie
    Join Date
    Sep 2009
    Posts
    29

    Default

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

  3. #18
    atferrari Good atferrari Good atferrari Good
    Join Date
    Oct 2003
    Location
    Buenos Aires - Argentina
    Posts
    571

    Default 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.
    Agustín Tomás
    In theory, there is no difference between theory and practice. In practice, however, there is.

  4. #19
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,784

    Default

    Quote Originally Posted by mischa View Post
    But how would i code this in c? And what about the 1ms part?
    Well, hopefully you are learning C and can code this yourselves.Post your attempt and we will help.

    Mike.

  5. #20
    mischa Newbie
    Join Date
    Sep 2009
    Posts
    29

    Default

    I am working on it. Will put up the code as soon as i am done

  6. #21
    mischa Newbie
    Join Date
    Sep 2009
    Posts
    29

    Default

    Hey. Here is my code. Please let me know if this code is right. I have decided to use a 12Mhz crystal instead of the 7.805Mhz.

    Just to recap:
    I want to generate a 120Khz square wave that will last for 1ms (done by gating the RC2 pin) on RC2 when a switch connected to portD.5 is pressed.

    Using the formulaes:
    PWM period= [((PR2)+1)*4*Tosc*TMR2 prescalar value] and
    PWM duty cycle = (CCPR1L:CCP1CON<5:4>)*Tosc*TMR2 prescalar value,

    I decided to use the prescale value =1, this then gave me the following values:
    PR2= 24 (0b00011000)
    CCPR1L:CCP1CON<5:4> = 50 (0b0000110010)-{10 bit value}
    Since CCP1CON<5:4> is the 2 LSb value = "10"--> CCP1CON="0b00101100"
    therefore: CCPR1L=8 Msb="0b00001100"

    CODE
    ------------------------------------------------------------------------------------

    #include "16F877.h"
    #include "DEFS_877.H"

    #include "string.h"
    #include <stdlib.h>
    #include <stdio.h>

    #use delay (clock=12000000)
    #fuses HS,NOWDT,PROTECT,NOLVP,BROWNOUT,PUT
    #use rs232(baud=56000,parity=N,xmit=PIN_C6,rcv=PIN_C7)

    set_tris_d(0xff); //setting port d as input

    void squarewave (void)
    {

    PR2=0b00011000; //set PWM period to 8.3333us
    CCPR1L=0b00001100; //sets Duty cycle to 50%
    CCP1CON=0b00101100; //Dc=50%
    T2CON=0b00000100; //enables timer 2 and sets the prescalar value
    set_tris_C(0x00); //output pwm to pin RC2
    }


    void main (void)
    {
    //just initailises and clears port c initially
    set_tris_c(0xFF); //set port c as input
    PortC=0; //clear portC

    while(1)
    {
    if (!input(PIN_D5)) //if switch is closed/pressed
    {
    squarewave();
    delay_ms(1); //wave on RC2 for a period of 1ms
    set_tris_c(0xFF); //sets RC2 as input, so no more
    //Pwm present on this pin
    }
    }
    }
    -----------------------------------------------------------------------------------

    Please take a look at my code and let me know if its accurate and correct. Thanks a lot

  7. #22
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,784

    Default

    Re your PM. I tried the above code in BoostC and it appears to work as intended. There was some problems but I think that might be just compiler differences. However, the line,

    set_tris_d(0xff); //setting port d as input

    will never get executed.

    Mike.

  8. #23
    mischa Newbie
    Join Date
    Sep 2009
    Posts
    29

    Default

    Why would that line not be executed?

  9. #24
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,784

    Default

    Because execution always starts at main. I'm surprised the complier doesn't error with that line.

    Mike.

+ Reply to Thread
Page 2 of 2
First 1 2

Similar Threads

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

Tags for this Thread