You are on page 1of 27

-1-

PROYECTOS CON PIC Y SIMULACION EN PROTEUS Por: Jorge Arturo Rodriguez Hernandez Email: george.manson.69@gmail.com

-2-

Este libro fue creado para la necesidad de hacer nuevos programas usando el compilador CCS C y unido a MPLAB IDE. Proyecto #1 Crear un Proyecto en Mplab Al abrir el programa de Mplab nos iremos a dar un click en <Project> <Project Wizard>

Debe de aparecer una ventana donde nos saluda WELCOME y damos un click en <next> nos mencionara que estamos en el primer paso Step One. 1.-Step One: Elegir el PIC a usar

-3En seste caso se usara el pic16f628a. 2.-Step Two: Elegir el compilador a usar.

Aqu usaremos el compilador CCS C.exe. 3.-Step Three: Lugar donde se grabara el proyecto.

-44.-Step Four: Agregar ms proyectos

Aqu por lo general solo se da click en next. Despues de esto solo se da en terminar Finish y ya esta el lugar de trabajo.

Source Files.- Aqu aparecer la hoja de trabajo osea la hoja donde estamos escribiendo el cdigo Header Files.- Aqu parecera los archivos .H. Son las caractersticas que contiene el PIC. Object Files.- Aparecen las salidas al compilar. Other Files.- Aparecen las salidas al compilar.

-5Despus de hacer el lugar de trabajo debemos de crear una hoja en blanco y luego grabarla en la misma carpeta donde se creo la hoja de trabajo con la extensin .c

. Ahora simple mente debemos incluir la hoja en blanco a Source Files para cuando se compile no cause error.

-6Ahora bien ya para terminar como el compilador fue adherido a MPLAB hay que configurarlo, hay que indicarle donde estan los archivos .h y los driver si e sque maneja en su programita. Haremos click en <build option>

Ahora buscamos donde dice mostrar directorios <show directories> y seleccionamos incluir <include search path> y hacemos click en new, y buscamos donde esta intalado el CCS C, (PICC) y elegimos driver y lo agregamos y otra vez hacemos click en new y agregamos devices y listo para programar!!!

-7Proyecto#2 Parpadeo de un LED

En el pin B0 del puerto B del PIC, encendera un led por 1 segundo y apagara por un 1 segundo es un simple parpadeo de un LED. El diagrama es el siguiente.

En cdigo siguiente no se usa el Master Clean. //Autor: J.A.R.H //Despripcion: Parpadeo de un led en el // PIN B0 #include<16F628A.h> #fuses INTRC_IO,NOWDT,NOLVP,NOMCLR,NOPROTECT #use delay(clock=4000000)

void main(void){ SET_TRIS_B(0x00); do{ output_high(PIN_B0); delay_ms(1000); output_low(PIN_B0); delay_ms(1000); }while(TRUE); }

-8-

Projecto#3

Contador de 00 a 99

Este es un circuito que pone aprueba el timer0 del pic, se usan dos display de nodo comn, cada vez que le picamos al push botn incrementara hasta llegar a 99 y luego se reseteara a 00. El circuito es el siguiente

El programa //------------------------------------//AUTOR:JORGE ARTURO RODRIGUEZ HERNANDEZ //TITLE;CONTADOR //DATE:22/JULIO/'09 //------------------------------------//CONFIGURACION/////////////////// #include<16f628a.h> #fuses INTRC_IO,NOWDT,NOLVP,MCLR,NOPROTECT #use delay(clock=4000000) //DEFINIMOS E/S #define EN1 PIN_A0 #define EN2 PIN_A1 #define PUSH PIN_A2 //ARREGLO PARA MOSTRAR EL NUMERO DEL 0 AL 9

-9-

int mostrar[]={0b11000000,0b11111001,0b10100100,0b10110000,0b10011001, 0b10010010,0b10000011,0b11111000,0b00000000,0b00011000}; //VARIABLES int N1,N2; //FUNCION DE CONFIGURACION void config(void){ set_tris_b(0x00); set_tris_a(0x04); setup_comparator(NC_NC_NC_NC); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2|RTCC_8_BIT); //overflow cada 5mS aprox set_timer0(0); //overflow=escala x clock x (256-timer0) } //INTERRUPCION POR TIMER0 #INT_TIMER0 void TIMER(void){ output_low(EN1); output_b(mostrar[N2]); output_high(EN2); delay_us(520); output_low(EN2); output_b(mostrar[N1]); output_high(EN1); } //INICIO DEL PROGRAMA void main(void){ config(); N1=0; N2=0; enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); while(TRUE){ delay_us(5);

- 10 while(~input(PUSH)); while(input(PUSH)); N2++; if(N2==10){ N2=0; N1++; if(N1==10){ N1=0; } } } }

- 11 Proyecto#4 Contador descendente de MM:SS

Al introducir el tiempo deseado ya sea hasta 59:59 por el botn PUSH, entonces al presionar el botn PUSH2 iniciara a descender.

El programa se muestra a continuacin /////////////////////////////////////// //AUTOR:JORGE ARTURO RODRIGUEZ HERNANDEZ //TITLE;CONTROL DE 4 DISPLAY //DATE:31/JULIO/2009 /////////////////////////////////////// //CONFIGURACION/////////////////// #include<16F628A.h> #fuses HS,NOWDT,NOLVP,NOMCLR,NOPROTECT #use delay(clock=20000000) //definiciones del cada display 1 al 4 #define EN1 PIN_A0 #define EN2 PIN_A1 #define EN3 PIN_A2 #define EN4 PIN_A3 #define PUSH PIN_A4 #define PUSH2 PIN_A5

- 12 //variable que contiene la decodificacion para mostrar cada numero en el display //de 7 segmentos int mostrar[]={0b01000000,0b01111001,0b00100100,0b00110000,0b00011001, 0b00010010,0b00000011,0b01111000,0b00000000,0b00011000}; //definicion de variables a usar signed int MEN1=0,MEN2=0,SEN3=0,SEN4=0; int i; long j; int OVERFLOW; //funcion: que establece los puertos como I/O establece el timer0 con 6.5536mS void config(void){ set_tris_a(0x30); set_tris_b(0x80); setup_comparator(NC_NC_NC_NC); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128|RTCC_8_BIT);//overflow 6.5536mS setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);//overflow 13.1072mS set_timer0(0); } #INT_TIMER0 void TIMER0(void){ OVERFLOW++; if(OVERFLOW>152){ OVERFLOW=0; SEN4--; if(SEN4<0){ SEN4=9; SEN3--; if(SEN3<0){ SEN3=5; MEN2--; if(MEN2<0){ MEN2=9; MEN1--; if(MEN1<0){ MEN1=0; } } }

- 13 } } } #INT_TIMER1 void TIMER1(void){ output_bit(EN1,0); output_bit(EN2,0); output_bit(EN3,0); output_b(mostrar[SEN4]); output_bit(EN4,1); delay_ms(6); output_bit(EN1,0); output_bit(EN2,0); output_bit(EN4,0); output_b(mostrar[SEN3]); output_bit(EN3,1); delay_ms(6); output_bit(EN1,0); output_bit(EN3,0); output_bit(EN4,0); output_b(mostrar[MEN2]); output_bit(EN2,1); delay_ms(6); output_bit(EN3,0); output_bit(EN2,0); output_bit(EN4,0); output_b(mostrar[MEN1]); output_bit(EN1,1); } //RUTINA DE TEMPORIZACION ANTIREBOTE void TIEMPO(void){ for(i=1;i<=100;i++){ delay_ms(1); } } //rutina de temporizacion de vizualizacion void TIEMPO1(void){ for(j=1;j<=1000;j++){ delay_us(1); } }

- 14 void main(void){ config(); MAIN: disable_interrupts(INT_TIMER0); enable_interrupts(INT_TIMER1); enable_interrupts(GLOBAL); //EN ESTA RUTINA INTRODUCES EL TIMEPO do{ if(~input(PUSH)){ TIEMPO(); SEN4++; if(SEN4>=10){ SEN4=0; SEN3++; if(SEN3>=6){ SEN3=0; MEN2++; if(MEN2>=10){ MEN2=0; MEN1++; if(MEN1>=6){ MEN1=0; } } } } } }while(input(PUSH2)); disable_interrupts(INT_TIMER1); enable_interrupts(INT_TIMER0); //EN ESAT RUTINA EMPIEZA A DECREMENTAR EL TIEMPO PUESTO ANTERIORMENTE do{ output_bit(EN1,0); output_bit(EN2,0); output_bit(EN3,0); output_b(mostrar[SEN4]); output_bit(EN4,1); TIEMPO1(); output_bit(EN1,0); output_bit(EN2,0);

- 15 output_bit(EN4,0); output_b(mostrar[SEN3]); output_bit(EN3,1); TIEMPO1(); output_bit(EN1,0); output_bit(EN3,0); output_bit(EN4,0); output_b(mostrar[MEN2]); output_bit(EN2,1); TIEMPO1(); output_bit(EN3,0); output_bit(EN2,0); output_bit(EN4,0); output_b(mostrar[MEN1]); output_bit(EN1,1); TIEMPO1(); }while(MEN1!=0 || MEN2!=0 || SEN3!=0 || SEN4!=0); disable_interrupts(GLOBAL); delay_ms(1000); goto MAIN; }

- 16 Proyecto#5 Cronometro

Este es un simple cronometro al presionar el botn de inicio correr hasta ser pausado con el botn Fin.

El programa se muestra. #include<16F628A.h> #fuses HS,NOWDT,NOLVP,MCLR,NOPROTECT #use delay(clock=20000000) #define LCD_DATA_PORT getenv("SFR:PORTB") #define LCD_ENABLE_PIN PIN_B0 #define LCD_RS_PIN PIN_B1 #define LCD_RW_PIN PIN_B2 #define LCD_DATA0 PIN_B4 #define LCD_DATA1 PIN_B5 #define LCD_DATA2 PIN_B6 #define LCD_DATA3 PIN_B7 #include<LCD.C> #define INICIO #defineFIN #defineRESET PIN_A0 PIN_A1 PIN_A2

int MILISEGUNDOS=0; int H1=0,M1=0,M2=0,S1=0,S2=0; int OVERFLOW=0;

- 17 -

void CONFIG(void){ SET_TRIS_A(0xE7); //R0:R2 COMO SALIDAS, RA5:RA7 COMO ENTRADAS SETUP_COMPARATOR(NC_NC_NC_NC); SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT); //OVERFLOW CADA APROX 0.7168mS SET_TIMER0(240); } #INT_TIMER0 void TIMER0(void){ OVERFLOW++; if(OVERFLOW>9){ OVERFLOW=0; MILISEGUNDOS++; if(MILISEGUNDOS>=100){ S1++; MILISEGUNDOS=0; if(S1==10){ S2++; S1=0; if(S2==6){ S2=0; M1++; if(M1==10){ M2++; M1=0; if(M2==6){ H1++; } } } } } } SET_TIMER0(240); } void main(void){ CONFIG(); //LLAMAMOS A LA FUNCION DE CONFIGURACION DEL PIC lcd_init(); //INICIALIZAMOS EL LCD

- 18 disable_interrupts(INT_TIMER0); disable_interrupts(GLOBAL); OFF //TIMER0 OFF //INTERRUPTIONES

lcd_gotoxy(1,1); lcd_putc(" *STOPWATCH* \n"); do{ if(!input(INICIO)){ MILISEGUNDOS=0; S1=0; S2=0; M1=0; M2=0; H1=0; enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); do{ lcd_gotoxy(1,2); printf(lcd_putc," %u:%u%u:%u%u.%u "H1,M2,M1,S2,S1,MILISEGUNDOS); }while(input(FIN)); disable_interrupts(INT_TIMER0); disable_interrupts(GLOBAL); } }while(TRUE); }//FIN

- 19 Proyecto#6 Password analoga

Es una contrasea anloga, donde se usa un teclado conectado a varias resistencias de un 1k y solo una lnea ser que se conecte al pin anlogo del microcontrolador.

El programa es el siguiente /////////////////////////////////////// //AUTOR:JORGE ARTURO RODRIGUEZ HERNANDEZ //TITLE;CONTRASENA //DATE:18/JUNIO/'09 /////////////////////////////////////// //CONFIGURACION/////////////////// #include<16f887.h> #fuses INTRC_IO,NOWDT,NOLVP,MCLR,NOPROTECT,NOPUT #use delay(clock=8000000) #include<LCD.C> int LEER_TECLADO; int TECLADO[]={"0123456789#*"}; int UNO,DOS,TRES,CUATRO; int UNO_P,DOS_P,TRES_P,CUATRO_P;

- 20 int GOA,i; void config(void){ write_eeprom(0x04,1); write_eeprom(0x05,2); write_eeprom(0x06,3); write_eeprom(0x07,4); set_tris_a(0x01); set_tris_c(0x00); setup_comparator(NC_NC_NC_NC); setup_adc(ADC_CLOCK_INTERNAL); setup_adc_ports(sAN0); output_low(PIN_C0); lcd_putc("\fCOMPILED ON\n"); lcd_putc(__DATE__); delay_ms(2000); } ADC(int VALOR_ADC){ NO_PULSADO: delay_ms(10); VALOR_ADC=read_adc(); if(VALOR_ADC>140){ goto NO_PULSADO; } else if(VALOR_ADC<24){ VALOR_ADC=1; } else if(VALOR_ADC<43){ VALOR_ADC=2; } else if(VALOR_ADC<60){ VALOR_ADC=3; } else if(VALOR_ADC<74){ VALOR_ADC=4; } else if(VALOR_ADC<88){ VALOR_ADC=5; } else if(VALOR_ADC<98){ VALOR_ADC=6; } else if(VALOR_ADC<108){ VALOR_ADC=7; }

- 21 else if(VALOR_ADC<114){ VALOR_ADC=8; } else if(VALOR_ADC<122){ VALOR_ADC=9; } else if(VALOR_ADC<128){ VALOR_ADC=0; } else if(VALOR_ADC<137){ VALOR_ADC=10; } else if(VALOR_ADC<140){ VALOR_ADC=11; } return(VALOR_ADC); } void ESCRIBIR_PASSWORD(void){ for(i=0;i<=3;i++){ delay_ms(500); LEER_TECLADO=ADC(LEER_TECLADO); write_eeprom(i,LEER_TECLADO); lcd_putc(TECLADO[LEER_TECLADO]); } delay_ms(500); } void CAMBIAR(void){ lcd_putc("\fESCRIBE AHORA\n"); ESCRIBIR_PASSWORD(); UNO=read_eeprom(0x00); DOS=read_eeprom(0x01); TRES=read_eeprom(0x02); CUATRO=read_eeprom(0x03); write_eeprom(0x04,UNO); write_eeprom(0x05,DOS); write_eeprom(0x06,TRES); write_eeprom(0x07,CUATRO); lcd_putc("\fCAMBIADO"); delay_ms(1000); }

- 22 -

void CHEKAR(void){ UNO=read_eeprom(0x00); DOS=read_eeprom(0x01); TRES=read_eeprom(0x02); CUATRO=read_eeprom(0x03); UNO_P=read_eeprom(0x04); DOS_P=read_eeprom(0x05); TRES_P=read_eeprom(0x06); CUATRO_P=read_eeprom(0x07); } void main(void){ lcd_init(); config(); AGAIN: do{ lcd_putc("\fCHANGE->(*)\n"); lcd_putc("ENTER-->(#)"); GOA=ADC(GOA); switch (GOA){ case 10: lcd_putc("\fPASSWORD:\n"); ESCRIBIR_PASSWORD(); goto CHEK; case 11: lcd_putc("\fESCRIBE EL\n"); lcd_putc("PASSWORD 1ERO"); delay_ms(800); lcd_putc("\fTYPE NOW\n"); ESCRIBIR_PASSWORD(); CHEKAR(); if(UNO==UNO_P && DOS==DOS_P && TRES==TRES_P && CUATRO==CUATRO_P){ lcd_putc("\fCORRECTO"); delay_ms(1000); CAMBIAR(); } else lcd_putc("\fINCORRECTO"); delay_ms(1000); break; } }while(TRUE);

- 23 -

CHEK:

CHEKAR();

if(UNO==UNO_P && DOS==DOS_P && TRES==TRES_P && CUATRO==CUATRO_P){ lcd_putc("\fCORRECTO"); output_high(PIN_C0); } else lcd_putc("\fINCORRECTO"); delay_ms(3000); output_low(PIN_C0); goto AGAIN; }

- 24 Proyecto#7 4-Sensores

Este es un simple circuito que capta 4 sensores de humedad y los proyecta en un LCD.

El programa es el siguiente /////////////////////////////////////// //AUTOR:JORGE ARTURO RODRIGUEZ HERNANDEZ //TITLE;SENSORES //DATE:28/AGOSTO/'09 /////////////////////////////////////// //CONFIGURACION/////////////////// #include<16F886.h> #device ADC=8 #fuses INTRC_IO,NOWDT,NOLVP,MCLR,NOPROTECT #use delay(clock=8000000) #include<LCD420_PORTC.C> #include<kbd_lib.C> int PLANTA1,PLANTA2,PLANTA3,PLANTA4; short DONE; int KEY; void config(void){ set_tris_a(0x0F);

- 25 set_tris_b(0xF0); port_b_pullups(0xF0); setup_adc(ADC_CLOCK_INTERNAL|VSS_VDD); setup_adc_ports(sAN0|sAN1|sAN2|sAN3); setup_comparator(NC_NC_NC_NC); disable_interrupts(GLOBAL); } void ADC_HECHO(void){ while(DONE!=1){ DONE=adc_done(); } } void main(void){ config(); lcd_init(); kbd_init(); START: lcd_gotoxy(1,1); lcd_putc("\f------SENSORES------"); lcd_gotoxy(1,2); lcd_putc("ENTRAR A MEDIR LOS"); lcd_gotoxy(1,3); lcd_putc("SENSORES:"); lcd_gotoxy(9,4); lcd_putc("YES(A)"); do{ KEY=kbd_getc(); if(KEY!=0){ if(KEY=='A') break; } }while(TRUE); lcd_gotoxy(1,1); lcd_putc("\f------SENSORES------"); do{ set_adc_channel(0); delay_us(50); ADC_HECHO();

- 26 PLANTA1=read_adc(); set_adc_channel(1); delay_us(50); ADC_HECHO(); PLANTA2=read_adc(); set_adc_channel(2); delay_us(50); ADC_HECHO(); PLANTA3=read_adc(); set_adc_channel(3); delay_us(50); ADC_HECHO(); PLANTA4=read_adc(); PLANTA1=2*PLANTA1; PLANTA2=2*PLANTA2; PLANTA3=2*PLANTA3; PLANTA4=2*PLANTA4; lcd_gotoxy(1,2); printf(lcd_putc,"1.- %u C 2.- %u C ",PLANTA1,PLANTA2); lcd_gotoxy(1,3); printf(lcd_putc,"3.- %u C 4.- %u C ",PLANTA3,PLANTA4); KEY=kbd_getc(); if(KEY!=0) goto START; }while(TRUE); }

- 27 -

01:11:09,8:14 pm Vol. 1.0

You might also like