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

Problem with calculate PWM PIC18F2480

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



Joined: 16 Feb 2011
Posts: 42

View user's profile Send private message

Problem with calculate PWM PIC18F2480
PostPosted: Tue Oct 24, 2017 8:54 pm     Reply with quote

Dear Sir

I have a problem with output PWM PIC18F2480 (PIN RC2).
I want to use AD0 to output PWM CCP1 from 0-100%.

Code:

 PWM Period = (PR2) + 1] • 4 • T OSC •(TMR2 Prescale Value)
 PWM Duty Cycle = (CCPR1L:CCP1CON<5:4>) •T OSC • (TMR2 Prescale Value)


It means that
Code:

 PWM % = (CCPR1L:CCP1CON<5:4>)*100 /  (PR2) + 1] • 4

With PR2 =24
AD0 = (CCPR1L:CCP1CON<5:4>)
Code:

        PWM % = AD0


Using PC817 opto + 220 Ohm resistor to isolation, 12 V output to N-Mosfet
But the problem is the output PWM is not true as the calculated. When AD0 >0, the output maximum 100%.
Code:

#include <18F2480.h>
//#include <18F248.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT //#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT
#device 18F2480*=16, ADC=8 // ADC 10 bits
//#device HIGH_INTS=true
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#use fast_io(c)
#include <can-18xxx8.c>
#include <LCD_TM_S.c>

static unsigned int A0,C,L,H;


#INT_EXT
void ext1_isr(void) {
  switch(C)
  {
   case 1:
 
      L=A0;
      break;

   case 2:
   
      H=A0;
      break;
  }
}   


#INT_EXT1
void ext1_isr1(void) {

C++;
if(C>9){
    C=0;}

}


void main ()
{
   setup_timer_2(T2_DIV_BY_16, 24, 1); 
  //setup_timer_2(T2_DIV_BY_16, 255, 1);   
          //   The cycle time will be (1/clock)*4*t2div*(period+1)
          //   In this program clock=20000000 and period=24 (below)
          //   For the three possible selections the cycle time is:
          //     (1/20000000)*4*1*25 =  51.2 us or 12,5 kHz
  // enable_interrupts(INT_TIMER2);   //enable timer2 interrupt
   
   //EXT_INT_EDGE(0,L_TO_H);
   clear_interrupt(INT_EXT);
   enable_interrupts(INT_EXT);// B0
   set_tris_c(0x00);
   EXT_INT_EDGE(1,H_TO_L);
   clear_interrupt(INT_EXT1);
   enable_interrupts(INT_EXT1);// B1
   output_low(PIN_C2);
   setup_ccp1(CCP_PWM);  // PIN_C2 AS CCP PWM
   enable_interrupts(GLOBAL);       //enable all interrupts (else timer2 wont happen)

   setup_adc(ADC_CLOCK_DIV_16); //ADC_CLOCK_DIV_8
   setup_adc_ports(AN0_TO_AN2); //ALL_ANALOG
  // setup_timer_2(T2_DIV_BY_16, 24, 1);   //
   //setup_ccp1(CCP_PWM_PLUS_3);
 
   lcd_init();
   printf("\r\nRunning...");
 
   while (TRUE) {
    set_adc_channel(0); // EasyPIC Board: Read ADC 2 for PWM fan
    delay_ms(20);
    A0= read_adc();
    lcd_gotoxy(1,1);
    printf(lcd_putc,"A      ");
    lcd_gotoxy(1,1);
    printf(lcd_putc,"A%u", A0);
               
    set_pwm1_duty(A0);

    delay_ms(200);
   }
}

Could you teach me how to solve it ? Thanks so much
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Oct 25, 2017 4:44 am     Reply with quote

Honestly, you need to delete that 'program' and start fresh!

Get rid of the commented out lines of code.
Check to see IF the PIC18 needs the *16 directive..(I don't think so.)
the ADC config is wrong (coded for 10, comment says 8), confusing.
Get rid of fast_io() since you do not 'tris' ALL the ports.
Get rid of the can driver, you're not using it.
Most LCd modules need a delay_ms(500) BEFORE the lcd_init() to allow them to 'bootup' properly.
A0 is probably an 8 bit variable, not 10.

You should look at the PWM examples that CCS supplies and build from there. Also press F11 and the manual will open while your project is open. Look in the manual to use the timer commands.

Jay
ntrungtruc2003



Joined: 16 Feb 2011
Posts: 42

View user's profile Send private message

PostPosted: Thu Oct 26, 2017 5:47 pm     Reply with quote

Thanks temtronic

The prolem has solved,

Because PC817 connect to Resistor 220 Ohm, therefore I have changed to 10K,
It's run.

Best regards
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