CCS C , PIC18F4550 ve Visual C#.NET 2K5 ile Usb Uygulaması

Usb donanımı bulunduran Pic 18f4550 ile ile yapılan projede ara yüz programı C#.NET 2K5 ile yapılmış.

Ekte verilen doya içerisinde ccs c dosyası, proteus simülasyonu ve arayüz uygulaması yer almakta.


/*
There is no any garentee for this package. Use on as is basis.
It is provided for learning only. No one can use it for Commercial.

This example show how to use PIC18F4550 as usb device.
Interface code is written in Visual C#.NET 2K5.
I am thankful to J.Nut (www.jnutthailand.com) for his great usb articale on usb.
I learned usb interfacing from his article.
If you upgrade or modify this code please do tell me at rafique4ms@gmail.com
I know this is not very good code but i wrote it for learning.
Thanks!
Muhammad Rafique. Islamabad Pakistan. rafique4m@gmail.com
and Sorry for my Bad english
Jan 2K9
*/
#include <18F4550.h>
#device ADC=10
#fuses HSPLL,USBDIV,PLL5,PUT,CPUDIV1,VREGEN,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=48000000)
#use rs232(baud=9600,rcv=pin_c7,xmit=pin_c6)

/////////////////////////////////////////////////////////////////////////////
//
// CCS Library dynamic defines.  For dynamic configuration of the CCS Library
// for your application several defines need to be made.  See the comments
// at usb.h for more information
//
/////////////////////////////////////////////////////////////////////////////
#define USB_HID_DEVICE     FALSE             //We are not using HID class
#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    32                 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE    32                //size to allocate for the rx endpoint 1 buffer
//You can change buffer size as you required.
// Include the CCS USB Libraries.  See the comments at the top of these
// files for more information
//
/////////////////////////////////////////////////////////////////////////////
#include <pic18_usb.h>     //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <MY_USB_Device.h>     //USB configuration and descriptors for this project
#include <usb.c>           //handles usb setup tokens and get descriptor reports
/////////////////////////////////////////////////////////////////////////////
#byte porta=getenv("SFR:PORTA")
#byte portb=getenv("SFR:PORTB")
#bit usb=portb.0
#bit led1=portb.1
#bit led2=portb.2

#byte portd=getenv("SFR:PORTD")
#bit s1=portd.0
#bit s2=portd.1
//========

#define  channel        datain[0]
#define  samples        datain[1]
#define  sample_delay   datain[2]
#define  cmd            datain[3]
#define  LEDS           datain[4]
//#define  sws            datain[5]
//#define  gain           datain[6]

#define mystatus     send_buffer[0]
#define adcbyte0     send_buffer[1]
#define adcbyte1     send_buffer[2]
#define  sws         send_buffer[3]
char logo[] ={'U','S','B','E','X','A','M','P','L','E',' ','r','a','f','i','q','u','e','4','m',' ','@','g','m','a','i','l','.','c','o','m'};

void Config_PIC(void)
{
   porta=0;
   set_tris_a(3); //A0~A1 are analog

   portb=0;
   set_tris_b(0xF0); //B0~B3 are output
   LED1=LED2=0;
   portd=0;
   set_tris_d(3); //D0,D1 are input
   
   setup_adc_ports(AN0_TO_AN1);
   setup_adc( ADC_CLOCK_INTERNAL);
   //Setup USB
   usb_init();                      //setup USB module of the device
   usb_task();                      //usb  interrupt

}
unsigned long FRead_ADC(byte ichannel,byte isamples,byte isample_delay)
{
   unsigned long Result=0;
   byte loop;
   set_adc_channel(ichannel);
   delay_ms(10);
   for(loop=0;loop<isamples;loop++)
      {
      Result+=read_adc();
      delay_us(isample_delay);
      }
   Result/=isamples;    //average
   return Result;
}
//============================================================================//
//                   Main Thread
//============================================================================//
void main(void)
{
   int8  datain[32];    //you can receive 32 byte
   int8  send_buffer[32];   //you can send 32 bytes
   int16 adc_value;
   Config_PIC();
   printf("\r\nIN THE NAME OF ALLAH ALMIGHTY\r\n");
   printf("\r\nUSB EXAMPLE DEVICE. BY WINSTARS PAKISTAN");
   printf("\r\nRAFIQUE4M@GMAIL.COM\r\n");   
   usb_wait_for_enumeration();      //wait from host PC
   usb=1;
   printf("\r\nUSB Connected");
   while (TRUE)
   {
      while(usb_enumerated())
      {
         if (usb_kbhit(1))          //new packet available
         {
            usb_get_packet(1, datain, 64); //read new packet
            switch(cmd)
               {
                  case 'R': 
                           adc_value=fread_adc(channel,samples,sample_delay);
                           adcbyte0=make8(adc_value,0);
                           adcbyte1=make8(adc_value,1);
                           usb_put_packet(1, send_buffer, 3, USB_DTS_TOGGLE);
                           printf("\r\nADC data sent");
                           cmd=0;
                           break;
                  case 'S':
                           sws=portd;  //get switches status
                           usb_put_packet(1, send_buffer, 4, USB_DTS_TOGGLE);
                           printf("\r\nSwitch status sent");                           
                           break;
                  case 'L':
                           if(LEDS==0) {LED1=0; LED2=0;}
                           if(LEDS==1) LED1=~LED1;
                           if(LEDs==2) LED2=~LED2;
                           printf("\r\nLEDs Updated");                                                      
                           break;
                  case 'E':
                           usb_put_packet(1,logo,32,USB_DTS_TOGGLE);
                           printf("\r\nLogo Sent");                           
                           break;
                           
               }//switch(cmd)
         }//if (usb_kbhit(1))
      }//while(usb_enumerated())

   }//while
}//main

Uygulama dosyaları:

USB_PIC_CCS_C#

Şifre: ccspic

Bu yazı CCS C Örnekleri kategorisine gönderilmiş ve , , , , ile etiketlenmiş. Kalıcı bağlantıyı yer imlerinize ekleyin.