damiano
Newbie level 1
- Joined
- Jul 4, 2014
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 6
hi everybody!
i'm student and i'm at my first experience with pic. my target is to produce a PWM signal with an output compare.
i found an example but i don't understand how i can modified the duty cycle. this is the code:
somebody can help me?
thanks in advance
i'm student and i'm at my first experience with pic. my target is to produce a PWM signal with an output compare.
i found an example but i don't understand how i can modified the duty cycle. this is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "p24FJ256DA210.h"
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx2 & FWDTEN_OFF)
_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_OFF & FNOSC_PRIPLL & POSCMOD_HS)
// Initialize I/O pins
void IOInit(void)
{
TRISDbits.TRISD6 = 1;
TRISDbits.TRISD0 = 0;
TRISDbits.TRISD13=1;
TRISAbits.TRISA7=1;
TRISDbits.TRISD7=1;
TRISAbits.TRISA6=0;
TRISAbits.TRISA3=0;
TRISAbits.TRISA2=0;
TRISEbits.TRISE9=0;
TRISBbits.TRISB5=0;
PORTA= 0X0000;
PORTD= 0x0000;
}
void PWM_init(void)
{
OC1CON1 = 0x0000; // Turn off Output Compare 1 Module
OC1R = 0x0000; // Initialize Compare Register1 with 0x0000
OC1RS = 0x0000; // Initialize Secondary Compare Register1 with 0x0000
OC1CON1bits.OCSIDL =0;
OC1CON1bits.OCFLT=0;
OC1CON1bits.OCTSEL=1;
OC1CON1bits.OCM =0x06; // PWM mode on OCx;Fault pin Disable
RPOR9bits.RP18R0=0;
RPOR9bits.RP18R1=1;
RPOR9bits.RP18R2=0;
RPOR9bits.RP18R3=0;
RPOR9bits.RP18R4=1;
RPOR9bits.RP18R5=0;
}
void Timer_Init(void)
{
T3CONbits.TON = 0;
T3CONbits.TSIDL = 1;
T3CONbits.TGATE=0;
T3CONbits.TCKPS= 0;
T3CONbits.TCS=0;
PR3 = 0x0000;
IPC1bits.T2IP = 1; // Setup Output Compare 1 interrupt priority
IFS0bits.T2IF = 0; // Clear Output Compare 1 interrupt flag
IEC0bits.T2IE = 1; // Enable Output Compare 1 interrupts
}
void __attribute__ ((__interrupt__)) _T3Interrupt(void)
{
IFS0bits.T3IF = 0; //Clear Timer3 interrupt flag
}
void Delay(void)
{
int i;
for(i = 0; i < 0xFFFF; i++);
}
int main(void)
{
IOInit();
PWM_init();
Timer_Init();
while(1)
{
OC1R = 50;
OC1RS = 50;
PR3 = 100;
T3CONbits.TON = 1; //Turn on Timer3
}
return 0;
}
somebody can help me?
thanks in advance
Last edited by a moderator: