Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with PWM code for PIC16F777 using PCM CCS compiler

Status
Not open for further replies.

Tom2

Full Member level 5
Joined
Nov 11, 2006
Messages
318
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Activity points
3,457
i use an analog input to pic and i generate a pwm.When i change the amplitute of the input i want to change the duty cycle of the pwm.This is not happen.Is anyone know why.I use Pic16F777 and pcm ccs compiler. My code is :

#include <16F777.h>
#device ADC=16
#fuses XT,NOWDT,NOPROTECT
#use delay(clock=4000000)
//long duty_cycle,period;







void main()
{
long adc_result;

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 255, 1);
WHILE (1)
{
set_adc_channel(0);
delay_ms(1);
adc_result=read_adc();
adc_result>>=6;




set_pwm1_duty(adc_result);

}
}
 

Re: PWM problem

Hi!
Why are u doing this

adc_result>>=6;

Because if the result is say 1023 ( 3FF,maximum possible) then after this operation it will become 15(0x0f). Now in CCSC whenever u r making the project and select a particular frequency for the PWM then it shows the range of duty cycle that can be used. You have to send only those ADC results to the set_pwm1_duty(); which falls within that range.

What i have understood from ur settings is that you are generating a PWM with a frequency of 3.906Khz with duty cycle programmable from 0-1024.
so u donot have to do any right shifting of the adc result. Simply get the resut and put it in the set_pwm1_duty(); function and it will work.
However it is better that firs simply check ur pwm function by executing this code in the while loop instead of the adc code.

set_pwm1_duty(512); //50% duty cycle
DELAY_MS(5000); //check the duty cycle with a scope
set_pwm1_duty(768); //75% duty cycle
DELAY_MS(5000); //check the duty cycle with a scope

once this test is ok, then use the adc based pwm code but remember not to right shift the adc_result.

hope this helps.
Regards.
 

Re: PWM problem

Something more after imstruction
adc_result=read_adc(); i put the instruction
output_high(PIN_B0); and i see that something going wrong when i read the results of the converter.


while(1){
set_adc_channel(0);
delay_us(1);

adc_result=read_adc();

output_high(PIN_B0);
....}
 

Re: PWM problem

Hi!
I will try to write a code for u. Meanwhile u try this piece of code

set_adc_channel(0);
while(1){
delay_ms(10);

adc_result=read_adc();

output_high(PIN_B0);
....}

The delay is very critical.

Donot despair as one learns only through his mistakes.

Regards.
 

Re: PWM problem

@ waseem can u please post the code for the same.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top