[PIC] ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

Status
Not open for further replies.

osman6464

Newbie
Joined
May 11, 2014
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
30
I need to change duty cycle with adc module. I wrote a program and it changes duty cycle with a warning ("adc conversion started before wait time has expired following previous conversion or channel change") in proteus.

how can I fix this ?
how can I add a subprogram for interrup?

thanks for your help.

here is my code:

Code:
 #include	<htc.h>
#include	<stdio.h>
#include	"delay.h"


unsigned int
read_adc(void){

	unsigned int val;

	ADIF = 0;
	GO_nDONE = 1;

	while ( GO_nDONE || !ADIF )	continue;

	val = ADRESL;
	val += ((unsigned int)ADRESH * 256);
        DelayMs(10);
	return val;
} //

void main(void)
{
TRISA=0x01;
TRISC=0x00;
PORTC=0x00;


PCFG3=1;
PCFG2=1;
PCFG1=1;
PCFG0=0;

ADFM=1;
ADON=1;

CHS2=0;
CHS1=0;
CHS0=0;

ADCS1=1;
ADCS0=1;


ADIF=0;
ADIE=1;
PEIE=1;
GIE=1;



CCP1X=1;
CCP1Y=1;
T2CKPS1=0;
T2CKPS0=0;
TOUTPS3=1;
TOUTPS2=1;
TOUTPS1=0;
TOUTPS0=0;
CCP1M0=0;
CCP1M1=0;
CCP1M2=1;
CCP1M3=1;
TMR2ON=1;





while(1)
{
unsigned int	adc_val;
		float	i;

		adc_val = read_adc();

		i = (249./1023)*adc_val;
		CCPR1L=i;
		PR2=249; 
		DelayMs(50);
	}
}



View attachment ADC ile PWM PROJE ödevi.X.rar
 

Move ADON=1; to after ADCS0=1; so you select the channel before turning on the ADC.
CCPR1L=i; may cause a problem because CCP reg needs integer, not a float.

You can find all the information in the HITECH manual.

void interrupt()
{
//add your code here
}
 
Last edited:

I've tried your suggestions but nothing has changed :/

I know the tittle of subprogram for interrupt. I meant which part of code should be in that.
I tried something but mplab x shows errors.
"
main.c:19: error: (204) void function can't return a value
main.c:77: error: (184) calling an interrupt function is illegal
??? -> unsigned int
"

- - - Updated - - -

I changed my code and there is no error. interrupt was the problem.
thanks for your reply @Vbase

here is my last code.
Code:
#include	<htc.h>
#include	<stdio.h>
#include	"delay.h"

void main(void)
{
TRISA=0x01;
TRISC=0x00;
PORTC=0x00;

PCFG3=1;
PCFG2=1;
PCFG1=1;
PCFG0=0;

ADCS1=1;
ADCS0=1;

ADON=1;
ADFM=1;

ADIF=0;
ADIE=1;
PEIE=1;
GIE=1;

CCP1X=1;
CCP1Y=1;
T2CKPS1=0;
T2CKPS0=0;
TOUTPS3=1;
TOUTPS2=1;
TOUTPS1=0;
TOUTPS0=0;
CCP1M0=0;
CCP1M1=0;
CCP1M2=1;
CCP1M3=1;
TMR2ON=1;

while(1)
{
CHS2=0; 
CHS1=0;
CHS0=0;
DelayMs(25);
ADIF = 0;
GO_nDONE=1; 
while(GO_nDONE);
                unsigned int	val;
		int	i;

		;
        val = ADRESL;
	val += ((unsigned int)ADRESH * 256);
        DelayMs(10);
		i = (249./1023)*val;
		CCPR1L=i;
		PR2=249; 
		DelayMs(50);
	}
}

static void interrupt
kesme(void)
{
if(ADIF) 
{
ADIF=0;
}
}
 
Last edited:

I can help with mikroC code because it has PWM library.
 

Problem has solved. thank you. @milan.rajik
 

Osman please tell us how did you solve this problem I have the same .
 

Osman please tell us how did you solve this problem I have the same .


hi cüneyt . I had used interrupt codes but I had not created an interrupt subprogram. so I added it.

Can you post your code and proteus file? I can think about it.

I improved my code. this is the last program that I compiled.

Code:
#include	<htc.h>
#include	<stdio.h>
#include	"delay.h"

void main(void)
{
TRISA=0x01;
TRISC=0x00;
PORTC=0x00;

PCFG3=1;
PCFG2=1;
PCFG1=1;
PCFG0=0;

ADCS1=1;
ADCS0=1;

ADON=1;
ADFM=1;

ADIF=0;
ADIE=1;
PEIE=1;
GIE=1;

CHS2=0;
CHS1=0;
CHS0=0;
DelayMs(25);

CCP1X=1;
CCP1Y=1;
T2CKPS1=0;
T2CKPS0=0;
TOUTPS3=1;
TOUTPS2=1;
TOUTPS1=0;
TOUTPS0=0;
CCP1M0=0;
CCP1M1=0;
CCP1M2=1;
CCP1M3=1;
TMR2ON=1;

while(1)
{

ADIF = 0;
GO_nDONE=1; 
                
	}
}

static void interrupt
kesme(void)
{
if(ADIF) 
{
                unsigned int	val;
		int	i;

                val = ADRESL;
                val += ((unsigned int)ADRESH * 256);
                DelayMs(50);
		i = (249./1023)*val;
		CCPR1L=i;
		PR2=249;
		DelayMs(50);
                ADIF=0;
}
}


I need to improve this code. I need to add 1 second of timer for "GO_nDONE=1;" bit but I haven't made it
 

Hi thank you for your codes I have given my files as proteus and MicroC in the thread " Result is not defined in function" "https://www.edaboard.com/threads/336080/" I think my problems are a little hard to solve .Now I read and learn about ADC PWM and PID .If I can not solve I think to ask Tahmid via this forum .But I dont know how can I contact to him.Bye
 

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…