Sunteți pe pagina 1din 21

CUPRINS 1. Schema bloc ................................................................................................................................. 3 2 Prezentarea circuitelor si schema electrica ................................................................................. 4 3. Cod sursa ..................................................................................................................................... 8 4. Parte practica ..............................................................................................................................

21 5. Bibliografie .................................................................................................................................. 21

1. SCHEMA BLOC:

2. PREZENTAREA CIRCUITELOR UTILIZATE


Caracteristicile Microcontrolui PIC18F4550 sunt:

Simbolul electric al PIC18F4550

1K byte Dual Port RAM + 1K byte GP RAM Full Speed Transceiver 16 Endpoints (IN/OUT) Streaming Port Internal Pull Up resistors (D+/D-) 48 MHz performance

Capsula circuitului integrat PIC18F4550


Acest uC este alimentat cu o tensiune de 5V .Pinii 19...22 sunt utilizati pentru comanda celor 4 Leduri de control. La pinul 18 este conectat un condensator de Cusb=47 uF care filtreaza tensiunea necesara portului USB. Configuratia pinilor cat si mai multe tipuri de conectori USB sunt prezentate in Figura urmatoare. Liniile de date DATA- si DATA+ sunt conectate la pini 23 si 24.

Capsula circuitului integrat PIC18F4550

Circuitul integrat ULN2003 este un low side driver care comanda releul prezentat in schema cu ajutorulul uC. Schema de principiu este formata dintr-un tranzistor Darlington care amplifica curentul pana la 500mA pentru comanda releului.

Configuratia interna a circuitului integrat ULN2003


Din figura urmatoare se obseva foarte clar ca IC UL2003 contine 7 tranzitoare in configuratie darlington care pot comanda diverse relee. In cazul proiectului de fata s-a realizat comanda unui singur releu.

Capsula circuitului ULN2003


In urmatoarea figura este prezentat Display-ul LCD pentru afisarea informatiei transmise cu ajutorul PC-ului cu ajutorul binecunoscutului HyperTerminal, aplicatie disponibila in Windows. Cu aceasta aplicatie se poate face conexiunea prin intermediul porturilor COM intre PC si diverse dispozitive.

Configuratia pinilor LCD 16x2


Pentru a face conversia transferului din modul serial transmis de uC in transmisie paralela se utilizeaza un circuitul 74HC4094. Acest circuit are conectat pinul 1 EnSTROBE la pinul 6 al uC care il comanda sa transmita date la LCD. Pinul 2 DATA este conectat la pinul 26 (SDO) pentru comunicatie seriala la uC. De asemenea pinul 3 al IC-ului este conectat la pinul 25 (CK) al uC.

Capsula circuitului 74HC4094


Schema pentru alimentarea dispozitivului este una clasica. Se obseva ca se foloseste D1 pentru protectie la tensiuni inverse, un regulator de tensiune 7805 si un LED pentru a vedea cand placa este conectata la o sursa de tensiune.

Schema electrica pentru alimentarea dispozitivului

3. COD SURSA:
#include "GenericTypeDefs.h" #include "Compiler.h" #include "usb_config.h" #include "./USB/usb_device.h" #include "./USB/usb.h" #include "./USB/usb_function_cdc.h" #include "HardwareProfile.h" #include "define_me.h"

#include "HardwareProfile.h" /** V A R I A B L E S ********************************************************/ #pragma udata char USB_Out_Buffer[CDC_DATA_OUT_EP_SIZE]; char RS232_Out_Data[CDC_DATA_IN_EP_SIZE]; unsigned char NextUSBOut; unsigned char NextUSBOut;

//char RS232_In_Data; unsigned char unsigned char LastRS232Out; // Number of characters in the buffer RS232cp; // current position within the buffer

unsigned char RS232_Out_Data_Rdy = 0; USB_HANDLE lastTransmission;

//My variable unsigned char s[30], m=0,n=0,adc_exist=0,vtmr1=0,usb_init=1; unsigned char buf_usb[2],command[40],command_exist=0,usb_index=0,command1[40]; unsigned char clear[]=" unsigned int tcont=0; int adc_res=0,adc_vect[4]; int grade=0; static void InitializeSystem(void); void ProcessIO(void); void main1(void); void YourHighPriorityISRCode(); void YourLowPriorityISRCode(); void BlinkUSBStatus(void); void UserInit(void); void blink_command_error(void); #pragma code HIGH_INTERRUPT_VECTOR = 0x08 void High_ISR (void) { _asm goto YourHighPriorityISRCode _endasm } #pragma code LOW_INTERRUPT_VECTOR = 0x18 void Low_ISR (void) { _asm goto YourLowPriorityISRCode _endasm \0",volt=0,adc_set=0;

#pragma code //These are your actual interrupt handling routines. #pragma interrupt YourHighPriorityISRCode void YourHighPriorityISRCode() { if(INTCONbits.TMR0IF==1) { INTCON=0x20; INTCONbits.TMR0IF==0; TMR0H=0xff; TMR0L=0x00; USBDeviceTasks();// Check bus status and service USB interrupts. // Interrupt or polling method. If using polling, must call // this function periodically. This function will take care // of processing and responding to SETUP transactions // (such as during the enumeration process when you first // plug in). USB hosts require that USB devices should accept // and process SETUP packets in a timely fashion. Therefore, // when using polling, this function should be called // frequently (such as once about every 100 microseconds) at any // time that a SETUP packet might reasonably be expected to // be sent by the host to your device. In most cases, the // USBDeviceTasks() function does not take very long to // execute (~50 instruction cycles) before it returns. // get usb command

10

CDCTxService(); //CDCTxService handles device-to-host transaction(s). //This function should be called once per Main Program loop.

if (getsUSBUSART(buf_usb,1)) { if (buf_usb[0]=='\r') {command_exist=1; command[usb_index]='\0'; //strcpy(&command1,command); usb_index=0; if(mUSBUSARTIsTxTrfReady()==1) { sprintf(s,"%c",buf_usb[0]); mUSBUSARTTxRam(s,1); } } else if(buf_usb[0]=='\b') { usb_index--; if (usb_index==255) usb_index=0; if(mUSBUSARTIsTxTrfReady()==1) { sprintf(s,"%c",buf_usb[0]); mUSBUSARTTxRam(s,1); } } else { command[usb_index]=buf_usb[0]; usb_index++; if(mUSBUSARTIsTxTrfReady()==1) {

11

sprintf(s,"%c",buf_usb[0]); mUSBUSARTTxRam(s,1); } } } else ; INTCON=0xe0; INTCON2=0x04; } if (tcont<4500) { TMR1H=0x00; TMR1L=0x01; } else if(tcont==4500) { TMR1H=0xFE; TMR1L=0xA6; } else if(tcont>4500) { tcont=0; } tcont++; if (adc_set==1) {adc_set=0; adc_start(); } } //This return will be a "retfie fast", since this is in a #pragma interrupt section #pragma interruptlow YourLowPriorityISRCode void YourLowPriorityISRCode() { //adc interrupt

12

if(PIR1bits.ADIF) { PIR1bits.ADIF=0; if(vtmr1==1) {vtmr1=0; adc_vect[0]=adc_result(); if (adc_vect[0]==adc_vect[1]) { adc_exist=0; } else {adc_exist=1; adc_vect[1]=adc_vect[0]; } } else adc_res=adc_result(); } //timer1 interrupt // INTCON=0x20; //disable interrupts if(PIR1bits.TMR1IF==1) { PIE1bits.TMR1IE=0; TMR1H=0x00; TMR1L=0x01; T1CON=0xb1; PIR1bits.TMR1IF=0; vtmr1=1; PIE1bits.TMR1IE=1; adc_start(); } // INTCON=0xe0; //enable interrupts } //This return will be a "retfie", since this is in a #pragma interruptlow section

13

void blink_command_error(void) {set_cursor(2,1); display_lcd(clear); Delay10KTCYx(255); sprintf(s,"Command ERROR!"); set_cursor(2,1); display_lcd(s); Delay10KTCYx(255); set_cursor(2,1); display_lcd(clear); Delay10KTCYx(255); set_cursor(2,1); display_lcd(s); set_cursor(2,1); display_lcd(clear); Delay10KTCYx(255); set_cursor(2,1); display_lcd(s); } void UserInit(void) { unsigned char i; // InitializeUSART(); adc_vect[0]=0; adc_vect[1]=0; // // // // // // // // // } } for (i=0; i<16; i++) { buf_usb[i] = 0; { command[i] = 0; Initialize the arrays for (i=0; i<sizeof(command); i++)

NextUSBOut = 0;

14

LastRS232Out = 0; lastTransmission = 0; //initialize pins for LCD TRISDbits.TRISD4=0; TRISDbits.TRISD5=0; TRISDbits.TRISD6=0; //spi initialization TRISCbits.TRISC7=0; TRISBbits.TRISB1=0; OpenSPI(SPI_FOSC_16,MODE_10,SMPMID); //LCD initialization init_lcd(); //init leds TRISDbits.TRISD0=0; TRISDbits.TRISD1=0; TRISDbits.TRISD2=0; TRISDbits.TRISD3=0; LATDbits.LATD0=0; LATDbits.LATD1=0; LATDbits.LATD2=0; LATDbits.LATD3=0; // //relay TRISCbits.TRISC6=0; LATCbits.LATC6=0; RCONbits.IPEN=1; //ADC TRISAbits.TRISA0=1; set_anch(0); adc_open(); //timer1 IPR1bits.TMR1IP=0; //timer1 low priority //1--->Enable priority levels on interrupts mInitAllLEDs();

15

PIR1bits.TMR1IF==0; TMR1H=0x00; TMR1L=0x01; T1CON=0xb0; PIE1bits.TMR1IE=0; //Overflow Interrupt Enable bit //interupts INTCON=0x60; INTCON2=0x04; //timer interrupts initialization T0CON=0x82; //prescaler 1/8 TMR0H=0xff; TMR0L=0x00; //enable interrupts INTCON=0xe0;

}//end UserInit *******************************************************************/ void InitializeSystem(void) { ADCON1 |= 0x0F; // Default all pins to digital

tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h

USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware //variables to known states. UserInit(); }//end InitializeSystem

16

void main1(void) { while(1) {if(command_exist) {command_exist=0; if (memcmppgm2ram(command,"goto1",5)==0) return; else {set_cursor(2,1); display_lcd(clear); sprintf(s,"FATAL ERROR"); set_cursor(2,1); display_lcd(s); } } } }

void main(void) { InitializeSystem(); //UserInit(); set_cursor(1,1); sprintf(s,"Proiect P.T.S.D."); display_lcd(s); set_cursor(2,1); while(1) {

17

ProcessIO(); }//end while }//end main //* //********************************************************** *******************/ #if defined(USB_CDC_SET_LINE_CODING_HANDLER) void mySetLineCodingHandler(void) { } else { DWORD_VAL dwBaud; //Update the baudrate info in the CDC driver CDCSetBaudRate(cdc_notice.GetLineCoding.dwDTERate.Val); //Update the baudrate of the UART #if defined(__18CXX) dwBaud.Val = (GetSystemClock()/4)/line_coding.dwDTERate.Val-1; SPBRG = dwBaud.v[0]; SPBRGH = dwBaud.v[1]; #elif defined(__C30__) dwBaud.Val = (((GetSystemClock()/2)+(BRG_DIV2/2*line_coding.dwDTERate.Val))/BRG_DIV2/line_c oding.dwDTERate.Val-1); U2BRG = dwBaud.Val; #endif } } #endif

18

if(command_exist) {command_exist=0; if (memcmppgm2ram(command,"cupla1",7)==0) {set_cursor(2,1); display_lcd(clear); set_cursor(2,1); display_lcd(command); LATCbits.LATC6=1; } else if(memcmppgm2ram(command,"cupla2",7)==0) {set_cursor(2,1); display_lcd(clear); set_cursor(2,1); display_lcd(command); LATCbits.LATC6=0; } else if(memcmppgm2ram(command,"display ",8)==0) {strcpy(command1,&command[8]); set_cursor(2,1); display_lcd(clear); set_cursor(2,1); display_lcd(command1); } else if (memcmppgm2ram(command,"goto",4)==0) {main1();

} else if ((memcmppgm2ram(command,"start",5)==0)&&(volt==0)) { adc_set=1; }

19

else if ((memcmppgm2ram(command,"read",5)==0)&&(volt==0)) { if (ADCON0bits.GO!=1) { set_cursor(2,1); display_lcd(clear); set_cursor(2,1); grade=(int)adc_res*0.48828125; sprintf(s,"%d C,%d",grade,adc_res); display_lcd(s); } } else if (memcmppgm2ram(command,"test",5)==0) { set_cursor(2,1); display_lcd(clear); set_cursor(2,1); sprintf(s,"%d,%d,%d,%d",PIR1,PIE1,IPR1,T1CON); display_lcd(s); set_cursor(1,1); display_lcd(clear); set_cursor(1,1); sprintf(s,"%d,%d",TMR1L,TMR1H); display_lcd(s); } else if (memcmppgm2ram(command,"vstart",6)==0) {//enable timer1 and timer1 interrupt,disable command read and start T1CONbits.TMR1ON=1; PIE1bits.TMR1IE=1; // // // // // set_cursor(2,1); display_lcd(clear); set_cursor(2,1); sprintf(s,"%d,%d,%d,%d",PIR1,PIE1,IPR1,RCONbits.IPEN); display_lcd(s);

20

// // // // // }

set_cursor(1,1); display_lcd(clear); set_cursor(1,1); sprintf(s,"%d,%d",TMR0L,TMR0H); display_lcd(s); volt=1; else if (memcmppgm2ram(command,"vstop",5)==0) {//disable timer1 interrupts disable timer1 , enable command read and start PIE1bits.TMR1IE=0; T1CON=0xB0; volt=0; } else { //blink_command_error(); set_cursor(2,1); display_lcd(clear); set_cursor(2,1); display_lcd(command); } } // voltmeter else if(adc_exist==1) {adc_exist=0; adc_vect[2]=(int)adc_vect[0]*4.8828125; sprintf(s,"%d",adc_vect[2]); set_cursor(1,1); display_lcd(s);}} //end ProcessIO

21

4. PARTE PRACTICA

5. BIBLIOGRAFIE:
[1] http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010300 [2] http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf [3] http://en.wikipedia.org/wiki/Universal_Serial_Bus [4] http://www.ti.com/lit/ds/symlink/uln2003a.pdf [5] http://h0w2.blogspot.ro/2011/09/how-to-enable-hyper-terminal-in-windows.html [6] http://www.datasheetcatalog.com/datasheets_pdf/7/4/H/C/74HC4094.shtml

22