electrophysics
Member level 3
- Joined
- May 10, 2010
- Messages
- 58
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 1,288
- Activity points
- 1,773
Hi! I'm programming a PIC16F690 to generate diagonal PWM outputs to be given to full bridge and later filtered. However the code is not working. May be some body rectify the issue. I'm getting some PWM output on P1A (RC5) pin only. The code is,
Code:
#include <xc.h>
#include <pic.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 20000000
// #define FOSC 20000000L
void init_micro (void);
void init_pwm_pos (void);
void init_pwm_neg (void);
void pwm_dr(unsigned int dr);
void gen_sin(void);
void init_pdr_array (void);
static int pdr, pdr_array[27];
void main (void)
{
init_micro();
init_pdr_array();
do
{
init_pwm_pos();
gen_sin();
init_pwm_neg();
gen_sin();
}
while(1);
}
void init_micro (void)
{
OSCCON= 0b00001000; // EXTERNAL HS OSCILLATOR
TRISA = 0b11101111;
TRISB = 0b00111111;
TRISC = 0b10000011;
ANSEL = 0b00010000;
ANSELH =0b00001100;
// RB6 = 1;
// RB7 = 1;
PORTC = 0b00000000;
}
void gen_sin(void)
{
int i;
for(i = 1; i < 25; i++)
{
pdr = pdr_array[i];
pwm_dr(pdr);
__delay_us(204);
}
for(i = 24; i > 0; i--)
{
pdr = pdr_array[i];
pwm_dr(pdr);
__delay_us(204);
}
}
void init_pdr_array(void)
{
pdr_array[0] = 0; //[0] INTENTIONALLY STOPPED TO OCCUR
pdr_array[1] = 66;
pdr_array[2] = 131;
pdr_array[3] = 196;
pdr_array[4] = 261;
pdr_array[5] = 324;
pdr_array[6] = 386;
pdr_array[7] = 446;
pdr_array[8] = 504;
pdr_array[9] = 560;
pdr_array[10] = 614;
pdr_array[11] = 666;
pdr_array[12] = 714;
pdr_array[13] = 760;
pdr_array[14] = 803;
pdr_array[15] = 842;
pdr_array[16] = 878;
pdr_array[17] = 910;
pdr_array[18] = 938;
pdr_array[19] = 962;
pdr_array[20] = 983;
pdr_array[21] = 999;
pdr_array[22] = 1012;
pdr_array[23] = 1020;
pdr_array[24] = 1024;
}
void init_pwm_pos (void)
{
TRISC = 0b10111111; //DISABLE OUTPUT
PORTC = 0b00000000;
PR2 = 255;
CCP1CON = 0b01001100; //ACTIVE HIGH, FULLBRIDGE A & D OUTPUT
pdr = 0;
pwm_dr(pdr);
PIR1 = (PIR1 | 2);
T2CON = 0b00000101; //POST SCALER 1:1, PRE 1:4 TIMER ON
while(!(PIR1 & 2));
TRISC = 0b10000011; //ENABLE OUTPUT
TMR2 = 0;
}
void init_pwm_neg (void)
{
TRISC = 0b10111111; //DISABLE OUTPUT
PORTC = 0b00000000;
PR2 = 255;
CCP1CON = 0b11001100; //ACTIVE HIGH, FULLBRIDGE B & C OUTPUT
pdr = 0;
pwm_dr(pdr);
PIR1 = (PIR1 | 2);
T2CON = 0b00000101; //POST SCALER 1:1, PRE 1:4 TIMER ON
while(!(PIR1 & 2));
TRISC = 0b10000011; //ENABLE OUTPUT
TMR2 = 0;
}
void pwm_dr(unsigned int dr)
{
CCPR1L = (dr >> 2);
CCP1CON = ((CCP1CON & 0xF) + ((dr << 4) & 0x30)); // set bits 4 and 5
}