Documentos de Académico
Documentos de Profesional
Documentos de Cultura
0014
Manual Practicas Pic en C
5. Juego de luces.
6. Contador ascendente.
7. Contador 0 a 9 BCD&Boton.
///PROGRAMA
void main(void)
{
set_tris_b(0xFE); //portb como salida(RB0,las demas desactivadas)
disable_interrupts(GLOBAL); //todas las interrupciones desactivadas
do{
output_low(PIN_B0); //led off
delay_ms(500);
output_high(PIN_B0); //led on
delay_ms(500);
}while(TRUE); //bucle infinito
}
2. Parpadeo de tres led cada 0.5 seg.
//Programa: Parpadeo de tres leds cada 0.5s
//Version: 0.1
//
//Dispositivo: PIC 16F648A Compilador: CCS vs3.227
//Entorno IDE: MPLAB IDE v7.20 Simulador: Proteus 6.7sp3
//
//Notas: Parpadeo de tres leds cada 0.5s de tres formas diferentes:
// RB0 -> con la funcion output_high()/output_low()
// RB1 -> definiendo el pin RB1
// RB2 -> con la funcion output_bit()
//////////////////////////////////////////////////////////////////////////////////
///PROGRAMA
void main(void)
{
set_tris_b(0xF8); //puerto b como salida
disable_interrupts(GLOBAL); //todas interrupciones desactivadas
///PROGRAMA
void main(void)
{
set_tris_b(0x00); //puerto b como salida
disable_interrupts(GLOBAL); //todas interrupciones desactivadas
///DECLARACIONES DE FUNCIONES
void derecha(void); //ilumina led"s derecha a izquierda
void izquierda(void); //ilumina led"s izquierda a derecha
///PROGRAMA
void main(void)
{
set_tris_a(0xF0); //porta como salida menos RA4(desactivado)
disable_interrupts(GLOBAL); //todas las interrupciones desactivadas
do{ //bucle...
derecha();
izquierda();
}while(TRUE); //...infinito
}
void derecha(void)
{
output_high(PIN_A0);
delay_ms(300);
output_low(PIN_A0);
output_high(PIN_A1);
delay_ms(300);
output_low(PIN_A1);
output_high(PIN_A2);
delay_ms(300);
output_low(PIN_A2);
output_high(PIN_A3);
delay_ms(300);
}
void izquierda(void)
{
output_low(PIN_A3);
output_high(PIN_A2);
delay_ms(300);
output_low(PIN_A2);
output_high(PIN_A1);
delay_ms(300);
output_low(PIN_A1);
}
5. Juego de luces
// Programa: Juego de luces
// Version: 0.0
//
// Dispositivo: PIC 16F648A Compilador: CCS vs3.227
// Entorno IDE: MPLAB IDE v7.20 Simulador: Proteus 6.7sp3
//
// Notas: Iluminacion de juego de luces fuera-dentro y viceversa por el puerto B.
// Adaptacion a c por vszener del codigo en basic de lordlafebre.
//
// RB0 -> 1 Led
// RB1 -> 2 Led
// RB2 -> 3 Led
// RB3 -> 4 Led
// RB4 -> 5 Led
// RB5 -> 6 Led
// RB6 -> 7 Led
// RB7 -> 8 Led
//////////////////////////////////////////////////////////////////////////////////
///PROGRAMA
void main(void)
{
signed char i; //variable indice
int leds[4]={0b10000001,0b01000010,0b00100100,0b00011000}; //led"s
do{ //bucle...
}while(TRUE); //...infinito
}
6. Contador ascendente.
// Programa: Contador ascendente 0-9
// Version: 0.0
//
// Dispositivo: PIC 16F648A Compilador: CCS vs3.227
// Entorno IDE: MPLAB IDE v7.20 Simulador: Proteus 6.7sp3
//
// Notas: Contador del 0 al 9 cada 0,5s y vuelta a empezar. Tener en cuenta
// que hay que poner la directiva NOLVP para que el pin B4 sea de salida
//////////////////////////////////////////////////////////////////////////////////
///PROGRAMA
void main(void)
{
char i=0; //contador para tabla 7 seg
int tab7seg[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67}; //7seg hex 0-9
do{ //bucle...
output_b(tab7seg[ i ]); //muestra por portb digito 7 segmentos
delay_ms(500);
i++; //incremento contador para visualizar siguiente digito
if(i>9) //ya se ha mostrado el digito 9?
{
i=0; //SI -> vuelve a empezar(digito 0)
}
}while(TRUE); //...infinito
}
7. Contador 0 a 9 BCD&Boton.
// Programa: Contador 0-9 display BDC & Boton
// Version: 0.0
//
// Dispositivo: PIC 16F648A Compilador: CCS vs3.227
// Entorno IDE: MPLAB IDE v7.20 Simulador: Proteus 6.7sp3
//
// Notas: Contador 0 al 9 cada vez que pulsemos el boton y vuelta a empezar. Tener
// en cuenta que hay que poner la directiva NOLVP para que el pin B4 sea de
// salida. Cuando agregamos un boton a nuestro circuito hay que tener en
cuenta
// que este dispositivo genera 'rebotes' que hay que ser eliminados para
// una correcta visualizacion en el display del digito seleccionado. Esta vez
// la eliminacion de 'los rebotes' se ha realizado mediante software.
// Cuando por el pin A0 del porta se introduce un '0' logico(low), se
// incrementa un digito en el display BCD.
//
// Conexiones: A0 -> boton
// B0 -> a
// B1 -> b
// B2 -> c
// B3 -> d
// BCD:
// d c b a NUM
// 0000 0
// 0001 1
// 0010 2
// 0011 3
// 0100 4
// 0101 5
// 0110 6
// 0111 7
// 1000 8
// 1001 9
//////////////////////////////////////////////////////////////////////////////////
///PROGRAMA
void main(void)
{
char i=0; //contador para tabla BCD
int
tabBCD[10]={0b0000,0b0001,0b0010,0b0011,0b0100,0b0101,0b0110,0b0111,0b1000,0b1001};
//BCD 0-9
set_tris_a(0xFF); //porta como entrada
disable_interrupts(GLOBAL); //todas las interrupciones desactivadas
for(;;){ //bucle...
if(!input(PIN_A0)) //se ha pulsado el boton?
{
delay_ms(151); //SI -> retardo para evitar los
rebotes
i++; //incremento contador
indice tabBCD
if(i>9) //se ha mostrado digito
9?
i=0; //SI -> restaura valor
indice(para mostrar digito 0)
output_b(tabBCD[i]); //muestra por portb digito 7
segmentos
}
} //...infinito
}
8. Sper contador 0000a9999 (7 seg.)
// Programa: Contador del 0000 al 9999
// Version: 0.0
//
// Dispositivo: PIC 16F648A Compilador: CCS vs3.227
// Entorno IDE: MPLAB IDE v7.20 Simulador: Proteus 6.7sp3
//
// Notas: Contador del 0000 al 9999 cada 1s y vuelta a empezar. Tener en cuenta
// que hay que poner la directiva NOLVP para que el pin B4 sea de salida.
// Se utiliza variables locales:
// * i -> indice tabla 7seg para mostrar digito por 1 7seg
// * j -> indice tabla 7seg para mostrar digito por 2 7seg
// * w -> indice tabla 7seg para mostrar digito por 3 7seg
// * z -> indice tabla 7seg para mostrar digito por 4 7seg
// * flag -> variable que cuenta 1s
// * var -> ajuste fino para que desborde cada segundo
// Utilizamos la funcion de interrupcion para actualizar indices de la
// tabla de 7seg para mostrar el digito correspondiente en el respectivo
// 7seg, para ello el TMR0 se desborda cada 1s, para ello debe ser cargado
// con 61(equivale a un desbordamiento cada 50ms mas o menos), para obtener
// un desbordamiento de 1s utilizamos una variable(flag) que no entra en
// la actualizacion de indices hasta transcurrido 1s.
// Conexiones:
// RA0 -> Display 1 7seg
// RA1 -> Display 2 7seg
// RA2 -> Display 3 7seg
// RA3 -> Display 4 7seg
// RB0 -> a 7seg
// RB1 -> b 7seg
// RB2 -> c 7seg
// RB3 -> d 7seg
// RB4 -> e 7seg
// RB5 -> f 7seg
// RB6 -> g 7seg
//////////////////////////////////////////////////////////////////////////////////
///PROGRAMA
void main(void)
{
int tab7seg[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67}; //7seg hex 0-9
do{ //bucle...
output_high(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_low(PIN_A3); //activado...
output_b(tab7seg[ i ]); //...1 7seg
delay_ms(10);
output_high(PIN_A0);
output_high(PIN_A1);
output_low(PIN_A2); //activado...
output_high(PIN_A3);
output_b(tab7seg[ j ]); //...2 7seg
delay_ms(10);
output_high(PIN_A0);
output_low(PIN_A1); //activado...
output_high(PIN_A2);
output_high(PIN_A3);
output_b(tab7seg[ w ]); //...3 7seg
delay_ms(10);
output_low(PIN_A0); //activado...
output_high(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
output_b(tab7seg[ z ]); //...4 7seg
delay_ms(10);
}while(TRUE); //...infinito
}
9. Dado digital (7 seg).
// Programa: Dado digital
// Version: 0.0
//
// Dispositivo: PIC 16F648A Compilador: CCS vs3.227
// Entorno IDE: MPLAB IDE v7.20 Simulador: Proteus 6.7sp3
//
// Notas: Dado digital, que al presionar el boton conectado al pin A0 del porta
// genera un numero pseudo-aleatorio mediante la funcion rand() que se
// encuentra en la libreria STDLIB.H. El numero pseudo-aleatorio es elegido
// mediante la funcion rand() y segun los "rebotes" producidos por el boton.Tener
// en cuenta que hay que poner la directiva NOLVP para que el pin B4 sea de
// salida. Cuando agregamos un boton a nuestro circuito hay que tener en cuenta
// que este dispositivo genera "rebotes" que hay que ser eliminados para
// una correcta visualizacion en el display del digito seleccionado. Esta vez
// la eliminacion de "los rebotes" se ha realizado mediante software.
//
// Conexiones: A0 -> boton
// B0 -> a
// B1 -> b
// B2 -> c
// B3 -> d
// BCD:
// d c b a NUM
// 0000 0
// 0001 1
// 0010 2
// 0011 3
// 0100 4
// 0101 5
// 0110 6
// 0111 7
// 1000 8
// 1001 9
//////////////////////////////////////////////////////////////////////////////////
///PROGRAMA
void main(void)
{
char num=0; //variable almacena numero aleatorio
///PROGRAMA
void main(void)
{
lcd_init(); //inicializa lcd
printf(lcd_putc,"hola mundo ; )
VsZeNeR"05" ); //muestra por pantalla el mensaje
}
11. Abecedario (Lcd).
// Programa: Abecedario
// Version: 0.0
//
// Dispositivo: PIC 16F648A Compilador: CCS vs3.227
// Entorno IDE: MPLAB IDE v7.21 Simulador: Proteus 6.7sp3
//
// Notas: Se muestra por pantalla de lcd(LM016L) el abecedario y vuelta a empezar
// Tener en cuenta que hay que poner la directiva NOLVP para que el pin B4 sea de
// salida.
// abecedario[] -> vector donde se almacena abecedario
// x -> indice para vector abecedario e indice para columna lcd
// y -> indice para fila lcd: y=1 -> Fila 1
// y=2 -> Fila 2
// Conexiones: B0 -> E
// B1 -> RS
// B2 -> RW
// B4 -> D4
// B5 -> D5
// B6 -> D6
// B7 -> D7
//////////////////////////////////////////////////////////////////////////////////
///PROGRAMA
void main(void)
{
char abecedario[27]={"
","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","
z"},x,y=1; //tabla y variables definidas
do{ //bucle...
for(x=1;x<27;x++){ //bucle para mostrar digito
if(y==1) //1 fila del lcd
lcd_gotoxy(x,y);
else //2 fila del lcd
lcd_gotoxy((x-16),y);
///PROGRAMA
void main(void)
{
char num=0; //variable almacena numero aleatorio
lcd_putc("fDado:
VsZeNeR"05" );
lcd_gotoxy(7,1); //Coordenadas dond se muestra el numero
for( ; ; ){ //bucle...
if(!input(PIN_A0)) //se ha pulsado el boton?
{
do{ //SI -> elimina...
num=rand(); //genera numero pseudo-aleatorio
}while(!input(PIN_A0)); //...rebotes
///VARIABLES GLOBALES
byte day,mth,year,dow,hour,min,sec; //variabes para ds1302
byte menu=0,flag=0,var=2; //variables para menu configurar
///DEFINICION DE FUNCIONES
void configurar(void);
void horas(void);
void minutos(void);
void dia(void);
void mes(void);
void anio(void);
void beep(void);
///PROGRAMA
void main(void){
puts("Reloj Calendario" );
puts("VsZeNeR"05" );
delay_ms(800); //...inicial
///FUNCION CONFIGURAR
void configurar(void){
disable_interrupts(GLOBAL); //desactivadas interrupciones
do{
switch(menu){
case 0: printf("fConfigurar
horas?" ); //horas
if(!input(PIN_A5)){
while(!input(PIN_A5)){}
beep();
horas();
menu=1; //apunta siguiente opcion
flag=1; //para el retorno funcion ver sig
}
break;
case 1: printf("fConfigurar
minutos?" ); //minutos
if(!input(PIN_A5)){
while(!input(PIN_A5)){}
beep();
minutos();
menu=2; //apunta siguiente opcion
flag=1; //para el retorno funcion ver sig
}
break;
case 2: printf("fConfigurar
dia?" ); //dias
if(!input(PIN_A5)){
while(!input(PIN_A5)){}
beep();
dia();
menu=3; //apunta siguiente opcion
flag=1; //para el retorno funcion ver sig
}
break;
case 3: printf("fConfigurar
mes?" ); //mes
if(!input(PIN_A5)){
while(!input(PIN_A5)){}
beep();
mes();
menu=4; //apunta siguiente opcion
flag=1; //para el retorno funcion ver sig
}
break;
case 4: printf("fConfigurar
ao?" ); //aos
if(!input(PIN_A5)){
while(!input(PIN_A5)){}
beep();
anio();
menu=5; //apunta siguiente opcion
flag=1; //para el retorno funcion ver sig
}
break;
case 5: printf("fSalir
configurar?" ); //salir configuracion
if(!input(PIN_A5)){
while(!input(PIN_A5)){} //elimina rebotes
menu=6;
flag=1;
beep();
}
}
flag=0;
}while(menu<6);
///PROGRAMA
void main(void)
{
setup_oscillator(OSC_4MHZ); //configuracion
del oscilador interno a 4MHz
enable_interrupts(INT_TIMER1); //interrupcion TIMER1
activada
SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8); //configuracion
interrupcion TMR1
set_timer1(10); //carga
TMR1
enable_interrupts(GLOBAL); //activadas
interrupciones
setup_uart(TRUE);
//activamos la uart
for(;;){} //bucle infinito -> espera interrupcion del TMR1
}