Вы находитесь на странице: 1из 8

SISTEMAS DIGITALES 3

UNIVERSIDAD ECCI

LABORATORIO 2
Erika Ortega, Juan Carlos Dimas, Luis Garcia
Erika.ortega1809@gmail.com
Juangt87@gmail.com
lumiga89@gmail.com
Bogot, Colombia

Abstract Using necessary libraries for the operation momento de finalizar el conteo suene una alarma.
of TIMER0 and PWM modules, programming is done Esto se llevara acabao utilizando el modulo de
so that the appropriate times are generated for a Timer0.
manual countdown entered time ( timer ) and the
manipulation speed of a motor.

1. INTRODUCCIN

Haciendo el uso de las libreras adecuadas para


lograr el correcto funcionamiento de los mdulos
TIMER0 con el fin de generar los tiempos
adecuados para generar un cronometro en cuenta
regresiva en donde el usuario tenga la opcin de
definir el momento inicial de conteo y el PWM
para controlar la velocidad y sentido de giro de un
motor.

2. OBJETIVOS

Aplicar las liberaras de Timer0 y PWM,


en el PIC de la familia 18F4550

Realizar los clculos necesarios para la


configuracin del TMR0 y PWM.

3. DESARROLLO

Se realiza la programacin y el diseo del


montaje para dar solucin a los ejercicios 1 y 2. El
montaje fsico consta:
PIC18F4550
Integrado L293d
Pulsadores
Dip-swichs CODIGO
LCD
Motor 5V. Ejercicio 1 Cronometro:

#include <xc.h>
4. EJERCICIO 1 #define _XTAL_FREQ 4000000
//#include <plib/timers.h>
Se plantea realizar un cronometro con cuenta #include "flex_lcd.h"
regresiva en donde el usuario por medio de #include <stdio.h>
pulsadores elija el desde donde quiere iniciar y al #include <stdlib.h>

1
SISTEMAS DIGITALES 3
UNIVERSIDAD ECCI
#pragma config MCLRE = ON // MCLR Pin
// CONFIG1L Enable bit (MCLR pin enabled; RE3 input pin
#pragma config PLLDIV = 1 // PLL Prescaler disabled)
Selection bits (No prescale (4 MHz oscillator input
drives PLL directly)) // CONFIG4L
#pragma config CPUDIV = OSC1_PLL2// System #pragma config STVREN = ON // Stack
Clock Postscaler Selection bits ([Primary Full/Underflow Reset Enable bit (Stack
Oscillator Src: /1][96 MHz PLL Src: /2]) full/underflow will cause Reset)
#pragma config USBDIV = 1 // USB Clock #pragma config LVP = OFF // Single-Supply
Selection bit (used in Full-Speed USB mode only; ICSP Enable bit (Single-Supply ICSP disabled)
UCFG:FSEN = 1) (USB clock source comes #pragma config ICPRT = OFF // Dedicated In-
directly from the primary oscillator block with no Circuit Debug/Programming Port (ICPORT)
postscale) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended
// CONFIG1H Instruction Set Enable bit (Instruction set extension
#pragma config FOSC = INTOSC_EC // Oscillator and Indexed Addressing mode disabled (Legacy
Selection bits (Internal oscillator, CLKO function mode))
on RA6, EC used by USB (INTCKO))
#pragma config FCMEN = OFF // Fail-Safe // CONFIG5L
Clock Monitor Enable bit (Fail-Safe Clock Monitor #pragma config CP0 = OFF // Code Protection
disabled) bit (Block 0 (000800-001FFFh) is not code-
#pragma config IESO = OFF // protected)
Internal/External Oscillator Switchover bit #pragma config CP1 = OFF // Code Protection
(Oscillator Switchover mode disabled) bit (Block 1 (002000-003FFFh) is not code-
protected)
#pragma config CP2 = OFF // Code Protection
// CONFIG2L bit (Block 2 (004000-005FFFh) is not code-
#pragma config PWRT = ON // Power-up protected)
Timer Enable bit (PWRT enabled) #pragma config CP3 = OFF // Code Protection
#pragma config BOR = OFF // Brown-out bit (Block 3 (006000-007FFFh) is not code-
Reset Enable bits (Brown-out Reset disabled in protected)
hardware and software)
#pragma config BORV = 3 // Brown-out Reset // CONFIG5H
Voltage bits (Minimum setting 2.05V) #pragma config CPB = OFF // Boot Block
#pragma config VREGEN = OFF // USB Code Protection bit (Boot block (000000-0007FFh)
Voltage Regulator Enable bit (USB voltage is not code-protected)
regulator disabled) #pragma config CPD = OFF // Data EEPROM
Code Protection bit (Data EEPROM is not code-
// CONFIG2H protected)
#pragma config WDT = OFF // Watchdog
Timer Enable bit (WDT disabled (control is placed // CONFIG6L
on the SWDTEN bit)) #pragma config WRT0 = OFF // Write
#pragma config WDTPS = 32768 // Watchdog Protection bit (Block 0 (000800-001FFFh) is not
Timer Postscale Select bits (1:32768) write-protected)
#pragma config WRT1 = OFF // Write
// CONFIG3H Protection bit (Block 1 (002000-003FFFh) is not
#pragma config CCP2MX = ON // CCP2 MUX write-protected)
bit (CCP2 input/output is multiplexed with RC1) #pragma config WRT2 = OFF // Write
#pragma config PBADEN = OFF // PORTB A/D Protection bit (Block 2 (004000-005FFFh) is not
Enable bit (PORTB<4:0> pins are configured as write-protected)
digital I/O on Reset) #pragma config WRT3 = OFF // Write
#pragma config LPT1OSC = OFF // Low-Power Protection bit (Block 3 (006000-007FFFh) is not
Timer 1 Oscillator Enable bit (Timer1 configured write-protected)
for higher power operation)
// CONFIG6H

2
SISTEMAS DIGITALES 3
UNIVERSIDAD ECCI
#pragma config WRTC = OFF // Configuration //****************************************
Register Write Protection bit (Configuration *************************
registers (300000-3000FFh) are not write- T0CON = 0b00000110;// timer de 16 bits
protected) TMR0L = 0xBA;
#pragma config WRTB = OFF // Boot Block TMR0H = 0xF0;
Write Protection bit (Boot block (000000- T0CONbits.TMR0ON = 1; //PRENDE EL
0007FFh) is not write-protected) TIMER
#pragma config WRTD = OFF // Data NOP();
EEPROM Write Protection bit (Data EEPROM is NOP();
not write-protected) //****************************************
*****************
// CONFIG7L Lcd_Init(); //inicializamos el lcd
#pragma config EBTR0 = OFF // Table Read Lcd_Cmd(LCD_CLEAR); //limpiamos lcd
Protection bit (Block 0 (000800-001FFFh) is not Lcd_Cmd(LCD_CURSOR_OFF); //apagamos
protected from table reads executed in other el cursor
blocks) __delay_ms(100); //esperamos 100ms
#pragma config EBTR1 = OFF // Table Read Lcd_Out(1, 1, "CRONOMETRO");
Protection bit (Block 1 (002000-003FFFh) is not //escribimos en la linea 2 espacio 1 la palabra
protected from table reads executed in other mkmekatronika
blocks) __delay_ms(100);
#pragma config EBTR2 = OFF // Table Read Lcd_Cmd(LCD_CLEAR); //limpiamos lcd
Protection bit (Block 2 (004000-005FFFh) is not
protected from table reads executed in other
blocks)
#pragma config EBTR3 = OFF // Table Read
Protection bit (Block 3 (006000-007FFFh) is not while(1)
protected from table reads executed in other {
blocks) if(INTCONbits.TMR0IF) // VERIFICAMOS,
OCURRIO DESBORDE?
// CONFIG7H {
#pragma config EBTRB = OFF // Boot Block //WriteTimer0(0xE1); // CARGO EL
Table Read Protection bit (Boot block (000000- TMR0 CON EL VALOR PARA LA
0007FFh) is not protected from table reads TEMPORIZACI?N
executed in other blocks) TMR0L = 0xBA;
TMR0H = 0xF0;
INTCONbits.TMR0IF = 0; // BORRO
unsigned char Bufer_1[20];//bufer para gusrdar LA BANDERA DE DESBORDE
datos de lcd if((minutos == 0)&&(segundos == 0))
unsigned char con =0; {}
unsigned char segundos =0; else
unsigned char minutos =0; {
segundos = segundos - 1;//segundos
void CONF_OSC(void); almacenados
void CONF_PUERTOS(void); }
void CONF_Reloj(void); }
void Conf_Segundos(void);
void Conf_Minutos(void);
if(PORTBbits.RB7 == 1)
{
__delay_ms(50);//tiempo espera antirrebote
if(PORTBbits.RB7 == 1)
void main (void)
{
{
CONF_Reloj();// llamado configuracion
CONF_OSC();
segundos y minutos
CONF_PUERTOS();
Lcd_Cmd(LCD_CLEAR);
//OpenTimer0(TIMER_INT_ON&T0_16BIT&T
//limpiamos lcd
0_SOURCE_INT&T0_PS_1_16);
}

3
SISTEMAS DIGITALES 3
UNIVERSIDAD ECCI
} void CONF_PUERTOS(void) ///
CONFIGURACI?N DE PUERTOS
if(segundos==0)//si segundos es igual a cero {
{ TRISB = 0b11000000; // salida lcd
if(minutos >= 1)//si minutos es mayor o TRISD = 0b00000000; //entrada enter,selector,y
igual a 1 salida busser
{ }
minutos--;//resta 1 a minutos
segundos=59;
} //===================================
} ====================================
=======
if((minutos == 0)&&(segundos == 0)) void CONF_Reloj(void)
{ {
if(con<2) unsigned char contador = 0;
{
LATDbits.LATD7 =1;
__delay_ms(100); while(contador < 2)
__delay_ms(100); {
__delay_ms(100);
__delay_ms(100); switch(contador)
LATDbits.LATD7 =0; {
__delay_ms(100); case 0:// setear segundos
__delay_ms(100); {
Conf_Segundos();
con=con++; // cantidad de veces q sonara el contador = contador + 1;
buzeer break;
} }
} case 1:// setear minutos
{
sprintf(Bufer_1,"%02d",minutos); Conf_Minutos();
//guardamos en el string Buffer1 la palabra Cuenta contador = contador + 1;
espacio y el valor de i con=0;
Lcd_Out2(2, 4, Bufer_1); break;
Lcd_Out2(2, 6,":"); }
sprintf(Bufer_1,"%02d",segundos);
//guardamos en el string Buffer1 la palabra Cuenta }
espacio y el valor de i }
Lcd_Out2(2, 7, Bufer_1);
}
} //===================================
} ====================================
=======
void Conf_Segundos(void)
////////////// FUNCIONES /////////////
{
void CONF_OSC(void) /// CONFIGURAMOS A __delay_ms(100);
4MHZ EL OSCILADOR Lcd_Cmd(LCD_CLEAR);//limpiamos lcd
{ Lcd_Out(1, 1, "segundos");
OSCCONbits.IRCF2 = 1; // registros asociados Lcd_Out(2, 1, " : ");
oscon, para configurar do
OSCCONbits.IRCF1 = 1; {
OSCCONbits.IRCF0 = 0; if(PORTBbits.RB6 == 1)
} {
segundos = segundos + 1;
if(segundos >=59){segundos = 0;}

4
SISTEMAS DIGITALES 3
UNIVERSIDAD ECCI
sprintf(Bufer_1,"%02d",segundos); Utilizando los modulos Timer y el PWM, se realiza
//guardamos en el string Buffer1 la palabra Cuenta un diseo que permite variar la velocidad de un
espacio y el valor de i motor DC a 5V, permitiendo aumentar y disminuir
Lcd_Out2(2, 7, Bufer_1); //escribimos en la velocidad por medio de un pulsado. El cambio
el renglon uno espaco 1 la que contiene buffer1 de giro se realiza con otro pulsador. Utilizamos el
__delay_ms(100); //esperamos 100ms y driver sugerido L293
repetimos

}
while(PORTBbits.RB7 == 0);// salir de la
funcion segundos

__delay_ms(100);
__delay_ms(100);
__delay_ms(100);

} Calculo para 1 segundo


//===================================
==================================== Tempo=1 Seg
=======
void Conf_Minutos(void) Xtal=4 Mhz
{
__delay_ms(100); Preescaler=16
Lcd_Out(1, 1, " ");
Lcd_Out(1, 1, "Minutos");
do 1
{ 65535N =
1
if(PORTBbits.RB6 == 1)//si se preciona boton 4X X 16
seleccion 4 Mhz
{
minutos = minutos + 1;
if(minutos >=59){minutos = 0;} 65535N =63500
sprintf(Bufer_1,"%02d",minutos);
//guardamos en el string Buffer1 la palabra Cuenta N=6553563500
espacio y el valor de i
Lcd_Out2(2, 4, Bufer_1); //escribimos en N=3035
el renglon uno espaco 1 la que contiene buffer1
__delay_ms(100); //esperamos 100ms y
repetimos
} CODIGO

} Ejercicio 2 Control Motor:


while(PORTBbits.RB7 == 0);// sale si se
preciona enter #include <xc.h>
__delay_ms(100); #define _XTAL_FREQ 4000000
__delay_ms(100); #include <delays.h>
__delay_ms(100); #include <pwm.h>

} // CONFIG1L
//======= #pragma config PLLDIV = 1 // PLL Prescaler
Selection bits (No prescale (4 MHz oscillator input
4. EJERCICIO 2 drives PLL directly))

5
SISTEMAS DIGITALES 3
UNIVERSIDAD ECCI
#pragma config CPUDIV = OSC1_PLL2// System #pragma config LVP = OFF // Single-Supply
Clock Postscaler Selection bits ([Primary ICSP Enable bit (Single-Supply ICSP disabled)
Oscillator Src: /1][96 MHz PLL Src: /2]) #pragma config ICPRT = OFF // Dedicated In-
#pragma config USBDIV = 1 // USB Clock Circuit Debug/Programming Port (ICPORT)
Selection bit (used in Full-Speed USB mode only; Enable bit (ICPORT disabled)
UCFG:FSEN = 1) (USB clock source comes #pragma config XINST = OFF // Extended
directly from the primary oscillator block with no Instruction Set Enable bit (Instruction set extension
postscale) and Indexed Addressing mode disabled (Legacy
mode))
// CONFIG1H
#pragma config FOSC = INTOSC_EC // Oscillator // CONFIG5L
Selection bits (Internal oscillator, CLKO function #pragma config CP0 = OFF // Code Protection
on RA6, EC used by USB (INTCKO)) bit (Block 0 (000800-001FFFh) is not code-
#pragma config FCMEN = OFF // Fail-Safe protected)
Clock Monitor Enable bit (Fail-Safe Clock Monitor #pragma config CP1 = OFF // Code Protection
disabled) bit (Block 1 (002000-003FFFh) is not code-
#pragma config IESO = OFF // protected)
Internal/External Oscillator Switchover bit #pragma config CP2 = OFF // Code Protection
(Oscillator Switchover mode disabled) bit (Block 2 (004000-005FFFh) is not code-
protected)
// CONFIG2L #pragma config CP3 = OFF // Code Protection
#pragma config PWRT = ON // Power-up bit (Block 3 (006000-007FFFh) is not code-
Timer Enable bit (PWRT enabled) protected)
#pragma config BOR = OFF // Brown-out
Reset Enable bits (Brown-out Reset disabled in // CONFIG5H
hardware and software) #pragma config CPB = OFF // Boot Block
#pragma config BORV = 3 // Brown-out Reset Code Protection bit (Boot block (000000-0007FFh)
Voltage bits (Minimum setting 2.05V) is not code-protected)
#pragma config VREGEN = OFF // USB #pragma config CPD = OFF // Data EEPROM
Voltage Regulator Enable bit (USB voltage Code Protection bit (Data EEPROM is not code-
regulator disabled) protected)

// CONFIG2H // CONFIG6L
#pragma config WDT = OFF // Watchdog #pragma config WRT0 = OFF // Write
Timer Enable bit (WDT disabled (control is placed Protection bit (Block 0 (000800-001FFFh) is not
on the SWDTEN bit)) write-protected)
#pragma config WDTPS = 32768 // Watchdog #pragma config WRT1 = OFF // Write
Timer Postscale Select bits (1:32768) Protection bit (Block 1 (002000-003FFFh) is not
write-protected)
// CONFIG3H #pragma config WRT2 = OFF // Write
#pragma config CCP2MX = ON // CCP2 MUX Protection bit (Block 2 (004000-005FFFh) is not
bit (CCP2 input/output is multiplexed with RC1) write-protected)
#pragma config PBADEN = OFF // PORTB A/D #pragma config WRT3 = OFF // Write
Enable bit (PORTB<4:0> pins are configured as Protection bit (Block 3 (006000-007FFFh) is not
digital I/O on Reset) write-protected)
#pragma config LPT1OSC = OFF // Low-Power
Timer 1 Oscillator Enable bit (Timer1 configured // CONFIG6H
for higher power operation) #pragma config WRTC = OFF // Configuration
#pragma config MCLRE = ON // MCLR Pin Register Write Protection bit (Configuration
Enable bit (MCLR pin enabled; RE3 input pin registers (300000-3000FFh) are not write-
disabled) protected)
#pragma config WRTB = OFF // Boot Block
// CONFIG4L Write Protection bit (Boot block (000000-
#pragma config STVREN = ON // Stack 0007FFh) is not write-protected)
Full/Underflow Reset Enable bit (Stack
full/underflow will cause Reset)

6
SISTEMAS DIGITALES 3
UNIVERSIDAD ECCI
#pragma config WRTD = OFF // Data if(CONT>2){CONT=0;}
EEPROM Write Protection bit (Data EEPROM is
not write-protected) }
if(CONT==1)
// CONFIG7L {
#pragma config EBTR0 = OFF // Table Read LATDbits.LATD0 = 1;
Protection bit (Block 0 (000800-001FFFh) is not LATDbits.LATD1 = 0;
protected from table reads executed in other }
blocks) if(CONT==0)
#pragma config EBTR1 = OFF // Table Read {
Protection bit (Block 1 (002000-003FFFh) is not LATDbits.LATD0 = 0;
protected from table reads executed in other LATDbits.LATD1 = 1;
blocks)
#pragma config EBTR2 = OFF // Table Read }
Protection bit (Block 2 (004000-005FFFh) is not }
protected from table reads executed in other }
blocks)
#pragma config EBTR3 = OFF // Table Read void CONF_OSC(void)
Protection bit (Block 3 (006000-007FFFh) is not {
protected from table reads executed in other
blocks) OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
// CONFIG7H OSCCONbits.IRCF0 = 0;
#pragma config EBTRB = OFF // Boot Block }
Table Read Protection bit (Boot block (000000- //+++++++++++++++++++++++++++++++++++
0007FFh) is not protected from table reads ++++++++++++++++++++++++++++++
executed in other blocks) void CONF_PUERTOS(void)
{
TRISC = 0b00000000;
void CONF_OSC(void); TRISB = 0b11000000;
void CONF_PUERTOS(void); TRISD = 0b00000000;// todos de salida
void PORCIENTO20(void); }
void CONF_INICIAL (void);
unsigned char CICLO_UTIL = 0,CONT = 0; void PORCIENTO20(void)
///////////////////////////////////////////////////////////////////////// {
void main (void) OpenTimer2(TIMER_INT_OFF &
{ T2_PS_1_16); //prescaler 1:16
CONF_OSC(); OpenPWM1(0x30);
CONF_PUERTOS(); SetDCPWM1(CICLO_UTIL);
CONF_INICIAL(); SetOutputPWM1(SINGLE_OUT,
PORCIENTO20(); PWM_MODE_1);
while(1) }
{ //////////////////////////////////////////////////////////////////////////
SetDCPWM1(CICLO_UTIL); void CONF_INICIAL (void)
while(PORTBbits.RB7==1) {
{ INTCONbits.INT0IE =1;
__delay_ms(30); INTCONbits.RBIE =1;
CICLO_UTIL = CICLO_UTIL + 2; INTCONbits.RBIF =1;
if(CICLO_UTIL > 20 ) INTCONbits.TMR0IE =1;
{
CICLO_UTIL = 0; }
}
} 5. CONCLUSIONES
while(PORTBbits.RB6==1)
{ 1. Se hace ms fcil la manipulacin del
CONT = CONT + 1; perifrico PWM por medio de libreras.

7
SISTEMAS DIGITALES 3
UNIVERSIDAD ECCI
2. Vemos la interaccin del microntrolador
con actuador externo como lo es un motor.

6. REFERENCIAS

Datasheet PIC 18F4550

Вам также может понравиться