TOAY i am using pic16f877a microcontroller to generate 10bit pwm

#pragma config FOSC = HS    // Oscillator Selection bits (HS oscillator)

#pragma config WDTE = OFF    // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = OFF   // Power-up Timer Enable bit (PWRT disabled)

#pragma config BOREN = OFF   // Brown-out Reset Enable bit (BOR disabled)

#pragma config LVP = OFF    // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)

#pragma config CPD = OFF    // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)

#pragma config WRT = OFF    // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)

#pragma config CP = OFF     // Flash Program Memory Code Protection bit (Code protection off)


// #pragma config statements should precede project file includes.

// Use project enums instead of #define for ON and OFF.


#include <xc.h>

#define _XTAL_FREQ 20000000

int main()

{

  

  int i = 0;


  TRISC = 0x00;  // Configure PORTC as output(RC2-PWM1, RC1-PWM2)


  PR2 = (_XTAL_FREQ/(50000*4*4)) - 1; //Setting the PR2 formulae using Datasheet // Makes the PWM work in 5KHZ

  CCP1M3 = 1; CCP1M2 = 1; //Configure the CCP1 module 

  T2CKPS0 = 1;T2CKPS1 = 0; TMR2ON = 1; //Configure the Timer module

  TRISC2 = 0; // make port pin on C as output


  while (1) 

  {

    for (i = 0; i<100; i++) // Keep increasing the dutyCyle to increase the brightness of LED

    {

      CCP1X = i & 1; //Store the 1st bit

  CCP1Y = i & 2; //Store the 0th bit

  CCPR1L = i>>2;// Store the remining 8 bit

      __delay_ms(100);

    }


    for (i = 100; i>1;i--) // Keep reducing the dutyCyle to decrease the brightness of LED

    {

      CCP1X = i &1; //Store the 1st bit

  CCP1Y = i & 2; //Store the 0th bit

  CCPR1L = i>>2;// 

      __delay_ms(100);

    }

  }

}

要查看或添加评论,请登录

Embedded enthusiastic的更多文章

社区洞察

其他会员也浏览了