I am trying to create a program with a pic to:
output a PWM signal that increments by 25% to 75% if right button is pressed.
output a PWM signal that decrements by 25% to 0% if left button is pressed.
I have done the inc/decrement part but a static variable that holds the present value isn't working so after a short delay the PWM% goes back to 0%.
I don't fully understand the static class specifier and I'm not sure if the flow of the program is correct. Can someone tell me what I'm doing wrong?
The code is below:
output a PWM signal that increments by 25% to 75% if right button is pressed.
output a PWM signal that decrements by 25% to 0% if left button is pressed.
I have done the inc/decrement part but a static variable that holds the present value isn't working so after a short delay the PWM% goes back to 0%.
I don't fully understand the static class specifier and I'm not sure if the flow of the program is correct. Can someone tell me what I'm doing wrong?
The code is below:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pic16f1939.h>
#include <xc.h>
#define _XTAL_FREQ 1000000
typedef unsigned char uint_8;
typedef unsigned int uint_16;
typedef unsigned short long int uint_24;
typedef unsigned long int uint_32;
#define left PORTAbits.RA0
#define right PORTAbits.RA1
#define dl __delay_ms(100)
//dcyc_0() to set initial PWM to 0%
void dcyc_0();
//dcyc_left() to decrease PWM by 25% if PWM is not 0%
uint_8 dcyc_left(uint_8 dc_rtn_value);
//dcyc_right() to increase PWM by 25% if PWM is not 75%
uint_8 dcyc_right(uint_8 dc_rtn_value);
//A static value is set to zero
void main()
{
//PWM (CPP1) pin is set
PORTA = 0x00;
LATA = 0x00;
ANSELA = 0x00;
TRISA = 0x03;
while(1)
{
while(left == 0 && right == 0)
{
/*static dc_rtn_value is zero
until right is called and dc_rtn_value
is incremented by 25 and the PWM value
for CCPR1L is 25
*/
static uint_8 dc_rtn_value = 0;
TRISC = 0xFB;
CCPTMRS0bits.C1TSEL = 0b00;
PR2 = 100;
CCP1CON = 0x3C;
CCPR1L = dc_rtn_value;
PIR1bits.TMR2IF = 0;
T1CONbits.T1CKPS = 0b10;
T2CONbits.TMR2ON = 1;
while(PIR1bits.TMR2IF == 0);
/*if left is pressed call dcyc_left()
send dc_rtn_value to it
if dc_rtn_value is zero add zero and return zero
if not decrement dc_rtn_value by 25
*/
if(left == 1)
{
dc_rtn_value = dcyc_left(dc_rtn_value);
dl;
}
/*if right is pressed call dcyc_right()
send dc_rtn_value to it
if dc_rtn_value is 75 add zero and return 75
if not increment dc_rtn_value by 25
*/
else if(right == 1)
{
dc_rtn_value = dcyc_right(dc_rtn_value);
dl;
}
}
}
}
uint_8 dcyc_left(uint_8 dc_rtn_value)
{
if(dc_rtn_value == 0)
{
dc_rtn_value = dc_rtn_value + 0;
}
else
{
dc_rtn_value = dc_rtn_value - 25;
}
return dc_rtn_value;
}
uint_8 dcyc_right(uint_8 dc_rtn_value)
{
if(dc_rtn_value == 75)
{
dc_rtn_value = dc_rtn_value + 0;
}
else
{
dc_rtn_value = dc_rtn_value + 25;
}
return dc_rtn_value;
}