aniketrane
New Member
Hi guys,
I am working on PIC18F452. I am having problem generating PWM with it. I am using Mplab 6.3 along with MPLAB-C18. I also tried to work on assembly code. It has been derived from the tutorial by Nigel Goodwin at
I have tried everything as the tutorial says but still I cant get the PWM output. I am using a DSO to observe the waveforms. My PIC board runs at 4MHz as in contrast to Nigel’s 20 MHz demo, but I think that I should get some output at different frequency.
Where I may be going wrong? Do I need to connect any Resistor or capacitor at PWM output pin to observe waveform ( I am directly connecting DSO probe to it.)?
Please reply urgent as possible.
Below is the Code for MPLAB-C18
I am working on PIC18F452. I am having problem generating PWM with it. I am using Mplab 6.3 along with MPLAB-C18. I also tried to work on assembly code. It has been derived from the tutorial by Nigel Goodwin at
I have tried everything as the tutorial says but still I cant get the PWM output. I am using a DSO to observe the waveforms. My PIC board runs at 4MHz as in contrast to Nigel’s 20 MHz demo, but I think that I should get some output at different frequency.
Where I may be going wrong? Do I need to connect any Resistor or capacitor at PWM output pin to observe waveform ( I am directly connecting DSO probe to it.)?
Please reply urgent as possible.
Code:
include "P18F452.inc"
LIST P=18F452
org 0x00
goto start
start:
MOVLW 0x06
MOVWF ADCON1
MOVLW 0 ;set PORTC as all outputs
MOVWF TRISC
MOVF CCP1CON,W ;set CCP1 as PWM
ANDLW 0xF0
IORLW 0x0C
MOVWF CCP1CON
MOVLW 126 ;set highest PWM value
BANKSEL PR2 ;over this (127) is permanently on
MOVWF PR2
MOVF T2CON,W ;set prescaler to 16
ANDLW 0xF8 ;PWM at 2500HZ
IORLW 0x02
MOVWF T2CON
MOVF T2CON,W ;set postscaler to 1
ANDLW 0x07
IORLW 0x00
MOVWF T2CON
CLRF CCPR1L ;set PWM to zero
BSF T2CON, TMR2ON ;and start the timer running
end
Below is the Code for MPLAB-C18
Code:
#include <p18F452.h>
#include<delays.h>
#include<timers.h>
#include<pwm.h>
void main()
{
ClrWdt();
TRISC=0x00;
TRISB=0x00;
PORTC=0x00;
While(1)
{
PORTB=0x80;
OpenTimer2(TIMER_INT_OFF & T2_PS_1_16& T2_POST_1_1 );
OpenPWM1(0xF8);
SetDCPWM1(126);
}
ClosePWM1();
CloseTimer2();
}