This work is author´s property, and has been registered on the Website http://www.safecreative.com with the registration code 1609269275953 and protected under the License Creative Commo
Trang 2
Guillermo Alberto Pérez Guillén
Trang 4
This work is author´s property, and has been registered on the Website
http://www.safecreative.com with the registration code 1609269275953 and protected under the License Creative Commons Attribution-NonCommercial-NoDerivatives 4.0
Trang 6
The main goal is to make a compendium of useful projects to solve practical problems
In the future I intend to add more projects; and I assume that you know electronic andMicrochip C18 compilerand C++ language
These projects have been compiled with MPLAB C18, and the board that I have used isPIC18F2550
The main sensors and electronic modules that I have been used in this compendium werethe following:
Trang 748 MHz performance (12 MIPS)Pin-to-pin compatible with PIC16C7X5Program Memory Type: Flash
Digital Communication Peripherals: 1-UART, 1-A/E/USART, 1-SPI, 1-I2C1-Capture/Compare/PWM Peripherals: 2 CCP
Timers: 1 x 8-bit, 3 x 16-bit
ADC: 10 ch, 10-bit
Trang 8USB (ch, speed, compliance): 1, FS Device, USB 2.0Temperature Range (C): -40 to 85
Trang 14
Using PIC18F2550 for connecting analogue and digital signals to the USB port IncludedVisual Basic 6 and Microchip C18 code The PIC microcontroller has 10 bit analogue todigital converter, by selecting 8 bits conversion the 2 LSB are ignored The circuit ispowered by the USB The interface to the PC is HID class The frequency counter has arange of up to 4MHz (24bits) Inputs samplings can be done at rates of 1 per second and 1
every 0.1 second HID class is a class of devices like the mouse and the keyboard; the
Trang 17// Central processing loop Whenever the firmware isn’t busy servicing
Trang 18PIR1bits.TMR2IF = 0;
Trang 22HIDTxBuffer[2]=HIBYTE(timestamp);
Trang 23for(i=4;i<HID_INPUT_REPORT_BYTES;i++)HIDTxBuffer[i]=HIDRxBuffer[i];
// The number of bytes in the report (from usb.h) wCount = HID_INPUT_REPORT_BYTES; transferType=0;
Trang 29//********************************************************************// Entry point for user initialization
Trang 32// Post processing for a SET_FEATURE request After all the data has// been delivered from host to device, this will be invoked to perform// application specific processing.
Trang 33HIDFeatureBuffer[0]=PORTA;
HIDFeatureBuffer[1]=0xF1; //this is to differentiate between differenttransfer types
Trang 35wCount = HID_INPUT_REPORT_BYTES; transferType=0;
Trang 38Using PIC18F2550 for connecting analogue and digital signals to USB port IncludedVisual Basic 6 code and Microchip C18 code, and a circuit diagram The PICmicrocontroller has 10 bit analogue to digital converter The circuit is powered by theUSB The interface to the PC is HID class The inputs and outputs are updated every10ms
Trang 41//********************************************************************// Entry point for user initialization
Trang 42ADCON0=0B0111; //read input ch.1 while(ADCON0bits.GO_DONE){} //wait until ADC done txBuffer[2] = ADRESL;
txBuffer[3] = ADRESH;
ADCON0=0B1011; //read input ch.2 while(ADCON0bits.GO_DONE){} //wait until ADC done txBuffer[4] = ADRESL;
txBuffer[5] = ADRESH;
ADCON0=0B1111; //read input ch.3 while(ADCON0bits.GO_DONE){} //wait until ADC done txBuffer[6] = ADRESL;
TRISB = 0B11110000; //RB4-RB7 are inputs
txBuffer[7] = ADRESH | (PORTB & 0B11110000); //add inputs to ADRESH// As long as the SIE is owned by the processor, we let USB tasks continue
Trang 43// Initialization for a SET_FEATURE request This routine will be// invoked during the setup stage and is used to set up the buffer
Trang 45{
// When the report arrives in the data stage, the data will be // stored in HIDFeatureBuffer
Trang 50Inputs samplings can be done at rates of 1 per second and 1 every 0.2 second
The cap meter (max 50uF) measures the period of charging the cap to the Vref Theaccuracy is about 5%, it depends on the charge resistors, can be calibrated by trimming1K5 and 1M5 resistors or by the VB code
Trang 53{
TRISCbits.TRISC0=1; //counter’s input timers 1,2,3 to be doneTRISBbits.TRISB0=0; //discharge
Trang 54while(!INTCONbits.TMR0IF){} //loop for 0.7 sec
Trang 55if(PIR1bits.TMR1IF){++freq3; PIR1bits.TMR1IF = 0;} }
while(!CMCONbits.C2OUT){}
T3CONbits.TMR3ON=0;
Trang 57//*********************************************************************// Initialization for a SET_FEATURE request This routine will be
Trang 60HIDTxBuffer[3]=LOBYTE(timestamp);
for(i=4;i<HID_INPUT_REPORT_BYTES;i++)HIDTxBuffer[i]=HIDRxBuffer[i];
Trang 63
Source code:
https://drive.google.com/file/d/0B8XMvcdJvBBYa0xzLVYtdmlsWWc/view? usp=sharing