Im relatively new to C programming and have been following books and online tutorials but i cant find an answer any where.
Ive written a program to adjust the brightness of an LED, when you hold switch RA4 the program goes to a function to adjust the PWM value "green_val" eveerything works as it should in the function but when I return to the main program the "green_val" registry returns to 0 rather then what it was set to in the function.
Ive written a program to adjust the brightness of an LED, when you hold switch RA4 the program goes to a function to adjust the PWM value "green_val" eveerything works as it should in the function but when I return to the main program the "green_val" registry returns to 0 rather then what it was set to in the function.
C:
#include <stdio.h>
#include <stdlib.h>
#include "pic16f874a_Config.h"
#include "lcd.h"
#include "pic16f874a.h"
#include "delays.h"
#define duty 1
void main(void)
{
/*
* configure CCP module as 4000 Hz PWM output
*/
PIR1 =255;
T2CON =0b00000101 ;
CCP1CON =0b00001100 ; //Configure PWM Mode, Set DB bits to 10
CCPR1L =0b00000000 ; // reset to zero
Init_Pic();
lcd_init();
RC0=1;
unsigned int green_val;
for(;;)
{
CCPR1L = green_val;
lcd_goto(0);
lcd_puts("green_val1");
if(RA4 == 0)
{
lcd_clear();
Set_Green(green_val);
}
}
}
unsigned int Set_Green(unsigned int green_val)
{
lcd_goto(0);
lcd_puts("Green");
while(RA4 == 0)
{ // digital adjustment of duty cycle
if(RA5 == 0)//Brightness up
{
if(green_val == 255)
{
}
else
{
green_val++;
lcd_goto(0x40);
lcd_putch(green_val);
__delay_ms(50);
}
}
if(RE0 == 0)//Brightness down
{
if(green_val == 0)
{
}
else
{
green_val--;
lcd_goto(0x40);
lcd_putch(green_val);
__delay_ms(50);
}
}
CCPR1L = green_val;
}
lcd_clear();
return green_val;
}
Last edited: