Autor Tema: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A  (Leído 4167 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado yamilongiano

  • PIC10
  • *
  • Mensajes: 37
Hola a todos sigo estudiando y aprendiendo lenguaje C para microcontroladores.

ahora estoy en un segundo ejercicio que me gustaria que me ayudaran a comprender, desde ya muchas gracias. acontinuacion imagenes y codigo.

primero la imagen del codigo.





https://imagizer.imageshack.us/v2/541x589q90/r/633/eG15ey.png




aqui el codigo escrito para ver si quizas tengo algun error, y , mostrar algunos errores del libro tambien como por ejemplo "res" que en algunas parte pone "re":

Código: [Seleccionar]

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "usart.h"
#define XTAL_FREQ_ 4000000
__CONFIG(FOSC_XT & WDTE_OFF & CP_OFF & PWRTE_ON & CP_OFF & CPD_OFF & BOREN_ON);

void main(void)
{
    int opcion, valor, res;
    char buff[80];                                                  
    init_commns();                                      // tampoco comprendo esta instruccion.
    printf("introduzca un valor distinto de 0:\n");
    printf("\n\n");
    gets(buf);                                          // supuestamente esta es para tomar un dato desde el hyperterminal ¿lo almacena en buf?.
    valor=atoi(buf);                                    // aqui lo copia a valor. pero no entiendo el comando "atoi(buf)"
    printf("***menu de opciones***\n\n");
    printf("1.- averigua el cuadrado:\n");
    printf("2.- averigua el cubo:\n");
    printf("1.- averigua si es par o no:\n");
    printf("\nIngrese opcion\n");
    printf("\n\n");
    gets(buf);
    opcion=atoi(buf);                     // aqui lo copia a opcion. pero no entiendo el comando "atoi(buf)"

    switch(opcion)
            {
        case 1:
            res = valor*valor;
            printf("el cuadrado de %¡ es %¡\n", valor, res);
            break;

        case 2:
            res= valor*valor*valor;
            printf("el cubo de %¡ es %¡\n", valor, res);
            break;

        case 3:                                             // SE QUE % ES PARA ALMACENAR SOLO ENTEROS,PERO,
            res = valor % 2;                           //SI ES ENTERO SE ALMACENA EN RES Y SI NO LO ES? QUE VALOR TOMA...
            if(res)                                         // ... COMO PREGUNTA LA VARIABLE AQUI, NO COMPRENDO(CON QUE LO COMPARA)?
                printf("el numero %¡ es impar\n", valor);
            else
                printf("el numero %¡ es par\n", valor);
            break;
        default:
            printf( "opcion erronea");
    }
 }



bueno ahora el circuito como lo tengo en el proteus (isis)






 me tiene bien complicado el case 3 de las opciones, ya que no comprendo como se compara y se guarda el valor en la instruccion IF.


los errores estan presentes y se ven con mas claridad en la imagen. pero los escribo a continucion.

error 1:
 // <editor-fold defaultstate="collapsed" desc="comment">
    init_commns(); // </editor-fold>


error 2:
    // <editor-fold defaultstate="collapsed" desc="comment">
    gets(buf);  // </editor-fold>

error 3:

    // <editor-fold defaultstate="collapsed" desc="comment">
    valor = atoi(buf); // </editor-fold>

eso serian  los errores el cual no me deja compilar el programa.

output:

Código: [Seleccionar]

CLEAN SUCCESSFUL (total time: 102ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
make  -f nbproject/Makefile-default.mk dist/default/production/proyecto_2_menu_de_opciones.X.production.hex
make[2]: Entering directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
"C:\Program Files\Microchip\xc8\v1.32\bin\xc8.exe" --pass1  --chip=16F877A -Q -G  --double=24 --float=24 --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,+osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"    -o"build/default/production/menu de opciones.p1"  "menu de opciones.c"
(908) exit status = 1
menu de opciones.c:12: error: (141) can't open include file "usart.h": No such file or directory
make[2]: *** [build/default/production/menu de opciones.p1] Error 1
make[1]: *** [.build-conf] Error 2
nbproject/Makefile-default.mk:91: recipe for target 'build/default/production/menu de opciones.p1' failed
make: *** [.build-impl] Error 2
make[2]: Leaving directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
nbproject/Makefile-default.mk:75: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed

BUILD FAILED (exit value 2, total time: 457ms)



espero alguien se de el tiempo para poder compilar los programas y seguir aprendiendo. muchas gracias.




« Última modificación: 01 de Agosto de 2014, 01:41:46 por yamilongiano »

Desconectado RALF2

  • Moderadores
  • PIC24H
  • *****
  • Mensajes: 2060
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #1 en: 01 de Agosto de 2014, 10:43:52 »
Hola yamilongiano
No conozco ese compiador pero si algo de C   :mrgreen:

Citar
error 1:
 // <editor-fold defaultstate="collapsed" desc="comment">
    init_commns(); // </editor-fold>


error 2:
    // <editor-fold defaultstate="collapsed" desc="comment">
    gets(buf);  // </editor-fold>

error 3:

    // <editor-fold defaultstate="collapsed" desc="comment">
    valor = atoi(buf); // </editor-fold>

No veo donde declaras el tipo de pic que estas utilizando, supongo que por default toma ese modelo pero abria que ver   :?
gets(buf),  en una instruccion para guardar varios caracteres ascii en un array (buf).
atoi = ascii to integer, convierte un caracter ascii a su equivalente entero.
int_commns() es un llamado a una funcion como indicas arriba, deberias ver que funcion hace abriendo el include usart.h.

Espero haberte aclarado algunos detalles.   :mrgreen:

Saludos

Desconectado thegame

  • PIC18
  • ****
  • Mensajes: 439
    • Mcu Tronics
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #2 en: 01 de Agosto de 2014, 16:15:15 »
Que tal, para mi lo primero que debes hacer es resolver este error:

Código: [Seleccionar]
error: (141) can't open include file "usart.h": No such file or directory
Que basicamente es, que no esta encontrando el archivo "usart.h", asegurate que se encuentra en el mismo directorio donde tienes tu codigo fuente.
Nunca se deja de aprender

Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #3 en: 01 de Agosto de 2014, 19:47:40 »
Hola, para resolver el error del usart.h debes especificar donde encontrar la libreria:
Código: C
  1. #include <plib/usart.h> //Libreria para el manejo del modulo USART
El error del buf es porque en la linea 19 te sobra una f:
Código: C
  1. char buff[80];
Código: C
  1. init_commns();
aqui te sobra la n

Saludos
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.

Desconectado yamilongiano

  • PIC10
  • *
  • Mensajes: 37
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #4 en: 03 de Agosto de 2014, 00:28:03 »
hola a todos , por lo que tengo entendido al poner el #include <usart.h>  el xc8 deberia incorporarlo automaticamente.

lo intentente de las 2 maneras

#include <usart.h>

y

#include "usart.h"

cual seria la manera correcta con las " ".

estava leyendo en el libro y estos archivos de cabecera (.h) se encuentran en

archivo de programas/microchip/xc8/1.0/include/plib. y efectivamente el archivo se encuentra ahi (usart.h)....


pero la version mia es la 1.32

el codigo es el siguiente.

Código: [Seleccionar]
#ifndef __USART_H
#define __USART_H
/******************************************************************************
 // *                  USART PERIPHERAL LIBRARY HEADER FILE
 ******************************************************************************
 * FileName:        usart.h
 * Dependencies:    See include below
 * Processor:        PIC18
 * Compiler:        MCC18
 * Company:          Microchip Technology, Inc.
 *
 * Software License Agreement
 * The software supplied herewith by Microchip Technology Incorporated
 * (the “Company”) for its PICmicro® Microcontroller is intended and
 * supplied to you, the Company’s customer, for use solely and
 * exclusively on Microchip PICmicro Microcontroller products. The
 * software is owned by the Company and/or its supplier, and is
 * protected under applicable copyright laws. All rights are reserved.
 * Any use in violation of the foregoing restrictions may subject the
 * user to criminal sanctions under applicable laws, as well as to
 * civil liability for the breach of the terms and conditions of this
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *****************************************************************************/
 
#include <pconfig.h>


/* PIC18 USART peripheral libraries. */

/* There are three library modules, corresponding to register names:
 *  USART1  (TXSTA1, RCSTA1, etc.)
 *  USART2  (TXSTA2, RCSTA2, etc.)
 *  USART  (TXSTA, RCSTA, etc.)
 *  Each module is defined only for those devices for which the register
 *  names are defined.
 */

 /* Corresponding to each module, there are several routines:
  *
  * The 'open' routine takes two parameters:
  *   - 'config' is the bitwise 'and' of the appropriate configuration
  *     bit masks (defined below);
  *   - 'spbrg' is the baud rate.
  * The registers associated with the USART module are set according to these
  * parameters; then, the transmitter and receiver are enabled.
  *
  * The 'datardy' routine returns 1 if data has been received, 0 otherwise.
  *
  * The 'read' routine returns the received byte.  It also sets the framing
  * and overrun error status bits (FRAME_ERROR & OVERRUN_ERROR) if necessary;
  * also, the status receive bit 8 (RX_NINE) is significant if 9-bit mode
  * is enabled.
  * (See status bit structure definition below).
  *
  * The 'write' routine accepts the byte to transmit.  If 9-bit mode is
  * enabled, the status trasmit bit 8 (TX_NINE) is also trasmitted.
  *
  * The 'gets' routine accepts a buffer and the buffer length in bytes as
  * parameters.  It fills the buffer with bytes as they are received; it will
  * wait for data if necessary in order to fill the entire buffer.
  *
  * The 'puts' routine accepts a null-terminated byte string.  All bytes
  * are transmitted, including the null character.  It will wait until the
  * USART is not busy in order to transmit all the bytes.
  *
  * The 'putrs' routine is identical to 'puts', except the byte string
  * resides in ROM.
  *
  * The 'close' routine disables the receiver, the transmitter, and the
  * interrupts for both.
  *
  * The 'busy' routine returns 1 if the transmit shift register is not empty;
  * otherwise, it returns 0.
  *
  * For devices with enhanced USART capability, an additional 'baud'
  * routine is provided.  This routine takes a 'config' parameter, which
  * is a bitwise 'and' of the baud configuration bit masks (see below).
  * The BAUDCON (a.k.a. BAUDCTL) register is configured appropriately.
  */

/* Change this to near if building small memory model versions of
 * the libraries. */
#define MEM_MODEL

/* Configuration bit masks to be 'anded' together and passed as the 'config'
 * parameter to the 'open' routine. */
//-----------AND OR MASK-------------------------------------------------
#ifndef USE_OR_MASKS

#define USART_TX_INT_ON   0b11111111  // Transmit interrupt on
#define USART_TX_INT_OFF  0b01111111  // Transmit interrupt off
#define USART_RX_INT_ON   0b11111111  // Receive interrupt on
#define USART_RX_INT_OFF  0b10111111  // Receive interrupt off
#define USART_BRGH_HIGH   0b11111111  // High baud rate
#define USART_BRGH_LOW    0b11101111  // Low baud rate
#define USART_CONT_RX     0b11111111  // Continuous reception
#define USART_SINGLE_RX   0b11110111  // Single reception
#define USART_SYNC_MASTER 0b11111111  // Synchrounous master mode
#define USART_SYNC_SLAVE  0b11111011  // Synchrounous slave mode
#define USART_NINE_BIT    0b11111111  // 9-bit data
#define USART_EIGHT_BIT   0b11111101  // 8-bit data
#define USART_SYNCH_MODE  0b11111111  // Synchronous mode
#define USART_ASYNCH_MODE 0b11111110  // Asynchronous mode
#define USART_ADDEN_ON    0b11111111  // Enables address detection
#define USART_ADDEN_OFF   0b11011111  // Disables address detection


//------------AND MASK------------------------------------------------
#else
#define USART_TX_INT_ON    0b10000000  // Transmit interrupt on
#define USART_TX_INT_OFF  0b00000000  // Transmit interrupt off
#define USART_TX_INT_MASK (~USART_TX_INT_ON) //Mask Trnasmit Interrupt select bit

#define USART_RX_INT_ON    0b01000000  // Receive interrupt on
#define USART_RX_INT_OFF  0b00000000  // Receive interrupt off
#define USART_RX_INT_MASK (~USART_RX_INT_ON) //Mask Receive Interrupt select bit

#define USART_ADDEN_ON    0b00100000  // Enables address detection
#define USART_ADDEN_OFF    0b00000000  // Disables address detection
#define USART_ADDEN_MASK (~USART_ADDEN_ON) //Mask address detection select bit

#define USART_BRGH_HIGH    0b00010000  // High baud rate
#define USART_BRGH_LOW    0b00000000  // Low baud rate
#define USART_BRGH_MASK (~USART_BRGH_HIGH) //Mask baud rate select bit

#define USART_CONT_RX      0b00001000  // Continuous reception
#define USART_SINGLE_RX    0b00000000  // Single reception
#define USART_CONT_RX_MASK (~USART_CONT_RX) //Mask Continuous Reception select bit

#define USART_SYNC_MASTER 0b00000100  // Synchrounous master mode
#define USART_SYNC_SLAVE  0b00000000  // Synchrounous slave mode
#define USART_SYNC_MASK (~USART_SYNC_MASTER) //Mask usart mode select bit

#define USART_NINE_BIT    0b00000010  // 9-bit data
#define USART_EIGHT_BIT    0b00000000  // 8-bit data
#define USART_BIT_MASK (~USART_NINE_BIT) //Mask 9 bit transmit  select bit

#define USART_SYNCH_MODE  0b00000001  // Synchronous mode
#define USART_ASYNCH_MODE 0b00000000  // Asynchronous mode
#define USART_MODE_MASK (~USART_SYNCH_MODE) //Mask sync/async mode select bit

#endif

/* These devices have enhanced USARTs. */
#if defined (EAUSART_V3 ) ||defined (EAUSART_V4 ) ||defined (EAUSART_V5 )||\
    defined (EAUSART_V6 ) ||defined (EAUSART_V7 ) ||defined (EAUSART_V8 ) ||\
defined (EAUSART_V9 ) ||defined (EAUSART_V10 ) || defined (EAUSART_V11) \
|| defined (EAUSART_V11_1) || defined (EAUSART_V12)

/* The baud configuration bit masks to be 'anded' together and passed to
 * the 'baud' routine. */
//--------------AND OR MASK------------------------------
#ifndef USE_OR_MASKS
#define BAUD_IDLE_CLK_HIGH  0b11111111  // idle state for clock is a high level
#define BAUD_IDLE_CLK_LOW   0b11101111  // idle state for clock is a low level
#define BAUD_16_BIT_RATE    0b11111111  // 16-bit baud generation rate
#define BAUD_8_BIT_RATE     0b11110111  // 8-bit baud generation rate
#define BAUD_WAKEUP_ON      0b11111111  // RX pin monitored
#define BAUD_WAKEUP_OFF     0b11111101  // RX pin not monitored
#define BAUD_AUTO_ON        0b11111111  // auto baud rate measurement enabled
#define BAUD_AUTO_OFF       0b11111110  // auto baud rate measurement disabled
//-----------------AND MASK-------------------------------
#else
#define BAUD_IDLE_CLK_HIGH  0b00010000  // idle state for clock is a high level
#define BAUD_IDLE_CLK_LOW   0b00000000  // idle state for clock is a low level
#define BAUD_IDLE_CLK_MASK (~BAUD_IDLE_CLK_HIGH) //Mask Idle Clock State bit

#define BAUD_16_BIT_RATE    0b00001000  // 16-bit baud generation rate
#define BAUD_8_BIT_RATE     0b00000000  // 8-bit baud generation rate
#define BAUD_BIT_RATE       (~BAUD_16_BIT_RATE) //Mask 16 bit baud rate select bit

#define BAUD_WAKEUP_ON      0b00000010  // RX pin monitored
#define BAUD_WAKEUP_OFF     0b00000000  // RX pin not monitored
#define BAUD_WAKEUP_MASK (~BAUD_WAKEUP_ON) //Mask Wake-up Enable bit

#define BAUD_AUTO_ON        0b00000001  // auto baud rate measurement enabled
#define BAUD_AUTO_OFF       0b00000000  // auto baud rate measurement disabled
#define BAUD_AUTO_MASK (~BAUD_AUTO_ON) //Mask Auto-Baud Detect Enable bit

#endif

#endif


/* Only these devices have two usart modules: USART1 & USART2. */
#if defined (AUSART_V2) ||defined (EAUSART_V6) ||defined (EAUSART_V7) ||\
    defined (EAUSART_V8) ||defined (EAUSART_V9)  || defined (EAUSART_V10) ||\
defined (EAUSART_V11) || defined (EAUSART_V11_1) || defined (EAUSART_V12)

/* ***** USART1 ***** */

/* status bits */
union USART1
{
  unsigned char val;
  struct
  {
    unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
    unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
    unsigned FRAME_ERROR:1;     // Framing Error for usart
    unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
    unsigned fill:4;
  };
};
extern union USART1 USART1_Status;

void Open1USART ( unsigned char config,  unsigned int spbrg);

#if defined (EAUSART_V7 ) ||defined (EAUSART_V8 )
/***********************************************************************************
Macro       : DataRdy1USART()

Include     : usart.h

Description : Macro returns if data available in the read buffer
 
Arguments   : None
 
Returns     : Returns the status of Receive interrupt flag bit status
***********************************************************************************/
#define DataRdy1USART( ) (PIR1bits.RCIF)
#else
/***********************************************************************************
Macro       : DataRdy1USART()

Include     : usart.h

Description : Macro returns if data available in the read buffer
 
Arguments   : None
 
Returns     : Returns the status of Receive interrupt flag bit status
***********************************************************************************/
#define DataRdy1USART( ) (PIR1bits.RC1IF)
#endif

char Read1USART (void);
void Write1USART ( char data);
void gets1USART ( char *buffer,  unsigned char len);
void puts1USART ( char *data);
void putrs1USART ( const MEM_MODEL char *data);

/**************************************************************************
Macro       : getc1USART

Description : macro is identical to Read1USART, #define to Read1USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define getc1USART Read1USART

/**************************************************************************
Macro       : putc1USART

Description : macro is identical to Write1USART, #define to Write1USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define putc1USART Write1USART

/***********************************************************************************
Macro       : Close1USART()

Include     : usart.h

Description : Macro Disable the specified usart
 
Arguments   : None
 
Returns     : disables the interrupts, transmitter and receiver for the specified usart
***********************************************************************************/
#define Close1USART( ) RCSTA1&=0b01001111,TXSTA1bits.TXEN=0,PIE1&=0b11001111

/***********************************************************************************
Macro       : Busy1USART()

Include     : usart.h

Description : Macro returns if the usart transmitting or not
 
Arguments   : None
 
Returns     : Returns a value indicating if the usart transmitter is currently busy.
This Macro should be used prior to commencing a new transmission
***********************************************************************************/
#define Busy1USART( )  (!TXSTA1bits.TRMT)
#endif

/* ***** USART2 ***** */
#if defined (AUSART_V2 ) ||defined (EAUSART_V6 ) ||defined (EAUSART_V7 ) ||\
    defined (EAUSART_V8 ) ||defined (EAUSART_V9 ) || defined (EAUSART_V11) \
|| defined (EAUSART_V12)
/* status bits */
union USART2
{
  unsigned char val;
  struct
  {
    unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
    unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
    unsigned FRAME_ERROR:1;     // Framing Error for usart
    unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
    unsigned fill:4;
  };
};
extern union USART2 USART2_Status;
void Open2USART ( unsigned char config,  unsigned int spbrg);

/***********************************************************************************
Macro       : DataRdy2USART()

Include     : usart.h

Description : Macro returns if data available in the read buffer
 
Arguments   : None
 
Returns     : Returns the status of Receive interrupt flag bit status
***********************************************************************************/
#define DataRdy2USART( ) (PIR3bits.RC2IF)

char Read2USART (void);
void Write2USART ( char data);
void gets2USART ( char *buffer,  unsigned char len);
void puts2USART ( char *data);
void putrs2USART ( const MEM_MODEL char *data);

/**************************************************************************
Macro       : getc2USART

Description : macro is identical to Read2USART, #define to Read2USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define getc2USART Read2USART

/**************************************************************************
Macro       : putc2USART

Description : macro is identical to Write2USART, #define to Write2USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define putc2USART Write2USART

/***********************************************************************************
Macro       : Close2USART()

Include     : usart.h

Description : Macro Disable the specified usart
 
Arguments   : None
 
Returns     : disables the interrupts, transmitter and receiver for the specified usart
***********************************************************************************/
#define Close2USART( ) RCSTA2&=0b01001111,TXSTA2bits.TXEN=0,PIE3&=0b11001111

/***********************************************************************************
Macro       : Busy2USART()

Include     : usart.h

Description : Macro returns if the usart transmitting or not
 
Arguments   : None
 
Returns     : Returns a value indicating if the usart transmitter is currently busy.
This Macro should be used prior to commencing a new transmission
***********************************************************************************/
#define Busy2USART( ) (!TXSTA2bits.TRMT)

#endif

/* ***** Only this device has 4 USARTS ********* */
/* ***** USART3 ***** */
#if defined (EAUSART_V12)
/* status bits */
union USART3
{
  unsigned char val;
  struct
  {
    unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
    unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
    unsigned FRAME_ERROR:1;     // Framing Error for usart
    unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
    unsigned fill:4;
  };
};
extern union USART3 USART3_Status;
void Open3USART ( unsigned char config,  unsigned int spbrg);

/***********************************************************************************
Macro       : DataRdy3USART()

Include     : usart.h

Description : Macro returns if data available in the read buffer
 
Arguments   : None
 
Returns     : Returns the status of Receive interrupt flag bit status
***********************************************************************************/
#define DataRdy3USART( ) (PIR6bits.RC3IF)

char Read3USART (void);
void Write3USART ( char data);
void gets3USART ( char *buffer,  unsigned char len);
void puts3USART ( char *data);
void putrs3USART ( const MEM_MODEL char *data);

/**************************************************************************
Macro       : getc3USART

Description : macro is identical to Read3USART, #define to Read3USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define getc3USART Read3USART

/**************************************************************************
Macro       : putc3USART

Description : macro is identical to Write3USART, #define to Write3USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define putc3USART Write3USART

/***********************************************************************************
Macro       : Close3USART()

Include     : usart.h

Description : Macro Disable the specified usart
 
Arguments   : None
 
Returns     : disables the interrupts, transmitter and receiver for the specified usart
***********************************************************************************/
#define Close3USART( ) RCSTA3&=0b01001111,TXSTA3bits.TXEN=0,PIE6&=0b11001111

/***********************************************************************************
Macro       : Busy3USART()

Include     : usart.h

Description : Macro returns if the usart transmitting or not
 
Arguments   : None
 
Returns     : Returns a value indicating if the usart transmitter is currently busy.
This Macro should be used prior to commencing a new transmission
***********************************************************************************/
#define Busy3USART( ) (!TXSTA3bits.TRMT)

/* ***** USART4 ***** */
/* status bits */
union USART4
{
  unsigned char val;
  struct
  {
    unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
    unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
    unsigned FRAME_ERROR:1;     // Framing Error for usart
    unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
    unsigned fill:4;
  };
};
extern union USART4 USART4_Status;
void Open4USART ( unsigned char config,  unsigned int spbrg);

/***********************************************************************************
Macro       : DataRdy4USART()

Include     : usart.h

Description : Macro returns if data available in the read buffer
 
Arguments   : None
 
Returns     : Returns the status of Receive interrupt flag bit status
***********************************************************************************/
#define DataRdy4USART( ) (PIR6bits.RC4IF)

char Read4USART (void);
void Write4USART ( char data);
void gets4USART ( char *buffer,  unsigned char len);
void puts4USART ( char *data);
void putrs4USART ( const MEM_MODEL char *data);

/**************************************************************************
Macro       : getc4USART

Description : macro is identical to Read4USART, #define to Read4USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define getc4USART Read4USART

/**************************************************************************
Macro       : putc4USART

Description : macro is identical to Write4USART, #define to Write4USART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define putc4USART Write4USART

/***********************************************************************************
Macro       : Close4USART()

Include     : usart.h

Description : Macro Disable the specified usart
 
Arguments   : None
 
Returns     : disables the interrupts, transmitter and receiver for the specified usart
***********************************************************************************/
#define Close4USART( ) RCSTA4&=0b01001111,TXSTA4bits.TXEN=0,PIE6&=0b00111111

/***********************************************************************************
Macro       : Busy4USART()

Include     : usart.h

Description : Macro returns if the usart transmitting or not
 
Arguments   : None
 
Returns     : Returns a value indicating if the usart transmitter is currently busy.
This Macro should be used prior to commencing a new transmission
***********************************************************************************/
#define Busy4USART( ) (!TXSTA4bits.TRMT)

#endif

#if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) ||\
    defined (EAUSART_V5 )
/* ***** usart (TXSTA, RCSTA, etc.) ***** */
/* status bits */
union USART
{
  unsigned char val;
  struct
  {
    unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
    unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
    unsigned FRAME_ERROR:1;     // Framing Error for usart
    unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
    unsigned fill:4;
  };
};
extern union USART USART_Status;
void OpenUSART ( unsigned char config,  unsigned spbrg);

/***********************************************************************************
Macro       : DataRdyUSART()

Include     : usart.h

Description : Macro returns if data available in the read buffer
 
Arguments   : None
 
Returns     : Returns the status of Receive interrupt flag bit status
***********************************************************************************/
#define DataRdyUSART( ) (PIR1bits.RCIF)

char ReadUSART (void);
void WriteUSART ( char data);
void getsUSART ( char *buffer,  unsigned char len);
void putsUSART ( char *data);
void putrsUSART ( const MEM_MODEL char *data);

/**************************************************************************
Macro       : getcUSART

Description : macro is identical to ReadUSART, #define to ReadUSART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define getcUSART ReadUSART

/**************************************************************************
Macro       : putcUSART

Description : macro is identical to WriteUSART, #define to WriteUSART in usart.h
 
Arguments   : None
 
Remarks     : None
***************************************************************************/
#define putcUSART WriteUSART

/***********************************************************************************
Macro       : CloseUSART()

Include     : usart.h

Description : Macro Disable the specified usart
 
Arguments   : None
 
Returns     : disables the interrupts, transmitter and receiver for the specified usart
***********************************************************************************/
#define CloseUSART( ) RCSTA&=0b01001111,TXSTAbits.TXEN=0,PIE1&=0b11001111

/***********************************************************************************
Macro       : BusyUSART()

Include     : usart.h

Description : Macro returns if the usart transmitting or not
 
Arguments   : None
 
Returns     : Returns a value indicating if the usart transmitter is currently busy.
This Macro should be used prior to commencing a new transmission
***********************************************************************************/
#define BusyUSART( ) (!TXSTAbits.TRMT)

#endif

#if defined (EAUSART_V4 ) ||defined (EAUSART_V3 ) ||defined (EAUSART_V5 )
void baudUSART ( unsigned char baudconfig);
#endif

#if defined (EAUSART_V7) ||defined (EAUSART_V8) ||defined (EAUSART_V9) ||\
    defined (EAUSART_V11) || defined (EAUSART_V12) || defined (EAUSART_V11_1) ||\
defined (EAUSART_V6 )  || defined (EAUSART_V10)
void baud1USART ( unsigned char baudconfig);
#endif

#if defined (EAUSART_V7) ||defined (EAUSART_V8) ||defined (EAUSART_V9) ||\
    defined (EAUSART_V11) || defined (EAUSART_V12)
void baud2USART ( unsigned char baudconfig);
#endif

#if defined (EAUSART_V12)
void baud3USART ( unsigned char baudconfig);
void baud4USART ( unsigned char baudconfig);
#endif


/*Macros for backward compatibility*/
#ifndef USE_OR_MASKS

#define BAUD_IDLE_RX_PIN_STATE_HIGH    0b11011111  // idle state for RX pin is high level
#define BAUD_IDLE_RX_PIN_STATE_LOW     0b11111111  // idle state for RX pin is low level
#define BAUD_IDLE_TX_PIN_STATE_HIGH    0b11101111  // idle state for TX pin is high level
#define BAUD_IDLE_TX_PIN_STATE_LOW     0b11111111  // idle state for TX pin is low level

#else
#define BAUD_IDLE_RX_PIN_STATE_HIGH    0b00000000  // idle state for RX pin is high level
#define BAUD_IDLE_RX_PIN_STATE_LOW     0b00100000  // idle state for RX pin is low level
#define BAUD_IDLE_TX_PIN_STATE_HIGH    0b00000000  // idle state for TX pin is high level
#define BAUD_IDLE_TX_PIN_STATE_LOW     0b00010000  // idle state for TX pin is low level
#define BAUD_IDLE_TX_RX_PIN_STATE_MASK (~0b00110000) //Mask idle tx rx pin state select bit
#endif

#if defined(USART_SFR_V1)
#define BAUDCON BAUDCTL
#endif

#endif


el error de buf[80] ya lo solucione.

aunque me sigue dando problemas la libreria usart.h y el gets(80)

a continuacion les pongo el codigo a ver si esta correcto.

ubicacion del archivo "C:\Program Files\Microchip\xc8\v1.32\include\plib\usart.h"

Código: [Seleccionar]
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "plib\usart.h" //gracias miguel pero aun no logro compilar.
#define XTAL_FREQ_ 4000000
__CONFIG(FOSC_XT & WDTE_OFF & CP_OFF & PWRTE_ON & CP_OFF & CPD_OFF & BOREN_ON);

void main(void)
{
    int opcion, valor, res;
    char buf[80];
  // <editor-fold defaultstate="collapsed" desc="comment">
    init_commns(); // </editor-fold>

    printf("introduzca un valor distinto de 0:\n");
    printf("\n\n");
    // <editor-fold defaultstate="collapsed" desc="comment">
    gets(buf); // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="comment">
    valor = atoi(buf); // </editor-fold>

    printf("***menu de opciones***\n\n");
    printf("1.- averigua el cuadrado:\n");
    printf("2.- averigua el cubo:\n");
    printf("1.- averigua si es par o no:\n");
    printf("\nIngrese opcion\n");
    printf("\n\n");
    gets(buf);
    opcion=atoi(buf);

    switch(opcion)
            {
        case 1:
            res = valor*valor;
            printf("el cuadrado de %¡ es %¡\n", valor, res);
            break;

        case 2:
            res= valor*valor*valor;
            printf("el cubo de %¡ es %¡\n", valor, res);
            break;

        case 3:               // SE QUE % ES PARA ALMACENAR SOLO ENTEROS,PERO,
            res = valor % 2;  //SI ES ENTERO SE ALMACENA EN RES Y SI NO LO ES? QUE VALOR TOMA...
            if(res)   // ... COMO PREGUNTA LA VARIABLE AQUI, NO COMPRENDO(CON QUE LO COMPARA)?
                printf("el numero %¡ es impar\n", valor);
            else
                printf("el numero %¡ es par\n", valor);
            break;
        default:
            printf( "opcion erronea");
    }
 }

output:

Código: [Seleccionar]
CLEAN SUCCESSFUL (total time: 52ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
make  -f nbproject/Makefile-default.mk dist/default/production/proyecto_2_menu_de_opciones.X.production.hex
make[2]: Entering directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
"C:\Program Files\Microchip\xc8\v1.32\bin\xc8.exe" --pass1  --chip=16F877A -Q -G  --double=24 --float=24 --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,+osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"    -o"build/default/production/menu de opciones.p1"  "menu de opciones.c"
C:\Program Files\Microchip\xc8\v1.32\include\plib\usart.h:32: error: (141) can't open include file "pconfig.h": No such file or directory
(908) exit status = 1
nbproject/Makefile-default.mk:91: recipe for target 'build/default/production/menu de opciones.p1' failed
make[2]: *** [build/default/production/menu de opciones.p1] Error 1
make[2]: Leaving directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
make[1]: *** [.build-conf] Error 2
nbproject/Makefile-default.mk:75: recipe for target '.build-conf' failed
make: *** [.build-impl] Error 2
make[1]: Leaving directory 'C:/Users/yamil/Desktop/proyectosMPLABX/proyecto 2 menu de opciones.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed

BUILD FAILED (exit value 2, total time: 454ms)

ahora dice que no encuentra el archivo pconfig.h tambien. tampoco comprendo por que pide este archivo.

Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #5 en: 03 de Agosto de 2014, 06:31:05 »
Hola de nuevo yamilongiano, no estoy seguro de lo que te contestare mejor que alguien mas experimentado que yo lo confirme. Lo que yo entiendo es que la libreria usart.h esta pensada para PIC18:
Código: C
  1. #ifndef __USART_H
  2. #define __USART_H
  3. /******************************************************************************
  4.  // *                  USART PERIPHERAL LIBRARY HEADER FILE
  5.  ******************************************************************************
  6.  * FileName:                    usart.h
  7.  * Dependencies:        See include below
  8.  * Processor:                   PIC18
  9.  * Compiler:                    MCC18
  10.  * Company:                     Microchip Technology, Inc.
  11.  *
  12.  * Software License Agreement
  13.  * The software supplied herewith by Microchip Technology Incorporated
  14.  * (the &#65533;Company&#65533;) for its PICmicro&#65533; Microcontroller is intended and
  15.  * supplied to you, the Company&#65533;s customer, for use solely and
  16.  * exclusively on Microchip PICmicro Microcontroller products. The
  17.  * software is owned by the Company and/or its supplier, and is
  18.  * protected under applicable copyright laws. All rights are reserved.
  19.  * Any use in violation of the foregoing restrictions may subject the
  20.  * user to criminal sanctions under applicable laws, as well as to
  21.  * civil liability for the breach of the terms and conditions of this
  22.  * license.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED IN AN &#65533;AS IS&#65533; CONDITION. NO WARRANTIES,
  25.  * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
  26.  * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27.  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
  28.  * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
  29.  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  30.  *****************************************************************************/
  31.  
  32. #include <pconfig.h>
  33.  
  34.  
  35. /* PIC18 USART peripheral libraries. */
  36.  
  37. /* There are three library modules, corresponding to register names:
  38.  *  USART1  (TXSTA1, RCSTA1, etc.)
  39.  *  USART2  (TXSTA2, RCSTA2, etc.)
  40.  *  USART  (TXSTA, RCSTA, etc.)
  41.  *  Each module is defined only for those devices for which the register
  42.  *  names are defined.
  43.  */
  44.  
  45.  /* Corresponding to each module, there are several routines:
  46.   *
  47.   * The 'open' routine takes two parameters:
  48.   *   - 'config' is the bitwise 'and' of the appropriate configuration
  49.   *     bit masks (defined below);
  50.   *   - 'spbrg' is the baud rate.
  51.   * The registers associated with the USART module are set according to these
  52.   * parameters; then, the transmitter and receiver are enabled.
  53.   *
  54.   * The 'datardy' routine returns 1 if data has been received, 0 otherwise.
  55.   *
  56.   * The 'read' routine returns the received byte.  It also sets the framing
  57.   * and overrun error status bits (FRAME_ERROR & OVERRUN_ERROR) if necessary;
  58.   * also, the status receive bit 8 (RX_NINE) is significant if 9-bit mode
  59.   * is enabled.
  60.   * (See status bit structure definition below).
  61.   *
  62.   * The 'write' routine accepts the byte to transmit.  If 9-bit mode is
  63.   * enabled, the status trasmit bit 8 (TX_NINE) is also trasmitted.
  64.   *
  65.   * The 'gets' routine accepts a buffer and the buffer length in bytes as
  66.   * parameters.  It fills the buffer with bytes as they are received; it will
  67.   * wait for data if necessary in order to fill the entire buffer.
  68.   *
  69.   * The 'puts' routine accepts a null-terminated byte string.  All bytes
  70.   * are transmitted, including the null character.  It will wait until the
  71.   * USART is not busy in order to transmit all the bytes.
  72.   *
  73.   * The 'putrs' routine is identical to 'puts', except the byte string
  74.   * resides in ROM.
  75.   *
  76.   * The 'close' routine disables the receiver, the transmitter, and the
  77.   * interrupts for both.
  78.   *
  79.   * The 'busy' routine returns 1 if the transmit shift register is not empty;
  80.   * otherwise, it returns 0.
  81.   *
  82.   * For devices with enhanced USART capability, an additional 'baud'
  83.   * routine is provided.  This routine takes a 'config' parameter, which
  84.   * is a bitwise 'and' of the baud configuration bit masks (see below).
  85.   * The BAUDCON (a.k.a. BAUDCTL) register is configured appropriately.
  86.   */
  87.  
  88. /* Change this to near if building small memory model versions of
  89.  * the libraries. */
  90. #define MEM_MODEL
  91.  
  92. /* Configuration bit masks to be 'anded' together and passed as the 'config'
  93.  * parameter to the 'open' routine. */
  94. //-----------AND OR MASK-------------------------------------------------
  95. #ifndef USE_OR_MASKS
  96.  
  97. #define USART_TX_INT_ON   0b11111111  // Transmit interrupt on
  98. #define USART_TX_INT_OFF  0b01111111  // Transmit interrupt off
  99. #define USART_RX_INT_ON   0b11111111  // Receive interrupt on
  100. #define USART_RX_INT_OFF  0b10111111  // Receive interrupt off
  101. #define USART_BRGH_HIGH   0b11111111  // High baud rate
  102. #define USART_BRGH_LOW    0b11101111  // Low baud rate
  103. #define USART_CONT_RX     0b11111111  // Continuous reception
  104. #define USART_SINGLE_RX   0b11110111  // Single reception
  105. #define USART_SYNC_MASTER 0b11111111  // Synchrounous master mode
  106. #define USART_SYNC_SLAVE  0b11111011  // Synchrounous slave mode
  107. #define USART_NINE_BIT    0b11111111  // 9-bit data
  108. #define USART_EIGHT_BIT   0b11111101  // 8-bit data
  109. #define USART_SYNCH_MODE  0b11111111  // Synchronous mode
  110. #define USART_ASYNCH_MODE 0b11111110  // Asynchronous mode
  111. #define USART_ADDEN_ON    0b11111111  // Enables address detection
  112. #define USART_ADDEN_OFF   0b11011111  // Disables address detection
  113.  
  114.  
  115. //------------AND MASK------------------------------------------------
  116. #else
  117. #define USART_TX_INT_ON                 0b10000000  // Transmit interrupt on
  118. #define USART_TX_INT_OFF                0b00000000  // Transmit interrupt off
  119. #define USART_TX_INT_MASK               (~USART_TX_INT_ON)      //Mask Trnasmit Interrupt select bit
  120.  
  121. #define USART_RX_INT_ON                 0b01000000  // Receive interrupt on
  122. #define USART_RX_INT_OFF                0b00000000  // Receive interrupt off
  123. #define USART_RX_INT_MASK               (~USART_RX_INT_ON)      //Mask Receive Interrupt select bit
  124.  
  125. #define USART_ADDEN_ON                  0b00100000  // Enables address detection
  126. #define USART_ADDEN_OFF                 0b00000000  // Disables address detection
  127. #define USART_ADDEN_MASK                (~USART_ADDEN_ON)       //Mask address detection select bit
  128.  
  129. #define USART_BRGH_HIGH                 0b00010000  // High baud rate
  130. #define USART_BRGH_LOW                  0b00000000  // Low baud rate
  131. #define USART_BRGH_MASK                 (~USART_BRGH_HIGH)      //Mask baud rate select bit
  132.  
  133. #define USART_CONT_RX                   0b00001000  // Continuous reception
  134. #define USART_SINGLE_RX                 0b00000000  // Single reception
  135. #define USART_CONT_RX_MASK              (~USART_CONT_RX)        //Mask Continuous Reception select bit
  136.  
  137. #define USART_SYNC_MASTER               0b00000100  // Synchrounous master mode
  138. #define USART_SYNC_SLAVE                0b00000000  // Synchrounous slave mode
  139. #define USART_SYNC_MASK                 (~USART_SYNC_MASTER)    //Mask usart mode select bit
  140.  
  141. #define USART_NINE_BIT                  0b00000010  // 9-bit data
  142. #define USART_EIGHT_BIT                 0b00000000  // 8-bit data
  143. #define USART_BIT_MASK                  (~USART_NINE_BIT)               //Mask 9 bit transmit  select bit
  144.  
  145. #define USART_SYNCH_MODE                0b00000001  // Synchronous mode
  146. #define USART_ASYNCH_MODE               0b00000000  // Asynchronous mode
  147. #define USART_MODE_MASK                 (~USART_SYNCH_MODE)     //Mask sync/async mode select bit
  148.  
  149. #endif
  150.  
  151. /* These devices have enhanced USARTs. */
  152. #if defined (EAUSART_V3 ) ||defined (EAUSART_V4 ) ||defined (EAUSART_V5 )||\
  153.     defined (EAUSART_V6 ) ||defined (EAUSART_V7 ) ||defined (EAUSART_V8 ) ||\
  154.         defined (EAUSART_V9 ) ||defined (EAUSART_V10 ) || defined (EAUSART_V11) \
  155.         || defined (EAUSART_V11_1) || defined (EAUSART_V12)
  156.  
  157. /* The baud configuration bit masks to be 'anded' together and passed to
  158.  * the 'baud' routine. */
  159. //--------------AND OR MASK------------------------------
  160. #ifndef USE_OR_MASKS
  161. #define BAUD_IDLE_CLK_HIGH  0b11111111  // idle state for clock is a high level
  162. #define BAUD_IDLE_CLK_LOW   0b11101111  // idle state for clock is a low level
  163. #define BAUD_16_BIT_RATE    0b11111111  // 16-bit baud generation rate
  164. #define BAUD_8_BIT_RATE     0b11110111  // 8-bit baud generation rate
  165. #define BAUD_WAKEUP_ON      0b11111111  // RX pin monitored
  166. #define BAUD_WAKEUP_OFF     0b11111101  // RX pin not monitored
  167. #define BAUD_AUTO_ON        0b11111111  // auto baud rate measurement enabled
  168. #define BAUD_AUTO_OFF       0b11111110  // auto baud rate measurement disabled
  169. //-----------------AND MASK-------------------------------
  170. #else
  171. #define BAUD_IDLE_CLK_HIGH  0b00010000  // idle state for clock is a high level
  172. #define BAUD_IDLE_CLK_LOW   0b00000000  // idle state for clock is a low level
  173. #define BAUD_IDLE_CLK_MASK      (~BAUD_IDLE_CLK_HIGH)   //Mask Idle Clock State bit
  174.  
  175. #define BAUD_16_BIT_RATE    0b00001000  // 16-bit baud generation rate
  176. #define BAUD_8_BIT_RATE     0b00000000  // 8-bit baud generation rate
  177. #define BAUD_BIT_RATE       (~BAUD_16_BIT_RATE)         //Mask 16 bit baud rate select bit
  178.  
  179. #define BAUD_WAKEUP_ON      0b00000010  // RX pin monitored
  180. #define BAUD_WAKEUP_OFF     0b00000000  // RX pin not monitored
  181. #define BAUD_WAKEUP_MASK        (~BAUD_WAKEUP_ON)               //Mask Wake-up Enable bit
  182.  
  183. #define BAUD_AUTO_ON        0b00000001  // auto baud rate measurement enabled
  184. #define BAUD_AUTO_OFF       0b00000000  // auto baud rate measurement disabled
  185. #define BAUD_AUTO_MASK          (~BAUD_AUTO_ON)                 //Mask Auto-Baud Detect Enable bit
  186.  
  187. #endif
  188.  
  189. #endif
  190.  
  191.  
  192. /* Only these devices have two usart modules: USART1 & USART2. */
  193. #if defined (AUSART_V2) ||defined (EAUSART_V6) ||defined (EAUSART_V7) ||\
  194.     defined (EAUSART_V8) ||defined (EAUSART_V9)  || defined (EAUSART_V10) ||\
  195.         defined (EAUSART_V11) || defined (EAUSART_V11_1) || defined (EAUSART_V12)
  196.  
  197. /* ***** USART1 ***** */
  198.  
  199. /* status bits */
  200. union USART1
  201. {
  202.   unsigned char val;
  203.   struct
  204.   {
  205.     unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
  206.     unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
  207.     unsigned FRAME_ERROR:1;     // Framing Error for usart
  208.     unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
  209.     unsigned fill:4;
  210.   };
  211. };
  212. extern union USART1 USART1_Status;
  213.  
  214. void Open1USART ( unsigned char config,  unsigned int spbrg);
  215.  
  216. #if defined (EAUSART_V7 ) ||defined (EAUSART_V8 )
  217. /***********************************************************************************
  218. Macro       : DataRdy1USART()
  219.  
  220. Include     : usart.h
  221.  
  222. Description : Macro returns if data available in the read buffer
  223.  
  224. Arguments   : None
  225.  
  226. Returns     : Returns the status of Receive interrupt flag bit status
  227. ***********************************************************************************/
  228. #define DataRdy1USART( ) (PIR1bits.RCIF)
  229. #else
  230. /***********************************************************************************
  231. Macro       : DataRdy1USART()
  232.  
  233. Include     : usart.h
  234.  
  235. Description : Macro returns if data available in the read buffer
  236.  
  237. Arguments   : None
  238.  
  239. Returns     : Returns the status of Receive interrupt flag bit status
  240. ***********************************************************************************/
  241. #define DataRdy1USART( ) (PIR1bits.RC1IF)
  242. #endif
  243.  
  244. char Read1USART (void);
  245. void Write1USART ( char data);
  246. void gets1USART ( char *buffer,  unsigned char len);
  247. void puts1USART ( char *data);
  248. void putrs1USART ( const MEM_MODEL char *data);
  249.  
  250. /**************************************************************************
  251. Macro       : getc1USART
  252.  
  253. Description : macro is identical to Read1USART, #define to Read1USART in usart.h
  254.  
  255. Arguments   : None
  256.  
  257. Remarks     : None
  258. ***************************************************************************/
  259. #define getc1USART Read1USART
  260.  
  261. /**************************************************************************
  262. Macro       : putc1USART
  263.  
  264. Description : macro is identical to Write1USART, #define to Write1USART in usart.h
  265.  
  266. Arguments   : None
  267.  
  268. Remarks     : None
  269. ***************************************************************************/
  270. #define putc1USART Write1USART
  271.  
  272. /***********************************************************************************
  273. Macro       : Close1USART()
  274.  
  275. Include     : usart.h
  276.  
  277. Description : Macro Disable the specified usart
  278.  
  279. Arguments   : None
  280.  
  281. Returns     : disables the interrupts, transmitter and receiver for the specified usart
  282. ***********************************************************************************/
  283. #define Close1USART( ) RCSTA1&=0b01001111,TXSTA1bits.TXEN=0,PIE1&=0b11001111
  284.  
  285. /***********************************************************************************
  286. Macro       : Busy1USART()
  287.  
  288. Include     : usart.h
  289.  
  290. Description : Macro returns if the usart transmitting or not
  291.  
  292. Arguments   : None
  293.  
  294. Returns     : Returns a value indicating if the usart transmitter is currently busy.
  295.                 This Macro should be used prior to commencing a new transmission
  296. ***********************************************************************************/
  297. #define Busy1USART( )  (!TXSTA1bits.TRMT)
  298. #endif
  299.  
  300. /* ***** USART2 ***** */
  301. #if defined (AUSART_V2 ) ||defined (EAUSART_V6 ) ||defined (EAUSART_V7 ) ||\
  302.     defined (EAUSART_V8 ) ||defined (EAUSART_V9 ) || defined (EAUSART_V11) \
  303.         || defined (EAUSART_V12)
  304. /* status bits */
  305. union USART2
  306. {
  307.   unsigned char val;
  308.   struct
  309.   {
  310.     unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
  311.     unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
  312.     unsigned FRAME_ERROR:1;     // Framing Error for usart
  313.     unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
  314.     unsigned fill:4;
  315.   };
  316. };
  317. extern union USART2 USART2_Status;
  318. void Open2USART ( unsigned char config,  unsigned int spbrg);
  319.  
  320. /***********************************************************************************
  321. Macro       : DataRdy2USART()
  322.  
  323. Include     : usart.h
  324.  
  325. Description : Macro returns if data available in the read buffer
  326.  
  327. Arguments   : None
  328.  
  329. Returns     : Returns the status of Receive interrupt flag bit status
  330. ***********************************************************************************/
  331. #define DataRdy2USART( ) (PIR3bits.RC2IF)
  332.  
  333. char Read2USART (void);
  334. void Write2USART ( char data);
  335. void gets2USART ( char *buffer,  unsigned char len);
  336. void puts2USART ( char *data);
  337. void putrs2USART ( const MEM_MODEL char *data);
  338.  
  339. /**************************************************************************
  340. Macro       : getc2USART
  341.  
  342. Description : macro is identical to Read2USART, #define to Read2USART in usart.h
  343.  
  344. Arguments   : None
  345.  
  346. Remarks     : None
  347. ***************************************************************************/
  348. #define getc2USART Read2USART
  349.  
  350. /**************************************************************************
  351. Macro       : putc2USART
  352.  
  353. Description : macro is identical to Write2USART, #define to Write2USART in usart.h
  354.  
  355. Arguments   : None
  356.  
  357. Remarks     : None
  358. ***************************************************************************/
  359. #define putc2USART Write2USART
  360.  
  361. /***********************************************************************************
  362. Macro       : Close2USART()
  363.  
  364. Include     : usart.h
  365.  
  366. Description : Macro Disable the specified usart
  367.  
  368. Arguments   : None
  369.  
  370. Returns     : disables the interrupts, transmitter and receiver for the specified usart
  371. ***********************************************************************************/
  372. #define Close2USART( ) RCSTA2&=0b01001111,TXSTA2bits.TXEN=0,PIE3&=0b11001111
  373.  
  374. /***********************************************************************************
  375. Macro       : Busy2USART()
  376.  
  377. Include     : usart.h
  378.  
  379. Description : Macro returns if the usart transmitting or not
  380.  
  381. Arguments   : None
  382.  
  383. Returns     : Returns a value indicating if the usart transmitter is currently busy.
  384.                 This Macro should be used prior to commencing a new transmission
  385. ***********************************************************************************/
  386. #define Busy2USART( ) (!TXSTA2bits.TRMT)
  387.  
  388. #endif
  389.  
  390. /* ***** Only this device has 4 USARTS ********* */
  391. /* ***** USART3 ***** */
  392. #if defined (EAUSART_V12)
  393. /* status bits */
  394. union USART3
  395. {
  396.   unsigned char val;
  397.   struct
  398.   {
  399.     unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
  400.     unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
  401.     unsigned FRAME_ERROR:1;     // Framing Error for usart
  402.     unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
  403.     unsigned fill:4;
  404.   };
  405. };
  406. extern union USART3 USART3_Status;
  407. void Open3USART ( unsigned char config,  unsigned int spbrg);
  408.  
  409. /***********************************************************************************
  410. Macro       : DataRdy3USART()
  411.  
  412. Include     : usart.h
  413.  
  414. Description : Macro returns if data available in the read buffer
  415.  
  416. Arguments   : None
  417.  
  418. Returns     : Returns the status of Receive interrupt flag bit status
  419. ***********************************************************************************/
  420. #define DataRdy3USART( ) (PIR6bits.RC3IF)
  421.  
  422. char Read3USART (void);
  423. void Write3USART ( char data);
  424. void gets3USART ( char *buffer,  unsigned char len);
  425. void puts3USART ( char *data);
  426. void putrs3USART ( const MEM_MODEL char *data);
  427.  
  428. /**************************************************************************
  429. Macro       : getc3USART
  430.  
  431. Description : macro is identical to Read3USART, #define to Read3USART in usart.h
  432.  
  433. Arguments   : None
  434.  
  435. Remarks     : None
  436. ***************************************************************************/
  437. #define getc3USART Read3USART
  438.  
  439. /**************************************************************************
  440. Macro       : putc3USART
  441.  
  442. Description : macro is identical to Write3USART, #define to Write3USART in usart.h
  443.  
  444. Arguments   : None
  445.  
  446. Remarks     : None
  447. ***************************************************************************/
  448. #define putc3USART Write3USART
  449.  
  450. /***********************************************************************************
  451. Macro       : Close3USART()
  452.  
  453. Include     : usart.h
  454.  
  455. Description : Macro Disable the specified usart
  456.  
  457. Arguments   : None
  458.  
  459. Returns     : disables the interrupts, transmitter and receiver for the specified usart
  460. ***********************************************************************************/
  461. #define Close3USART( ) RCSTA3&=0b01001111,TXSTA3bits.TXEN=0,PIE6&=0b11001111
  462.  
  463. /***********************************************************************************
  464. Macro       : Busy3USART()
  465.  
  466. Include     : usart.h
  467.  
  468. Description : Macro returns if the usart transmitting or not
  469.  
  470. Arguments   : None
  471.  
  472. Returns     : Returns a value indicating if the usart transmitter is currently busy.
  473.                 This Macro should be used prior to commencing a new transmission
  474. ***********************************************************************************/
  475. #define Busy3USART( ) (!TXSTA3bits.TRMT)
  476.  
  477. /* ***** USART4 ***** */
  478. /* status bits */
  479. union USART4
  480. {
  481.   unsigned char val;
  482.   struct
  483.   {
  484.     unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
  485.     unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
  486.     unsigned FRAME_ERROR:1;     // Framing Error for usart
  487.     unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
  488.     unsigned fill:4;
  489.   };
  490. };
  491. extern union USART4 USART4_Status;
  492. void Open4USART ( unsigned char config,  unsigned int spbrg);
  493.  
  494. /***********************************************************************************
  495. Macro       : DataRdy4USART()
  496.  
  497. Include     : usart.h
  498.  
  499. Description : Macro returns if data available in the read buffer
  500.  
  501. Arguments   : None
  502.  
  503. Returns     : Returns the status of Receive interrupt flag bit status
  504. ***********************************************************************************/
  505. #define DataRdy4USART( ) (PIR6bits.RC4IF)
  506.  
  507. char Read4USART (void);
  508. void Write4USART ( char data);
  509. void gets4USART ( char *buffer,  unsigned char len);
  510. void puts4USART ( char *data);
  511. void putrs4USART ( const MEM_MODEL char *data);
  512.  
  513. /**************************************************************************
  514. Macro       : getc4USART
  515.  
  516. Description : macro is identical to Read4USART, #define to Read4USART in usart.h
  517.  
  518. Arguments   : None
  519.  
  520. Remarks     : None
  521. ***************************************************************************/
  522. #define getc4USART Read4USART
  523.  
  524. /**************************************************************************
  525. Macro       : putc4USART
  526.  
  527. Description : macro is identical to Write4USART, #define to Write4USART in usart.h
  528.  
  529. Arguments   : None
  530.  
  531. Remarks     : None
  532. ***************************************************************************/
  533. #define putc4USART Write4USART
  534.  
  535. /***********************************************************************************
  536. Macro       : Close4USART()
  537.  
  538. Include     : usart.h
  539.  
  540. Description : Macro Disable the specified usart
  541.  
  542. Arguments   : None
  543.  
  544. Returns     : disables the interrupts, transmitter and receiver for the specified usart
  545. ***********************************************************************************/
  546. #define Close4USART( ) RCSTA4&=0b01001111,TXSTA4bits.TXEN=0,PIE6&=0b00111111
  547.  
  548. /***********************************************************************************
  549. Macro       : Busy4USART()
  550.  
  551. Include     : usart.h
  552.  
  553. Description : Macro returns if the usart transmitting or not
  554.  
  555. Arguments   : None
  556.  
  557. Returns     : Returns a value indicating if the usart transmitter is currently busy.
  558.                 This Macro should be used prior to commencing a new transmission
  559. ***********************************************************************************/
  560. #define Busy4USART( ) (!TXSTA4bits.TRMT)
  561.  
  562. #endif
  563.  
  564. #if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) ||\
  565.     defined (EAUSART_V5 )
  566. /* ***** usart (TXSTA, RCSTA, etc.) ***** */
  567. /* status bits */
  568. union USART
  569. {
  570.   unsigned char val;
  571.   struct
  572.   {
  573.     unsigned RX_NINE:1;         // Receive Bit 8 if 9-bit mode is enabled
  574.     unsigned TX_NINE:1;         // Transmit Bit 8 if 9-bit mode is enabled
  575.     unsigned FRAME_ERROR:1;     // Framing Error for usart
  576.     unsigned OVERRUN_ERROR:1;   // Overrun Error for usart
  577.     unsigned fill:4;
  578.   };
  579. };
  580. extern union USART USART_Status;
  581. void OpenUSART ( unsigned char config,  unsigned spbrg);
  582.  
  583. /***********************************************************************************
  584. Macro       : DataRdyUSART()
  585.  
  586. Include     : usart.h
  587.  
  588. Description : Macro returns if data available in the read buffer
  589.  
  590. Arguments   : None
  591.  
  592. Returns     : Returns the status of Receive interrupt flag bit status
  593. ***********************************************************************************/
  594. #define DataRdyUSART( ) (PIR1bits.RCIF)
  595.  
  596. char ReadUSART (void);
  597. void WriteUSART ( char data);
  598. void getsUSART ( char *buffer,  unsigned char len);
  599. void putsUSART ( char *data);
  600. void putrsUSART ( const MEM_MODEL char *data);
  601.  
  602. /**************************************************************************
  603. Macro       : getcUSART
  604.  
  605. Description : macro is identical to ReadUSART, #define to ReadUSART in usart.h
  606.  
  607. Arguments   : None
  608.  
  609. Remarks     : None
  610. ***************************************************************************/
  611. #define getcUSART ReadUSART
  612.  
  613. /**************************************************************************
  614. Macro       : putcUSART
  615.  
  616. Description : macro is identical to WriteUSART, #define to WriteUSART in usart.h
  617.  
  618. Arguments   : None
  619.  
  620. Remarks     : None
  621. ***************************************************************************/
  622. #define putcUSART WriteUSART
  623.  
  624. /***********************************************************************************
  625. Macro       : CloseUSART()
  626.  
  627. Include     : usart.h
  628.  
  629. Description : Macro Disable the specified usart
  630.  
  631. Arguments   : None
  632.  
  633. Returns     : disables the interrupts, transmitter and receiver for the specified usart
  634. ***********************************************************************************/
  635. #define CloseUSART( ) RCSTA&=0b01001111,TXSTAbits.TXEN=0,PIE1&=0b11001111
  636.  
  637. /***********************************************************************************
  638. Macro       : BusyUSART()
  639.  
  640. Include     : usart.h
  641.  
  642. Description : Macro returns if the usart transmitting or not
  643.  
  644. Arguments   : None
  645.  
  646. Returns     : Returns a value indicating if the usart transmitter is currently busy.
  647.                 This Macro should be used prior to commencing a new transmission
  648. ***********************************************************************************/
  649. #define BusyUSART( ) (!TXSTAbits.TRMT)
  650.  
  651. #endif
  652.  
  653. #if defined (EAUSART_V4 ) ||defined (EAUSART_V3 ) ||defined (EAUSART_V5 )
  654. void baudUSART ( unsigned char baudconfig);
  655. #endif
  656.  
  657. #if defined (EAUSART_V7) ||defined (EAUSART_V8) ||defined (EAUSART_V9) ||\
  658.     defined (EAUSART_V11) || defined (EAUSART_V12) || defined (EAUSART_V11_1) ||\
  659.         defined (EAUSART_V6 )  || defined (EAUSART_V10)
  660. void baud1USART ( unsigned char baudconfig);
  661. #endif
  662.  
  663. #if defined (EAUSART_V7) ||defined (EAUSART_V8) ||defined (EAUSART_V9) ||\
  664.     defined (EAUSART_V11) || defined (EAUSART_V12)
  665.         void baud2USART ( unsigned char baudconfig);
  666. #endif
  667.  
  668. #if defined (EAUSART_V12)
  669. void baud3USART ( unsigned char baudconfig);
  670. void baud4USART ( unsigned char baudconfig);
  671. #endif
  672.  
  673.  
  674. /*Macros for backward compatibility*/
  675. #ifndef USE_OR_MASKS
  676.        
  677. #define BAUD_IDLE_RX_PIN_STATE_HIGH    0b11011111  // idle state for RX pin is high level
  678. #define BAUD_IDLE_RX_PIN_STATE_LOW     0b11111111  // idle state for RX pin is low level
  679. #define BAUD_IDLE_TX_PIN_STATE_HIGH    0b11101111  // idle state for TX pin is high level
  680. #define BAUD_IDLE_TX_PIN_STATE_LOW     0b11111111  // idle state for TX pin is low level       
  681.        
  682. #else
  683. #define BAUD_IDLE_RX_PIN_STATE_HIGH    0b00000000  // idle state for RX pin is high level
  684. #define BAUD_IDLE_RX_PIN_STATE_LOW     0b00100000  // idle state for RX pin is low level
  685. #define BAUD_IDLE_TX_PIN_STATE_HIGH    0b00000000  // idle state for TX pin is high level
  686. #define BAUD_IDLE_TX_PIN_STATE_LOW     0b00010000  // idle state for TX pin is low level
  687. #define BAUD_IDLE_TX_RX_PIN_STATE_MASK (~0b00110000)    //Mask idle tx rx pin state select bit
  688. #endif
  689.  
  690. #if defined(USART_SFR_V1)
  691. #define BAUDCON BAUDCTL
  692. #endif
  693.  
  694. #endif

Código: C
  1. #ifndef __PCONFIG_H
  2. #define __PCONFIG_H
  3. /*-------------------------------------------------------------------------
  4. * PIC18 peripheral configuration file.
  5. * This file is auto generated by the Library Configuration tool.
  6. * DO NOT MODIFY unless you are absolutely sure of what you are doing.
  7. *
  8. * (c) Copyright 2007 Microchip Technology, All rights reserved
  9. *-------------------------------------------------------------------------*/
  10.  
  11.  
  12.  
  13. #ifndef _OMNI_CODE_
  14. #include <p18cxxx.h>
  15. #endif
De ser asi deberas de crearte la libreria.

Miquel_S
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.

Desconectado alev2477

  • PIC10
  • *
  • Mensajes: 21
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #6 en: 03 de Agosto de 2014, 08:00:24 »
Saludos.

Cuando el include se coloca en doble comillas es por que esta ubicado el archivo en el directorio local del proyecto . El libro lo emplea de esa manera, por lo que supongo es que ese archivo de cabecera lo creó el autor del libro.

Además de que es cierto plib/usart.h esta pensado para los 18. Pero no se sí sirve para los 16.

En las página anteriores no habla de esa cabecera¿¿
Al preguntar aprendo y al responder aprendo tambien

Desconectado yamilongiano

  • PIC10
  • *
  • Mensajes: 37
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #7 en: 03 de Agosto de 2014, 13:16:49 »
Saludos.

Cuando el include se coloca en doble comillas es por que esta ubicado el archivo en el directorio local del proyecto . El libro lo emplea de esa manera, por lo que supongo es que ese archivo de cabecera lo creó el autor del libro.

Además de que es cierto plib/usart.h esta pensado para los 18. Pero no se sí sirve para los 16.

En las página anteriores no habla de esa cabecera¿¿

La verdad no, no dice nada sobre el archivo, si es creado o no.

Mmmm pense que iva a ser mas facil aprender con este libro pero al parecer se esta poniendo color de hormiga. Podre encontrar un archivo de cabecera. O tendre que crear uno yo. No se por donde comenzar. Pero si voy a crear uno, mejor cambio el chip. A uno mas comun. ¿Como el pic16f628A ?

Voy a hechar una mirada al archivo...

Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #8 en: 03 de Agosto de 2014, 13:35:40 »
Hola yamilongiano si realmente la libreria solo sirve para los pic's 18 con el 16f628A tendras el mismo problema, yo no he creado ninguna pero creo que mirando otras no debe ser muy dificil.
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.

Desconectado rjrpaz

  • PIC10
  • *
  • Mensajes: 1
Re: 2do ejercicio libro xc8 dudas sobre codigo y coneccion. pic16f877A
« Respuesta #9 en: 07 de Septiembre de 2014, 02:58:19 »
Hola yamilongiano. Ante todo, debo decir que me crucé con este post, porque yo también estoy padeciendo el mismo libro (el cual viene acompañado por un DVD con las supuestas "clases" del mismo tema, pero que no tienen nada que ver). Estoy intentando hacer funcionar los ejemplos propuestos, y a pesar de que el libro indica que los mismos funcionan con la nueva familia de compiladores, lo cierto es que no, ya que utiliza muchas funciones que están "deprecated" o en desuso.

Luego de un rato de pasar por aquí lo pude hacer funcionar. La versión que funciona la puedes ver y/o bajar en mi repositorio en github:

https://github.com/rjrpaz/Microchip-Libro_MCElectronics_I/tree/master/Pag42.X

Este ejemplo está probado y funcionando con la placa de desarrollo que viene con el PicKit3, la cual contiene un PIC18F45K20. La comunicación la probé con una raspberry pi, porque no tenía MAX232 a mano (tampoco es que tenga puerto serie en la PC de todas formas).
.
Como comentario general, es correcto que se puedan utilizar las funciones clásicas del lenguaje C como printf() o gets(), pero hay que tener en cuenta algunas cosas. El "XC8 Compiler User's Guide" (pag. 352) indica que, cada vez que vas a utilizar la función "gets()", debes definir en tu propio código la función "getche()", ya que la primera va a utilizar esta última de manera interna. Asimismo, cada vez que vayas a utilizar la función "printf()", debes definir en tu propio código la función "putch()", por el mismo motivo (pag. 370).

En el ejemplo vas a ver como redefiní estas funciones (en el libro, estas funciones están redefinidas en los archivos "usart.c" y "usart.h" que detalla en un apéndice al final de ese capítulo). A medida que vaya logrando hacer funcionar los ejemplos de este libro los iré subiendo, pero no sé con que frecuencia lo haré, ya que no dispongo de mucho tiempo.

Saludos y suerte.
« Última modificación: 07 de Septiembre de 2014, 03:42:13 por rjrpaz »


 

anything