Hi Ian ,
I have seen your posts in the topic "HOW to Start with PIC16F676" and the help you provided to Mr.RITESH for PWM generation on PIC16F676.
I need some help regarding this, I want to implement 8bit PWM at 2kHZ as u did in the topic, but i want to change the duty cylcle like say i want PWM at pin2(RA5) and for some period of time like 500ms the duty cycle should be 80% and for other 500ms the duty cycle should be 5%.
Can u please help me, i am not able to do it, either i am able to acheive 80% duty cycle or 5% duty cycle
code written in MPLAB IDE:
/*
* File: PWM.c
* Author: s00742788
*
* Created on February 3, 2013, 12:31 PM
*/
#include <xc.h>
#include<htc.h>
#include<pic.h>
#include<pic16f676.h>
#define _XTAL_FREQ 4000000
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
int OnPulse =0;
int OffPulse = 250;
char TOG = 0;
int i = 0;
void interrupt Interrupt_Service_Subroutine(void)
{
T0IF = 0;
i = i+1;
TOG = ~TOG;
if(i>=2001)
{
i=0;
}
if(i<=1000)
{
if(TOG)
{
RA5 = 1;
TMR0 = 242;
}
else
{
RA5 = 0;
TMR0 = 14;
}
}
if((i>1000) && (i<2001))
{
if(TOG)
{
RA5 = 1;
TMR0 = 32;
}
else
{
RA5 = 0;
TMR0 = 224;
}
}
}
void main()
{
int pulse;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC0 = 1;
TRISAbits.TRISA5 = 0;
CMCON = 7;
TMR0 = OnPulse;
ANSEL = 0x10;
OPTION_REG = 0;
T0IE = 1;
GIE = 1;
RC3=1;
while(1)
{
}
}
}
I have seen your posts in the topic "HOW to Start with PIC16F676" and the help you provided to Mr.RITESH for PWM generation on PIC16F676.
I need some help regarding this, I want to implement 8bit PWM at 2kHZ as u did in the topic, but i want to change the duty cylcle like say i want PWM at pin2(RA5) and for some period of time like 500ms the duty cycle should be 80% and for other 500ms the duty cycle should be 5%.
Can u please help me, i am not able to do it, either i am able to acheive 80% duty cycle or 5% duty cycle
code written in MPLAB IDE:
/*
* File: PWM.c
* Author: s00742788
*
* Created on February 3, 2013, 12:31 PM
*/
#include <xc.h>
#include<htc.h>
#include<pic.h>
#include<pic16f676.h>
#define _XTAL_FREQ 4000000
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
int OnPulse =0;
int OffPulse = 250;
char TOG = 0;
int i = 0;
void interrupt Interrupt_Service_Subroutine(void)
{
T0IF = 0;
i = i+1;
TOG = ~TOG;
if(i>=2001)
{
i=0;
}
if(i<=1000)
{
if(TOG)
{
RA5 = 1;
TMR0 = 242;
}
else
{
RA5 = 0;
TMR0 = 14;
}
}
if((i>1000) && (i<2001))
{
if(TOG)
{
RA5 = 1;
TMR0 = 32;
}
else
{
RA5 = 0;
TMR0 = 224;
}
}
}
void main()
{
int pulse;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC0 = 1;
TRISAbits.TRISA5 = 0;
CMCON = 7;
TMR0 = OnPulse;
ANSEL = 0x10;
OPTION_REG = 0;
T0IE = 1;
GIE = 1;
RC3=1;
while(1)
{
}
}
}