CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

help pwm !!!!! control step pwm

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
smash
Guest







help pwm !!!!! control step pwm
PostPosted: Tue Dec 02, 2008 3:54 am     Reply with quote

I'm having problems using the PWM of my PIC16F877. I want to change pwm using delay time such as from set_pwm1_duty(100) to set_pwm1_duty(200), and change it in single steps using delay_ms(50) +1 delay_ms(50) +1 from 100 +1 delay 50ms -->101 +1 delay 50ms --->102 +1 delay 50ms ---->....... to 200 and 200----->100 pwm
Help me
Code:

#include <16F877.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

int c2,D2;
int16 value;
float c;
 
void main()

   setup_timer_2(T2_DIV_BY_1,249,1); //20 khz
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_DIV_32);
   
while(true)
   {
   set_adc_channel(0);
   delay_us(30);
   value=read_adc(); //read adc
 
   c = (float)(value * 5)/1023.0;
 
   c2 = c*10; // max value 50

    if(c2>=10) 
      {
        output_high(PIN_B1);
        output_low(PIN_B0);
       
        D2=c2*2;// value = 100

        set_pwm1_duty(D2);
      }
    else if(c2>=0) 
      {
        output_high(PIN_B1);
        output_low(PIN_B0);
       
        D2=c2*4; // value = 200

        set_pwm1_duty(D2);
      }
}
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Dec 02, 2008 4:20 am     Reply with quote

If you want to continue monitoring the input while altering the PWM then something like this may do.

Code:

int speed = 100;

while(true)
{
  set_adc_channel(0);
  delay_us(30);
  value=read_adc();//read adc
  c = (float)(value * 5)/1023.0;
  c2= c*10;// max value 50
 
  if(c2>=10)
    D2=c2*2;// value = 100
  else if(c2>=0)
    D2=c2*4;// value = 200

  output_high(PIN_B1);
  output_low(PIN_B0);

  if (speed < D2)
    speed++;
  else if (speed > D2)
    speed--;

  set_pwm1_duty(speed);
  delay_ms(50);  // this will slow your response input response time down, there are other options if this is a problem!
}

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group