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

PicUSB and RF 433Mhz problem

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



Joined: 29 Sep 2010
Posts: 73
Location: Brazil

View user's profile Send private message MSN Messenger

PicUSB and RF 433Mhz problem
PostPosted: Wed Oct 27, 2010 3:40 pm     Reply with quote

Hi people. I have another problem here.

I did this project PicUSB by J1M: http://www.ccsinfo.com/forum/viewtopic.php?t=24207&postdays=0&postorder=asc&start=0

It is working very fine. I modified the program in Visual C# 2010 and it is working like a charm. I can read temperature with LM35 and show on screen of a PC. OK! :D I am using a 18F4550.

But I like to comunicate with another 18F4550 with RF 433Mhz. I bought one module but the communication over RS232 is not working.

I tried with one wire to simulate the RF module and didint worked.

I do not know how to calculate the baud rate. Embarassed

My cristal is 12Mhz. But with the fuses that I am usign, I put 48Mhz to the clock.

I am using CCS 4.057. With Proteus it worked.

My codes:

Transmitter:

Code:
//
#include <18F4550.h>
#device adc=10
#define WireTX PIN_C6 // <--------- C 6
#define WireRX PIN_C7
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=2400,xmit=WireTX , rcv=WireRX ,ERRORS , STREAM=COM_A )


#define USB_HID_DEVICE     FALSE             //deshabilitamos el uso de las directivas HID
#define USB_EP1_TX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE    2                 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE    5                 //size to allocate for the rx endpoint 1 buffer



#include <pic18_usb.h>     //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <PicUSB.h>         //Configuración del USB y los descriptores para este dispositivo
#include <usb.c>           //handles usb setup tokens and get descriptor reports




#define LED_OK           PIN_B6
#define LED_FAIL         PIN_B7
#define LED_ON           output_high
#define LED_OFF          output_low

int periodo;
int ciclo1=0;
int ciclo2=0;
int ciclo3=0;

#define modo      recibe[0]
#define param1    recibe[1]
#define param2    recibe[2]
#define param3    recibe[3]       
#define param4    recibe[4]
#define resultado envia[0]
#define barra     send[0]
#define status    send2[0]

/*=====================
A interrupção:
=======================*/

#int_timer1
void TIMER1_isr (void)
{     
   set_timer1(65480);                             //Preload do timer1
   
/*=====================
Testes para ciclo = 0:
=======================*/
   
   if (ciclo1 == 0)
   {
      output_low (PIN_B0);
   }
   if (ciclo2 == 0)
   {
      output_low (PIN_B1);
   }
   if (ciclo3 == 0)
   {
   output_low (PIN_B2);
   }

/*==============================
Já que o ciclo é diferente de 0:
================================*/
   if (periodo <255)
   {
      If (periodo < ciclo1)
      {
         output_high (PIN_B0);
      }
      else
      {
         output_low (PIN_B0);
      }
     
         
       If (periodo < ciclo2)
      {
         output_high (PIN_B1);
      }
      else
      {
         output_low (PIN_B1);
      }
     
       If (periodo < ciclo3)
      {
         output_high (PIN_B2);
      }
      else
      {
         output_low (PIN_B2);
      }
     
   }
   else if (periodo == 255)
   {
      output_high (PIN_B0);
      output_high (PIN_B1);
      output_high (PIN_B2);
      periodo=0;
   }   
   periodo++;   
}

void main(void) {
   
   int8 recibe[5];                  //declaramos variables
   int8 envia[2];
   int8 send[1]; 
   int8 send2[1];
   int16 adc=0;

   LED_OFF(LED_OK);                   //encendemos led rojo
   LED_ON(LED_FAIL);

   usb_init();                      //inicializamos el USB 
   
   setup_adc_ports(AN0);         //Configura canais analógico
   setup_adc(ADC_CLOCK_INTERNAL);    //De acordo com relógio interno.

   usb_task();                      //habilita periferico usb e interrupciones
   usb_wait_for_enumeration();      //esperamos hasta que el PicUSB sea configurado por el host

   LED_OFF(LED_FAIL);
   LED_ON(LED_OK);                    //encendemos led verde

   while (TRUE)
   {
      if(usb_enumerated())          //si el PicUSB está configurado
      {
         if (usb_kbhit(1))          //si el endpoint de salida contiene datos del host
         {
           
            usb_get_packet(1, recibe, 5); //cojemos el paquete de tamaño 3bytes del EP1 y almacenamos en recibe           
           
            if (modo == 0) // Modo_Suma
            {
               resultado = param1 + param2;  //hacemos la suma               
               usb_put_packet(1, envia, 2, USB_DTS_TOGGLE); //enviamos el paquete de tamaño 1byte del EP1 al PC
            }

            if (modo == 1) // Modo_Carro
            {
               if (param1 == 0)
               {                   
                  fputc('U',COM_A);
                  delay_ms(20);
                 
               }
               
               if (param1 == 1)
               {
                  fputc('R',COM_A);
                  delay_ms(20);
               
               }
               
               if (param1 == 2)
               {
                  fputc('D',COM_A);
                  delay_ms(20);
               
               }
               
               if (param1 == 3)
               {
                  fputc('L',COM_A);
                  delay_ms(20);
               }
               
               if (param1 == 4)
               {
                  fputc ('S');
               }
            }
           
            if (modo == 2) // Envio da Temperatura
            {
               set_adc_channel(0);           //Habilitación canal0
               delay_us(20);                 
               adc = read_adc();
               barra=((5*adc*100)/1024);
               usb_put_packet(1, send, 1, USB_DTS_TOGGLE); //enviamos el paquete de tamaño 1byte del EP1 al PC
            }
            if (modo == 3) //Modo PWM
            {
               if (param1 == 0)  //pwm desativado, pinos em nível baixo
               {
                  SETUP_TIMER_1 (T1_DISABLED);       //Configurar timer1 para clock iterno/8
                 
               }
               
               if (param1 == 1)  //pwm ativado
               {
                  SETUP_TIMER_1 (T1_INTERNAL|T1_DIV_BY_2);       //Configurar timer1 para clock iterno/8
                  enable_interrupts (INT_TIMER1);                //Habilitar Interrupções
                  enable_interrupts (global);
                  ciclo1=param2;
                  ciclo2=param3;
                  ciclo3=param4;
               }
            }
           
            if (modo == 4)
            {
               status=1;
               usb_put_packet(1, send2, 1, USB_DTS_TOGGLE); //enviamos el paquete de tamaño 1byte del EP1 al PC
            }
           
         }
      }
   }
}


Receiver:

Code:
#include <18f4550.h>

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,PLL3,CPUDIV1,VREGEN
#use delay (clock = 48000000)
//------------------------------
#define WireTX PIN_C6
#define WireRX PIN_C7 //<-----
//------------------------------
#use rs232(baud=2400, xmit=WireTX, rcv=WireRX, ERRORS, STREAM=COM_A)
unsigned int8 data;
int flag=0;
//#include <lcd.c>

#define LED1 PIN_B0

#int_rda
   void rd_isr(void)               
   {
      disable_interrupts(INT_RDA);           // Disable Serial Recieve Interrupt
      disable_interrupts(GLOBAL);            // Disable Global Interrupts
   
      data= fgetc(COM_A);
      if(data=='U')
      {
         flag=1;
      }
     
      if(data=='R')
      {
         flag=2;
      }
     
      if(data=='D')
      {
         flag=3;
      }
     
      if(data=='L')
      {
         flag=4;
      }
     
      if(data=='S')
      {
         flag=5;
      }
   
      enable_interrupts(GLOBAL);
      enable_interrupts(INT_RDA);
   }
   
void main()
{
   //lcd_init();
   enable_interrupts(global);
   enable_interrupts(int_rda);
   output_high (PIN_D2);

   for(;;)
   {
     Switch (flag)
     {
         Case 1:
         {
            //lcd_putc ("\fU");
            output_toggle(LED1);
            delay_ms (100);
            break;
         }
         
         Case 2:
         {
            //lcd_putc ("\fR");
           
            output_toggle(LED1);
            delay_ms (100);
            break;
         }
         
         Case 3:
         {
            //lcd_putc ("\fD");
            output_toggle(LED1);
            delay_ms (100);
            break;
         }
         
         Case 4:
         {
            //lcd_putc ("\fL");
            output_toggle(LED1);
            delay_ms (100);
            break;
         }
         
         Case 5:
         {
            //lcd_putc ("\fS");
            output_low(LED1);
            delay_ms (100);
            break;
         }           
         
        flag=0;
     } 
   }
}


thanks for any reply

MatheusLPS from Brazil

bye
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