16f877a 14 porttan pwm olusturmak için yardım

Başlatan sagamen, 02 Ağustos 2008, 13:48:20

sagamen

merhaba arkadaşlar 16f877a ile  14 porttan pwm elde etmek istiyorum  normalde derleme hatası yok yalnız  4 adet port pwm elde ediyorum geri kalan portlarda aynı anda pwm elde ediyor yardımlarınızı beklıyorum

kod
[/code]
//main.c
#TYPE short=8, int=16, long=32
#if defined(__PCM__)
#include <16F877A.h>
#fuses XT,NOWDT,PROTECT,NOLVP,BROWNOUT,PUT
#use delay(clock=4000000)
//#ZERO_RAM
#endif
//#include   "main.h"
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)


#define leda  PIN_B7
#define ledb  PIN_B6
#define ledc  PIN_B5
#define ledd  PIN_B4
#define lede  PIN_B3
#define ledf  PIN_B2
#define ledg  PIN_B1
#define ledh  PIN_D7
#define ledi  PIN_D6
#define ledj  PIN_D5
#define ledk  PIN_D4
#define ledl  PIN_D3
#define ledm  PIN_C7
#define ledn  PIN_C6

//**************************************************************************************************
unsigned int16 ticks=0;
unsigned char   a=0, b=0, c=0,d=0, e=0, f=0,g=0, h=0, i=0,j=0, k=0, l=0,m=0, n=0;
unsigned int16   timer_a;
unsigned int16   timer_b;
unsigned int16   timer_c;
unsigned int16   timer_d;
unsigned int16   timer_e;
unsigned int16   timer_f;
unsigned int16   timer_g;
unsigned int16   timer_h;
unsigned int16   timer_i;
unsigned int16   timer_j;
unsigned int16   timer_k;
unsigned int16   timer_l;
unsigned int16   timer_m;
unsigned int16   timer_n;
unsigned char   up_a=0;
unsigned char   up_b=0;
unsigned char   up_c=0;
unsigned char   up_d=0;
unsigned char   up_e=0;
unsigned char   up_f=0;
unsigned char   up_g=0;
unsigned char   up_h=0;
unsigned char   up_i=0;
unsigned char   up_j=0;
unsigned char   up_k=0;
unsigned char   up_l=0;
unsigned char   up_m=0;
unsigned char   up_n=0;
//unsigned int1   toggle;
unsigned char   tmr;

/*
#define    REDCLK      24
#define    GREENCLK      21
#define   BLUECLK      27

#define    REDCLK      58
#define    GREENCLK      42
#define   BLUECLK      52
*/

#define   aCLK      18//72
#define   bCLK      24//63
#define   cCLK      32/81
#define   dCLK      40/72
#define   eCLK      48/63
#define   fCLK      56/81
#define   gCLK      64/72
#define   hCLK      72/63
#define   iCLK      80/81
#define   jCLK      88/72
#define   kCLK      96//63
#define   lCLK      104//81
#define   mCLK      112//72
#define   nCLK      120//63

#int_TIMER2
TIMER2_isr()
{
  output_high(PIN_B0);
  ticks++;

     if( ticks >= timer_a)
     {
        if(up_a==0)
        {
           a++;
           if(a >= 63) up_a=1;
        }
        else
        {
           a--;
           if(a==0) up_a=0;
        }
        timer_a=(unsigned int16)ticks+(unsigned int16)aCLK;
     }

     if (ticks >= timer_b )
     {
        if(up_b==0)
        {
           b++;
           if(b >= 63) up_b=1;
        }
        else
        {
           b--;
           if(b==0) up_b=0;
        }
        timer_b=(unsigned int16)ticks+(unsigned int16)bCLK;
     }

     if (ticks >= timer_c )
     {
        if(up_c==0)
        {
           c++;
           if(c >= 63) up_c=1;
        }
        else
        {
           c--;
           if(c==0) up_c=0;
        }
        timer_c=(unsigned int16)ticks+(unsigned int16)cCLK;
     }
//////////////////////////////////////////////////////////////////////////////
     if (ticks >= timer_d )
     {
        if(up_d==0)
        {
           d++;
           if(d >= 63) up_d=1;
        }
        else
        {
           d--;
           if(d==0) up_d=0;
        }
        timer_d=(unsigned int16)ticks+(unsigned int16)dCLK;
     }
//////////////////////////////////////
     if (ticks >= timer_e )
     {
        if(up_e==0)
        {
           e++;
           if(e >= 63) up_e=1;
        }
        else
        {
           e--;
           if(e==0) up_e=0;
        }
        timer_e=(unsigned int16)ticks+(unsigned int16)eCLK;
     }
///////////////////////////
     if (ticks >= timer_f )
     {
        if(up_f==0)
        {
           f++;
           if(f >= 63) up_f=1;
        }
        else
        {
           f--;
           if(f==0) up_f=0;
        }
        timer_f=(unsigned int16)ticks+(unsigned int16)fCLK;
     }
///////////////////////////////////
     if (ticks >= timer_g )
     {
        if(up_g==0)
        {
           g++;
           if(g >= 63) up_g=1;
        }
        else
        {
           g--;
           if(g==0) up_g=0;
        }
        timer_g=(unsigned int16)ticks+(unsigned int16)gCLK;
     }
/////////////////////////////////////
     if (ticks >= timer_h )
     {
        if(up_h==0)
        {
           h++;
           if(c >= 63) up_h=1;
        }
        else
        {
           h--;
           if(h==0) up_h=0;
        }
        timer_h=(unsigned int16)ticks+(unsigned int16)hCLK;
     }
///////////////////////////////
     if (ticks >= timer_i )
     {
        if(up_i==0)
        {
          i++;
           if(i >= 63) up_i=1;
        }
        else
        {
           i--;
           if(i==0) up_i=0;
        }
        timer_i=(unsigned int16)ticks+(unsigned int16)iCLK;
     }
///////////////////////////////////////
     if (ticks >= timer_j )
     {
        if(up_j==0)
        {
           j++;
           if(j >= 63) up_j=1;
        }
        else
        {
           j--;
           if(j==0) up_j=0;
        }
        timer_j=(unsigned int16)ticks+(unsigned int16)jCLK;
     }
///////////////////////////////
     if (ticks >= timer_k )
     {
        if(up_k==0)
        {
           k++;
           if(k >= 63) up_k=1;
        }
        else
        {
           k--;
           if(k==0) up_k=0;
        }
        timer_k=(unsigned int16)ticks+(unsigned int16)kCLK;
     }
//////////////////////////////////
     if (ticks >= timer_l )
     {
        if(up_l==0)
        {
           l++;
           if(l >= 63) up_l=1;
        }
        else
        {
           l--;
           if(l==0) up_l=0;
        }
        timer_l=(unsigned int16)ticks+(unsigned int16)lCLK;
     }
///////////////////////
if (ticks >= timer_m )
     {
        if(up_m==0)
        {
           m++;
           if(m >= 63) up_m=1;
        }
        else
        {
           m--;
           if(m==0) up_m=0;
        }
        timer_m=(unsigned int16)ticks+(unsigned int16)mCLK;
     }
////////////////////////////////
if (ticks >= timer_n )
     {
        if(up_n==0)
        {
           n++;
           if(n >= 63) up_n=1;
        }
        else
        {
           n--;
           if(n==0) up_n=0;
        }
        timer_n=(unsigned int16)ticks+(unsigned int16)nCLK;
     }
  output_low(PIN_B0);
}
void main()
{
   set_tris_b(0x00);
   set_tris_c(0x00);
   set_tris_d(0x00);  
   setup_timer_1(T1_DISABLED);
   setup_comparator(A0_A3_A1_A2);
   setup_timer_2(T2_DIV_BY_16,255,1);
   setup_vref(FALSE);
     enable_interrupts(INT_TIMER2);
     enable_interrupts(global);

  timer_a=ticks+aCLK;
  timer_b=ticks+bCLK;
  timer_c=ticks+cCLK;
  timer_d=ticks+dCLK;
  timer_e=ticks+eCLK;
  timer_f=ticks+fCLK;
  timer_g=ticks+gCLK;
  timer_h=ticks+hCLK;
  timer_i=ticks+iCLK;
  timer_j=ticks+jCLK;
  timer_k=ticks+kCLK;
  timer_l=ticks+lCLK;
  timer_m=ticks+mCLK;
  timer_n=ticks+nCLK;
for(;;)
  {
     tmr++;
     tmr &=0xdF;

     if(tmr > a)
        output_low(LEDa);
     else
        output_high(LEDa);

     if(tmr > b)
        output_low(LEDb);
     else
        output_high(LEDb);

     if(tmr > c)
        output_low(LEDc);
     else
        output_high(LEDc);

     if(tmr > d)
        output_low(LEDd);
     else
        output_high(LEDd);

     if(tmr > e)
        output_low(LEDe);
     else
        output_high(LEDe);

     if(tmr > f)
        output_low(LEDf);
     else
        output_high(LEDf);

     if(tmr > g)
        output_low(LEDg);
     else
        output_high(LEDg);

     if(tmr > h)
        output_low(LEDh);
     else
        output_high(LEDh);

     if(tmr > i)
        output_low(LEDi);
     else
        output_high(LEDi);

     if(tmr > j)
        output_low(LEDj);
     else
        output_high(LEDj);

     if(tmr > k)
        output_low(LEDk);
     else
        output_high(LEDk);

     if(tmr > l)
        output_low(LEDl);
     else
        output_high(LEDl);

     if(tmr > m)
        output_low(LEDm);
     else
        output_high(LEDm);

     if(tmr > n)
        output_low(LEDn);
     else
        output_high(LEDn);
  }
}[align=center][/align]
[code]
herkEse kolay gelsin

stlg

Pic ile 14 pwm sinyalini olusturmak cok zor, sadece pwm pinlerinden bu işi yapabilirsiniz, yok ben pwm sinyalini kendim olusuracagım derseniz bu iş cok zor cunku aynı anda tum cıkısları kontrol edemezsiniz ,pic bir işi bitirmeden digerini yapamaz
ALLAHA EMANET OLUNUZ Hoştur bana senden gelen,  Ya gonca gül yahut diken,  Ya kaftandır yahut kefen,  Kereminde hoş lütfunda hoş.

707yunus

umarım işini görür.iş süresi bu programda elle ayarlanmıştır bunu program içinde otomatik olarak da ayarlayabilirsin.






#include <16f877.h>

#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD

#use delay(clock=4000000)

#use fast_io(b)
#use fast_io(c)
unsigned int16 x=500;


void main ()
{
  setup_psp(PSP_DISABLED);        // PSP birimi devre dışı
  setup_spi(SPI_SS_DISABLED);     // SPI birimi devre dışı
  setup_timer_1(T1_DISABLED);     // T1 zamanlayıcısı devre dışı
  setup_timer_2(T2_DISABLED,0,1); // T2 zamanlayıcısı devre dışı
  setup_adc_ports(NO_ANALOGS);    // ANALOG giriş yok
  setup_adc(ADC_OFF);             // ADC birimi devre dışı
  setup_CCP1(CCP_OFF);            // CCP1 birimi devre dışı
  setup_CCP2(CCP_OFF);            // CCP2 birimi devre dışı

  set_tris_b(0x00);   //portb çıkış
  output_b(0x00);  
 
  set_tris_c(0x03);  // pinc0 ve c1 giriş
 
 
 

while(1)
{

  output_b(0xff);
  delay_us(x);
  output_b(0x00);
  delay_us(1500-x);
 
 


 
  if(input(pin_c0)==0 && x!=1500)//pwm iş süresi arttır
  {
  x=x+40;                       //iş süresi artış miktarı
  while(input(pin_c0)==0);
  }
 
  if(input(pin_c1)==0 && x!=0)//pwm iş süresi azalt
  {
  x=x-40;                      // iş süresi azalış miktarı
  while(input(pin_c1)==0); //butonun bırakılması bekleniyor
  }
 
//toplam 1.5 ms lik periyot seçimiştir bunu yukarıdaki komutlrla değiştirebilirsin
}
}





selamlar....

bigbey

ASM ile yazdığım 8 adet PWM çıkış veren bir çalışma.
Bu çalışma işini görürse 14 adet olacak şekilde modifiye ederim.
dosya
TA6R

sagamen

arkadaşlar ilginiz ve alakanız için teşekkurler yazdığım kodda değişiklik yaparak istediğim kıvama getirdim sayılır

[code]:
if( ticks >= timer_a)
{
if(up_a==0)
{
a++;
if(a >= 63) up_a=1;
}
else
{
a--;
if(a==0) up_a=0;
}

buradakı 63 ifadelerini sıralamamısım  hepsi aynı  kotrol edersenız  ve timer ile biraz ayar yapıldıgında 14 adet pwm elde ediliyor  yalnız  kontrolu zorlanıyor ve  30 sn sonra sıralama şaşıyor  bu şaşmayı cozmeyi calısıyorum  normalde leda  ile pwm uretmeye baslıyorum sonundada ledn de pwm son bulması gerekiyor ama  30 sn sonra sapıtıyor..
herkEse kolay gelsin

707yunus

bnim gönderdiğimi bi dene istediğin gibiyse diğer portlarada uygularsın

sagamen

"707yunus" arkadaşım ilgine cok teşekkur ederim ama maalesef olmadı  :oops:
herkEse kolay gelsin

707yunus

programı mı çalıştıramadın yoksa istediğin gibi mi çalışmıyor?

sagamen

istediğim gibi calışmıyor sadece :oops:
herkEse kolay gelsin

707yunus

#include <16f84.h>

#fuses XT,NOWDT,NOPROTECT

#use delay(clock=4000000)

#use fast_io(b)
#use fast_io(a)
unsigned int16 x=0,
int1 i=0;

void main ()
{

  set_tris_b(0x00);   //portb çıkış
  output_b(0x00);  
 
  set_tris_a(0x00);  // porta çıkış
 
while(1)
{

  output_b(0xff);
  output_a(0xff);
  delay_ms(x);
  output_b(0x00);
  output_a(0x00);
  delay_ms(100-x);
 

  if(x<100 && i==0)
  {
  x=x+2;
  if (x==100) i=1;
  }
 
if( x!=0 && i==1)
{
  x=x-2;
  if(x==0) i=0;
}  


}
}

f84 ün  bütün bacaklarına 330 ohm ile led bağla ve sadece izle...

selamlar...

sagamen

dediğin programı 877a ya uyarladım sadece  portların hepsı yanıp sonuyor  hızlıdan yavasa ve yavastan hızlıya  

siz benim ne yapacagımı anladıgınızdan emınım
herkEse kolay gelsin

707yunus

hocam smilasyonda mı denediniz.smilasyonda bnde de öyle görünüyor.siz smilasyonda birkaç pine osiloskop bağlayın anlarsınız ve devreyi bide gercekte kurun yanıp sönüyor gibi görünebileceğini zannetmiyorum.
proteusta zaten bi ledin parlaklaığını tam anlayamazsınız .

sagamen

similasyoda değil  devre uzerinde calıstırdım
877a nın b portu ve d portunda sırayla pwm olusturacam
herkEse kolay gelsin

707yunus

demin yanlış bigi veridim proteusta led parlaklığı anlaşılabilir.bide bn bu kadar yardımcı olablirim.kolay gelsin.dahası arkadaşların dediği gibi çok zordur bütün bacaklardan pwm için.bnimde aklıma bu algoritma geldi sana yolladım.kolay gelsin.

halukece

maxPWM=200;

void PWM14(void){
     PORTB=0;
     PORTD=0;
     for (i=0;i<maxPWM;i++){
         m=0;
         n=0;

         if (a>0){m=m+1;a=a-1;}
         if (b>0){m=m+2;b=b-1;}
         if (c>0){m=m+4;c=c-1;}
         if (d>0){m=m+8;d=d-1;}
         if (e>0){m=m+16;e=e-1;}
         if (f>0){m=m+32;f=f-1;}
         if (g>0){m=m+64;g=g-1;}
         if (h>0){m=m+128;h=h-1;}
         if (j>0){n=n+1;j=j-1;}
         if (k>0){n=n+2;k=k-1;}
         if (l>0){n=n+4;l=l-1;}
         if (o>0){n=n+8;o=o-1;}
         if (p>0){n=n+16;p=p-1;}
         if (r>0){n=n+32;r=r-1;}

         PORTB=m;
         PORTD=n;
     }
}

// Ana programda istediğin yerde çalıştır.
// maxpwm=200 değeri 18ms yapıyor.

   while(1){
   a=100; b=50; c=10;
   d=100; e=50; f=10;
   g=100; h=50; j=10;
   k=100; l=50;  o=10;
   p=100; r=50;
   PWM14();
   }


Bunu dene bakalım. PORTB 8 pin, PORTD ilk 6 pin çıkış kabul edilmiştir.
Pinler karışık ise m ve n atamalarını yeniden düzenlemen gerekir.