Author Topic: I need help with PWM  (Read 7788 times)

0 Members and 1 Guest are viewing this topic.

Offline mdanaciTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
I need help with PWM
« on: January 08, 2010, 09:56:30 AM »
Hi there

I'm trying to run a dc motor using 16f877A and L298. I'm trying to simulate it in Proteus 7.6
I'm working at 20mhz. I'm adjusting the pwm with pot.
(pot -> 16f877A RA0 pin ->generating pwn with 16f877A -> 16f877A RC2/CPP1 pin -> L298 enable a pin)


i'm writing code with picc. When i try to simulate it with proteus, i get lots of warning messages and my computers slows down.
Here is the warning


i think i have a problem with C code. Setting up ccp1 and adc adjustments.
Can anybody help me?
Here is the code
Code: [Select]
#include <16F877A.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7)
#use fast_io(C)
#use fast_io(D)
#use fast_io(B)
#org 0x1F00,0X1FFF {}
#opt 9

void ss_disp(int num) 
{
   byte const
   ssData[16]={123,80,103,117,92,61,63,120,127,125,126,31,7,87,47,46};
   if(num<16)
   {
      output_c((SSData[num]&240)>>1);
      output_d(SSData[num]&15);
   };
}


void main ()
{
   long adval;
   set_tris_c(128);  set_tris_d(0);
   set_tris_b(0b00111111);
   output_c(0); output_d(0);
   
   setup_ccp1(CCP_PWM);
   setup_timer_2(T2_DIV_BY_2,255,1);
   setup_port_a(AN0);
   setup_adc(ADC_CLOCK_DIV_2);
   set_adc_channel(0);
   
   while(TRUE)   
   {
      delay_us(200);
      adval=read_ADC();   
      set_pwm1_duty(adval);
      ss_disp(adval>>6);

      if(input(pin_b1))
      {
         if(input(pin_b0))
         {
            output_high(pin_b7);
            output_low(pin_b6);
         }
         else if(!input(pin_b0))
         {
         output_low(pin_b7);
         output_high(pin_b6);
         }
      }

      else if(!input(pin_b1))
      {
         output_low(pin_b7);
         output_low(pin_b6);
      }
   }
}


Offline mdanaciTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: I need help with PWM
« Reply #1 on: January 08, 2010, 09:59:36 AM »
i think warning image is too small to be seen. Here is the warning
"(PIC16 ADC) PC=0x006A, ADC conversion clock period (2e-07) is less than min TAd=1.6us and is possibly invalid for device clock frequency."

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: I need help with PWM
« Reply #2 on: January 08, 2010, 10:22:27 AM »
Go read the data sheet.
Pay attention to Equation 11-1 and Table 11-1.

Offline mdanaciTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: I need help with PWM
« Reply #3 on: January 08, 2010, 11:38:16 AM »
Go read the data sheet.
Pay attention to Equation 11-1 and Table 11-1.

I read it.
From what i understand, i'm using 20mhz so i need to set tosc to 32 or 64. "setup_adc(ADC_CLOCK_DIV_32);"
Proteus stopped giving warnings but my dc motor stopped too :)

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: I need help with PWM
« Reply #4 on: January 08, 2010, 02:04:56 PM »
I'm confused....if the code didn't compile then how could the motor run?

Have you looked at the PIC's PWM output on a scope?
Did you run the code in a simulator or a debugger and see what the PWM frequency and duty cycle is?

Is set_pwm1_duty(adval); expecting a value of type long?
What value is the read_ADC() returning?

You could have the RMW (Read-Modify-Write) issue on RB6 & 7. Check the data sheet section 4.1 and this thread on the Microchip support forum.
http://www.microchip.com/forums/tm.aspx?m=287664&mpage=1&key=RMW&#287678


Offline mdanaciTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: I need help with PWM
« Reply #5 on: January 08, 2010, 03:59:59 PM »
I'm confused....if the code didn't compile then how could the motor run?

Have you looked at the PIC's PWM output on a scope?
Did you run the code in a simulator or a debugger and see what the PWM frequency and duty cycle is?

Is set_pwm1_duty(adval); expecting a value of type long?
What value is the read_ADC() returning?

You could have the RMW (Read-Modify-Write) issue on RB6 & 7. Check the data sheet section 4.1 and this thread on the Microchip support forum.
http://www.microchip.com/forums/tm.aspx?m=287664&mpage=1&key=RMW&#287678

Thanks for your help...
I can compile it with Picc but when i simule it with proteus it gives about 6000 warnings and my computer slows down.

No problem with RB6 and RB7 i checked the RMW . Adval returns between 0 and 1023.
I searched a lot and they say there is bug with proteus 7.6 with RC0 and RC1 pins.
I'm going to revert to older versions..
Thanks again, i hope i can handle.

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: I need help with PWM
« Reply #6 on: January 08, 2010, 08:40:39 PM »
In another thread some one posted a link to a great sumo bot. The site has the 16F876 code (same PIC family as the 877) written in CCS C. So I though this may help.
http://www.huv.com/miniSumo/seeker2/index.html

 

SMF spam blocked by CleanTalk