El presente sitio presenta diseños y proyectos realizados con Solidworks y otros programas 3d , además de laminas y modelos 3d para practicar en casa, con el único objetivo de poder aprender y explorar la capacidad de Solidworks, no existiendo en ningún caso fines de lucro.
Showing posts with label CODIGO C18. Show all posts
Showing posts with label CODIGO C18. Show all posts

Friday, March 1, 2013

USO DEL I2C EN UNA MEMORIA EEPROM 24C01B C18

Posted by Juan Francisco | Friday, March 1, 2013 | Category: | 1 comentarios


//                USO DEL I2C EN UNA MEMORIA EEPROM 24C01B   
//Problemas:
//    Si se manda una cadena sin temporizacion osea sin tiempo
//    de transiscion puede fallar fisicamente.
//    POR EJEMPLOS
//        ENVIAR DATO                                            /
//        RETARDO DE 200mS                                    /
//        ENVIAR DATO                                            /
//        RETARDO DE 200mS                                    /
//        y asi                                                /
/////////////////////////////////////////////////////////////
#include
#include"eepromdr.h"
#include
#include
//#include    //uso de conversiones printf
#include    //libreria para retardos

//Fuses para trabajar con 8Mhz/////////

#pragma config FOSC   = INTOSCIO_EC        //usamos 8Mhz internos
#pragma config PLLDIV = 5                //PLL DIV 20Mhz/5=4Mhz
#pragma config CPUDIV = OSC1_PLL2         //CPUDIV1 96Mhz/2=48Mhz
#pragma config USBDIV = 1                //tRABAJAMOS CON USB CLOCK divido en 1
#pragma config VREGEN = OFF     //Trabajamos sin regulador interno 3.3v para usb
#pragma config FCMEN  = OFF, IESO    = OFF 
#pragma config PWRT   = ON, BOR     = OFF,BORV   = 0
#pragma config WDT    = OFF,WDTPS   = 32768 
#pragma config MCLRE  = ON, LPT1OSC = OFF,PBADEN = OFF,CCP2MX = ON 
#pragma config STVREN = OFF,LVP     = OFF,XINST  = OFF,DEBUG  = OFF 
#pragma config CP0    = OFF,CP1     = OFF,CP2    = OFF
#pragma config CPB    = ON, CPD     = ON 
#pragma config WRT0   = OFF,WRT1    = OFF,WRT2   = OFF 
#pragma config WRTB   = OFF,WRTC    = OFF,WRTD   = OFF
#pragma config EBTR0  = OFF,EBTR1   = OFF,EBTR2  = OFF 
#pragma config EBTRB  = OFF


//Variables a usar//////
unsigned char msje;

////////////////////////////////////////////
//Funcion de configuracion
//USART    I2C
////////////////////////////////////////////
void config(void){
    TRISC=0X80;
    //Configuramos UART
    OpenUSART(    USART_TX_INT_OFF    &    //Disable Interrupts
                USART_RX_INT_OFF    &    //----------------
                USART_ASYNCH_MODE    &    //Modo asincronico
                USART_EIGHT_BIT        &    //8 bit
                USART_CONT_RX        &    //resepcion continua
                USART_BRGH_HIGH        ,
                51);                //FOSC / (16 * (spbrg + 1))
                                    //spbrg=((FOSC/BAUD)/16)-1
                                    //51=((8Mhz/9600)/16)-1
    //cONFIGURACION DEL I2C
    OpenI2C(MASTER    ,    //I2C como master
            SLEW_ON        //a 400khz
           );
    SSPADD=4;    //CLOCK=FOSC/(4*(SSPADD+1)
                //SSPADD=[(FOSC/CLOCK)/4]-1
                //Velocidad de 400khz de 8Mhz
    INTCONbits.GIE=0;    //Desactivamos interrupciones globales
}


////////////////////////////////////////////
//FUNCION PRINCIPAL
///////////////////////////////////////////
void main(void){
    OSCCON=0x70;        //EMPIEZA A CORRER A FOSC=8Mhz

    config();
    
    putrsUSART("Programa para leer y escribir en una memoria eeprom \r\n");
    putrsUSART("Escribiendo....\r\n");
    Delay1KTCYx(100);
    //--------Guarda datos en la eeprom--------------///
    WriteEE(0X02,'O');
    putrsUSART("LEIENDO.....\r\n");
    //--------LEE MEMORIA EEPROM---------------------///
    msje=ReadEE(0x02);
    putcUSART(msje);
    while(1);
}

uso del pwm usart y adc C18

Posted by Juan Francisco | | Category: | 0 comentarios


//                USO DEL PWM CON ADC Y USART         
#include
#include    //tiempos 
#include    //USAREMOS INTERRUPCION POR TIMERS
#include        //PWM LIBRARY
#include        //Trabajamos con ADC
#include    //Incluimos usart 
#include    //uso de conversiones printf

//Fuses para trabajar con 8Mhz/////////
#pragma config FOSC   = INTOSCIO_EC        //usamos 8Mhz internos
#pragma config PLLDIV = 5                //PLL DIV 20Mhz/5=4Mhz
#pragma config CPUDIV = OSC1_PLL2         //CPUDIV1 96Mhz/2=48Mhz
#pragma config USBDIV = 1                //tRABAJAMOS CON USB CLOCK divido en 1
#pragma config VREGEN = OFF  //Trabajamos sin regulador interno 3.3v para usb
#pragma config FCMEN  = OFF, IESO    = OFF 
#pragma config PWRT   = ON, BOR     = OFF,BORV   = 0
#pragma config WDT    = OFF,WDTPS   = 32768 
#pragma config MCLRE  = ON, LPT1OSC = OFF,PBADEN = OFF,CCP2MX = ON 
#pragma config STVREN = OFF,LVP     = OFF,XINST  = OFF,DEBUG  = OFF 
#pragma config CP0    = OFF,CP1     = OFF,CP2    = OFF
#pragma config CPB    = ON, CPD     = ON 
#pragma config WRT0   = OFF,WRT1    = OFF,WRT2   = OFF 
#pragma config WRTB   = OFF,WRTC    = OFF,WRTD   = OFF
#pragma config EBTR0  = OFF,EBTR1   = OFF,EBTR2  = OFF 
#pragma config EBTRB  = OFF


/////Declaracion de variables    ///////
/*------------------------------------------*/
unsigned int adc;
unsigned char cont;
unsigned int percent;
unsigned char flag=0;

/*------------------------------------------*/

//Prototipos de funciones        ///////
/*------------------------------------------*/
void ISRTimer0(void);            //Funcion de Interrupcion
void config(void);                //Funcion de Configuraciones de puertos, Timer,pwm,usart,adc

/*------------------------------------------*/
//Inicio de la interrupcion por timer 0//////
//                                       //////
//0x08 es la posicion de alta prioridad//////
//0x18 es posicion de baja prioridad   //////
#pragma code Interrupcion = 0X0008
void VectorInterrupcion(void){
    _asm goto ISRTimer0 _endasm
}
#pragma code
//Funcion de Interrupcion/////////////
#pragma interrupt ISRTimer0
void ISRTimer0(void){
    cont++;
    if(INTCONbits.TMR0IF==1 && cont==2){
        flag=1;
        INTCONbits.TMR0IF=0;
        cont=0;
    }
    WriteTimer0(61629);
}
//Fin de la interrupcion            //////////
/*------------------------------------------*/
//Incio de la funcion de configuracion///////
void config(void){
    TRISA=0x01;
    TRISC=0X80;
    //Configuramos UART
    OpenUSART(    USART_TX_INT_OFF    &    //Disable Interrupts
                USART_RX_INT_OFF    &    //----------------
                USART_ASYNCH_MODE    &    //Modo asincronico
                USART_EIGHT_BIT        &    //8 bit
                USART_CONT_RX        &    //resepcion continua
                USART_BRGH_HIGH        ,
                25);                //FOSC / (16 * (spbrg + 1))
                                    //spbrg=((FOSC/BAUD)/16)-1
                                    //25=((8Mhz/19200)/16)-1

    //Configuramos TIMER0
    OpenTimer0(TIMER_INT_ON &     //Interrupciones por timer0 activado
               T0_16BIT     &    //16-bit
               T0_SOURCE_INT&     //Contar los ciclos internos 48Mhz/4
               T0_EDGE_FALL &     //~~_
               T0_PS_1_256);    //Preescalar =256
                //Interrupcion timpo=(1/(FOSC/4))*preescalar*(65536-timer0)
                //               .500s=tiempo maximo
                //               timer0=+65536-{tiempo/[(1/(FOSC/4))*preescalar]}
    WriteTimer0(61629);                //Cargamos el timer0=0
    //Configuramos ADC
    OpenADC(ADC_FOSC_RC     &    //Clock Interno
            ADC_RIGHT_JUST    &    //10bit
            ADC_20_TAD        ,    //20TAD
            ADC_CH0            &    //CANAL0
            ADC_INT_OFF        &    //INTERRUPCIONES OFF
            ADC_REF_VDD_VSS ,    //+5,GND
            ADC_1ANA);            //canal 0 analogo, resto digital
    //Configuramos PWM        
    OpenTimer2(TIMER_INT_OFF     &//Interrupcion timer2 OFF
               T2_PS_1_16        &//preescalar = 16
               T2_POST_1_1);     //POst escalar=1
                                //
    OpenPWM1(124);
                    //PWM period =   [(period  ) + 1] x 4 x Tosc x TMR2 prescaler
                    //PWM period =   [(255)+1]x(4/8Mhz)x16
                    // [.001s/((4/8Mhz)*16)]-1=period 
                    // [1/(f*(4/Tosc)*preescalar)]-1=period
    OpenPWM2(124);
    SetDCPWM1(127);
    SetDCPWM2(127);
    //Habilitamos interrupciones
    RCONbits.IPEN=0;            //Deshabilitamos prioridades
    INTCONbits.PEIE=1;            //Activamos Interrupciones por Timer0
    INTCONbits.GIE=1;            //Interrupciones global Activadas
    
}

void main(void){
    OSCCON=0x70;                //Oscilador a 8Mhz
                                //Se tiene que poner antes de todas las funciones
    config();                    //Funcion de configuracion

    while(1){//bucle infinito
        if(flag==1){            //Ha desbordado?        
            INTCONbits.PEIE=0;    //Desactivamos TIMER0
            flag=0;                //TOGGLE timer0
            Delay1KTCYx(1);        //500uS
            SetChanADC(0);        //canal 0 empieza la conversion
            ConvertADC();        //start convert
            while(BusyADC());    //Ha terminado?
            adc=ReadADC();        //lee dato
            printf("ADC = %i\r\n",adc);    //IMPRIME ADC
            SetDCPWM1(adc);        //CAMBIA VALORES DEL duty
            SetDCPWM2(adc);        //------------------------
            INTCONbits.PEIE=1;    //Activamos adc
        }
    }
}

uso del usart y pwm C18

Posted by Juan Francisco | | Category: | 0 comentarios


#include
#include
#include
#include
#include

#pragma config FOSC = XT_XT,FCMEN = OFF,IESO = OFF, CPUDIV = OSC1_PLL2 
#pragma config PWRT = ON,BOR = OFF,BORV = 0 
#pragma config WDT = OFF,WDTPS = 32768 
#pragma config MCLRE = ON,LPT1OSC = OFF,PBADEN = OFF,CCP2MX = OFF 
#pragma config STVREN = OFF,LVP = OFF,XINST = OFF,DEBUG = OFF 
#pragma config CP0 = ON,CP1 = ON,CP2 = ON 
#pragma config CPB = ON,CPD = ON 
#pragma config WRT0 = ON,WRT1 = ON,WRT2 = ON 
#pragma config WRTB = ON,WRTC = ON,WRTD = ON 
#pragma config EBTR0 = ON,EBTR1 = ON,EBTR2 = ON 
#pragma config EBTRB = ON



void main(void){

    char string[6];
    unsigned int value;
    TRISC=0x80;
    OpenUSART(    USART_TX_INT_OFF    &    //Disable Interrupts
                USART_RX_INT_OFF    &    //----------------
                USART_ASYNCH_MODE    &    //Modo asincronico
                USART_EIGHT_BIT        &    //8 bit
                USART_CONT_RX        &    //resepcion continua
                USART_BRGH_HIGH,
                25);                //FOSC / (16 * (spbrg + 1))
                                    //
    OpenTimer2(    TIMER_INT_OFF         &
                T2_PS_1_16            &
                T2_POST_1_16);
    OpenPWM1(255);
    SetDCPWM1(100);
    printf("HOLA MUNDO!!!\r\n");
    getsUSART(string,5);
    if(string[0]=='A') PORTC=0x01 & 0x0F;
    printf("\r\n%s",string);
    
    while(1);
}

uso del usart con oscilador interno C18

Posted by Juan Francisco | | Category: | 0 comentarios


#include
#include
#include
#include

//Fuses para trabajar con 8Mhz/////////
#pragma config FOSC   = INTOSCIO_EC        //Usamos UN CRISTAL DE 20MHZ JUNTO CON UN PLL
#pragma config PLLDIV = 5                //PLL DIV 20Mhz/5=4Mhz
#pragma config CPUDIV = OSC1_PLL2         //CPUDIV1 96Mhz/2=48Mhz
#pragma config USBDIV = 2                //tRABAJAMOS CON USB CLOCK divido en dos
#pragma config VREGEN = OFF     //Trabajamos sin regulador interno 3.3v para usb
#pragma config FCMEN = ON,IESO = ON 
#pragma config PWRT = ON, BOR   = OFF,BORV = 0
#pragma config WDT  = OFF,WDTPS = 32768 
#pragma config MCLRE = ON,LPT1OSC = OFF,PBADEN = OFF,CCP2MX = OFF 
#pragma config STVREN = OFF,LVP = OFF,XINST = OFF,DEBUG = OFF 
#pragma config CP0 = OFF,CP1 = OFF,CP2 = OFF
#pragma config CPB = ON,CPD = ON 
#pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF 
#pragma config WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF 
#pragma config EBTRB = OFF



void config(void){
    TRISC&=0x80;    
    OpenUSART(    USART_TX_INT_OFF    &    //Disable Interrupts
                USART_RX_INT_OFF    &    //----------------
                USART_ASYNCH_MODE    &    //Modo asincronico
                USART_EIGHT_BIT        &    //8 bit
                USART_CONT_RX        &    //resepcion continua
                USART_BRGH_HIGH,
                25);                //FOSC / (16 * (spbrg + 1))
                                    //spbrg=((FOSC/BAUD)/16)-1
                                    //25=((8Mhz/19200)/16)-1
}

void main(void){
    OSCCON=0x70;
    config();

    printf("HOLA PIC18\r\nCorriendo a 8Mhz\r\n");

    while(1){
        printf("Velocidad a 8Mhz/4\r\n");
        Delay10KTCYx(255);
    }
}

Ejemplo del uso del lcd 2x16 con ADC C18

Posted by Juan Francisco | | Category: | 0 comentarios


//                        USO DEL LCD                     

#include
#include    //tiempos 
#include    //uso de conversiones printf
#include
#include
#define LCD_4X20    //usamos un LCD4x20
                    //Si usamos un LCD 16x2 quitamos el #define

//Fuses para trabajar con 8Mhz/////////
#pragma config FOSC   = INTOSCIO_EC        //usamos 8Mhz internos
#pragma config PLLDIV = 5                //PLL DIV 20Mhz/5=4Mhz
#pragma config CPUDIV = OSC1_PLL2         //CPUDIV1 96Mhz/2=48Mhz
#pragma config USBDIV = 1                //tRABAJAMOS CON USB CLOCK divido en 1
#pragma config VREGEN = OFF      //Trabajamos sin regulador interno 3.3v para usb
#pragma config FCMEN  = OFF, IESO    = OFF 
#pragma config PWRT   = ON, BOR     = OFF,BORV   = 0
#pragma config WDT    = OFF,WDTPS   = 32768 
#pragma config MCLRE  = ON, LPT1OSC = OFF,PBADEN = OFF,CCP2MX = ON 
#pragma config STVREN = OFF,LVP     = OFF,XINST  = OFF,DEBUG  = OFF 
#pragma config CP0    = OFF,CP1     = OFF,CP2    = OFF
#pragma config CPB    = ON, CPD     = ON 
#pragma config WRT0   = OFF,WRT1    = OFF,WRT2   = OFF 
#pragma config WRTB   = OFF,WRTC    = OFF,WRTD   = OFF
#pragma config EBTR0  = OFF,EBTR1   = OFF,EBTR2  = OFF 
#pragma config EBTRB  = OFF



//Funciones prototipos                    ////////
void cmdXLCD(unsigned char cd);
void gotoXYLCD(unsigned char x,unsigned char y);
////////////////////////////////////////////////
///Variables globales                    ////////
////////////////////////////////////////////////
char buf[15];
char buf2[15];
const char adc[15]={"valor de ADC:"};
const char yo[20]={"Hecho en Mexico"};
char email[20]={"george.manson.69"};
unsigned int value1,value2,ch;
/////////////////////////////////////////////////
////Funciones necesarias para uso del LCD ///////
/////////////////////////////////////////////////
void DelayFor18TCY(void){
    Delay10TCYx(4);
}

void DelayPORXLCD (void){
  Delay1KTCYx(30); // Delay of 15ms
  return;          // Cycles = (TimeDelay * Fosc) / 4
                   // Cycles = (15ms * 8MHz) / 4
                   // Cycles = 30,000 
}
void DelayXLCD (void){
  Delay1KTCYx(10); // Delay of 5ms
  return;          // Cycles = (TimeDelay * Fosc) / 4
                   // Cycles = (5ms * 8MHz) / 4
                   // Cycles = 10,000
}
///////////////////////////////////
////Funcion de configuracion /////
///////////////////////////////////
void config(void){
    TRISA=0x03;//RA0=e,RA1=e
    OpenXLCD(FOUR_BIT &     //4-bit
             LINES_5X7);    //
    //Configuramos ADC
    OpenADC(ADC_FOSC_RC     &    //Clock Interno
            ADC_RIGHT_JUST    &    //10bit
            ADC_20_TAD        ,    //20TAD
            ADC_CH0            &    //CANAL0
            ADC_CH1            &    //CANAL1
            ADC_INT_OFF        &    //INTERRUPCIONES OFF
            ADC_REF_VDD_VSS ,    //+5,GND
            ADC_2ANA);            //canal 0,1 analogo, resto digital
}

////Funcion de comandos para lcd/////
///    cd=0x01 clear screen        /////
/// cd=0x0c ON creen            /////
void cmdXLCD(unsigned char cd){
    while(BusyXLCD());
    WriteCmdXLCD(cd);
}

///Funcion de posicionamiento    /////
/// x=renglon                    /////
///    y=columna                    /////
///Si queremos usar un lcd4x20  /////
///definimos despues del        /////
///#include                /////
///#define LCD_4X20                /////
#ifndef    LCD_4X20
void gotoXYLCD(unsigned char x,unsigned char y){
    unsigned char adr;

    if(y!=1) adr=0x40;
    else     adr=0;

    adr+=x-1;
    cmdXLCD(0x80|adr);
}
#else
void gotoXYLCD(unsigned char x,unsigned char y){
    unsigned char adr;
   switch(y) {
     case 1 : adr=0x80;break;
     case 2 : adr=0xc0;break;
     case 3 : adr=0x94;break;
     case 4 : adr=0xd4;break;
   }
   adr+=x-1;
   cmdXLCD(adr);
}
#endif

void main(void){
    /*Calibramos el oscilador Interno del PIC*/
    OSCCON=0x70;
    /*Llamamos la funcion de configuracion*/
    config();

    cmdXLCD(0x0c);    //
    cmdXLCD(0x01);    //

    gotoXYLCD(1,1);
    putsXLCD(adc);
    gotoXYLCD(1,3);
    putsXLCD(yo);
    gotoXYLCD(1,4);
    putsXLCD(email);

    while(1){
        Delay100TCYx(0);
        SetChanADC(ADC_CH0);//canal ch empieza la conversion
        ConvertADC();        //start convert
        while(BusyADC());    //Ha terminado?
        value1=ReadADC();
        Delay100TCYx(0);
        SetChanADC(ADC_CH1);//canal ch empieza la conversion
        ConvertADC();        //start convert
        while(BusyADC());    //Ha terminado?
        value2=ReadADC();

        sprintf(buf,"VAL=%i  ",value1);    //string"buf"="VALUE=value"
        sprintf(buf2,"VAL2=%i ",value2);    //string"buf2"="VALUE=value2"
        gotoXYLCD(1,2);        //segunda linea
        putsXLCD(buf);        //imprime
        gotoXYLCD(10,2);    //segunda linea
        putsXLCD(buf2);        //imprime
    }
        
}

uso simple del lcd 2x16 C18

Posted by Juan Francisco | | Category: | 0 comentarios


//                        USO DEL LCD 

#include
#include    //tiempos 
#include
#include
#define LCD_4X20    //usamos un LCD4x20
                    //Si usamos un LCD 16x2 quitamos el #define

//Fuses para trabajar con 8Mhz/////////
#pragma config FOSC   = INTOSCIO_EC        //usamos 8Mhz internos
#pragma config PLLDIV = 5                //PLL DIV 20Mhz/5=4Mhz
#pragma config CPUDIV = OSC1_PLL2         //CPUDIV1 96Mhz/2=48Mhz
#pragma config USBDIV = 1                //tRABAJAMOS CON USB CLOCK divido en 1
#pragma config VREGEN = OFF                //Trabajamos sin regulador interno 3.3v para usb
//-----------------------------------------------------------------
#pragma config FCMEN  = OFF, IESO    = OFF 
#pragma config PWRT   = ON, BOR     = OFF,BORV   = 0
#pragma config WDT    = OFF,WDTPS   = 32768 
#pragma config MCLRE  = ON, LPT1OSC = OFF,PBADEN = OFF,CCP2MX = ON 
#pragma config STVREN = OFF,LVP     = OFF,XINST  = OFF,DEBUG  = OFF 
#pragma config CP0    = OFF,CP1     = OFF,CP2    = OFF
#pragma config CPB    = ON, CPD     = ON 
#pragma config WRT0   = OFF,WRT1    = OFF,WRT2   = OFF 
#pragma config WRTB   = OFF,WRTC    = OFF,WRTD   = OFF
#pragma config EBTR0  = OFF,EBTR1   = OFF,EBTR2  = OFF 
#pragma config EBTRB  = OFF



char buf[10];
int z,a=1,b=66;

////Funciones necesarias para uso del LCD /////
void DelayFor18TCY(void){
    Delay10TCYx(4);
}

void DelayPORXLCD (void){
  Delay1KTCYx(30); // Delay of 15ms
  return;          // Cycles = (TimeDelay * Fosc) / 4
                   // Cycles = (15ms * 8MHz) / 4
                   // Cycles = 30,000 
}
void DelayXLCD (void){
  Delay1KTCYx(10); // Delay of 5ms
  return;          // Cycles = (TimeDelay * Fosc) / 4
                   // Cycles = (5ms * 8MHz) / 4
                   // Cycles = 10,000
}
////Funcion de configuracion /////
void config(void){
    OpenXLCD(FOUR_BIT &     //4-bit
             LINES_5X7);    //
}
////Funcion de comandos para lcd/////
///    cd=0x01 clear screen        /////
/// cd=0x0c ON creen            /////
void cmdXLCD(unsigned char cd){
    while(BusyXLCD());
    WriteCmdXLCD(cd);
}
///Funcion de posicionamiento    /////
/// x=renglon                    /////
///    y=columna                    /////
///Si queremos usar un lcd4x20  /////
///definimos despues del        /////
///#include                /////
///#define LCD_4X20                /////
#ifndef    LCD_4X20
void gotoXYLCD(unsigned char x,unsigned char y){
    unsigned char adr;

    if(y!=1) adr=0x40;
    else     adr=0;

    adr+=x-1;
    cmdXLCD(0x80|adr);
}
#else
void gotoXYLCD(unsigned char x,unsigned char y){
    unsigned char adr;
   switch(y) {
     case 1 : adr=0x80;break;
     case 2 : adr=0xc0;break;
     case 3 : adr=0x94;break;
     case 4 : adr=0xd4;break;
   }
   adr+=x-1;
   cmdXLCD(adr);
}
#endif


void main(void){
    OSCCON=0x70;
    config();
    cmdXLCD(0x01);
    
    putrsXLCD("A=1\n");
    putrsXLCD("B=66");
    gotoXYLCD(1,3);
    putrsXLCD("z=A+B");
    gotoXYLCD(1,4);
    putrsXLCD("z=");
    z=a+b;
    itoa(z,buf);
    gotoXYLCD(3,4);
    putsXLCD(buf);
    while(1);
}

Configuracion del oscilador INterno y uso de la libreria USART C18

Posted by Juan Francisco | | Category: | 0 comentarios


#include
#include

//Fuses para trabajar con 48Mhz/////////

#pragma config FOSC   = INTOSCIO_EC    //usamos 8Mhz internos
#pragma config PLLDIV = 5              //PLL DIV 20Mhz/5=4Mhz
#pragma config CPUDIV = OSC1_PLL2      //CPUDIV1 96Mhz/2=48Mhz
#pragma config USBDIV = 1              //tRABAJAMOS CON USB CLOCK divido en 1
#pragma config VREGEN = OFF      //Trabajamos sin regulador interno 3.3v para usb
#pragma config FCMEN  = OFF, IESO    = OFF 
#pragma config PWRT   = ON, BOR     = OFF,BORV   = 0
#pragma config WDT    = OFF,WDTPS   = 32768 
#pragma config MCLRE  = ON, LPT1OSC = OFF,PBADEN = OFF,CCP2MX = OFF 
#pragma config STVREN = OFF,LVP     = OFF,XINST  = OFF,DEBUG  = OFF 
#pragma config CP0    = OFF,CP1     = OFF,CP2    = OFF
#pragma config CPB    = ON, CPD     = ON 
#pragma config WRT0   = OFF,WRT1    = OFF,WRT2   = OFF 
#pragma config WRTB   = OFF,WRTC    = OFF,WRTD   = OFF
#pragma config EBTR0  = OFF,EBTR1   = OFF,EBTR2  = OFF 
#pragma config EBTRB  = OFF



//Prototipos de funciones        ///////
/*------------------------------------------*/
void ISRTimer0(void);            //Funcion de Interrupcion
void config(void);                //Funcion de Configuraciones de puertos, Timer
/*------------------------------------------*/
/*------------------------------------------*/
//Inicio de la interrupcion por timer 0//////
//                                       //////
//0x08 es la posicion de alta prioridad//////
//0x18 es posicion de baja prioridad   //////
#pragma code Interrupcion = 0X0008
void VectorInterrupcion(void){
    _asm goto ISRTimer0 _endasm
}
#pragma code
//Funcion de Interrupcion/////////////
#pragma interrupt ISRTimer0
void ISRTimer0(void){
    if(INTCONbits.TMR0IF==1){
        PORTBbits.RB0=~PORTBbits.RB0;
        INTCONbits.TMR0IF=0;
    }
    WriteTimer0(61629);    //Cargamos el timer0
}

//Fin de Interrupcion por timer0
//Funcion de Configuracion
void config(void){
    TRISB=0x00;    //Puerto b como salida
    CMCON&=0x07;//Apagamos comparadores
    ADRES=0x00; //Salida/entradas digitales
    //Configuramos TIMER0
    OpenTimer0(TIMER_INT_ON &     //Interrupciones por timer0 activado
               T0_16BIT     &    //16-bit
               T0_SOURCE_INT&     //Contar los ciclos internos 48Mhz/4
               T0_EDGE_FALL &     //~~_
               T0_PS_1_256);    //Preescalar =256
                //Interrupcion timpo=(1/(FOSC/4))*preescalar*(65536-timer0)
                //               .500s=tiempo maximo
                //               timer0=-{tiempo/[(1/(FOSC/4))*preescalar]}+65536
    WriteTimer0(61629);                //Cargamos el timer0=0
    RCONbits.IPEN=0;            //Deshabilitamos prioridades
    INTCONbits.PEIE=1;            //Activamos Interrupciones por Timer0
    INTCONbits.GIE=1;            //Interrupciones global Activadas
}

void main(void){
    OSCCON=0b01110000;    //Corriendo a 8Mhz
    config();

    while(1);
}