18
Prof. Luis Zurita Microcontroladores II //***SENCILLO PROGRAMA QUE ACTIVA Y DESACTIVA DOS LEDS DE FORMA INTERMITENTE CADA 1 SEGUNDO*** #include<16f84A77.h> #fuses XT,NOWDT,PUT,NOPROTECT #USE delay(clock=4000000) #use fast_io(B) void main() { set_tris_B(0); while (TRUE) { output_high(PIN_B0); output_low(PIN_B1); delay_ms(1000); output_low(PIN_B0); output_high(PIN_B1); delay_ms(1000); } }

EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Embed Size (px)

Citation preview

Page 1: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

//***SENCILLO PROGRAMA QUE ACTIVA Y DESACTIVA DOS LEDS DE FORMA INTERMITENTE CADA 1 SEGUNDO***

#include<16f84A77.h>

#fuses XT,NOWDT,PUT,NOPROTECT

#USE delay(clock=4000000)

#use fast_io(B)

void main()

{

set_tris_B(0);

while (TRUE)

{

output_high(PIN_B0);

output_low(PIN_B1);

delay_ms(1000);

output_low(PIN_B0);

output_high(PIN_B1);

delay_ms(1000);

}

}

Page 2: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

//***PROGRAMA PARA DEMOSTRAR EL USO DE EVALUAR VARIAS CONDICIONES EN UNA MISMA EXPRESIÓN***

#include <16F877.h>

#fuses XT,NOWDT,NOLVP,NOPROTECT

#use delay(clock=4000000)

#use fast_io(A)

#use fast_io(B)

void main (void){

set_tris_A(0x0f);

set_tris_B(0x00);

output_B(0);

while (true) {

if ((input(PIN_A0)==0) && (input(PIN_A1)==0) && (input(PIN_A2)==1)){

output_high(PIN_B0);

output_low(PIN_B1);}

else {

output_high(PIN_B1);

output_low(PIN_B0);}

}

}

Page 3: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

Page 4: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

/// Programa: CONTADOR DE 8 bits por el puerto B /// ///////////////////////////// DIRECTIVAS DE PREPROCESADO ////////////////////// /// libreria para el manejo del pic16f877a #include <16F877A.h> /// declara la frecuencia del cristal #use delay(clock=4000000) ///configura los fusibles /// XT es la configuración del oscilador para 4 Mhz. /// NOWDT deshabilita el Watch Dog Timer /// NOPUT deshabilita le reseteo de power up timer /// NOPROTECT deshabilita la proteccion del codigo del pic. #fuses XT,NOWDT,NOPUT,NOPROTECT /// asignamos a variable port_b el espacio memoria 0x06 que es la dir de port_b #byte port_b=0x06 //////////////////////////GLOBALES////////////////////////////////////////////// /// Estos parámetros son visibles desde todo el código // /// por eso se les llama globales /// /// Al ser constantes, lo más práctico es declararlas aquí. // //////////////////////////////////////////////////////////////////////////////// int contador; void main(){ // Principal // set_tris_b(0); /// declaramos el puerto B como salidas /// lo igualamos con cero para que tengamos un valor inicial port_b=0; /// bucle infinito para que las instrucciones que tiene siempre se ejecuten contador=0x00;

while (true) { contador++; /// activa todos los pins del puerto B

Page 5: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

port_b=contador; /// Retardo de 1 segundo delay_ms(1000); } /// fin de bucle while

} /// fin de función principal main

Page 6: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

/// Programa: LEDs Parpadeando como el Auto fantástico /// /// libreria para el manejo del pic16f877a #include <16F877A.h> ///configura los fusibles /// XT es la configuración del oscilador para 4 Mhz. /// NOWDT deshabilita el Watch Dog Timer /// NOPUT deshabilita le reseteo de power up timer /// NOPROTECT deshabilita la proteccion del codigo del pic. #fuses XT,NOWDT,NOPUT,NOPROTECT /// declara la frecuencia del cristal #use delay(clock=4000000) /// asignamos a variable port_b el espacio memoria 0x06 que es la dir de port_b #byte port_b=0x06 void main() //Programa principal { set_tris_b(0); //Puerto B como salida port_b=0x01; while (true){ /// bucle infinito para que las instrucciones que tiene siempre se ejecuten port_b=port_b<<1; //Rota un bit a la izquierda if (port_b==0){ //Si llega a cero carga el valor inicial para rotar a la derecha port_b=0b10000000; //y entra en el bucle de rotación a la derecha while (true){ port_b=port_b>>1; //Rota un bit a la derecha if (port_b==0){ //Si llega a cero carga valor inicial port_b=0x01; //y sale de este bucle regresando al bucle anterior break;} delay_ms(100); } } delay_ms(100); // Retardo de 100 milisegundo } // fin de bucle while } // fin de funcion principal main

Page 7: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

Page 8: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

//***PROGRAMA QUE DEMUESTRA EL USO DEL CASE*** #include <16F84A.h> #fuses XT,NOWDT,PUT,NOPROTECT #use fast_io(A) #use fast_io(B) int DATOA=0; void main (){ set_tris_a(0x1f); set_tris_b(0x00); output_b(0); while (TRUE){ DATOA=input_a(); switch (DATOA){ case 0: output_b(0b00000011); break; case 1: output_b(0b00001100); break; case 2: output_b(0b00000111); break; default: output_b(0b00001111); break; } } }

Page 9: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

Page 10: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

//***PROGRAMA DE UN CONTADOR SENCILLO DE DOS CIFRAS ASCENDENTES*** #include <16f877.h> #fuses XT,NOWDT,PUT,NOPROTECT,NOLVP #use delay(clock=4000000) #use fast_io(A) #use fast_io(B) int UNI=0,DEC=0; void mostrar(){ output_low(PIN_B1); output_low(PIN_B2); output_a(UNI); output_high(PIN_B1); output_low(PIN_B2); delay_ms(5); output_low(PIN_B1); output_low(PIN_B2); output_a(DEC); output_low(PIN_B1); output_high(PIN_B2); delay_ms(5); output_low(PIN_B2); } void mostrar1(){ output_low(PIN_B1); output_low(PIN_B2); output_a(UNI); output_high(PIN_B1); output_low(PIN_B2); delay_ms(5); } void main ( ) // Programa principal { set_tris_B(0x01); set_tris_A(0);

Page 11: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

while (1) { if (bit_test (PORTB,0)==0)

{ if (DEC==0){ mostrar1();} else{ mostrar();} delay_ms(5); if (bit_test (PORTB,0)==1) { if (UNI>=9){ UNI=0; if (DEC>=9){ DEC=0;} else{ DEC=DEC+1;} } else { UNI=UNI+1; } } } else if (DEC==0){ mostrar1();} else { mostrar(); } } }

Page 12: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

Page 13: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

//***PROGRAMA DEL ESTACIONAMIENTO EN LENGUAJE C*** #include <16f877.h> #fuses XT,NOWDT,PUT,NOPROTECT,NOLVP #use delay(clock=4000000) #use fast_io(A) #use fast_io(B) int UNI=0,DEC=0; void mostrar(){ //Función para mostrar datos en los displays con multiplexación output_low(PIN_B0); output_low(PIN_B1); output_a(UNI); output_high(PIN_B0); output_low(PIN_B1); delay_ms(5); output_low(PIN_B0); output_low(PIN_B1); output_a(DEC); output_low(PIN_B0); output_high(PIN_B1); delay_ms(5); output_low(PIN_B1); } void main ( ) // Programa principal { set_tris_B(0b11110000); set_tris_A(0); while (true) { if(input(PIN_B4)==0) //Pregunta si se activó la entrada 1 {

Page 14: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

mostrar(); delay_ms(5); if(input(PIN_B4)==1){ if (UNI>=9){ UNI=0; if (DEC>=4){ DEC=5; output_high(PIN_B3); //Led NOHAYCUPO=0n output_low(PIN_B2); //Led HAYCUPO=0ff } else{ DEC=DEC+1;} } else { if (DEC==5){ DEC=5; output_high(PIN_B3); //Led NOHAYCUPO=0n output_low(PIN_B2);} //Led HAYCUPO=0ff else{ UNI=UNI+1;} } } } else if(input(PIN_B5)==0) //Pregunta si se activó la entrada 2 { mostrar(); delay_ms(5); if(input(PIN_B5)==1){ if (UNI>=9){ UNI=0;

Page 15: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

if (DEC>=4){ DEC=5; output_high(PIN_B3); output_low(PIN_B2); } else{ DEC=DEC+1;} } else { if (DEC==5){ UNI=0; DEC=5; output_high(PIN_B3); output_low(PIN_B2);} else{ UNI=UNI+1;} } } } else if(input(PIN_B6)==0) //Pregunta si se activó la salida 1 { mostrar(); delay_ms(5); if(input(PIN_B6)==1){ if (UNI<=0){ UNI=9; if (DEC<=0){ DEC=0; UNI=0;} else{

Page 16: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

DEC=DEC-1; output_high(PIN_B2); //Led HAYCUPO=0n output_low(PIN_B3); //Led NOHAYCUPO=off } } else { UNI=UNI-1; output_high(PIN_B2); //Led HAYCUPO=0n output_low(PIN_B3); //Led NOHAYCUPO=off } } } else if(input(PIN_B7)==0) //Pregunta si se activó la salida 2 { mostrar(); delay_ms(5); if(input(PIN_B7)==1){ if (UNI<=0){ UNI=9; if (DEC<=0){ DEC=0; UNI=0;} else{ DEC=DEC-1; output_high(PIN_B2); output_low(PIN_B3); } } else { UNI=UNI-1;

Page 17: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II

output_high(PIN_B2); output_low(PIN_B3); } } } else { mostrar(); } } }

Page 18: EJERCICIOS TEMA 1. MICROCONTROLADORES II EN C

Prof. Luis Zurita Microcontroladores II