iki pic arası seri iletişim

realistic

Üye
Katılım
20 May 2008
Mesajlar
13
Puanları
1
Yaş
39
Merhaba arkadaşlar 18f4550 ve 18f452 arası seri iletişim yapmaya çalışıyorum
piclerin rx ve tx uçlarındaki verileri max232 ile test ettim Sorunsuz ve tam istediğim gibi çalışıyor. Fakat birbirlerine bağlayınca 18f452 18f4550den gelen verileri almıyor bi türlü.
2 haftadır uğraşıyorum üzerinde hala çözemedim
Yardımlarınıza ihtiyacım var


***************18f4550 KODLARI********************
Kod:
#define __USB_PIC_PERIF__ 1
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include <stdlib.h>
#include <math.h>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, stop=1, BRGH1OK)
#include <usb_cdc.h>
#use fast_io(d)

const int8 uzuv1 = 24;
const int8 uzuv2 = 15;
const int8 uzaklik = 3;
const int8 x_scale = 30;
const int8 y_scale = 30;

static int8 last_cdc;
int8 new_cdc;
float servo1,servo2,servo3,aci_step;
unsigned int8 pozisyon_x,pozisyon_y;
char gelen;
int8 kontrol=1;
int8 sepet;
unsigned int8 servo1_int;
unsigned int8 servo2_int;
unsigned int8 servo3_int;
int16 bekle = 1000;





//***  SERVOLARA ACI DEGERLERINI GONDEREN FONKSIYON  ***\\
void cisme_git()
{
   output_high(pin_d0);
   putc(servo1_int);
   delay_ms(bekle);
   putc(servo2_int);
   delay_ms(bekle);
   output_low(pin_d0);
     
   output_high(pin_d1);
   putc(servo3_int);
   delay_ms(bekle);
   output_low(pin_d1);
     
   delay_ms(1000);
}



//*** 3 farkli sepetin gonderilmesi icin servolara aci degerleri gonderir ***\\
void sepete_git(int z)
{
   output_high(pin_a3);
   delay_ms(200);
   switch (z)
   {
      case 49:
         output_high(pin_d0);
         putc(30);
         delay_ms(bekle);
         putc(95);
         delay_ms(bekle);
         output_low(pin_d0);
         output_high(pin_d1);
         putc(120);
         delay_ms(bekle);
         break;
      case 50:
         output_high(pin_d0);
         putc(50);
         delay_ms(bekle);
         putc(95);
         delay_ms(bekle);
         output_low(pin_d0);
         output_high(pin_d1);
         putc(120);
         delay_ms(bekle);
         break;
      case 51:
         output_high(pin_d0);
         putc(120);
         delay_ms(bekle);
         putc(95);
         delay_ms(bekle);
         output_low(pin_d0);
         output_high(pin_d1);
         putc(120);
         delay_ms(bekle);
         break;
   }     
  
   delay_ms(1000);
   output_low(pin_a3);
  
   printf(usb_cdc_putc, "\n\rok");
}


//*** 254 x 254 birimlik alandaki pozisyon bilgirenie göre servo acisi hesaplnamasi ***\\

void aci_hesapla(float x, float y)
{
   float a,b;
   float temp,p;

   if (x<=127)
   {
      a = (x_scale/2)-((x*x_scale)/254);     
      b = y_scale - (y/254)*y_scale;     
      temp =(b+uzaklik)*(b+uzaklik) + a*a;     
      p = sqrt(temp);     
      aci_step=asin((b+uzaklik)/p);
   }
  
   else
   {
      a = ((x-127)/254)*x_scale;     
      b = y_scale - (y/254)*y_scale;     
      temp =(b+uzaklik)*(b+uzaklik) + a*a;
      p = sqrt(temp);
      aci_step = asin(a/p);
      aci_step = aci_step+pi/2;
   }
  
   temp = ((uzuv1*uzuv1) + (uzuv2*uzuv2) - (p*p))/(2*uzuv1*uzuv2);     

   servo2 = acos(temp);
  
   temp = (uzuv2*sin(servo2))/p;
   servo1 = acos(temp);
   servo3 = pi+servo1-servo2;
  
   servo1=floor(180/pi*servo1);
   servo2=floor(180/pi*servo2);
   servo3=floor(180/pi*servo3);
  
   servo1_int=servo1;
   servo2_int=servo2;
   servo3_int=servo3;
  
}


void main()
{ 
   set_tris_a(0x00);
   set_tris_d(0x00);
   setup_adc(ADC_OFF);
   setup_adc_ports(NO_ANALOGS);
   setup_ccp1(CCP_OFF);
   setup_ccp2(CCP_OFF);
  
   output_low(pin_d0);
  
  while (1)
  {
      usb_task();
      new_cdc=usb_cdc_connected();
      if (usb_enumerated()) {output_high(pin_B4);}
      if (usb_cdc_connected()) {output_high(pin_B5);}
     
      while (new_cdc && !last_cdc)
      {
       
        
         if (usb_cdc_kbhit())
         {
           
           
            switch (kontrol)
            {
               case 1:
                  gelen = usb_cdc_getc();
                  pozisyon_x=(int8)gelen;
                  kontrol = 2;
                  break;
               case 2:
                  gelen = usb_cdc_getc();
                  pozisyon_y=(int8)gelen;
                  kontrol = 3;
                  break;
               case 3:
                  gelen = usb_cdc_getc();
                  sepet = (int8)gelen;
                  kontrol = 4;
                  break;
            }
           
         }
              
         delay_ms(200);
        
         if (kontrol == 4)
         {
            kontrol = 1;
            aci_hesapla(pozisyon_x,pozisyon_y);
            cisme_git();     
            sepete_git(sepet);
            output_high(pin_d0);
            delay_ms(1000);
            output_low(pin_d0);
         }
        
      }
    
     
      last_cdc=new_cdc;
   }
}

******************18F452 KODLARI*********************
Kod:
#include <18F452.h>
#include <stdlib.h>
#include <math.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay(clock=4915200)
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7, parity = N, stop =1, BRGH1OK)
#use fast_io(c)
#use fast_io(e)
#use fast_io(b)

char servo1;
char servo2;
unsigned int8 servo1_duty;
unsigned int8 servo2_duty;
unsigned int8 aci;
int8 kontrol, kanal;
char temp_char;

void cycle_forward (void)
{
 
   output_high (pin_B4);
   delay_ms (500);
   output_low (pin_B4);
   output_high (pin_B3);
   delay_ms (500);
   output_low (pin_B3);
}

int aci_hesapla(unsigned int8 x)
{
   float temp2;
   printf("\r\nfonksiyonun icine giren deger  :%u",x);
  
   temp2=floor(76.5 + (x* ( 76.5 / 180 ) ) );
   printf("\n\rtemp2 :%f",temp2);
  
   aci=(unsigned int8)temp2;
  
   printf("\r\n int sonuc :%u",aci);
   return aci;
  
}

void main()
{
   setup_psp(PSP_DISABLED);
   setup_timer_1(T1_DISABLED);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   setup_timer_2(T2_DIV_BY_16,255,1);
   enable_interrupts(GLOBAL);
  
   set_tris_c(0x80);
   set_tris_e(0xFF);
   set_tris_b(0x00);
  
   kontrol=1;
   set_pwm1_duty(0);
   set_pwm2_duty(0);
  
   while(1)
   {     
      if (kbhit())
      {
         if (kanal == 1)
         {
            switch (kontrol)
            {
            case 1:                    
               servo1=getc();
               printf("\n\rbirinci servonun acisi %c",servo1);
               kontrol=2;
               break;
            case 2:
               servo2=getc();
               printf("\n\rikinci servonun acisi %c",servo2);
               kontrol=1;
               break;
            }
  
            if (kontrol == 1)
            {
               servo1_duty=aci_hesapla(servo1);
               servo2_duty=aci_hesapla(servo2);
             
               printf("\r\nduty cycle degeri 1  :%u",servo1_duty);
               printf("\r\nduty cycle degeri 2  :%u",servo2_duty);  
               set_pwm1_duty(servo1_duty);
               set_pwm2_duty(servo2_duty);
            }
         }
         else
         {
            switch (kontrol)
            {
            case 1:                    
               temp_char=getc();
               printf("\n\rCOP1 %c",temp_char);
               kontrol=2;
               break;
            case 2:
            temp_char=getc();
            printf("\n\rCOP2 %c",temp_char);
            kontrol=1;
            break;
         }
  
         }
      }
      output_low(pin_B5);
      cycle_forward();
      kanal = input(pin_E0);
     

   }
}
 

Forum istatistikleri

Konular
128,203
Mesajlar
915,794
Kullanıcılar
449,983
Son üye
ardaxsm

Yeni konular

Geri
Üst