Autor Tema: Mini curso "Programación en XC8"  (Leído 588339 veces)

0 Usuarios y 2 Visitantes están viendo este tema.

Desconectado Picuino

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 5892
    • Picuino
Re: Mini curso "Programación en XC8"
« Respuesta #75 en: 18 de Junio de 2013, 14:57:58 »
El buffer que da error al copiar es una estructura de 6 bytes de tamaño:

Código: [Seleccionar]
typedef union {
   unsigned char  byte[6];
   unsigned int   word[3];
   struct {
      unsigned int __word;
      unsigned long dword;
   };
   struct {
      unsigned b0:1;      // LSB
      unsigned b1:1;
      unsigned b2:1;
      unsigned b3:1;
      unsigned b4:1;
      unsigned b5:1;
      unsigned b6:1;
      unsigned b7:1;
      unsigned b8:1;
      unsigned b9:1;
      unsigned b10:1;
      unsigned b11:1;
      unsigned b12:1;
      unsigned b13:1;
      unsigned b14:1;
      unsigned b15:1;
      unsigned b16:1;
      unsigned b17:1;
      unsigned b18:1;
      unsigned b19:1;
      unsigned b20:1;
      unsigned b21:1;
      unsigned b22:1;
      unsigned b23:1;
      unsigned b24:1;
      unsigned b25:1;
      unsigned b26:1;
      unsigned b27:1;
      unsigned b28:1;
      unsigned b29:1;
      unsigned b30:1;
      unsigned b31:1;
      unsigned b32:1;
      unsigned b33:1;
      unsigned b34:1;
      unsigned b35:1;
      unsigned b36:1;
      unsigned b37:1;
      unsigned b38:1;   // MSB
      unsigned b39:1;   // MANTISSA SIGN
      unsigned b40:1;
      unsigned b41:1;
      unsigned b42:1;
      unsigned b43:1;
      unsigned b44:1;
      unsigned b45:1;
      unsigned b46:1;
      unsigned b47:1;
      unsigned b48:1;
   };
} uint48;

uint48 time_buf[BUF_LEN];

Saludos.

Desconectado Rseliman

  • PIC16
  • ***
  • Mensajes: 239
Re: Mini curso "Programación en XC8"
« Respuesta #76 en: 18 de Junio de 2013, 15:33:46 »
Hola a todos ...por favor alguien sabe de que se trata este error en xc8 ..

:: warning: Omniscient Code Generation not available in Free mode
C:\Users\Ramiro\AppData\Local\Temp\s4e8.:712: error: undefined symbol "_STATUS"
(908) exit status = 1
make[2]: *** [dist/default/production/Rs232Prueba.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `C:/Users/Ramiro/pic/pic16f819/Rs232Prueba.X'
make[1]: Leaving directory `C:/Users/Ramiro/pic/pic16f819/Rs232Prueba.X'

BUILD FAILED (exit value 2, total time: 2s)


Muchas gracias Saludos
Las Grandes Obras las sueñan los grandes locos , mientras los inutiles las critican !!

Desconectado AngelGris

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 2480
Re: Mini curso "Programación en XC8"
« Respuesta #77 en: 18 de Junio de 2013, 22:15:11 »
El programa es bastante grande, pero he conseguido acotar el problema a unas pocas instrucciones.

Si ejecuto lo siguiente dentro de la rutina de interrupción no me da error:

Código: [Seleccionar]
void interrupt low_priority low_isr(void) {

   // CCP1 interrupt
   if (PIR1bits.CCP1IF == 1) {
      PIR1bits.CCP1IF = 0;

      for(char i=0; i<BUF_LEN; ) {
         time_buf[i++] = time_buf[i];
      }
   }
}


Por el contrario, si lo ejecuto en una función separada, me da error:

Código: [Seleccionar]

void interrupt low_priority low_isr(void) {

   // CCP1 interrupt
   if (PIR1bits.CCP1IF == 1) {
      PIR1bits.CCP1IF = 0;

      fill_buffer();
   }
}


void fill_buffer(void) {
   for(char i=0; i<BUF_LEN; ) {
      time_buf[i++] = time_buf[i];
   }
}

El error es:


Microchip MPLAB XC8 C Compiler V1.12
Copyright (C) 2012 Microchip Technology Inc.
License type: Node Configuration

Advisory[1233] Employing 18F2550 errata work-arounds:
Advisory[1234]  * Corrupted fast interrupt shadow registers
Warning [1273] ; . Omniscient Code Generation not available in Free mode
Error   [800] C:\DOCUME~1\Usuario\CONFIG~1\Temp\s368.; 11618. undefined symbol "??_fill_buffer"
Error   [800] C:\DOCUME~1\Usuario\CONFIG~1\Temp\s368.; 11620. undefined symbol "fill_buffer@i"
(908) exit status = 1

********** Build failed! **********


Saludos.


  ¿Tienes la definición de la función fill_bufer() antes que la interrupción? ¿La variable I, es global? Porque no la veo definida dentro de la función fill_buffer.

  A su vez, los bit de tu buffer deberían ir de 0 a 47 para que sean justo 6 bytes y no de 0 a 48 ya que eso serían 49 bits.
De vez en cuando la vida
nos besa en la boca
y a colores se despliega
como un atlas

Desconectado AngelGris

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 2480
Re: Mini curso "Programación en XC8"
« Respuesta #78 en: 18 de Junio de 2013, 22:22:28 »
Hola a todos ...por favor alguien sabe de que se trata este error en xc8 ..

:: warning: Omniscient Code Generation not available in Free mode
C:\Users\Ramiro\AppData\Local\Temp\s4e8.:712: error: undefined symbol "_STATUS"
(908) exit status = 1
make[2]: *** [dist/default/production/Rs232Prueba.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `C:/Users/Ramiro/pic/pic16f819/Rs232Prueba.X'
make[1]: Leaving directory `C:/Users/Ramiro/pic/pic16f819/Rs232Prueba.X'

BUILD FAILED (exit value 2, total time: 2s)


Muchas gracias Saludos

  Pon tu programa Rseliman.
De vez en cuando la vida
nos besa en la boca
y a colores se despliega
como un atlas

Desconectado Picuino

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 5892
    • Picuino
Re: Mini curso "Programación en XC8"
« Respuesta #79 en: 19 de Junio de 2013, 04:50:14 »
  ¿Tienes la definición de la función fill_bufer() antes que la interrupción? ¿La variable I, es global? Porque no la veo definida dentro de la función fill_buffer.

  A su vez, los bit de tu buffer deberían ir de 0 a 47 para que sean justo 6 bytes y no de 0 a 48 ya que eso serían 49 bits.

Declaro fill_buffer() antes de la interrupción (con un fichero *.h) y la defino después.
La variable i es local y la defino como char dentro del bucle for.

Voy a mirar lo de los bits.

Saludos.

Desconectado Picuino

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 5892
    • Picuino
Re: Mini curso "Programación en XC8"
« Respuesta #80 en: 19 de Junio de 2013, 04:51:46 »
Tienes razón, me he pasado 1 bit en la definición del tipo uint48  :shock:

Lo cambio ahora mismo.

Saludos.

Desconectado Rseliman

  • PIC16
  • ***
  • Mensajes: 239
Re: Mini curso "Programación en XC8"
« Respuesta #81 en: 19 de Junio de 2013, 14:32:31 »
Amigos ..les paso una subrutina que Hizo AngelGris para HiTech ...es una Uart por soft ...estoy intentando hacerla andar en xc8 y la verdad me esta volviendo loco ...el error que tengo aparentemente es un error del xc8 en putch ...por lo que alcanzo a entender ...el error me lo da con el main  en estas lineas

       putst_s ("Hola");   // envia el string "Hola" al puerto serie
       putst_s ("\n");     // envia el salto de linea al puerto serie
       putst_s (pantalla);  // envia el string apuntado por pantalla al puerto serie

Si las saco compila bien ....pero si estan me da este error

:: warning: Omniscient Code Generation not available in Free mode
C:\Users\Ramiro\AppData\Local\Temp\so4g.:714: error: undefined symbol "_STATUS"
(908) exit status = 1
make[2]: Leaving directory `C:/Users/Ramiro/pic/pic16f819/Rs232Prueba.X'
make[1]: Leaving directory `C:/Users/Ramiro/pic/pic16f819/Rs232Prueba.X'
make[2]: *** [dist/default/production/Rs232Prueba.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 8s)

el error es un error de link no de linea .....



Les paso el codigo

main.c


Código: [Seleccionar]
#define _XTAL_FREQ 8000000         // esto es de HiTech para los delays    

    #include <stdio.h>
    #include <stdlib.h>
    #include <htc.h>
    #include "uart.c"
  


 void init (void);


     void main ()


     {
      
    OSCCONbits.IRCF2 = 1; //
    OSCCONbits.IRCF1 = 1; // defino oscilador interno en 8 mhz
    OSCCONbits.IRCF0 = 1; //

    TRISAbits.TRISA0 = 0 ;
    TRISAbits.TRISA1 = 1 ;
      
      
    const char *pantalla = "Mundo";


       InitSoftUart;
       putst_s ("Hola");   // envia el string "Hola" al puerto serie
       putst_s ("\n");     // envia el salto de linea al puerto serie
       putst_s (pantalla);  // envia el string apuntado por pantalla al puerto serie

     }

     void init (void)
     {
         ADCON0bits.ADON = 0;  //
    ADCON1bits.PCFG3 = 0; //
    ADCON1bits.PCFG2 = 1; // pongo todas las entradas digitales porta
    ADCON1bits.PCFG1 = 1; //
    ADCON1bits.PCFG0 = 1; //
    ADCON1bits.ADCS2 = 0; //
    
    TRISB =1;
    PORTB =0;
     }
    


uart.c

Código: [Seleccionar]
  /**************************************************************************
    *    Libreria para emular puerto serie asincronico (UART) para HiTech     *
    ***************************************************************************

    ***************************************************************************
    * Con clock de 4MHz la velocidad maxima de transmision es de 38400 Baud   *
    * Con clock de 8MHz en adelante la velocidad minima de transmision es de  *
    * 2400 Baud                                                               *
    ***************************************************************************

    ***************************************************************************
    *                                                                         *
    * Antes de incluir este archivo en nuestro programa hay que definir       *
    * algunos parametros. Estos son                                           *
    *                                                                         *
    * PIC_CLK   (es necesario para los delay usados internamente)             *
    *                                                                         *
    * TxPin     (puerto de salida)                                            *
    * RxPin     (puerto de entrada)                                           *
    * BaudRate  (velocidad de transferencia)                                  *
    *                                                                         *
    ***************************************************************************

    ***************************************************************************
    *                                                                         *
    * Las rutinas de delay se encuentran en el archivo "delayhd.h"            *
    * El nombre original del archivo es:                                      *
    * "delay_alternative_enchanced_precision.h" y esta incluido dentro de     *
    * "PIC_Hi-Tech_C_delay_and_timeout_routines_for_PIC16xxxx_v7-1.zip"       *
    * Bajado de www.microchipc.com                                            *
    *                                                                         *
    * Hay que tener este archivo dentro de la carpeta de trabajo.             *
    *                                                                         *
    *  Si se quiere transmitir, el pin que se utilice como TX tiene que       *
    * configurarse como salida.                                               *
    *  Si se quiere recibir, el pin que se utilice como RX tiene que          *
    * configurarse como entrada.                                              *
    *                                                                         *
    ***************************************************************************

    ***************************************************************************
    *                                                                         *
    * En esta libreria se encuentran las siguientes funciones                 *
    *                                                                         *
    * InitSoftUart()                (inicializa el puerto serie)              *
    * char getch_s()                (lee un byte del puerto serie)            *
    * putch_s(char)                 (envia un byte al puerto serie)           *
    * putst_s(char*)                (envia un string al puerto serie)         *
    * char * getst_s(char*)         (lee una cadena que termine con '\r')     *
    *                                                                         *
    *  El parametro de la funcion anterior puede ser un puntero a un String   *
    * o el propio String                                                      *
    *                                                                         *
    ***************************************************************************

    * Ejemplo de configuracion y utilizacion

     #define PIC_CLK 10000000
     #define TxPin RA0
     #define RxPin RB1
     #define BaudRate 9600

     #include "SoftUart.c"

     void main (void)
     {
       const char * pantalla = "Mundo";

       .....
       .....
       .....
       TRISA0 = 0;
       TRISB1 = 1;
       InitSoftUart();
       Putst_s ("Hola");   // envia el string "Hola" al puerto serie
       Putst_s ("\n");     // envia el salto de linea al puerto serie
       Putst_s (pantalla)  // envia el string apuntado por pantalla al puerto serie

     }

    */
#include <htc.h>
#include "delayhd.h"



/*******************funcionrd prototipos*************************/

//void InitSoftUart(void);
 unsigned char getch_s();
 unsigned char getch_s();
 extern void putch_s (unsigned char data);
 void putst_s (const char *word);
 char * getst_s (char * st);



    #define TxPin PORTAbits.RA0
    #define RxPin PORTAbits.RA1
    #define BaudRate 9600

  
    
    


#if defined (PIC_CLK) && defined (BaudRate) && defined (TxPin) && defined (RxPin)

     #if (PIC_CLK < 16000000)
       #define BitDelay (1000000/BaudRate)
       #define HalfBitDelay (500000/BaudRate)
     #else
       #define BitDelay (500000/BaudRate)
       #define HalfBitDelay (250000/BaudRate)
     #endif

     #if (PIC_CLK == 4000000)
       #define DelayReceivebit DelayUs(BitDelay-7)\
    DelayUs(HalfBitDelay-7)
       #if (BaudRate <= 19200)
         #define DelayStartBit DelayUs(BitDelay-7)
         #define DelaySendBit NDelayUs(BitDelay-18)
       #elif (BaudRate == 28800)
         #define DelayStartBit DelayUs(BitDelay-6)
         #define DelaySendBit NDelayUs(BitDelay-18)
       #elif (BaudRate == 38400)
         #define DelayStartBit DelayUs(BitDelay-7)\
    dly2u
         #define DelaySendBit DelayUs(BitDelay-19)\
      dly1u
       #endif

     #elif (PIC_CLK == 8000000)
       #define DelayReceivebit DelayUs(BitDelay-7)\
    DelayUs(HalfBitDelay-2)
       #define DelayStartBit DelayUs(BitDelay-4)
       #if (BaudRate <= 38400)
         #define DelaySendBit NDelayUs(BitDelay-10)
       #else  //(BaudRate == 57600)
         #define DelaySendBit NDelayUs(BitDelay-9)
    #endif

     #elif (PIC_CLK == 10000000)
       #define DelayReceivebit DelayUs(BitDelay-7)\
      DelayUs(HalfBitDelay)
       #if (BaudRate <= 38400)
         #define DelayStartBit DelayUs(BitDelay-4)
         #define DelaySendBit NDelayUs(BitDelay-8)
       #else //(BaudRate == 57600)
         #define DelayStartBit DelayUs(BitDelay-3)
         #define DelaySendBit NDelayUs(BitDelay-7)
    #endif

     #elif (PIC_CLK == 16000000) || (PIC_CLK == 20000000)
       #if (PIC_CLK == 16000000)
         #define DelayReceivebit DelayUs(BitDelay)\
     DelayUs(BitDelay)\
     DelayUs(BitDelay-5)
       #else //(PIC_CLK == 20000000)
         #define DelayReceivebit DelayUs(BitDelay)\
     DelayUs(BitDelay)\
     DelayUs(BitDelay-4)
       #endif
    #if (BaudRate <= 38400)
         #define DelayStartBit DelayUs(BitDelay)\
    DelayUs(BitDelay-2)
         #define DelaySendBit NDelayUs(BitDelay)\
      NDelayUs(BitDelay-4)
       #else //(BaudRate == 57600)
         #define DelayStartBit DelayUs(BitDelay)\
    DelayUs(BitDelay-1)
         #define DelaySendBit NDelayUs(BitDelay)\
      NDelayUs(BitDelay-3)
       #endif
     #endif

    /*
    void InitSoftUart(void)
    {
     TxPin = 1;
    }
    */
    #else
    #error Faltan definir parametros
    #endif
    


 #define InitSoftUart TxPin = 1


unsigned char getch_s(void)
 {
   unsigned char contador = 7;
   unsigned char letra = 0;

   while (RxPin == 1);
   DelayReceivebit;
   do
   {
     if (RxPin == 1) letra += 128;
     letra = letra >> 1;
     contador--;
     DelaySendBit;
   }
   while (contador > 0);
   DelaySendBit;
   return letra;
 }

 void putch_s (unsigned char data)
 {
   unsigned char contador = 8;

   TxPin = 0;
   DelayStartBit;
   do
   {
     TxPin = data;
     DelaySendBit;
     data = data >> 1;
     contador--;
   }
   while (contador > 0);
   TxPin = 1;
   DelayStartBit;   //tiempo de espera del bit de stop
 }

 void putst_s (const char *word)
 {
   while (*word != 0)
   {
     putch_s(*word);
     word++;
   }
 }

 char * getst_s (char * st)
 {
   char * st1;
   char caracter;

   st1 = st;
   do
   {
     caracter = getch_s();
     if (caracter != '\r')
     {
       *st1 = caracter;
       st1++;
     }
     *st1 = '\0';
   }
   while (caracter != '\r');
   return st;
 }

delayhd.h

Código: [Seleccionar]
/*

lowlevel delay routines

Designed by Shane Tolmie of www.microchipC.com corporation.  Freely distributable.
Questions and comments to webmaster@microchipC.com.

Completely re-written by Matthew Swabey and Paul Hoy of Southampton University, July 2005.
Questions and comments to mas@ecs.soton.ac.uk

Freely distributable, as above. Please add any other clock frequencies you want.

This uses no memory resources, and is precise.

Hi-Tech C and

Example C:

#define PIC_CLK 8000000
#include "delay.h"

void main(void)
{
TRISC = 0x00;
while(1)
{
PORTC = 0x00;
DelayUs(x);
PORTC = 0xFF;
}
}

*/

//#include "delay.h"
#define PIC_CLK 8000000



#ifndef __DELAY_H
#define __DELAY_H

#if (PIC_CLK == 4000000)
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n please remove; for 8Mhz+ only
#define dly1u asm("nop")
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 8000000)
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n asm("nop")
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 10000000)
#define dly400n asm("nop")
#elif (PIC_CLK == 16000000)
#define dly125n please remove; for 32Mhz+ only
#define dly250n asm("nop")
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 20000000)
#define dly200n asm("nop")
#define dly400n dly200n;dly200n
#define dly1u dly400n;dly400n;dly200n
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 32000000)
#define dly125n asm("nop")
#define dly250n dly125n;dly125n
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#else
#error please define PIC_CLK correctly

#endif

/* ======================================== */
/* Delay Routines for less than 255us       */
/* These give a precise delay.              */
/* DelayUs(10) is 10us exactly!             */
/* Only multiples of 2 available at slower  */
/* speeds (16Mhz or less) */

#if PIC_CLK == 4000000
#define DelayUs(x)\
asm("\tMOVLW "___mkstr(x>>2));\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 2");
#elif PIC_CLK == 8000000
#define DelayUs(x)\
asm("\tMOVLW "___mkstr(x>>1));\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 2");
#elif PIC_CLK == 10000000
#define DelayUs(x)\
asm("\tMOVLW "___mkstr(x>>1));\
asm("\tNOP");\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 3");
#elif PIC_CLK == 16000000
#define DelayUs(x)\
asm("\tMOVLW "___mkstr(x));\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 2");
#elif PIC_CLK == 20000000
#define DelayUs(x)\
asm("\tMOVLW "___mkstr(x));\
asm("\tNOP");\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 3");
#elif PIC_CLK == 32000000
#define DelayUs(x)\
asm("\tMOVLW "___mkstr(x));\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 6");
#else
#error please define PIC_CLK correctly
#endif

/* ======================================== */
/* NDelay Routines for less than 255us      */
/* These give a precise delay to the Next   */
/* Instructions ACTIVATION, so NDelayUs(10) */
/* is 10us - 1 instruction!                 */

#if PIC_CLK == 4000000
#define NDelayUs(x)\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tMOVLW "___mkstr((x>>2)-1) );\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 2");
#elif PIC_CLK == 8000000
#define NDelayUs(x)\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tMOVLW "___mkstr((x>>1)-1) );\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 2");
#elif PIC_CLK == 10000000
#define NDelayUs(x)\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tMOVLW "___mkstr((x>>1)-1) );\
asm("\tNOP");\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 3");
#elif PIC_CLK == 16000000
#define NDelayUs(x)\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tMOVLW "___mkstr(x-1));\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 2");
#elif PIC_CLK == 20000000
#define NDelayUs(x)\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tMOVLW "___mkstr(x-1));\
asm("\tNOP");\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 3");
#elif PIC_CLK == 32000000
#define NDelayUs(x)\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tMOVLW "___mkstr(x-1));\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tNOP");\
asm("\tADDLW 0xFF");\
asm("\tBTFSS _STATUS, 2");\
asm("\tGOTO $ - 6");
#else
#error please define PIC_CLK correctly
#endif

#endif

bits de configuracion

Código: [Seleccionar]
// PIC16F819 Configuration Bit Settings

#include <xc.h>

// CONFIG
#pragma config FOSC = INTOSCCLK // Oscillator Selection bits (INTRC oscillator; CLKO function on RA6/OSC2/CLKO pin and port I/O function on RA7/OSC1/CLKI pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB2      // CCP1 Pin Selection bit (CCP1 function on RB2)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)





Muchas gracias a todos

« Última modificación: 19 de Junio de 2013, 14:53:49 por Rseliman »
Las Grandes Obras las sueñan los grandes locos , mientras los inutiles las critican !!

Desconectado AngelGris

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 2480
Re: Mini curso "Programación en XC8"
« Respuesta #82 en: 20 de Junio de 2013, 17:15:09 »
  Aquí dejo una librería en para manejo de UART por SoftWare
  Hay que tener en cuenta que no todas las velocidades de comunicación serán posibles en todas las frecuencias de trabajo del microcontrolador. Por ejemplo, con 4MHz no se puede comunicar a una velocidad mayor a 19200
  En el header de la librería están definidos los tiempos y hay una "especie de compensación" según la frecuencia a la que esté trabajando.

  Lo simulé en ISIS con un 16F876A y va bien.

swuart.h
Código: C
  1. #ifndef SWUART_H
  2. #define SWUART_H
  3.  
  4. #ifndef _XC_H_
  5. #include <xc.h>
  6. #endif
  7.  
  8.  
  9. /*******************************************************************************
  10. *  Parametros configurables por el usuario                                     *
  11. *******************************************************************************/
  12.  
  13. /*
  14.  * El valor de _XTAL_FREQ debe coincidir con el valor definido
  15.  * en el programa principal
  16.  */
  17.  
  18. /*
  19.  * En el caso que solo se desee transmitir se debe definir TXONLY
  20.  * En el caso que solo se desee recibir se debe definir RXONLY
  21.  * En el caso de querer transmitir y recibir se debe definir TXRX
  22.  */
  23.  
  24. #ifndef _XTAL_FREQ
  25. #define _XTAL_FREQ 4000000
  26. #endif
  27.  
  28. #define TXPIN PORTBbits.RB0
  29. #define RXPIN PORTBbits.RB1
  30. #define TXDIR TRISBbits.TRISB0
  31. #define RXDIR TRISBbits.TRISB1
  32.  
  33. #define SWUARTBAUD 19200
  34. #define TXRX
  35.  
  36.  
  37. /*******************************************************************************
  38. *  Parametros internos para los tiempos de cada bit                            *
  39. *******************************************************************************/
  40.  
  41. #define _BitOutDelay (1000000UL/SWUARTBAUD)
  42. #define StartDelay (1500000UL/SWUARTBAUD)
  43.  
  44. #if (_XTAL_FREQ <= 4000000)
  45. #define BitOutDelay (_BitOutDelay - 24)
  46. #define BitInDelay (_BitOutDelay - 24)
  47. #elif (_XTAL_FREQ <= 6000000) && (_XTAL_FREQ > 4000000)
  48. #define BitOutDelay (_BitOutDelay - 16)
  49. #define BitInDelay (_BitOutDelay - 16)
  50. #elif (_XTAL_FREQ <= 8000000) && (_XTAL_FREQ > 6000000)
  51. #define BitOutDelay (_BitOutDelay - 12)
  52. #define BitInDelay (_BitOutDelay -12)
  53. #elif (_XTAL_FREQ <= 10000000) && (_XTAL_FREQ > 8000000)
  54. #define BitOutDelay (_BitOutDelay - 10)
  55. #define BitInDelay (_BitOutDelay -9)
  56. #elif (_XTAL_FREQ <= 12000000) && (_XTAL_FREQ > 10000000)
  57. #define BitOutDelay (_BitOutDelay - 8)
  58. #define BitInDelay (_BitOutDelay - 7)
  59. #elif (_XTAL_FREQ <= 16000000) && (_XTAL_FREQ > 12000000)
  60. #define BitOutDelay (_BitOutDelay - 6)
  61. #define BitInDelay (_BitOutDelay - 5)
  62. #elif (_XTAL_FREQ <= 20000000) && (_XTAL_FREQ > 16000000)
  63. #define BitOutDelay (_BitOutDelay - 5)
  64. #define BitInDelay (_BitOutDelay - 4)
  65. #elif (_XTAL_FREQ <= 26000000) && (_XTAL_FREQ > 20000000)
  66. #define BitOutDelay (_BitOutDelay - 3)
  67. #define BitInDelay (_BitOutDelay - 3)
  68. #elif (_XTAL_FREQ <= 48000000) && (_XTAL_FREQ > 26000000)
  69. #define BitOutDelay (_BitOutDelay - 2)
  70. #define BitInDelay (_BitOutDelay - 2)
  71. #endif
  72.  
  73.  
  74. /*******************************************************************************
  75. *  Definicion de funciones                                                     *
  76. *******************************************************************************/
  77.  
  78. /*
  79.  * La funcion swgets recibe una cadena que termine con '\r' (decimal 13)
  80.  * la cadena es devuelta a travez del puntero que se le pasa como parametro
  81.  */
  82.  
  83. void swinit_uart(void);
  84. void swputch(unsigned char);
  85. void swputs(unsigned char *);
  86. unsigned char swgetch(void);
  87. void swgets(unsigned char *);
  88.  
  89. #endif  /* SWUART_H */

swuart.c
Código: C
  1. #ifndef SWUART_H
  2. #include "swuart.h"
  3. #endif
  4.  
  5. #if !defined(TXPIN) || !defined(RXPIN) || !defined(TXDIR) || !defined(RXDIR)
  6. #error No estan definidos los pines
  7. #else
  8. #ifndef SWUARTBAUD
  9. #error Falta definir la velocidad
  10. #endif
  11.  
  12. void swinit_uart(void)
  13. {
  14. #if defined TXONLY
  15.     TXDIR = 0;
  16. #elif defined RXONLY
  17.     RXDIR = 1;
  18. #elif defined TXRX
  19.     TXDIR = 0;
  20.     RXDIR = 1;
  21. #endif
  22. }
  23.  
  24. void swputch(unsigned char _data)
  25. {
  26.     unsigned char _bits = 8;
  27.  
  28.     TXPIN = 0;
  29.     __delay_us(BitOutDelay);
  30.     do
  31.     {
  32.         TXPIN = _data & 1;
  33.         __delay_us(BitOutDelay);
  34.         _data = _data >> 1;
  35.         _bits--;
  36.     }
  37.     while (_bits > 0);
  38.     TXPIN = 1;
  39.     __delay_us(_BitOutDelay);
  40. }
  41.  
  42. void swputs(unsigned char *_st)
  43. {
  44.     while (*_st != 0)
  45.     {
  46.         swputch(*_st);
  47.         _st++;
  48.     }
  49. }
  50.  
  51. unsigned char swgetch(void)
  52. {
  53.     unsigned char _datain = 0;
  54.     unsigned char _bits = 8;
  55.  
  56.     while (RXPIN == 1);
  57.     __delay_us(StartDelay);
  58.     do
  59.     {
  60.         _datain = _datain >> 1;
  61.         if (RXPIN == 1) _datain |= 0b10000000;
  62.         __delay_us(BitInDelay);
  63.         _bits--;
  64.     }
  65.     while (_bits > 0);
  66.     __delay_us(BitOutDelay/2);
  67.     return _datain;
  68. }
  69.  
  70.  void swgets(unsigned char *st)
  71. {
  72.     unsigned char *word;
  73.     unsigned char character;
  74.  
  75.     word = st;
  76.     do
  77.     {
  78.         character = swgetch();
  79.         if (character != '\r')
  80.         {
  81.             *word = character;
  82.             word++;
  83.         }
  84.     }
  85.     while (character != '\r');
  86.     *word = 0;
  87. }
  88. #endif

Programa Ejemplo
Código: C
  1. #include <xc.h>
  2.  
  3. #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
  4. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
  5. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  6. #pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
  7. #pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  8. #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  9. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  10. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
  11.  
  12.  
  13.  
  14. //#define _XTAL_FREQ 26000000
  15. #include "swuart.h"
  16.  
  17. void main(void)
  18. {
  19.     unsigned char mensaje[10] = "TodoPic";
  20.     unsigned char *punterostring;
  21.     unsigned char caracter;
  22.  
  23.     punterostring = mensaje;
  24.     caracter = 'a';
  25.     swinit_uart();           //inicialiso el UART
  26.     swputch('H');
  27.     swputch('o');
  28.     swputch('l');
  29.     swputch(caracter);       //mando la palabra Hola letra por letra
  30.     swputch('\r');           //salto de linea
  31.     swputs(mensaje);         //mando la cadena almacenada en mensaje
  32.     swputch('\r');           //salto de linea
  33.     swgets(punterostring);   //recibo una cadena terminada con '\r'
  34.     swputch('\r');           //salto de linea
  35.     swputs(punterostring);   //mando la cadena apuntada por punterostring
  36.                              //(recibida anteriormente)
  37.  
  38.     while(1);
  39. }
De vez en cuando la vida
nos besa en la boca
y a colores se despliega
como un atlas

Desconectado gab163

  • PIC16
  • ***
  • Mensajes: 111
Re: Mini curso "Programación en XC8"
« Respuesta #83 en: 21 de Junio de 2013, 01:22:52 »
Les queria compartir otra libreria para manejo de lcd en modo control cuatro bits. Les cuelgo un codigo main y la libreria flex_lcd. El programa esta para funcionar con un PIC16f887 pero facilmente puede funcionar con cualquier otro:

main:
Código: [Seleccionar]
#include <xc.h>
#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
#define _XTAL_FREQ 8000000
#include "flex_lcd.h"

// CONFIG1
#pragma config FOSC = INTRC_CLKOUT// Oscillator Selection bits (INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

void main(void)
{
unsigned char i, j;
unsigned char buffer1[20];

ANSEL=0;
ANSELH=0;
Lcd_Init();
Lcd_Cmd(LCD_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
__delay_ms(100);

for(i=10; i>0; i--)
{
//sprintf(buffer1,"%3d",i);      //Right aligned text
sprintf(buffer1,"Cuenta %03d",i);   //Right aligned text and adding zeros if necessary
//sprintf(buffer1,"%d   ",i);  //Left  aligned text
Lcd_Out2(1, 1, buffer1);
__delay_ms(100);
}
Lcd_Out(2, 1, "Gabriel");
__delay_ms(600);
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
__delay_ms(1000);
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
Lcd_Cmd(LCD_UNDERLINE_ON);
__delay_ms(1000);
Lcd_Cmd(LCD_CURSOR_OFF);
__delay_ms(1000);
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1, 4, "Gracias");
Lcd_Out(2, 4, "Thank u");
while(1);
}

lcd_flex.h
Código: [Seleccionar]
//******************************************
//  Libreria para control de LCD           *

//#define LCD_RD7     PORTBbits.RB7       // D7
#define LCD_RD7     LATDbits.LATD7       // D7
//#define TRISRD7     TRISBbits.TRISB7
#define TRISRD7     TRISDbits.TRISD7
//#define LCD_RD6     PORTBbits.RB6       // D6
#define LCD_RD6     LATDbits.LATD6       // D6
//#define TRISRD6     TRISBbits.TRISB6
#define TRISRD6     TRISDbits.TRISD6
//#define LCD_RD5     PORTBbits.RB5       // D5
#define LCD_RD5     LATDbits.LATD5       // D5
//#define TRISRD5     TRISBbits.TRISB5
#define TRISRD5     TRISDbits.TRISD5
//#define LCD_RD4     PORTBbits.RB4       // D4
#define LCD_RD4     LATDbits.LATD4       // D4
//#define TRISRD4     TRISBbits.TRISB4
#define TRISRD4     TRISDbits.TRISD4
//#define LCD_EN      PORTBbits.RB3       // EN
#define LCD_EN      LATEbits.LATE2       // EN
//#define TRISEN      TRISBbits.TRISB3
#define TRISEN      TRISEbits.TRISE2
//#define LCD_RS      PORTBbits.RB2       // RS
#define LCD_RS      LATEbits.LATE1       // RS
//#define TRISRS      TRISBbits.TRISB2
#define TRISRS      TRISEbits.TRISE1

//comandos disponibles
#define      LCD_FIRST_ROW           128
#define      LCD_SECOND_ROW          192
#define      LCD_THIRD_ROW           148
#define      LCD_FOURTH_ROW          212
#define      LCD_CLEAR               1
#define      LCD_RETURN_HOME         2
#define      LCD_CURSOR_OFF          12
#define      LCD_UNDERLINE_ON        14
#define      LCD_BLINK_CURSOR_ON     15
#define      LCD_MOVE_CURSOR_LEFT    16
#define      LCD_MOVE_CURSOR_RIGHT   20
#define      LCD_TURN_OFF            0
#define      LCD_TURN_ON             8
#define      LCD_SHIFT_LEFT          24
#define      LCD_SHIFT_RIGHT         28


void Lcd_Init(void);
void Lcd_Out(unsigned char y, unsigned char x, const char *buffer);
void Lcd_Out2(unsigned char y, unsigned char x, char *buffer);
void Lcd_Chr_CP(char data);
void Lcd_Cmd(unsigned char data);

void Lcd_Init(void){
unsigned char data;
TRISRD7 = 0;
TRISRD6 = 0;
TRISRD5 = 0;
TRISRD4 = 0;
TRISEN = 0;
TRISRS = 0;
LCD_RD7 = 0;
LCD_RD6 = 0;
LCD_RD5 = 0;
LCD_RD4 = 0;
LCD_EN = 0;
LCD_RS = 0;
__delay_us(5500);
__delay_us(5500);
__delay_us(5500);
__delay_us(5500);
__delay_us(5500);
__delay_us(5500);
for(data = 1; data < 4; data ++)
{
    LCD_RD7 = 0;    LCD_RD6 = 0;    LCD_RD5 = 1;    LCD_RD4 = 1;    LCD_EN = 0;
    LCD_RS = 0;    LCD_RD7 = 0;    LCD_RD6 = 0;    LCD_RD5 = 1;    LCD_RD4 = 1;
    LCD_EN = 1;    LCD_RS = 0;
    __delay_us(5);
    LCD_RD7 = 0;    LCD_RD6 = 0;    LCD_RD5 = 1;    LCD_RD4 = 1;    LCD_EN = 0;
    LCD_RS = 0;
    __delay_us(5500);
}
LCD_RD7 = 0; LCD_RD6 = 0; LCD_RD5 = 1; LCD_RD4 = 0; LCD_EN = 0; LCD_RS = 0;
LCD_RD7 = 0; LCD_RD6 = 0; LCD_RD5 = 1; LCD_RD4 = 0; LCD_EN = 1; LCD_RS = 0;
__delay_us(5);
LCD_RD7 = 0; LCD_RD6 = 0; LCD_RD5 = 1; LCD_RD4 = 0; LCD_EN = 0; LCD_RS = 0;
__delay_us(5500);
data = 40; Lcd_Cmd(data);
data = 16; Lcd_Cmd(data);
data = 1;  Lcd_Cmd(data);
data = 15; Lcd_Cmd(data);
}


void Lcd_Out(unsigned char y, unsigned char x, const char *buffer)
{
unsigned char data;
switch (y)
{   
    case 1: data = 128 + x; break;
    case 2: data = 192 + x; break;
    case 3: data = 148 + x; break;
    case 4: data = 212 + x; break;
    default: break;
}
Lcd_Cmd(data);
while(*buffer)              // Write data to LCD up to null
{               
    Lcd_Chr_CP(*buffer);
    buffer++;               // Increment buffer
}
return;
}


void Lcd_Out2(unsigned char y, unsigned char x, char *buffer)
{
unsigned char data;
switch (y)
{
    case 1: data = 128 + x; break;
    case 2: data = 192 + x; break;
    case 3: data = 148 + x; break;
    case 4: data = 212 + x; break;
    default: break;
}
Lcd_Cmd(data);
while(*buffer)              // Write data to LCD up to null
{               
    Lcd_Chr_CP(*buffer);
    buffer++;               // Increment buffer
}
return;
}


void Lcd_Chr_CP(char data){
LCD_EN = 0; LCD_RS = 1;
LCD_RD7 = (data & 0b10000000)>>7; LCD_RD6 = (data & 0b01000000)>>6;
LCD_RD5 = (data & 0b00100000)>>5; LCD_RD4 = (data & 0b00010000)>>4;
_delay(10);
LCD_EN = 1; __delay_us(5); LCD_EN = 0;
LCD_RD7 = (data & 0b00001000)>>3; LCD_RD6 = (data & 0b00000100)>>2;
LCD_RD5 = (data & 0b00000010)>>1; LCD_RD4 = (data & 0b00000001);
_delay(10);
LCD_EN = 1; __delay_us(5); LCD_EN = 0;
__delay_us(5); __delay_us(5500);
}


void Lcd_Cmd(unsigned char data){
LCD_EN = 0; LCD_RS = 0;
LCD_RD7 = (data & 0b10000000)>>7; LCD_RD6 = (data & 0b01000000)>>6;
LCD_RD5 = (data & 0b00100000)>>5; LCD_RD4 = (data & 0b00010000)>>4;
_delay(10);
LCD_EN = 1; __delay_us(5); LCD_EN = 0;
LCD_RD7 = (data & 0b00001000)>>3; LCD_RD6 = (data & 0b00000100)>>2;
LCD_RD5 = (data & 0b00000010)>>1; LCD_RD4 = (data & 0b00000001);
_delay(10);
LCD_EN = 1; __delay_us(5); LCD_EN = 0;
__delay_us(5500);//Delay_5us();
}
#endif

Desconectado Limbo

  • PIC10
  • *
  • Mensajes: 44
Re: Mini curso "Programación en XC8"
« Respuesta #84 en: 21 de Junio de 2013, 11:04:44 »
Hola Rseliman,

He probado tu libreria de LCD tal y como la subistes y me da este error:
Código: [Seleccionar]
lcd_pic16.c:145: warning: function declared implicit int
lcd_pic16.c:150: warning: function declared implicit int
lcd_pic16.c:192: error: undefined identifier "PORTB"
lcd_pic16.c:193: error: undefined identifier "TRISB"
lcd_pic16.c:199: error: undefined identifier "TRISBbits"
lcd_pic16.c:199: error: struct/union required
lcd_pic16.c:200: error: struct/union required
lcd_pic16.c:201: error: struct/union required
lcd_pic16.c:202: error: undefined identifier "PORTBbits"
lcd_pic16.c:202: error: struct/union required
lcd_pic16.c:203: error: struct/union required
lcd_pic16.c:204: error: struct/union required
lcd_pic16.c:278: error: undefined identifier "TRISB"
lcd_pic16.c:279: error: undefined identifier "PORTB"
lcd_pic16.c:286: error: undefined identifier "PORTBbits"
lcd_pic16.c:286: error: struct/union required
lcd_pic16.c:287: error: struct/union required
lcd_pic16.c:289: error: struct/union required
lcd_pic16.c:291: error: struct/union required
lcd_pic16.c:300: error: struct/union required
lcd_pic16.c:302: error: struct/union required
lcd_pic16.c:336: error: undefined identifier "TRISB"
lcd_pic16.c:337: error: undefined identifier "PORTB"
lcd_pic16.c:337: advisory: too many errors (21)

Es como si no los #define no hicieran su trabajo ¿Puede que sea alguna configuracion del MPLABX?
Gracias.

Edito: Con la libreria del compañero gab163 me da el mismo error..
« Última modificación: 21 de Junio de 2013, 11:27:42 por Limbo »

Desconectado AngelGris

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 2480
Re: Mini curso "Programación en XC8"
« Respuesta #85 en: 21 de Junio de 2013, 11:41:46 »
Hola Rseliman,

He probado tu libreria de LCD tal y como la subistes y me da este error:
Código: [Seleccionar]
lcd_pic16.c:145: warning: function declared implicit int
lcd_pic16.c:150: warning: function declared implicit int
lcd_pic16.c:192: error: undefined identifier "PORTB"
lcd_pic16.c:193: error: undefined identifier "TRISB"
lcd_pic16.c:199: error: undefined identifier "TRISBbits"
lcd_pic16.c:199: error: struct/union required
lcd_pic16.c:200: error: struct/union required
lcd_pic16.c:201: error: struct/union required
lcd_pic16.c:202: error: undefined identifier "PORTBbits"
lcd_pic16.c:202: error: struct/union required
lcd_pic16.c:203: error: struct/union required
lcd_pic16.c:204: error: struct/union required
lcd_pic16.c:278: error: undefined identifier "TRISB"
lcd_pic16.c:279: error: undefined identifier "PORTB"
lcd_pic16.c:286: error: undefined identifier "PORTBbits"
lcd_pic16.c:286: error: struct/union required
lcd_pic16.c:287: error: struct/union required
lcd_pic16.c:289: error: struct/union required
lcd_pic16.c:291: error: struct/union required
lcd_pic16.c:300: error: struct/union required
lcd_pic16.c:302: error: struct/union required
lcd_pic16.c:336: error: undefined identifier "TRISB"
lcd_pic16.c:337: error: undefined identifier "PORTB"
lcd_pic16.c:337: advisory: too many errors (21)

Es como si no los #define no hicieran su trabajo ¿Puede que sea alguna configuracion del MPLABX?
Gracias.

Edito: Con la libreria del compañero gab163 me da el mismo error..

Pon tu programa, Limbo
De vez en cuando la vida
nos besa en la boca
y a colores se despliega
como un atlas

Desconectado Limbo

  • PIC10
  • *
  • Mensajes: 44
Re: Mini curso "Programación en XC8"
« Respuesta #86 en: 21 de Junio de 2013, 12:24:29 »
Es identico al de Rseliman..
main.c
Código: [Seleccionar]
#include <xc.h>
#define _XTAL_FREQ 8000000
#include <stdio.h>
#include <stdlib.h>
#include "lcd_pic16.c"
#include "delay.h"

// PIC16F819 Configuration Bit Setting
// CONFIG
#pragma config FOSC = INTOSCCLK // Oscillator Selection bits (INTRC oscillator; CLKO function on RA6/OSC2/CLKO pin and port I/O function on RA7/OSC1/CLKI pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = OFF       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF      // Low-Voltage Programming Enable bit (RB3/PGM pin has PGM function, Low-Voltage Programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB2      // CCP1 Pin Selection bit (CCP1 function on RB2)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// CONFIG2
//#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable




//variables globales
unsigned char buffer [14];
// unsigned int contador=0;
//funciones prototipo
void init(void);


void main(void)
{

   OSCCONbits.IRCF2 = 1; //
   OSCCONbits.IRCF1 = 1; // defino oscilador interno en 4 mhz
   OSCCONbits.IRCF0 = 0; //


init();


           
SetDDRamAddr(0x00);
putrsXLCD("Ramiro Seliman");
                putsXLCD(buffer);
                SetDDRamAddr(0x40);
putrsXLCD("Victoria E.R");
        }

void init(void)
{
    ADCON0bits.ADON = 0;

    TRISB=0;    //todo el PORTB como salidas digitales
    PORTB=0;
    OpenXLCD(FOUR_BIT & LINES_5X7 );


}

lcd_pic16.c
Código: [Seleccionar]
* For 8-bit operation uncomment the #define BIT8
 */
// #define BIT8

/* When in 4-bit interface define if the data is in the upper
 * or lower nibble.  For lower nibble, comment the #define UPPER
 */


#define UPPER

/* When in 6-10-bit interface comment the #define BUSY_LCD and conected PIN
 * R/W to GND in LCD
 */
 //#define BUSY_LCD

/* DATA_PORT defines the port to which the LCD data lines are connected */
 #define DATA_PORT      PORTB
 #define TRIS_DATA_PORT TRISB

/* CTRL_PORT defines the port where the control lines are connected.
 * These are just samples, change to match your application.
 */
 #define RW_PIN   PORTBbits.RB0    /* PORT for RW */
 #define TRIS_RW  TRISBbits.TRISB0    /* TRIS for RW */

 #define RS_PIN   PORTBbits.RB1    /* PORT for RS */
 #define TRIS_RS  TRISBbits.TRISB1    /* TRIS for RS */

 #define E_PIN    PORTBbits.RB2  /* PORT for D  */
 #define TRIS_E   TRISBbits.TRISB2    /* TRIS for E  */

/* Display ON/OFF Control defines */
#define DON         0b00001111  /* Display on      */
#define DOFF        0b00001011  /* Display off     */
#define CURSOR_ON   0b00001111  /* Cursor on       */
#define CURSOR_OFF  0b00001101  /* Cursor off      */
#define BLINK_ON    0b00001111  /* Cursor Blink    */
#define BLINK_OFF   0b00001110  /* Cursor No Blink */

/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
#define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
#define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
#define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */

/* Function Set defines */
#define FOUR_BIT   0b00101100  /* 4-bit Interface               */
#define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
#define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
#define LINE_5X10  0b00110100  /* 5x10 characters               */
#define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */

#ifdef _OMNI_CODE_
#define PARAM_SCLASS
#else
#define PARAM_SCLASS auto
#endif

/* CLS_Line2
 * Clear Screen Line 2
 */
void CLS_Line2(void);

/* CLS_Line1
 * Clear Screen Line 1
 */
void CLS_Line1(void);
/* OpenXLCD
 * Configures I/O pins for external LCD
 */
void OpenXLCD(PARAM_SCLASS unsigned char);

/* SetCGRamAddr
 * Sets the character generator address
 */
void SetCGRamAddr(PARAM_SCLASS unsigned char);

/* SetDDRamAddr
 * Sets the display data address
 */
void SetDDRamAddr(PARAM_SCLASS unsigned char);

/* BusyXLCD
 * Returns the busy status of the LCD
 */
unsigned char BusyXLCD(void);

/* ReadAddrXLCD
 * Reads the current address
 */
unsigned char ReadAddrXLCD(void);

/* ReadDataXLCD
 * Reads a byte of data
 */
char ReadDataXLCD(void);

/* WriteCmdXLCD
 * Writes a command to the LCD
 */
void WriteCmdXLCD(PARAM_SCLASS unsigned char);

/* WriteDataXLCD
 * Writes a data byte to the LCD
 */
void WriteDataXLCD(PARAM_SCLASS char);

/* putcXLCD
 * A putc is a write
 */
#define putcXLCD WriteDataXLCD

/* putsXLCD
 * Writes a string of characters to the LCD
 */
void putsXLCD(PARAM_SCLASS char *);

/* putrsXLCD
 * Writes a string of characters in  to the LCD
 */
void putrsXLCD(const char *);

// Rutinas de tiempo auxiliares para la libreria XLCD
void DelayFor18TCY(void)
{
__delay_us(18);
}

void DelayPORXLCD(void)
{
__delay_ms(20); //Delay de 15 ms
}

void DelayXLCD(void)
{
__delay_ms(20); //Delay de 20 ms
}

void CLS_Line2(void)
{
    SetDDRamAddr(0x40);
    putrsXLCD("                ");
}

void CLS_Line1(void)
{
    SetDDRamAddr(0x00);
    putrsXLCD("                ");
}
/********************************************************************
*       Function Name:  OpenXLCD                                    *
*       Return Value:   void                                        *
*       Parameters:     lcdtype: sets the type of LCD (lines)       *
*       Description:    This routine configures the LCD. Based on   *
*                       the Hitachi HD44780 LCD controller. The     *
*                       routine will configure the I/O pins of the  *
*                       microcontroller, setup the LCD for 4- or    *
*                       8-bit mode and clear the display. The user  *
*                       must provide three delay routines:          *
*                       DelayFor18TCY() provides a 18 Tcy delay     *
*                       DelayPORXLCD() provides at least 15ms delay *
*                       DelayXLCD() provides at least 5ms delay     *
********************************************************************/
void OpenXLCD(unsigned char lcdtype)
{
        // The data bits must be either a 8-bit port or the upper or
        // lower 4-bits of a port. These pins are made into inputs
#ifdef BIT8                             // 8-bit mode, use whole port
        DATA_PORT = 0;
        TRIS_DATA_PORT = 0x00;
#else                                   // 4-bit mode
#ifdef UPPER                            // Upper 4-bits of the port
        DATA_PORT &= 0x0f;
        TRIS_DATA_PORT &= 0x0F;
#else                                   // Lower 4-bits of the port
        DATA_PORT &= 0xf0;
        TRIS_DATA_PORT &= 0xF0;
#endif
#endif
        TRIS_RW = 0;                    // All control signals made outputs
        TRIS_RS = 0;
        TRIS_E = 0;
        RW_PIN = 0;                     // R/W pin made low
        RS_PIN = 0;                     // Register select pin made low
        E_PIN = 0;                      // Clock pin made low

        // Delay for 15ms to allow for LCD Power on reset
        DelayPORXLCD();
 //-------------------reset procedure through software----------------------       
WriteCmdXLCD(0x30);
__delay_ms(5);

WriteCmdXLCD(0x30);
__delay_ms(1);


WriteCmdXLCD(0x32);
while( BusyXLCD() );
//------------------------------------------------------------------------------------------


        // Set data interface width, # lines, font
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(lcdtype);          // Function set cmd

        // Turn the display on then off
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF);        // Display OFF/Blink OFF
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON);           // Display ON/Blink ON

        // Clear display
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(0x01);             // Clear display

        // Set entry mode inc, no shift
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(SHIFT_CUR_RIGHT);   // Entry Mode
 
while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(0x06);    // Incremente
 
        while(BusyXLCD());              // Wait if LCD busy
        SetDDRamAddr(0x80);             // Set Display data ram address to 0
       
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(CURSOR_OFF);       // Cursor OFF
        return;
}


/********************************************************************
*       Function Name:  WriteDataXLCD                               *
*       Return Value:   void                                        *
*       Parameters:     data: data byte to be written to LCD        *
*       Description:    This routine writes a data byte to the      *
*                       Hitachi HD44780 LCD controller. The user    *
*                       must check to see if the LCD controller is  *
*                       busy before calling this routine. The data  *
*                       is written to the character generator RAM or*
*                       the display data RAM depending on what the  *
*                       previous SetxxRamAddr routine was called.   *
********************************************************************/
void WriteDataXLCD(char data)
{
#ifdef BIT8                             // 8-bit interface
        TRIS_DATA_PORT = 0;             // Make port output
        DATA_PORT = data;               // Write data to port
        RS_PIN = 1;                     // Set control bits
        RW_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock data into LCD
        DelayFor18TCY();
        E_PIN = 0;
        RS_PIN = 0;                     // Reset control bits
        TRIS_DATA_PORT = 0xff;          // Make port input
#else                                   // 4-bit interface
#ifdef UPPER                            // Upper nibble interface
        TRIS_DATA_PORT &= 0x0f;
        DATA_PORT &= 0x0f;
        DATA_PORT |= data&0xf0;
#else                                   // Lower nibble interface
        TRIS_DATA_PORT &= 0xf0;
        DATA_PORT &= 0xf0;
        DATA_PORT |= ((data>>4)&0x0f);
#endif
        RS_PIN = 1;                     // Set control bits
        RW_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock nibble into LCD
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                            // Upper nibble interface
        DATA_PORT &= 0x0f;
        DATA_PORT |= ((data<<4)&0xf0);
#else                                   // Lower nibble interface
        DATA_PORT &= 0xf0;
        DATA_PORT |= (data&0x0f);
#endif
        DelayFor18TCY();
        E_PIN = 1;                      // Clock nibble into LCD
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                            // Upper nibble interface
        TRIS_DATA_PORT |= 0xf0;
#else                                   // Lower nibble interface
        TRIS_DATA_PORT |= 0x0f;
#endif
#endif
        return;
}

/********************************************************************
*       Function Name:  WriteCmdXLCD                                *
*       Return Value:   void                                        *
*       Parameters:     cmd: command to send to LCD                 *
*       Description:    This routine writes a command to the Hitachi*
*                       HD44780 LCD controller. The user must check *
*                       to see if the LCD controller is busy before *
*                       calling this routine.                       *
********************************************************************/
void WriteCmdXLCD(unsigned char cmd)
{
#ifdef BIT8                             // 8-bit interface
        TRIS_DATA_PORT = 0;             // Data port output
        DATA_PORT = cmd;                // Write command to data port
        RW_PIN = 0;                     // Set the control signals
        RS_PIN = 0;                     // for sending a command
        DelayFor18TCY();
        E_PIN = 1;                      // Clock the command in
        DelayFor18TCY();
        E_PIN = 0;
        DelayFor18TCY();
        TRIS_DATA_PORT = 0xff;          // Data port input
#else                                   // 4-bit interface
#ifdef UPPER                            // Upper nibble interface
        TRIS_DATA_PORT &= 0x0f;
        DATA_PORT &= 0x0f;
        DATA_PORT |= cmd&0xf0;
#else                                   // Lower nibble interface
        TRIS_DATA_PORT &= 0xf0;
        DATA_PORT &= 0xf0;
        DATA_PORT |= (cmd>>4)&0x0f;
#endif
        RW_PIN = 0;                     // Set control signals for command
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock command in
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                            // Upper nibble interface
        DATA_PORT &= 0x0f;
        DATA_PORT |= (cmd<<4)&0xf0;
#else                                   // Lower nibble interface
        DATA_PORT &= 0xf0;
        DATA_PORT |= cmd&0x0f;
#endif
        DelayFor18TCY();
        E_PIN = 1;                      // Clock command in
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                            // Make data nibble input
        TRIS_DATA_PORT |= 0xf0;
#else
        TRIS_DATA_PORT |= 0x0f;
#endif
#endif
        return;
}

/********************************************************************
*       Function Name:  SetDDRamAddr                                *
*       Return Value:   void                                        *
*       Parameters:     CGaddr: display data address                *
*       Description:    This routine sets the display data address  *
*                       of the Hitachi HD44780 LCD controller. The  *
*                       user must check to see if the LCD controller*
*                       is busy before calling this routine.        *
********************************************************************/
void SetDDRamAddr(unsigned char DDaddr)
{
#ifdef BIT8                                     // 8-bit interface
        TRIS_DATA_PORT = 0;                     // Make port output
        DATA_PORT = DDaddr | 0b10000000;        // Write cmd and address to port
        RW_PIN = 0;                             // Set the control bits
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                              // Clock the cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
        DelayFor18TCY();
        TRIS_DATA_PORT = 0xff;                  // Make port input
#else                                           // 4-bit interface
#ifdef UPPER                                    // Upper nibble  interface
        TRIS_DATA_PORT &= 0x0f;                 // Make port output
        DATA_PORT &= 0x0f;                      // and write upper nibble
        DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
#else                                           // Lower nibble interface
        TRIS_DATA_PORT &= 0xf0;                 // Make port output
        DATA_PORT &= 0xf0;                      // and write upper nibble
        DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
#endif
        RW_PIN = 0;                             // Set control bits
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                              // Clock the cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                                    // Upper nibble interface
        DATA_PORT &= 0x0f;                      // Write lower nibble
        DATA_PORT |= ((DDaddr<<4)&0xf0);
#else                                           // Lower nibble interface
        DATA_PORT &= 0xf0;                      // Write lower nibble
        DATA_PORT |= (DDaddr&0x0f);
#endif
        DelayFor18TCY();
        E_PIN = 1;                              // Clock the cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                                    // Upper nibble interface
        TRIS_DATA_PORT |= 0xf0;                 // Make port input
#else                                           // Lower nibble interface
        TRIS_DATA_PORT |= 0x0f;                 // Make port input
#endif
#endif
        return;
}

/********************************************************************
*       Function Name:  SetCGRamAddr                                *
*       Return Value:   void                                        *
*       Parameters:     CGaddr: character generator ram address     *
*       Description:    This routine sets the character generator   *
*                       address of the Hitachi HD44780 LCD          *
*                       controller. The user must check to see if   *
*                       the LCD controller is busy before calling   *
*                       this routine.                               *
********************************************************************/
void SetCGRamAddr(unsigned char CGaddr)
{
#ifdef BIT8                                     // 8-bit interface
        TRIS_DATA_PORT = 0;                     // Make data port ouput
        DATA_PORT = CGaddr | 0b01000000;        // Write cmd and address to port
        RW_PIN = 0;                             // Set control signals
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                              // Clock cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
        DelayFor18TCY();
        TRIS_DATA_PORT = 0xff;                  // Make data port inputs
#else                                           // 4-bit interface
#ifdef UPPER                                    // Upper nibble interface
        TRIS_DATA_PORT &= 0x0f;                 // Make nibble input
        DATA_PORT &= 0x0f;                      // and write upper nibble
        DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
#else                                           // Lower nibble interface
        TRIS_DATA_PORT &= 0xf0;                 // Make nibble input
        DATA_PORT &= 0xf0;                      // and write upper nibble
        DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
#endif
        RW_PIN = 0;                             // Set control signals
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                              // Clock cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                                    // Upper nibble interface
        DATA_PORT &= 0x0f;                      // Write lower nibble
        DATA_PORT |= ((CGaddr<<4)&0xf0);
#else                                           // Lower nibble interface
        DATA_PORT &= 0xf0;                      // Write lower nibble
        DATA_PORT |= (CGaddr&0x0f);
#endif
        DelayFor18TCY();
        E_PIN = 1;                              // Clock cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                                    // Upper nibble interface
        TRIS_DATA_PORT |= 0xf0;                 // Make inputs
#else                                           // Lower nibble interface
        TRIS_DATA_PORT |= 0x0f;                 // Make inputs
#endif
#endif
        return;
}

/********************************************************************
*       Function Name:  ReadDataXLCD                                *
*       Return Value:   char: data byte from LCD controller         *
*       Parameters:     void                                        *
*       Description:    This routine reads a data byte from the     *
*                       Hitachi HD44780 LCD controller. The user    *
*                       must check to see if the LCD controller is  *
*                       busy before calling this routine. The data  *
*                       is read from the character generator RAM or *
*                       the display data RAM depending on what the  *
*                       previous SetxxRamAddr routine was called.   *
********************************************************************/
char ReadDataXLCD(void)
{
        char data;

#ifdef BIT8                             // 8-bit interface
        RS_PIN = 1;                     // Set the control bits
        RW_PIN = 1;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock the data out of the LCD
        DelayFor18TCY();
        data = DATA_PORT;               // Read the data
        E_PIN = 0;
        RS_PIN = 0;                     // Reset the control bits
        RW_PIN = 0;
#else                                   // 4-bit interface
        RW_PIN = 1;
        RS_PIN = 1;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock the data out of the LCD
        DelayFor18TCY();
#ifdef UPPER                            // Upper nibble interface
        data = DATA_PORT&0xf0;          // Read the upper nibble of data
#else                                   // Lower nibble interface
        data = (DATA_PORT<<4)&0xf0;     // read the upper nibble of data
#endif
        E_PIN = 0;                      // Reset the clock line
        DelayFor18TCY();
        E_PIN = 1;                      // Clock the next nibble out of the LCD
        DelayFor18TCY();
#ifdef UPPER                            // Upper nibble interface
        data |= (DATA_PORT>>4)&0x0f;    // Read the lower nibble of data
#else                                   // Lower nibble interface
        data |= DATA_PORT&0x0f;         // Read the lower nibble of data
#endif
        E_PIN = 0;                                     
        RS_PIN = 0;                     // Reset the control bits
        RW_PIN = 0;
#endif
        return(data);                   // Return the data byte
}

/*********************************************************************
*       Function Name:  ReadAddrXLCD                                 *
*       Return Value:   char: address from LCD controller            *
*       Parameters:     void                                         *
*       Description:    This routine reads an address byte from the  *
*                       Hitachi HD44780 LCD controller. The user     *
*                       must check to see if the LCD controller is   *
*                       busy before calling this routine. The address*
*                       is read from the character generator RAM or  *
*                       the display data RAM depending on what the   *
*                       previous SetxxRamAddr routine was called.    *
*********************************************************************/
unsigned char ReadAddrXLCD(void)
{
        char data;                      // Holds the data retrieved from the LCD

#ifdef BIT8                             // 8-bit interface
        RW_PIN = 1;                     // Set control bits for the read
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock data out of the LCD controller
        DelayFor18TCY();
        data = DATA_PORT;               // Save the data in the register
        E_PIN = 0;
        RW_PIN = 0;                     // Reset the control bits
#else                                   // 4-bit interface
        RW_PIN = 1;                     // Set control bits for the read
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock data out of the LCD controller
        DelayFor18TCY();
#ifdef UPPER                            // Upper nibble interface
        data = DATA_PORT&0xf0;          // Read the nibble into the upper nibble of data
#else                                   // Lower nibble interface
        data = (DATA_PORT<<4)&0xf0;     // Read the nibble into the upper nibble of data
#endif
        E_PIN = 0;                      // Reset the clock
        DelayFor18TCY();
        E_PIN = 1;                      // Clock out the lower nibble
        DelayFor18TCY();
#ifdef UPPER                            // Upper nibble interface
        data |= (DATA_PORT>>4)&0x0f;    // Read the nibble into the lower nibble of data
#else                                   // Lower nibble interface
        data |= DATA_PORT&0x0f;         // Read the nibble into the lower nibble of data
#endif
        E_PIN = 0;
        RW_PIN = 0;                     // Reset the control lines
#endif
        return (data&0x7f);             // Return the address, Mask off the busy bit
}

/********************************************************************
*       Function Name:  putsXLCD
*       Return Value:   void
*       Parameters:     buffer: pointer to string
*       Description:    This routine writes a string of bytes to the
*                       Hitachi HD44780 LCD controller. The user
*                       must check to see if the LCD controller is
*                       busy before calling this routine. The data
*                       is written to the character generator RAM or
*                       the display data RAM depending on what the
*                       previous SetxxRamAddr routine was called.
********************************************************************/
void putsXLCD(char *buffer)
{
        while(*buffer)                  // Write data to LCD up to null
        {
                while(BusyXLCD());      // Wait while LCD is busy
                WriteDataXLCD(*buffer); // Write character to LCD
                buffer++;               // Increment buffer
        }
        return;
}

/********************************************************************
*       Function Name:  putrsXLCD
*       Return Value:   void
*       Parameters:     buffer: pointer to string
*       Description:    This routine writes a string of bytes to the
*                       Hitachi HD44780 LCD controller. The user
*                       must check to see if the LCD controller is
*                       busy before calling this routine. The data
*                       is written to the character generator RAM or
*                       the display data RAM depending on what the
*                       previous SetxxRamAddr routine was called.
********************************************************************/
void putrsXLCD(const  char *buffer)
{
        while(*buffer)                  // Write data to LCD up to null
        {
                while(BusyXLCD());      // Wait while LCD is busy
                WriteDataXLCD(*buffer); // Write character to LCD
                buffer++;               // Increment buffer
        }
        return;
}

/********************************************************************
*       Function Name:  BusyXLCD                                    *
*       Return Value:   char: busy status of LCD controller         *
*       Parameters:     void                                        *
*       Description:    This routine reads the busy status of the   *
*                       Hitachi HD44780 LCD controller.             *
********************************************************************/
unsigned char BusyXLCD(void)
{
#ifdef BUSY_LCD
        RW_PIN = 1;                     // Set the control bits for read
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock in the command
        DelayFor18TCY();
#ifdef BIT8                             // 8-bit interface
        if(DATA_PORT&0x80)                      // Read bit 7 (busy bit)
        {                               // If high
                E_PIN = 0;              // Reset clock line
                RW_PIN = 0;             // Reset control line
                return 1;               // Return TRUE
        }
        else                            // Bit 7 low
        {
                E_PIN = 0;              // Reset clock line
                RW_PIN = 0;             // Reset control line
                return 0;               // Return FALSE
        }
#else                                   // 4-bit interface
#ifdef UPPER                            // Upper nibble interface
        if(DATA_PORT&0x80)
#else                                   // Lower nibble interface
        if(DATA_PORT&0x08)
#endif
        {
                E_PIN = 0;              // Reset clock line
                DelayFor18TCY();
                E_PIN = 1;              // Clock out other nibble
                DelayFor18TCY();
                E_PIN = 0;
                RW_PIN = 0;             // Reset control line
                return 1;               // Return TRUE
        }
        else                            // Busy bit is low
        {
                E_PIN = 0;              // Reset clock line
                DelayFor18TCY();
                E_PIN = 1;              // Clock out other nibble
                DelayFor18TCY();
                E_PIN = 0;
                RW_PIN = 0;             // Reset control line
                return 0;               // Return FALSE
        }
#endif
#else
        __delay_ms(5);
        return 0;
#endif

}

Ese codigo me da todo este churro de errores:
Código: [Seleccionar]
lcd_pic_16.c:127: warning: function declared implicit int
lcd_pic_16.c:132: warning: function declared implicit int
lcd_pic_16.c:174: error: undefined identifier "PORTB"
lcd_pic_16.c:175: error: undefined identifier "TRISB"
lcd_pic_16.c:181: error: undefined identifier "TRISBbits"
lcd_pic_16.c:181: error: struct/union required
lcd_pic_16.c:182: error: struct/union required
lcd_pic_16.c:183: error: struct/union required
lcd_pic_16.c:184: error: undefined identifier "PORTBbits"
lcd_pic_16.c:184: error: struct/union required
lcd_pic_16.c:185: error: struct/union required
lcd_pic_16.c:186: error: struct/union required
lcd_pic_16.c:260: error: undefined identifier "TRISB"
lcd_pic_16.c:261: error: undefined identifier "PORTB"
lcd_pic_16.c:268: error: undefined identifier "PORTBbits"
lcd_pic_16.c:268: error: struct/union required
lcd_pic_16.c:269: error: struct/union required
lcd_pic_16.c:271: error: struct/union required
lcd_pic_16.c:273: error: struct/union required
lcd_pic_16.c:282: error: struct/union required
lcd_pic_16.c:284: error: struct/union required
lcd_pic_16.c:318: error: undefined identifier "TRISB"
lcd_pic_16.c:319: error: undefined identifier "PORTB"
lcd_pic_16.c:319: advisory: too many errors (21)

Desconectado gab163

  • PIC16
  • ***
  • Mensajes: 111
Re: Mini curso "Programación en XC8"
« Respuesta #87 en: 21 de Junio de 2013, 13:49:06 »
Que versión tienes de xc8? Limbo

Desconectado Rseliman

  • PIC16
  • ***
  • Mensajes: 239
Re: Mini curso "Programación en XC8"
« Respuesta #88 en: 21 de Junio de 2013, 14:00:46 »
Limbo :

Fijate que yo pase el zippara que lo bajes y habras directamente con mplabx ...y tambien para la simulacion con proteus ...

Saludos
Las Grandes Obras las sueñan los grandes locos , mientras los inutiles las critican !!

Desconectado Limbo

  • PIC10
  • *
  • Mensajes: 44
Re: Mini curso "Programación en XC8"
« Respuesta #89 en: 21 de Junio de 2013, 14:09:45 »
Citar
Que versión tienes de xc8? Limbo)
xc8 v1.12

Citar
Fijate que yo pase el zippara que lo bajes y habras directamente con mplabx ...y tambien para la simulacion con proteus ...
Ya ya, tus archivos no me compilan tampoco.. por eso creo que es algo del compilador pero no logro descubrir que es..


 

anything