Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 63 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
63
Dung lượng
1,25 MB
Nội dung
Guillermo Alberto Pérez Guillén Projects With PIC18F2550 And C18 Compiler INDEX Introduction Blink a LED USB Frequency Counter And Voltmeter USB PC Oscilloscope USB PC Automation Home Capacitance Meter 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 INTRODUCTION 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 and Microchip C18 compilerand C++ language These projects have been compiled with MPLAB C18, and the board that I have used is PIC18F2550 The main sensors and electronic modules that I have been used in this compendium were the following: US1881 LM35DZ MQ-2 MQ-7 PIR FAN MOTOR CD PUMP LED Each chapter has a simple explanation, electronic diagram, list of part, source code, photographs and links of a video, tutorial and download source My Youtube channel and blogs are the following: https://www.youtube.com/channel/UCuePeN-Qvq3lvUVQavo4jiQ http://hubpages.com/@guillengap http://guillengap.blogspot.mx/ Email: guillengap@gmail.com Features Full Speed USB 2.0 (12Mbit/s) interface 1K byte Dual Port RAM + 1K byte GP RAM Full Speed Transceiver 16 Endpoints (IN/OUT) Internal Pull Up resistors (D+/D-) 48 MHz performance (12 MIPS) Pin-to-pin compatible with PIC16C7X5 Program Memory Type: Flash Program Memory (KB): 32 CPU Speed (MIPS): 12 RAM Bytes: 2,048 Data EEPROM (bytes): 256 Digital Communication Peripherals: 1-UART, 1-A/E/USART, 1-SPI, 1-I2C1MSSP(SPI/I2C) Capture/Compare/PWM Peripherals: 2 CCP Timers: 1 x 8-bit, 3 x 16-bit ADC: 10 ch, 10-bit Comparators: 2 USB (ch, speed, compliance): 1, FS Device, USB 2.0 Temperature Range (C): -40 to 85 Operating Voltage Range (V): 2 to 5.5 Pin Count: 28 BLINK A LED Circuit Diagram Components: Microcontroller PIC18F2550 Crystal 4 MHz Capacitor 22 pF Source code with the explanation in the comments and compiled with C18 code #pragma config FOSC = XT_XT,FCMEN = OFF,IESO = OFF, CPUDIV = OSC1_PLL2 #pragma config PWRT = ON,BOR = OFF,BORV = 0 #pragma config WDT = OFF,WDTPS = 32768 #pragma config MCLRE = ON,LPT1OSC = OFF,PBADEN = OFF,CCP2MX = OFF #pragma config STVREN = OFF,LVP = OFF,XINST = OFF,DEBUG = OFF #pragma config CP0 = ON,CP1 = ON,CP2 = ON #pragma config CPB = ON,CPD = ON #pragma config WRT0 = ON,WRT1 = ON,WRT2 = ON #pragma config WRTB = ON,WRTC = ON,WRTD = ON #pragma config EBTR0 = ON,EBTR1 = ON,EBTR2 = ON #pragma config EBTRB = ON #include “p18f2550.h” #include “delays.h” void main (void) { TRISB = 0b01111111; // PORTB bit 7 to output (0) ; bits 6:0 are inputs (1) while (1) { LATBbits.LATB7 = ~LATBbits.LATB7; // toggle LATD Delay1KTCYx(50); // Delay 50 x 1000 = 50,000 cycles; 200ms @ 1MHz } } CAPACITANCE METER Using PIC18F2550 for connecting analogue and digital signals to USB port Included Visual Basic 6 software, PIC C code and a circuit diagram The PIC microcontroller has 10 bit analogue to digital converter, by selecting 8 bits conversion the 2 LSB are ignored The circuit is powered by the USB The interface to the PC is HID class The frequency meter has a range of up to 4MHz (24bits) The meter accuracy is 0.2% or better Inputs 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 The accuracy is about 5%, it depends on the charge resistors, can be calibrated by trimming 1K5 and 1M5 resistors or by the VB code Circuit Diagram Components: Microcontroller PIC18F2550 USB Type A Crystal 4 MHz Transistor NPN Diode 1N4148 Capacitor 22 pF, 0.22 uF Electrolytic capacitor 1 uF Resistor 1K, 1K5, 10K, 1M5 Source code with the explanation in the comments and compiled with C18 code #include #include #include #include “usb.h” #include // Note: there are timing related problems associated with GET_FEATURE // when run at less than 48 MHz //#define CLK_48MHZ 1 #define dis PORTBbits.RB0 #define range1 PORTBbits.RB1 #define range2 PORTBbits.RB2 #define puls PORTBbits.RB3 #pragma config PLLDIV=1, CPUDIV=OSC1_PLL2, USBDIV=2, FOSC=XTPLL_XT//, FCMEM=OFF //CPUDIV=OSC1_PLL2=48MHz, CPUDIV=OSC3_PLL4=24MHz #pragma config IESO=OFF, VREGEN=ON, WDT=OFF PWRT=OFF, BOR=ON_ACTIVE, BORV=3, #pragma config MCLRE=OFF, PBADEN=OFF, CCP2MX=OFF, STVREN=ON, LVP=OFF, XINST=OFF, DEBUG=OFF // HID feature buffer volatile unsigned char HIDFeatureBuffer[HID_FEATURE_REPORT_BYTES]; #pragma code low_vector=0x8 void low_interrupt (void) { } #pragma code // Allocate buffers in RAM for storage of bytes that have either just // come in from the SIE or are waiting to go out to the SIE unsigned char txBuffer[HID_INPUT_REPORT_BYTES]; unsigned char rxBuffer[HID_OUTPUT_REPORT_BYTES]; unsigned int timestamp=0; extern byte transferType; unsigned char timebase, freq3; //counter’s vars //prototypes void UserInit(void); void ProcessIO(void); //***************************************************************** // Entry point of the firmware void main(void) { // Set all I/O pins to digital //ADCON1 |= 0x0F; // Initialize USB UCFG = 0x14; // Enable pullup resistors; low speed=0x10, full speed mode=0x14 deviceState = DETACHED; remoteWakeup = 0x00; currentConfiguration = 0x00; // Call user initialization function UserInit(); while(1) { // Ensure USB module is available EnableUSBModule(); // As long as we aren’t in test mode (UTEYE), process USB transactions if(UCFGbits.UTEYE != 1) ProcessUSBTransactions(); // Application specific tasks ProcessIO(); } } //******************************************************************** // Entry point for user initialization void UserInit(void) { TRISCbits.TRISC0=1; //counter’s input timers 1,2,3 to be done TRISBbits.TRISB0=0; //discharge TRISBbits.TRISB1=0; //range1 TRISBbits.TRISB2=0; //range2 TRISBbits.TRISB3=0; //puls TRISBbits.TRISB4=1; TRISBbits.TRISB5=0; TRISAbits.TRISA0=1; TRISAbits.TRISA1=1; //V input TRISAbits.TRISA2=1; //comp2 in TRISAbits.TRISA3=1; //comp1 in CMCON = 0b1110; //2 comparators, inputs RA3 RA2 ADCON1=0B1000; //ref 5v, ch0-4 analogue ADCON2=0B101110; //Ttad, left just ADCON0=0B101; //input on ch.1, ADC on T1CON=0B1000110; //prescale=1:1, timer=off, osc=off, input=pin11 T0CON=0B110; //prescale=1:128, timer=off, 16bits T2CON=0B1111011; //prescale=1:16, output=1:16, timer=off PR2=233; //5mS period } #define LOBYTE(x) (*((char *)&x)) #define HIBYTE(x) (*(((char *)&x)+1)) //******************************************************************** // Central processing loop Whenever the firmware isn’t busy servicing // the USB, we will get control here to do other processing void ProcessIO(void) { byte rxCnt; unsigned char counts; //timebase = 0; // User Application USB tasks if ((deviceState < CONFIGURED) || (UCONbits.SUSPND==1)) return; // Find out if an Output report has been received from the host rxCnt = HIDRxReport(rxBuffer, HID_OUTPUT_REPORT_BYTES); // If no bytes in, then nothing to do if (rxCnt == 0) return; //delay of 0.7 sec for 1 sec sampling option if(rxBuffer[1]){ TMR0L = TMR0H = INTCONbits.TMR0IF = 0; //reset TMR0 T0CONbits.TMR0ON = 1; while(!INTCONbits.TMR0IF){} //loop for 0.7 sec T0CONbits.TMR0ON = 0; } ADCON0bits.GO_DONE = 1; //start ADC //Frequency Meter TMR1L = TMR1H = freq3 = 0; //clear timers PIR1bits.TMR1IF = 0; PIR1bits.TMR2IF = 0; T1CONbits.TMR1ON = 1; //start count T2CONbits.TMR2ON = 1; for(counts=0;counts