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

Need Help [to generate sine wave by using pwm] [pic18f46k20]

Status
Not open for further replies.

himarmy

New Member
Hey all,
Right now I try to modified the program that come up with the board PIC18f4xk20 starter kit.
After passing rc low pass filter on the board the result are not near sine wave. Could your guys guide me how to do this? I need a sine wave that have very high frequency but right now the most frequency I got is just 1kHz by using prescale of 1:1. How to increase the maximum frequency??

This is the schemetic of the rc low pass filter but I m not sure which type it is and dont know its cut off frequency.
HTML:
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en535806
From port rc2 to j4


Thanks,

Code:
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                       // CONFIG1H
#pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30                        // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                     // CONFIG2H
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC      // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF                                                  // CONFIG7H

/** I N C L U D E S **************************************************/
#include "p18f46k20.h"
#include "delays.h"
#include "12 CCP PWM.h"  // header file

/** V A R I A B L E S *************************************************/
#pragma idata   // declare statically allocated initialized variables


/** D E C L A R A T I O N S *******************************************/
#pragma code  // declare executable instructions

void main (void)
{
    unsigned char brightness = 125; // = 0x7D

 
	TRISCbits.TRISC2 = 0;

    // Set up 8-bit Timer2 to generate the PWM period (frequency)
    T2CON = 0b00011100;// Prescale = 1:1, timer on, postscale not used with CCP module
    PR2 = 249;         // Timer 2 Period Register = 250 counts
    // Thus, the PWM frequency is:
    // 1MHz clock / 4 = 250kHz instruction rate.
    // (250kHz / 11 prescale) / 250) = 1kHz, a period of 16ms.

    // The Duty Cycle is controlled by the ten-bit CCPR1L<7,0>:DC1B1:DC1B0
    // 50% Duty Cycle = 0.5 * (250 * 4) = 500
    CCPR1L = 0x7D;   // The 8 most significant bits of the value 500 = 0x1F4 are 0x7D
                     // The 2 least significant bits of the value (0b00) are written
                     // to the DC1B1 and DC1B0 bits in CCP1CON
    CCP1CON = 0b10001101;
                     // P1Mx = 00 steering mode, 10 = p1A,B modulate
                     // signal on P1D to LED7.  Only Single Output (00) is needed,
                     // but the P1A pin does not connect to a demo board LED
                     // CCP1Mx = 1101 P1A high   p1b low

    // The LED brightness is affected by by the Duty Cycle, which determines how much
    // of each 16ms period it is on and how much it is off.  As the duty cycle gets
    // less than 50%, it is off more than it is on so the LED becomes dimmer.  As the 
    //duty cycle increases above 50%, it is on more than off, so it gets brighter.
    //
    // This increases the brightness over 2 seconds, then decreases it over the next 2 seconds
    // Updating the CCPR1L value more than once per 16ms period has no benefit, so we'll update
    // it a total of 125 times, once per period, which works out to 2 seconds.
    //
    // Although we have nearly ten bits of resolution in the duty cycle (1000 counts)
    // we'll increment the duty cycle by 8 each time as we only have 125 levels over the
    // 2 second period.
    while(1)
    {
        // increase brightness over 2 seconds.
        do
        {
            brightness += 2;
            CCPR1L = brightness;    // + 8 including 2 bits DC1Bx in CCP1CON
            PIR1bits.TMR2IF = 0;        // clear interrupt flag; set on every TMR2 = PR2 match
            while (PIR1bits.TMR2IF == 0); // watch for match, which is end of period.
        } while (brightness < 250);

      

        // decrease brightness over 2 seconds.
        do
        {
            brightness -= 2;
            CCPR1L = brightness;    // - 8 including 2 bits DC1Bx in CCP1CON
            PIR1bits.TMR2IF = 0;        // clear interrupt flag; set on every TMR2 = PR2 match
            while (PIR1bits.TMR2IF == 0); // watch for match, which is end of period.
        } while (brightness > 1);

        
    };    	
}



Thx so much
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top