changing pwm output by adc

Status
Not open for further replies.

abinesh raja

Newbie level 6
Joined
Mar 3, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
INDIA
Visit site
Activity points
1,374
hello,
I am doing a project regarding intelligent battery charger for than i am using pwm from the micro controller to change the voltage using fet. In order to obtain constant voltage i am using adc to control the voltage. But i am having problem in my program in changing the pwm signal. Can anyone help pls.

- - - Updated - - -

Code:
#include<htc.h>
#include<pic.h>
#include"delay.c"
float val;

float temp=1.5;
char pw=0;

int read_adc()
{
DelayMs(10);
GODONE=1;
DelayMs(10);
val=ADRES;
val=val;
while(GODONE);
return val;
}

void pwm()
{
	TRISB=0b00000000;
        PR2=0b11111001;
 	CCP1CON=0b00111100; 
        TMR2IF=0;
	GIE=1;
	PEIE=1;
    T2CON=0b00000100;
}

void adc()
{
	PORTB=0;
	TRISB=0b00000000;
	PORTA=0x00;
	TRISA=0x01;
	ADCON0=0x81;
	ADCON1=0x40;
}

main()
{	

	adc();
	pwm();
        while(1)
	{
	read_adc();
	if(val>temp)
	{
 	pw--;
        CCPR1L=pw;
	}
	if(val<temp)
	{
	pw++;
	CCPR1L=pw;
	}
}
}

- - - Updated - - -

Both are working separately i need to make the pwm voltage constant foe the battery charging.
can anyone help pls its urgent for me.
 

The logic seems to be correct, what is that exactly your problem..? please be specific, you are not able to update the " CCPR1L" reg. is that ur problem?
 

It's keep on circulating from lower value to higher value,if val > temp. And,circulating from higher value to lower value if val< temp.
 

#include <main.h>
#byte ADRESH = 0x1E
#byte ADRESL = 0x9E

int8 value;
int8 flag=0;

#int_TIMER1
void TIMER1_isr(void)
{
set_timer1(63036);
set_timer0(ADRESH);
output_bit( PIN_B2, 1);

}

#int_TIMER0
void TIMER0_isr(void)
{
output_bit( PIN_B2, 0);
set_timer0(0);
}

void main()
{
setup_timer_0(T0_INTERNAL | T0_DIV_32);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_timer1(63036);
//output_bit( PIN_A2, 1);
output_bit( PIN_A3, 1);

setup_port_a( AN0 );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
while(TRUE)
{

value = Read_ADC();
for(int i=0 ; i<10 ; i++){}



}
}


This written in ccs compiler and works f9 it uses software routine and timer for pwm signal and works fine. The duty cycle will be varied depending upon adc value on channel 0 of pic16f877A. You can change frequency by varying timer1 value .crystal frequency is 4Mhz. It may help you?
 
Hi abinesh ,

Try updating the " CCPR1L" reg just after "read_adc()" like below

while(1)
{
read_adc();
CCPR1L=pw;

if(val>temp)
{
pw--;
...
...
....
....}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…