Jump to content

Recommended Posts

hi all

im new to boostc and i cant find a clear example of pwm.

 

i use a pic16f876a and all i ask is a simple example of pwm running with 20mhz crystal

with a pwm of 2khz, 50% duty cycle on RC2.

 

i cant find any referance in manual of on the web, so dont tell me to check some site

 

any body can help me ?

Link to post
Share on other sites

I have some PIC16F887 code hanging around. It wil need a few changes for your PIC:

/*********************** PWMTEST ****************/
/* For PIC16F887 on Microchip 44-Pin Demo Board */
/* in Pickit 2 Debug Express kit				*/
/* Takes AN0 ADC input (from pot) and outputs   */
/* inverted value as PWM on RC2.		*/
/************************************************/

#pragma CLOCK_FREQ 8000000
#include <PIC16F887.h>
#include <boostc.h>
#include <adc.h>

//see DS41291E, page 210 
#pragma DATA _CONFIG1,	   _LVP_OFF	 /* RB3 pin is digital I/O, HV on MCLR must be used for programming */\
					& _FCMEN_OFF/* Fail-Safe Clock Monitor is disabled */\
					& _IESO_OFF /* Internal/External Switchover mode is disabled */\
					& _BOR_ON	 /* BOR enabled */\
					& _CPD_OFF	 /* Data memory code protection is disabled */\
					& _CP_OFF	 /* Program memory code protection is disabled */\
					& _MCLRE_ON /* RE3/MCLR pin function is MCLR */\
					& _PWRTE_OFF/* PWRT disabled */\
					& _WDT_OFF	 /* WDT disabled and can be enabled by SWDTEN bit of the WDTCON register */\
					& _INTOSCIO /* I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN */

#pragma DATA _CONFIG2,	   _WRT_OFF	/* No prog memmory write protection */\
					& _BOR40V	 /* Brown-out Reset set to 4.0V */

// The demo board has a pot (0-5V) on RA0/AN0 and an active low button on
// RB0/INT.  The eight LEDs on port D are active high. 
// RB6 and RB7 are reserved for ICSP/Debugging and should be set as inputs 
// and not used.

void init(void)
{
//Initialise PortD
trisd = 0;		//configure port D
portd = 0;		//clear port D

//Initialise ADC
ansel=0b00000001;	// AN0 is analog, all others (AN7-AN1) digital
anselh=0b000000;	// All (AN8-AN13) digital

adcon1.ADFM=1; // AD result needs to be right justified
adcon1.VCFG0=0; // Vref+ = Vdd
adcon1.VCFG1=0; // Vref- = Vss
set_bit( adcon0, ADCS1 ); // Select Tad = 32 * Tosc (this depends on the Xtal here 8 MHz, should work up to 20 MHz)
clear_bit( adcon0, CHS0 ); // Channel 0
clear_bit( adcon0, CHS1 ); //
clear_bit( adcon0, CHS2 ); //
volatile bit adc_on @ ADCON0 . ADON; //AC activate flag
adc_on = 1; // Activate AD module

//Example 14-5: PWM Initialization - from MPASM code in datasheet
#define PWM1 2
ccp1con=0; // CCP Module is off
tmr2=0; // Clear Timer2
pr2=1000/4; // period register is 8 bit, 2 implied low bits

ccpr1l=0; // Duty Cycle is 0% of PWM Period
intcon=0; // Disable interrupts and clear T0IF
trisc.PWM1=0; // Make pin output
pie1=0; // Disable peripheral interrupts
pir1=0; // Clear peripheral interrupts Flags
ccp1con=0x0C; // PWM mode, 2 LSbs of Duty cycle = 00
t2con.T2CKPS1=1; // Select /16 prescaler
t2con.TMR2ON=1; // Timer2 starts to increment
/*
; The CCP1 interrupt is disabled,
; do polling on the TMR2 Interrupt flag bit
;
PWM_Period_Match
BTFSS PIR1, TMR2IF
GOTO PWM_Period_Match
;
; Update this PWM period and the following PWM Duty cycle
;
BCF PIR1, TMR2IF
*/
}

void main(void)
{
unsigned short temp;
init();	
//Every 100ms sample ADC, discard 2 low bits and output on port D LEDS
while( 1 )
{
	temp = adc_measure( 0 );
	portd=temp>>2;
	temp=1023-temp;
	ccp1con=(ccp1con&0x0f)|((temp&0x03)<<4);
	ccpr1l=temp>>2;
	delay_ms( 100 );
}
}

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...