quarta-feira, 25 de janeiro de 2017

Usando o Timer0 do 18F4550 no XC8

/*
 *                                              Usando o timer0 do  18F4550
 *
 * Compilador : MPlabXC8.
 * Microcontrolador: 18F4550. 
 * Autor: aguivone.
 * Versão: 1.
 * Data :  25 de janeiro de 2017.
 */ 
#include <xc.h>
#define _XTAL_FREQ 20000000    //  
/* COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS*/
#include "Wiznet.h"
// CONFIG1L
#pragma config PLLDIV = 4      // depois testar se não vai dar problemas com o ADC e wiznet
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = HS //HSPLL_HS  //HS para cristal de 16 ou 20mhz(habilitei o pll interno mas para este exemplo nem precisava podia usar somete o HS)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = ON        // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOR = ON        // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 2         // Brown-out Reset Voltage bits (Setting 1 2.79V)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = OFF     // CCP2 MUX bit (CCP2 input/output is multiplexed with RB3)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other 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 Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

unsigned int guiPisca;
unsigned char gucAlternaPino;
//Defines
#define LED_STATUS                 LATDbits.LATD0

//***********************************************************************************************************
// FUNÇÕES
//***********************************************************************************************************

//===========================================================================================================
// Função:     _inicializa_timer0
// Parâmetros: nenhum
// Retorno:    nenhum
// Descrição:  configura o timer0.
//===========================================================================================================
void inicializa_timer0() //
{         
    TMR0IP  = 1;//configura timer0 como alta prioridade 
    //T0CON = 0X80;//habilita timer/modo 16bits/clock interno/prescaler dividido por 2
    T0CON = 0XC5;//habilita timer/modo 8bits/clock interno/prescaler dividido por 64
    TMR0IE  = 1; // habilita interrupção de estouro do timer.
    TMR0 = 250;//zera registrador de contagem
}


//===========================================================================================================
// Checagem de Interrupção 
//===========================================================================================================
void interrupt interrupcao(void)//vetor de interrupção
 {
    if(TMR0IF)//interrupçao de timer0
    {    //interrupção do timer0 - estoura quando atinge 255(0XFF) - 8bits
        //interrupção do timer0 - estoura quando atinge 65536(0XFF) - 16bits
        //updateWizTimer();
        TMR0 = 178;//reinicia timer com o valor calculado para 1ms 
        TMR0IF = 0;       
        if(guiPisca>0)
        {guiPisca--;}
    }
    
 }
void main(void) 
{
    TRISB = 0X00;   //Saida B1 e B0
    TRISA = 0XFF;   //
    TRISD = 0X00;   //saidas
    TRISE = 0X00;   //saidas
    TRISC = 0X10;   //todas são entradas//somente C4 é entrada (SDI)
    ADCON0 = 0x00;//configura os pinos para portas digitais(ADC OFF)
    ADCON1 = 0x0F;//configura os pinos para portas digitais(ADC OFF)    
    INTCON = 0XE0;//habilita interupção global , interrupção de periferico e
    inicializa_timer0();  
    
    for(;;)    
       {
        if(guiPisca == 0)
            {
                 if(gucAlternaPino == '0')
                {
                  LED_STATUS=0;
                  gucAlternaPino = '1';
                }
                else
                {
                  LED_STATUS=1;
                  gucAlternaPino = '0';
                }
                guiPisca = 250;//250ms               
            }
       }
}

2 comentários :

olá,digite aqui seu comentário!