CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

[RESOLVED]pic18f4550 and usart

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

[RESOLVED]pic18f4550 and usart
PostPosted: Sun Jul 23, 2017 10:38 am     Reply with quote

Hi,

I want to use usart with my pic that is connected to a tactile keypad with serial.
I connected TX from my keypad to PIN_C7, but CCS not recognize all register TXSTA, RCSTA, SPGRG, RCIF, ...


Example:

Code:

void UART_Init(unsigned int32 v_baudRate_u32)
{   
    set_tris_c(0x80);      // Configure Rx pin as input and Tx as output 
    TXSTA=0x20;      // Asynchronous mode, 8-bit data & enable transmitter
    RCSTA=0x90;      // 8-bit continous receive enable
    UART_SetBaudRate(v_baudRate_u32);
}


The compiler say, undefined identifier TXSTA and undefined identifier RCSTA.

My main.h:
Code:

#include <18F4550.h>
#device ADC=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES VREGEN                   //USB voltage regulator enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTC                   //Configuration registers not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads

#use delay(crystal=16000000)
#use rs232(baud=9600,PARITY=N,XMIT=PIN_C6,RCV=PIN_C7,bits=8,STOP=1)



file uart.h

Code:

/***************************************************************************************************
                                   ExploreEmbedded   
****************************************************************************************************
 * File:   uart.h
 * Version: 15.0
 * Author: ExploreEmbedded
 * Website: http://www.exploreembedded.com/wiki
 * Description: Contains the baudrate configurations and function prototypes for UART routines

The libraries have been tested on ExploreEmbedded development boards. We strongly believe that the
library works on any of development boards for respective controllers. However, ExploreEmbedded
disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly.
Files may be subject to change without prior notice. The revision history contains the information
related to updates.


GNU GENERAL PUBLIC LICENSE:
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Errors and omissions should be reported to codelibraries@exploreembedded.com
 **************************************************************************************************/



/***************************************************************************************************
                             Revision History
****************************************************************************************************
15.0: Initial version
***************************************************************************************************/
#ifndef _UART_H
#define _UART_H


#include "stdutils.h"

/***************************************************************************************************
                             Baudrate configurations
***************************************************************************************************/
#define C_MinBaudRate_U32 2400
#define C_MaxBaudRate_U32 115200UL
#define F_CPU 16000000UL
#define M_GetBaudRateGeneratorValue(baudrate)  (unsigned int32)((F_CPU / (64*(unsigned int32)baudrate)) - 1)
/**************************************************************************************************/





/***************************************************************************************************
                      PreCompile configurations to enable/disable the functions
****************************************************************************************************
PreCompile configuration to enable or disable the API's.
 1.Required interfaces can be enabled/disabled by configuring
   its respective macros to 1/0.
 2. By default all the API's are enabled except for FLOAT transmission.
 3. Transmitting of floating number takes huge controller
    resources and need to be enabled only if required.
   This implies for other interfaces as well.
***************************************************************************************************/
#define    Enable_UART_TxString          0
#define    Enable_UART_RxString          1
#define    Enable_UART_TxNumber          1
#define    Enable_UART_TxFloatNumber     0
#define    Enable_UART_Printf            0
/**************************************************************************************************/





/***************************************************************************************************
                             Commonly used UART macros/Constants
***************************************************************************************************/
#define C_DefaultDigitsToTransmit_U8          0xffu    // Will transmit the exact digits in the number
#define C_MaxDigitsToTransmit_U8              10u      // Max decimal/hexadecimal digits to be transmitted
#define C_NumOfBinDigitsToTransmit_U8         16u      // Max bits of a binary number to be transmitted
#define C_MaxDigitsToTransmitUsingPrintf_U8   C_DefaultDigitsToTransmit_U8 /* Max dec/hexadecimal digits to be displayed using printf */
/**************************************************************************************************/





/***************************************************************************************************
                             Function Prototypes
***************************************************************************************************/

void UART_Init(unsigned int32 v_baudRate_u32);
void UART_SetBaudRate(unsigned int32 v_baudRate_u32);
void UART_TxChar(char v_uartData_u8);
char UART_RxChar(void);
void UART_TxString(char *ptr_string);
uint8_t UART_RxString(char *ptr_string);
void UART_TxNumber(unsigned int8 v_numericSystem_u8, unsigned int32 v_number_u32, unsigned int8 v_numOfDigitsToTransmit_u8);
void UART_TxFloatNumber(float v_floatNumber_f32);
void UART_Printf(char *argList, ...);
/**************************************************************************************************/


#endif



file uart.c

Code:

/***************************************************************************************************
                                   ExploreEmbedded   
****************************************************************************************************
 * File:   uart.c (AVR controllers)
 * Version: 15.0
 * Author: ExploreEmbedded
 * Website: http://www.exploreembedded.com/wiki
 * Description: File contains the Library routines for UART

The libraries have been tested on ExploreEmbedded development boards. We strongly believe that the
library works on any of development boards for respective controllers. However, ExploreEmbedded
disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly.
Files may be subject to change without prior notice. The revision history contains the information
related to updates.


GNU GENERAL PUBLIC LICENSE:
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Errors and omissions should be reported to codelibraries@exploreembedded.com
***************************************************************************************************/


/**************************************************************************************************
                             Revision History
****************************************************************************************************
15.0: Initial version
16.0: Updated the Tx Number function to support bin,dec,hex in one function.
***************************************************************************************************/
#include <stdarg.h>
#include "uart.h"

/*#pragma warning push
#pragma warning disable 752   // Suppress warnings related to size of variables(conversion to shorter data type).
#pragma warning disable 1496  // Suppress pointer related warnings
#pragma warning disable 520   // Suppress warning for un used/called functions.
#pragma warning disable 1498  // Handling string as arrays and vice versa
#pragma warning disable 356   // Suppress warning for float to integer conversion
#pragma warning disable 359   // Suppress warning for Illegal Pointer conversion during string handling*/


/***************************************************************************************************
                         void UART_Init(uint32_t v_baudRate_u32)
****************************************************************************************************
 * I/P Arguments: uint32_t : Baudrate to be configured.
 * Return value    : none

 * description  :This function is used to initialize the UART at specified baud rate.
                 If the requested baud rate is not within the supported range then
                 the default baud rate of 9600 is set.


            Refer uart.h file for Supported(range) baud rates.       
***************************************************************************************************/
void UART_Init(unsigned int32 v_baudRate_u32)
{   
    set_tris_c(0x80);      // Configure Rx pin as input and Tx as output 
    TXSTA=0x20;      // Asynchronous mode, 8-bit data & enable transmitter
    RCSTA=0x90;      // 8-bit continous receive enable
    UART_SetBaudRate(v_baudRate_u32);
}






/***************************************************************************************************
                         void UART_SetBaudRate(uint32_t v_baudRate_u32)
 ***************************************************************************************************
 * I/P Arguments: uint32_t : v_baudRate_u32 to be configured.
 * Return value    : none

 * description  :This function is used to Set/Change the baudrate on the fly.
                 If the requested baud rate is not within the supported range then
                 the default baudrate of 9600 is set.

            Refer uart.h file for Supported range of baud rates.
***************************************************************************************************/
void UART_SetBaudRate(unsigned int32 v_baudRate_u32)
{
    unsigned int8 RegValue;

    if((v_baudRate_u32 >= C_MinBaudRate_U32) && (v_baudRate_u32<=C_MaxBaudRate_U32))
    {
        /* Check if the requested baudate is within range,
         If yes then calculate the value to be loaded into baud rate generator. */
        RegValue = (unsigned int8)M_GetBaudRateGeneratorValue(v_baudRate_u32);
    }
    else
    {
        /*     Invalid baudrate requested, hence set it to default baudrate of 9600 */
        RegValue = (unsigned int8)M_GetBaudRateGeneratorValue(9600);
    }

    SPBRG = RegValue;
}





/***************************************************************************************************
                              char UART_RxChar()
 ***************************************************************************************************
 * I/P Arguments: none.
 * Return value    : char: Ascii value of the character received

 * description :This function is used to receive a char from UART module.
                It waits till a char is received and returns it after reception.
***************************************************************************************************/
char UART_RxChar()
{
    while(RCIF==0);    // Wait till the data is received
    //RCIF=0;            // Clear receiver flag
    return(RCREG);     // Return the received data to calling function
}








/***************************************************************************************************
                         void UART_TxChar(char v_uartData_u8)
****************************************************************************************************
 * I/P Arguments: char--> Ascii value of the character to be transmitted.
 * Return value    : none.

 * description  :This function is used to transmit a char through UART module.
***************************************************************************************************/
void UART_TxChar(char v_uartData_u8)
{
    while(TXIF==0);       // Wait till the transmitter register becomes empty
    //TXIF=0;               // Clear transmitter flag
    TXREG=v_uartData_u8;  // load the char to be transmitted into transmit reg
}







/***************************************************************************************************
                         void UART_TxString(char *ptr_string)
****************************************************************************************************
 * I/P Arguments: String(Address of the string) to be transmitted.
 * Return value    : none

 * description :This function is used to transmit a NULL terminated string through UART.
               1.The ptr_string points to the first char of the string
                    and traverses till the end(NULL CHAR) and transmits a char each time
***************************************************************************************************/
#if ((Enable_UART_TxString==1)|| (Enable_UART_Printf == 1))
void UART_TxString(int8 *ptr_string)
{
    while(*ptr_string)
        UART_TxChar(*ptr_string++);// Loop through the string and transmit char by char
}
#endif






/***************************************************************************************************
                         uint8_t UART_RxString(char *ptr_string)
****************************************************************************************************
 * I/P Arguments: char *:  Address of the string/buffer where the received data needs to be stored
 * Return value    : uint8_t: Number of chars received.

 * description  :
              1.This function is used to receive a ASCII string through UART till the carriage_return/New_line
              2.The stream of data is copied to the buffer till carriage_return/New_line is encountered.
              3. Once the Carriage_return/New_Line is received the string will be null terminated.

******NOTE*******:
  1.The received char is ECHOED back,
    if not required then comment UART_TxChar(ch) in the code.
  2.BackSlash is not taken care.
***************************************************************************************************/
#if (Enable_UART_RxString==1)
uint8_t UART_RxString(char *ptr_string)
{
    char ch;
    uint8_t len = 0;
    while(1)
    {
        ch=UART_RxChar();    //Receive a char
        UART_TxChar(ch);     //Echo back the received char

        if((ch=='\r') || (ch=='\n')) //read till enter key is pressed
        {                             //once enter key is pressed null terminate the string
            ptr_string[len]=0;           //and break the loop
            break;                 
        }
        else if((ch=='\b') && (len!=0))
        {
            len--;    //If backspace is pressed then decrement the index to remove the old char
        }
        else
        {
            ptr_string[len]=ch; //copy the char into string and increment the index
            len++;
        }
    }
  return len;   
}
#endif






/***************************************************************************************************
void UART_TxNumber(uint8_t v_numericSystem_u8, uint32_t v_number_u32, uint8_t v_numOfDigitsToTransmit_u8)
****************************************************************************************************
 * I/P Arguments:
                  uint8_t :  specifies type of number C_BINARY_U8(2),C_DECIMAL_U8(10), C_HEX_U8(16)
                  uint32_t: Number to be transmitted on UART.
                  uint8_t : Number of digits to be transmitted

 * Return value    : none

 * description  :This function is used to transmit a max of 10digit decimal number.
                2nd parameter specifies the number of digits from the right side to be transmitted
                 The output for the input combinations is as below

    Binary:     1.(10,4) then 4-LSB will be transmitted ie. 1010
                2.(10,8) then 8-LSB will be transmitted ie. 00001010
                3.(10,2) then 2-LSB will be transmitted ie. 10     

    Decimal           
                4.(12345,4) then 4-digits ie. 2345 will be transmitted
                5.(12345,6) then 6-digits ie. 012345 will be transmitted
                6.(12345,C_DefaultDigitsToTransmit_U8) then 12345 will be transmitted.

    Hex:
                7.(0x12AB,3) then 3-digits ie. 2AB will be transmitted
                8.(0x12AB,6) then 6-digits ie. 0012AB will be transmitted
                9.(0x12AB,C_DefaultDigitsToTransmit_U8) then 12AB will be transmitted.   
****************************************************************************************************/
#if ((Enable_UART_TxNumber==1) || (Enable_UART_TxFloatNumber==1) || (Enable_UART_Printf == 1))
void UART_TxNumber(unsigned int8 v_numericSystem_u8, unsigned int32 v_number_u32, unsigned int8 v_numOfDigitsToTransmit_u8)
{
    unsigned int8 i=0,a[10];

    if(C_BINARY_U8 == v_numericSystem_u8)
    {
        while(v_numOfDigitsToTransmit_u8!=0)
        {
            /* Start Extracting the bits from the specified bit positions.
             Get the Acsii values of the bits and transmit */
            i = util_GetBitStatus(v_number_u32,(v_numOfDigitsToTransmit_u8-1));
            UART_TxChar(util_Dec2Ascii(i));
            v_numOfDigitsToTransmit_u8--;
        }   
    }     
    else if(v_number_u32==0)
    {
        /* If the number is zero then Transmit Specified number of zeros*/
        /*TODO: trsnamit single zero or multiple, Currently single zero is transmitted*/
       // for(i=0;((i<v_numOfDigitsToTransmit_u8) && (i<C_MaxDigitsToTransmit_U8)) ;i++)
            UART_TxChar('0');
    }
    else
    {
        for(i=0;i<v_numOfDigitsToTransmit_u8;i++)
        {
            /* Continue extracting the digits from right side
               till the Specified v_numOfDigitsToTransmit_u8 */
            if(v_number_u32!=0)
            {
                /* Extract the digits from the number till it becomes zero.
                First get the remainder and divide the number by 10 each time.

                Decimal number:
                If v_number_u32 = 123 then extracted remainder will be 3 and number will be 12.
                The process continues till it becomes zero or max digits reached*/
                a[i]=util_GetMod32(v_number_u32,v_numericSystem_u8);
                v_number_u32=v_number_u32/v_numericSystem_u8;
            }
            else if( (v_numOfDigitsToTransmit_u8 == C_DefaultDigitsToTransmit_U8) ||
                    (v_numOfDigitsToTransmit_u8 > C_MaxDigitsToTransmit_U8))
            {
                /* Stop the iteration if the Max number of digits are reached or
                 the user expects exact(Default) digits in the number to be transmitted */
                break;
            }
            else
            {
                /*In case user expects more digits to be transmitted than the actual digits in number,
                  then update the remaining digits with zero.
                Ex: v_number_u32 is 123 and user wants five digits then 00123 has to be transmitted */
                a[i]=0;
            }
        }

        while(i)
        {
            /* Finally get the ascii values of the digits and transmit*/
            UART_TxChar(util_Hex2Ascii(a[i-1]));
            i--;
        }
    }


}
#endif









/***************************************************************************************************
            void  UART_TxFloatNumber(float v_floatNumber_f32)
****************************************************************************************************
 * Function name:  UART_TxFloatNumber()
 * I/P Arguments: float: float Number to be transmitted on UART.

 * Return value    : none

 * description  :This function is used to transmit a floating point number

 * Note         :It supports 6digits of precision. 
         Float will be disabled by default as it takes huge controller resources
         It can be enabled by changing value of Enable_UART_TxFloatNumber to 1 in uart.h     
****************************************************************************************************/
#if (Enable_UART_TxFloatNumber==1)
void UART_TxFloatNumber(float v_floatNumber_f32)
{
    uint32_t v_decNumber_u32;
    /* Dirty hack to support the floating point by extracting the integer and fractional part.
      1.Type cast the number to int to get the integer part.
      2.transmit the extracted integer part followed by a decimal point(.).
      3.Later the integer part is made zero by subtracting with the extracted integer value.
      4.Finally the fractional part is multiplied by 100000 to support 6-digit precision */

    v_decNumber_u32 = (uint32_t) v_floatNumber_f32;
    UART_TxNumber(C_DECIMAL_U8,v_decNumber_u32,C_DefaultDigitsToTransmit_U8);

    UART_TxChar('.');

    v_floatNumber_f32 = v_floatNumber_f32 - v_decNumber_u32;
    v_decNumber_u32 = v_floatNumber_f32 * 1000000;
    UART_TxNumber(C_DECIMAL_U8,v_decNumber_u32,C_DefaultDigitsToTransmit_U8);
}
#endif






/***************************************************************************************************
            void UART_Printf(const char *argList, ...)
****************************************************************************************************
 * Function name:  UART_Printf()
 * I/P Arguments: variable length arguments similar to printf

 * Return value    : none

 * description  :This function is similar to printf function in C.
                 It takes the arguments with specified format and prints accordingly
                 The supported format specifiers are as below.
                 1. %c: character
                 2. %d: signed 16-bit number
                 3. %D: signed 32-bit number
                 4. %u: unsigned 16-bit number
                 5. %U: unsigned 32-bit number
                 6. %b: 16-bit binary number
                 7. %B: 32-bit binary number
                 8. %f: Float number
                 9. %x: 16-bit hexadecimal number
                 10. %X: 32-bit hexadecimal number
                 11. %s: String

  Note: By default all the functions will be disabled. The required functions can be enabled by
        setting the respective compiler switch to 1 in uart.h file.
        Ex:  setting Enable_UART_TxDecimalNumber to 1 will enable %d
             setting Enable_UART_TxHexNumber to 1 will enable %x

  Extra feature is available to specify the number of digits to be transmitted using printf.
     ex: %4d: will transmit the lower four digits of the decimal number.
         %12b: will transmit the 12-LSB of the number
         %d: Will transmit the exact digits of the number
         
#####: In case of printing the variables(8-bit) its recommended to type cast and promote them to uint16_t.
        uint8_t v_Num_u8;
        UART_Printf("num1:%u",(uint16_t)v_Num_u8);         
***************************************************************************************************/
#if ( Enable_UART_Printf   == 1 )
void UART_Printf(const char *argList, ...)
{
    const char *ptr;
    va_list argp;
    sint16_t v_num_s16;
    sint32_t v_num_s32;
    uint16_t v_num_u16;
    uint32_t v_num_u32;
    char *str;
    char  ch;
    uint8_t v_numOfDigitsToTransmit_u8;
#if (Enable_UART_TxFloatNumber==1)
    double v_floatNum_f32;
#endif   


    va_start(argp, argList);

    /* Loop through the list to extract all the input arguments */
    for(ptr = argList; *ptr != '\0'; ptr++)
    {

        ch= *ptr;
        if(ch == '%')         /*Check for '%' as there will be format specifier after it */
        {
            ptr++;
            ch = *ptr;
           if((ch>=0x30) && (ch<=0x39))
            {
               v_numOfDigitsToTransmit_u8 = 0;
               while((ch>=0x30) && (ch<=0x39))
                {
                   v_numOfDigitsToTransmit_u8 = (v_numOfDigitsToTransmit_u8 * 10) + (ch-0x30);
                   ptr++;
                   ch = *ptr;
                }
            }
            else
            {
              v_numOfDigitsToTransmit_u8 = C_MaxDigitsToTransmitUsingPrintf_U8;
            }               


            switch(ch)       /* Decode the type of the argument */
            {
            case 'C':
            case 'c':     /* Argument type is of char, hence read char data from the argp */
                ch = va_arg(argp, uint8_t);
                UART_TxChar(ch);
                break;

            case 'd':    /* Argument type is of signed integer, hence read 16bit data from the argp */
                v_num_s16 = va_arg(argp, sint16_t);
                if(v_num_s16<0)
                 { /* If the number is -ve then display the 2's complement along with '-' sign */
                   v_num_s16 = -v_num_s16;
                   UART_TxChar('-');
                 }
                UART_TxNumber(C_DECIMAL_U8,v_num_s16,v_numOfDigitsToTransmit_u8);
                break;
               
            case 'D':    /* Argument type is of integer, hence read 16bit data from the argp */
                v_num_s32 = va_arg(argp, sint32_t);               
                if(v_num_s32<0)
                 { /* If the number is -ve then display the 2's complement along with '-' sign */
                   v_num_s32 = -v_num_s32;
                   UART_TxChar('-');
                 }
                UART_TxNumber(C_DECIMAL_U8,v_num_s32,v_numOfDigitsToTransmit_u8);           
                break;   

            case 'u':    /* Argument type is of unsigned integer, hence read 16bit unsigned data */
                v_num_u16 = va_arg(argp, uint16_t);           
                UART_TxNumber(C_DECIMAL_U8,v_num_u16,v_numOfDigitsToTransmit_u8);               
                break;
           
            case 'U':    /* Argument type is of integer, hence read 32bit unsigend data */
                v_num_u32 = va_arg(argp, uint32_t);           
                UART_TxNumber(C_DECIMAL_U8,v_num_u32,v_numOfDigitsToTransmit_u8);               
                break;           

            case 'x':  /* Argument type is of hex, hence hexadecimal data from the argp */
                v_num_u16 = va_arg(argp, uint16_t);               
                UART_TxNumber(C_HEX_U8,v_num_u16,v_numOfDigitsToTransmit_u8);           
                break;

            case 'X':  /* Argument type is of hex, hence hexadecimal data from the argp */
                v_num_u32 = va_arg(argp, uint32_t);                       
                UART_TxNumber(C_HEX_U8,v_num_u32,v_numOfDigitsToTransmit_u8);               
                break;

           
            case 'b':  /* Argument type is of binary,Read int and convert to binary */
                v_num_u16 = va_arg(argp, uint16_t);       
               
                if(v_numOfDigitsToTransmit_u8 == C_MaxDigitsToTransmitUsingPrintf_U8)
                   v_numOfDigitsToTransmit_u8 = 16;
               
                UART_TxNumber(C_BINARY_U8,v_num_u16,v_numOfDigitsToTransmit_u8);           
                break;

            case 'B':  /* Argument type is of binary,Read int and convert to binary */
                v_num_u32 = va_arg(argp, uint32_t);           
               
                if(v_numOfDigitsToTransmit_u8 == C_MaxDigitsToTransmitUsingPrintf_U8)
                   v_numOfDigitsToTransmit_u8 = 32;       
               
                UART_TxNumber(C_BINARY_U8,v_num_u32,v_numOfDigitsToTransmit_u8);               
                break;


            case 'F':
            case 'f': /* Argument type is of float, hence read double data from the argp */
#if (Enable_UART_TxFloatNumber==1)               
                v_floatNum_f32 = va_arg(argp, double);               
                UART_TxFloatNumber(v_floatNum_f32);
#endif           
                break;               


            case 'S':
            case 's': /* Argument type is of string, hence get the pointer to sting passed */
                str = va_arg(argp, char *);
                UART_TxString(str);           
                break;

            case '%':
                UART_TxChar('%');
                break;
            }
        }
        else
        {
            /* As '%' is not detected transmit the char passed */
            UART_TxChar(ch);
        }
    }

    va_end(argp);
}
#endif


//#pragma warning pop


this code is from explorer embedded

i use only PIN_C6 and PIN_C7, the PIN_C7 is connecte to pin TX from my module ET_TOUCHPAD_4x4.

i have tested this code but it don't work, my crystal is 16Mhz.

Code:

void main ()
{
   
   char i;

   glcd_init_basic();
   
   while(1){
      if(kbhit())
      {                              // If data has been received
         i = getchar();                             // UART read
       
         glcd_data(i);                                // Send it back
         
     }
  }   
 
}


Any idea why register is not recognized in this IDE CCS ?

Thanks for your response.


Last edited by andromeda92 on Sun Jul 23, 2017 4:33 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Jul 23, 2017 11:37 am     Reply with quote

You are already setting all these registers.
That is what #use rs232 does.

In CCS, you almost never need to touch the registers yourself, since the compiler can do all this for you.

This line:

#use rs232(baud=9600,PARITY=N,XMIT=PIN_C6,RCV=PIN_C7,bits=8,STOP=1)

sets TXSTA, RCSTA, the TRIS bits for the TX and RX bits, the baud rate generator for 9600bps etc..

Your function to set the baud rate, already exists as 'set_uart_speed', which will change the BRG setting whenever you want.

There are ways of accessing the registers, but you should use the CCS functions first.

Getc, and putc, already handle receiving and sending a character. You already have a printf. Everything you post, only needs about half a dozen lines of CCS code....
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PostPosted: Sun Jul 23, 2017 12:09 pm     Reply with quote

I have added these lines and errors disappear
but it don't work.
Code:

//****** define  address *****//
#byte TXSTA    =     0x98
#byte RCSTA    =   0x18
#byte SPBRG    =   0x99
#byte PIE1     =   0x8c
#byte INTCON   =   0x8b
#byte TXREG    =   0x19
#byte RCREG    =   0x1A
#byte PIR1     =   0x0c
#BIT  RCIF      =  PIR1.5
#BIT  RCIE      =  PIE1.5
#BIT  TXIF      =  PIR1.4

I don't know if value for pic18f4550 is correct because i have take this code on google.

But you say that i don't need uart.h and uart.c ?

I have deleted uart.c and uart.h for my project.
If i use the code below, it don't work, nothing is received.
Code:

void main ()
{   
   set_uart_speed(9600);
   char i; 
   glcd_init_basic();
   
   while(1){
                             
         i = getc();                           // UART read     
         glcd_data(i);                                // Send it back to glcd
       
  }   
 
}


why PIN_C7 receive nothing ?

thanks for your response
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Jul 23, 2017 12:52 pm     Reply with quote

Seriously, don't add the lines.

Use the compiler.

Everything you have there is already supplied as part of the compiler.

What you are trying to use is a UART library for a compiler that does not include UART handling, while CCS already has this handling built in.

A huge amount of pointless work.

OK.

Now:
Code:

void main ()
{   
   char i; //Initialisations should be at the start of C sections

   set_uart_speed(9600);
   glcd_init_basic();
   
   while(1)
   {
                             
         if (kbhit())
         {
             i = getc();                           // UART read     
             glcd_data(i);                        // Send it back to glcd
         }       
    }   
}


Should basically work, but you need to start by proving that first the glcd routine works. So test this by sending a string to it. Get this working first.

Have you verified your CPU is working?.
Your clock settings are not complete:

#clock(CRYSTAL=16MHz, CLOCK=16MHz)

Since the chip has a PLL, you need to either specify the two figures here to ensure it is disabled, or manually set the fuses for this.

Then is the data from your keyboard in ASCII?.

Like all debugging, you need to do one thing at a time.

You should then work out a way of testing that the serial is receiving. For instance flash an LED when a character is received.
If it isn't, you have a hardware problem. Do you have a link to details of the keyboard?.
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PostPosted: Sun Jul 23, 2017 1:40 pm     Reply with quote

hi,

The cpu is working.
The routines from glcd (st7920) work very well, isn't that.

The keyboard is
http://www.gravitech.us/4x4topadcte.html

When i type in this keyboard, this keyboard must send me <0x50><ASCII CODE><0D> on TX pin from keyboard.
You are right, one of the problems has been resolved due to clock.

I changing to
Code:

#use delay(CRYSTAL=16MHz, CLOCK=16MHz)

and now when i connect usb rs232 cable to my dev board, with terminal, when i type in my keyboard character, it appears on my glcd. That is ok now from rs232 but not from PIN_C7, but when i type on my tactile keyboard, nothing works, maybe its a problem for this keyboard. However this keyboard works with Arduino or avr. I am not sure that the problem is the keyboard.

I have tested with this code below, with keyboard connected to rs232 from my dev board. It works but when i type on tactile keyboard nothing is received from it.

Code:

#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;


#int_rda
void serial_isr() {
   int t;

   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}

void main ()
{
   enable_interrupts(int_rda);
   #if defined(__PCD__)
   enable_interrupts(intr_global);
   #else
   enable_interrupts(global);
   #endif
     
   glcd_init_basic();
   
   while(1)
   {
      while(bkbhit)           
         glcd_data(bgetc());       
     
  }   
 
}

Maybe is a problem with #fuses.

Thanks you for your help.
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PostPosted: Sun Jul 23, 2017 2:10 pm     Reply with quote

I think the problem is from my keyboard tactile. I have connected GND, TX, VCC to an USB TTL, and the terminal receives nothing, the keyboard is out, I had not used it for a while.
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PostPosted: Sun Jul 23, 2017 4:33 pm     Reply with quote

I confirm my keypad tactile is out. I have tested with a lcd backpack serial from Sparkfun. I can send information, the backpack works.

Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Jul 24, 2017 1:01 am     Reply with quote

Well done.

You see the point about debugging. Test one thing at a time.

The LCD was working because it obviously had flexible enough timings to not worry that the CPU was running at the wrong rate. The UART though requires the clock settings to be right....
Then you had a separate problem with the keypad. Ouch. Sad

You can see possibly though why people like CCS. Look at just how small the code is compared to the library you were trying to use!.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group