Sie sind auf Seite 1von 17

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * DEFINIO PIC * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include <p18F4550.h> // Register definitions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * INCLUDES DAS FUNES DE PERIFRICOS DO PIC * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include <pwm.

h> //PWM library functions #include <adc.h> //ADC library functions #include <timers.h> //Timer library functions #include <delays.h> //Delay library functions #include <i2c.h> //I2C library functions #include <stdlib.h> //Library functions #include <usart.h> //USART library functions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Configuraes para gravao * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #pragma config FOSC = HS #pragma config CPUDIV = OSC1_PLL2 #pragma config WDT = ON #pragma config WDTPS = 128 #pragma config LVP = OFF #pragma config PWRT = ON #pragma config BOR = OFF #pragma config BORV = 0 #pragma config PBADEN = OFF #pragma config DEBUG = OFF

#pragma config PLLDIV = 1 #pragma config USBDIV = 1 #pragma config FCMEN = OFF #pragma config IESO = OFF #pragma config VREGEN = OFF #pragma config MCLRE = ON #pragma config LPT1OSC = OFF #pragma config CCP2MX = ON #pragma config STVREN = OFF #pragma config ICPRT = OFF /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Definio e inicializao das variveis Globais * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //Neste bloco esto definidas as variveis globais do programa. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Constantes internas * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //A definio de constantes facilita a programao e a manuteno. #define t_filtro 240 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Definio e inicializao das variveis globais * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //Neste bloco esto definidas as variveis globais do programa. unsigned char display = 0; // atualiza os displays unsigned char filtro_d = t_filtro; // inicia filtro do cdigo unsigned char filtro_z = t_filtro; // inicia filtro do cdigo unsigned char filtro_dados = t_filtro; // inicia filtro do cdigo

unsigned char filtro_d_zero = t_filtro; // inicia filtro do cdigo unsigned char unidade = 10; // varivel unidade unsigned char dezena = 10; // varivel dezena unsigned char multiplicador = 0; // multiplicador do timer1 unsigned char DEBOUNCE = 0; // Sinalizao de debounce de codigo unsigned char delta_timer1 = 65536; // carga para timer1 unsigned char RISING = 0; //indicativo de variao em VT (DECOD) unsigned char L_D = 0; //indicativo de LIGADO/DESLIGADO // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // * TABELA DE CONVERSO BINRIO -> DISPLAY * // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ESTA ROTINA IR RETORNAR EM W, O SIMBOLO CORRETO QUE DEVE SER // MOSTRADO NO DISPLAY PARA CADA VALOR DE INTENSIDADE. O RETORNO J EST // FORMATADO PARA AS CONDIES DE LIGAO DO DISPLAY AO PORTD. // a

// ********** // * // f * * *b

// * g * // ********** // * // e * * *c

// * d * // ********** *. const rom unsigned char converte[] = {// .GFEDCBA POSIO CORRETA DOS SEGMENTOS 0b00111111, // 00 - RETORNA SMBOLO CORRETO 0

0b00000110, // 01 - RETORNA SMBOLO CORRETO 1 0b01011011, // 02 - RETORNA SMBOLO CORRETO 2 0b01001111, // 03 - RETORNA SMBOLO CORRETO 3 0b01100110, // 04 - RETORNA SMBOLO CORRETO 4 0b01101101, // 05 - RETORNA SMBOLO CORRETO 5 0b01111101, // 06 - RETORNA SMBOLO CORRETO 6 0b00000111, // 07 - RETORNA SMBOLO CORRETO 7 0b01111111, // 08 - RETORNA SMBOLO CORRETO 8 0b01101111, // 09 - RETORNA SMBOLO CORRETO 9 0b0000000}; // Blank /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Declarao dos flags de software *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //A definio de flags ajuda na programao e economiza memria RAM. struct { unsigned BIT0:1; unsigned BIT1:1; unsigned BIT2:1; unsigned BIT3:1; unsigned BIT4:1; unsigned BIT5:1; unsigned BIT6:1; unsigned BIT7:1; }FLAGSbits; //ARMAZENA OS FLAGS DE CONTROLE #define estado_BT_ON FLAGSbits.BIT0 //STATUS DO DEBOUNCE DO BOTAO LIGA #define estado_BT_OFF FLAGSbits.BIT1 //STATUS DO DEBOUNCE DO BOTAO DESLIGA

#define estado_BT_UP FLAGSbits.BIT2 //STATUS DO DEBOUNCE DO BOTAO AUMENTA #define estado_BT_DOWN FLAGSbits.BIT3 //STATUS DO DEBOUNCE DO BOTAO DIMINUI #define estado_DADO FLAGSbits.BIT4 //STATUS DE RECEBIMENTO DE COMANDO

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Definio e inicializao dos port's *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PROTOTIPAGEM DE FUNES *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void TRATA_HIGH_INT(void); void TRATA_INT_TIMER0(void); void TRATA_INT_TIMER1(void); void TRATA_INT_CROSS(void); void decrementa_timer(void); void incrementa_timer(void); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ENTRADAS *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // AS SADAS DEVEM SER ASSOCIADAS A NOMES PARA FACILITAR A PROGRAMAO E //FUTURAS ALTERAES DO HARDWARE. #define BT_ON PORTBbits.RA0 //BT_ON //0 -> PRESSIONADO //1 -> LIBERADO #define BT_OFF PORTBbits.RA1 //BT_OFF //0 -> PRESSIONADO //1 -> LIBERADO #define BT_UP PORTBbits.RA2 //BT_UP //0 -> PRESSIONADO //1 -> LIBERADO

#define BT_DOWN PORTBbits.RA3 //BT_DOWN //0 -> PRESSIONADO //1 -> LIBERADO #define Z_CROSS PORTBbits.RB0 //Z_CROSS //1 -> CRUZAMENTO //0 -> NAO CRUZAMENTO #define DECOD PORTBbits.RB1 //DECOD //1 -> CODIGO RECEBIDO //0 -> OCIOSO /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * SADAS *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // AS SADAS DEVEM SER ASSOCIADAS A NOMES PARA FACILITAR A PROGRAMAO E //FUTURAS ALTERAES DO HARDWARE. #define MOC LATBbits.LATB2 // CONTROLE DO TRIAC #define disp0 LATCbits.LATC0 // seleo do display unidade (0) #define disp1 LATCbits.LATC1 // seleo do display dezena (1)

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Funo de decremento do Timer *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void decrementa_timer(void) { unidade --; //DECREMENTA UNIDADE if (dezena == 0x01) //DEZENA = 1? { if (unidade == 0xFF) //UNIDADE = 255? { unidade = 9; //UNIDADE = 9 dezena --; //DECREMENTA DEZENA

} } else { if (unidade == 0xFF) //UNIDADE = 255? { unidade ++; } }

} /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Funo de incremento do Timer *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void incrementa_timer(void) { unidade ++; //INCREMENTA UNIDADE if (dezena == 0x00) //DEZENA = 0? { if (unidade == 10) { unidade = 0; dezena ++; } } else {

unidade = 0; }

} /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Funo de tratamento de interrupo de Timer1 *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void TRATA_INT_TIMER1(void) { PIR1bits.TMR1IF = 0; //LIMPA FLAG DE INTERRUPO MOC = 1; CloseTimer1(); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Rotina de Tratamento de interrupo de TMR0 *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // Esta interrupo ocorrer a cada 256us. void TRATA_INT_TIMER0(void) { INTCONbits.TMR0IF = 0; //LIMPA FLAG DE INTERRUPO switch(display) // incio da varredura dos displays { case 0: display++; // incrementa a varivel de varredura disp1 = 0; // desliga o display 1 PORTD = converte[unidade]; // atualiza o portd disp0 = 1; // liga o display 0

break; // sai case 1: display--; // inicia em 0 a varivel de varredura disp0 = 0; // desliga o display 0 PORTD = converte[dezena]; // atualiza o portd disp1 = 1; // liga o display 1 break; // sai } } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Funo de tratamento de interrupo de Cruzamento por Zero *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void TRATA_INT_CROSS(void) { INTCONbits.INT0IF = 0; //LIMPA FLAG DE INTERRUPO if(estado_DADO == 1) { if((estado_BT_ON == 1)&&(estado_BT_OFF == 1)&&(estado_BT_UP == 1)&&(estado_BT_DOWN == 1)) //LIGA { unidade = 0; dezena = 1; multiplicador = 10; MOC = 1; L_D = 1; } if((estado_BT_ON == 0)&&(estado_BT_OFF == 1)&&(estado_BT_UP == 1)&&(estado_BT_DOWN == 1)) //DESLIGA

{ unidade = 10; dezena = 10; multiplicador = 0; MOC = 0; L_D = 1; } if((estado_BT_ON == 1)&&(estado_BT_OFF == 0)&&(estado_BT_UP == 1)&&(estado_BT_DOWN == 1)) //INCREMENTA { multiplicador++; if((multiplicador == 10)||(multiplicador == 11)) { multiplicador = 10; MOC = 1; L_D = 1; } else { delta_timer1 = 65536 - ((10 multiplicador) * 104); MOC = 0; L_D = 0; WriteTimer1(delta_timer1); // carraga timer1 OpenTimer1(T1_OSC1EN_OFF & TIMER_INT_ON & T1_SOURCE_INT & T1_8BIT_RW & T1_PS_1_8);

incrementa_timer();

} if((estado_BT_ON == 1)&&(estado_BT_OFF == 1)&&(estado_BT_UP == 0)&&(estado_BT_DOWN == 1)) //DECREMENTA { multiplicador--; if((multiplicador == 0)||(multiplicador == 0xFF)) { multiplicador = 0; MOC = 0; L_D = 1; } else { multiplicador--; delta_timer1 = 65536 - ((10 multiplicador) * 104); MOC = 0; L_D = 0; WriteTimer1(delta_timer1); // carraga timer1 OpenTimer1(T1_OSC1EN_OFF & TIMER_INT_ON & T1_SOURCE_INT & T1_8BIT_RW & T1_PS_1_8); } decrementa_timer(); } estado_DADO = 0; }

if(L_D == 0) { MOC = 0; WriteTimer1(delta_timer1); // carraga timer1 OpenTimer1(T1_OSC1EN_OFF & TIMER_INT_ON & T1_SOURCE_INT & T1_8BIT_RW & T1_PS_1_8); } } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Funo Principal *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void main () { PORTA = 0x00; //Limpa PORTA PORTB = 0x00; //Limpa PORTB PORTC = 0x00; //Limpa PORTC PORTD = 0x00; //Limpa PORTD PORTE = 0x00; //Limpa PORTE LATA = 0x00; //Limpa PORTA LATB = 0x00; //Limpa PORTB LATC = 0x00; //Limpa PORTC LATD = 0x00; //Limpa PORTD LATE = 0x00; //Limpa PORTE TRISA = 0b11111111; //CONFIG DIREO DOS PINOS PORTA TRISB = 0b00000011; //CONFIG DIREO DOS PINOS PORTB TRISC = 0b11111100; //CONFIG DIREO DOS PINOS PORTC TRISD = 0b00000000; //CONFIG DIREO DOS PINOS PORTD TRISE = 0b11111111; //CONFIG DIREO DOS PINOS PORTE

ADCON1 = 0b00001111; //DESLIGA CONVERSORES A/D OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_2); //CONFIGURAO DO TIMER0 OpenTimer1(T1_OSC1EN_OFF & TIMER_INT_OFF & T1_SOURCE_INT & T1_8BIT_RW & T1_PS_1_8); //CONFIGURAO DO TIMER1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ while(RCONbits.NOT_TO); //AGUARDA ESTOURO DO WDT INTCONbits.PEIE = 1; //LIGA CHAVE DOS PERIFRICOS INTCONbits.GIE = 1; //LIGA A CHAVE GERAL CloseTimer1(); INTCONbits.INT0IE = 1; //HABILITA INT0 INTCON2bits.INTEDG0 = 1; //INT0 EM RISING EDGE Inicializao do Sistema

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Rotina Principal *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Loop principal *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ loop: while(1) { ClrWdt(); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Tratamento do Sinal DECOD

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ if(DECOD == 1) //Comando recebido? { filtro_d --; // decrementa o filtro if(filtro_d == 0) // fim do filtro do sinal? { if(RISING == 0) { if(DEBOUNCE == 0) {

estado_BT_ON = BT_ON;

estado_BT_OFF = BT_OFF;

estado_BT_UP = BT_UP;

estado_BT_DOWN = BT_DOWN;

DEBOUNCE = 1; } else {

if((estado_BT_ON == BT_ON)&&(estado_BT_OFF == BT_OFF)&&(estado_BT_UP == BT_UP)&&(estado_BT_DOWN == BT_DOWN))

filtro_dados --;

if(filtro_dados == 0)

estado_DADO = 1;

filtro_d = t_filtro;

filtro_dados = t_filtro;

DEBOUNCE = 0;

RISING = 1;

} } } } } if(DECOD == 0) { filtro_d_zero --; // decrementa o filtro if(filtro_d_zero == 0) // fim do filtro do sinal? { if(RISING == 1) { RISING = 0; }

filtro_d_zero = t_filtro; } } } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // * ROTINA DE TRATAMENTO DE INT DE ALTA PRIORIDADE *

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #pragma code VETOR_HIGH_PRIORITY = 0x0008 //VETOR DE ALTA PRIORIDADE void HIGH_int (void) { _asm goto TRATA_HIGH_INT _endasm } #pragma code

#pragma interrupt TRATA_HIGH_INT void TRATA_HIGH_INT(void) { if(INTCONbits.TMR0IF) TRATA_INT_TIMER0(); if(PIR1bits.TMR1IF) TRATA_INT_TIMER1(); if(INTCONbits.INT0IF) { filtro_z --; if(filtro_z == 0) { filtro_z = t_filtro;

TRATA_INT_CROSS(); } } }

Das könnte Ihnen auch gefallen