1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Serial comm between uC and PC...

Discussion in 'Microcontrollers' started by koolguy, Apr 26, 2013.

  1. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    I working on serial comm from uC to PC i am using this USB and this terminal program window 7....
    here is the showing error........


    Code (text):
    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
    #define NINE 0     /* Use 9bit communication? FALSE=8bit */

    #define BAUD 9600  

        #define RX_PIN TRISB2
        #define TX_PIN TRISB5
    #define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -1))
    #if NINE == 1
    #define NINE_BITS 0x40
    #else
    #define NINE_BITS 0
    #endif
    #if HIGH_SPEED == 1
    #define SPEED 0x4
    #else
    #define SPEED 0
    #endif
     Initialize ();







    void main ()
    {
        Initialize ()
    {  
        while(1)
        {
            putchar('H');   //print the letter 'H'
            putchar('i');   //print 'i'
            putchar('\r');  // return to beginning of line
            putchar('\n');  //new line
        }
    }    
    }



    Initialize ()
    {
        RX_PIN = 1;    
        TX_PIN = 1;          
        SPBRG = DIVIDER;        
        RCSTA = (NINE_BITS|0x90);
        TXSTA = (SPEED|NINE_BITS|0x20);
    }


    Make: The target "C:\Users\Abc\Documents\mplab\UART.p1" is out of date.
    Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 C:\Users\Abc\Documents\mplab\UART.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
    Error [312] C:\Users\Abc\Documents\mplab\UART.c; 32.1 ";" expected
    Warning [361] C:\Users\Abc\Documents\mplab\UART.c; 36.1 function declared implicit int
    Error [285] C:\Users\Abc\Documents\mplab\UART.c; 40.1 no identifier in declaration
    Warning [374] C:\Users\Abc\Documents\mplab\UART.c; 40.1 missing basic type; int assumed
    Error [314] C:\Users\Abc\Documents\mplab\UART.c; 40.1 ";" expected
    Error [285] C:\Users\Abc\Documents\mplab\UART.c; 41.1 no identifier in declaration
    Warning [374] C:\Users\Abc\Documents\mplab\UART.c; 41.1 missing basic type; int assumed
    Error [314] C:\Users\Abc\Documents\mplab\UART.c; 41.1 ";" expected

    ********** Build failed! **********
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Code (text):

    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
    #define NINE 0     /* Use 9bit communication? FALSE=8bit */
     
    #define BAUD 9600  
     
        #define RX_PIN TRISB2
        #define TX_PIN TRISB5
    #define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -1))
    #if NINE == 1
    #define NINE_BITS 0x40
    #else
    #define NINE_BITS 0
    #endif
    #if HIGH_SPEED == 1
    #define SPEED 0x4
    #else
    #define SPEED 0
    #endif
    void Initialize (void);  <----- YOU FORGOT THE "void's"
     
     
     
     
    void main ()
    {
        Initialize ();  <---- YOU FORGOT THE";"
    {  
        while(1)
        {
            putchar('H');   //print the letter 'H'
            putchar('i');   //print 'i'
            putchar('\r');  // return to beginning of line
            putchar('\n');  //new line
        }
    }    
    }
     
     
     
    void Initialize ()  <----- YOU FORGOT THE "void"
    {
        RX_PIN = 1;    
        TX_PIN = 1;          
        SPBRG = DIVIDER;        
        RCSTA = (NINE_BITS|0x90);
        TXSTA = (SPEED|NINE_BITS|0x20);
    }
    You might need to provide the putchar routine... I'll check and get back to you...


    Yep!!! I thought so.... putch() and getch() are empty functions.... You need to place the code into them... If you go to the Hitech directory you'll find them in "sources" you need to modify them so they output and input to the serial port
     
    Last edited: Apr 27, 2013
  3. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I have modified the code so, i want to ask one thing in datasheet pin 25 and 26 where Rx and Tx but here portb is used, why?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    again showing error.........

    Error [1098] C:\Program Files (x86)\HI-TECH Software\PICC\9.83\sources\putchar.c; 16. conflicting declarations for variable "_putchar" (C:\Users\Abc\Documents\mplab\main.c:31)
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Have you re-declared putchar ??? Why..

    The Rx and Tx are on PORTC.6 and 7 not PORTB
     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    What do you mean by this and i am using code as it is only removing comments..!!

    Then what is this :

    #define BAUD 9600

    #define RX_PIN TRISB2
    #define TX_PIN TRISB5
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    I don't know..... I don't know where you got it from....

    In my tutorials 7,7... There is hardware serial...
     
  9. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I have got it from hi tech c samples-> UART...
    this was the problem in this pat..

    #if defined(_16F87) || defined(_16F88)
    #define RX_PIN TRISB2
    #define TX_PIN TRISB5
    #else
    #define RX_PIN TRISC7
    #define TX_PIN TRISC6
    #endif
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    I thought you were using a pic16f877a???

    The code above selects trisc if its NOT the other two chips...
     
  11. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Yes i am using pic16f877a.....

    The problem is the code is to be modified and i am unable what to do?
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Why don't you use the tutorial.... It's VERY easy to understand and it works..
    This is for a pic16f877a..

    Code (C):
    include <pic.h>             // pic specific identifiers
    #define _XTAL_FREQ  20000000        // Xtal speed
    __CONFIG(0x3F71);               // Config bits

    #define LCD_PORT    PORTB       // constants
    #define LCD_TRIS    TRISB       //
    #define LCD_RS      RB4
    #define LCD_RW      RB6
    #define LCD_E       RB7

            // Required prototypes.. each function needs to be declared
            // if called BEFORE definition.

    void LCD_init(void), LCD_cmd(unsigned char ch);
    void LCD_goto(char line, char column), LCD_clr(void);
    void LCD_cur(unsigned char ch), pulse_E(void), LCD_hex(int value);
    void LCD_char(unsigned char ch), LCD_charD(unsigned char ch);
    unsigned char  HSerin(void);
    void HSerout(unsigned char ch), HSerinit(void);

    void main(void)                     // program entry
        {
        int index = 0;
        unsigned char ch;
        ADCON1 = 0x6;                   // Analogue off
        LCD_TRIS = 0b00000000;          // Led port as outputs
        __delay_ms(150);                // let LCD stabilise
        LCD_init();                     // Initalise screen
        LCD_goto(1,0);                  // line 1.
        HSerinit();
        while(1)                        // endless Loop
            {
            ch = HSerin();              // wait for a character

            if(index == 16)             // new line
                LCD_goto(2,0);
            if(index == 32)             // goto line 1
                {
                LCD_clr();
                index = 0;              // re-set
                }
            LCD_char(ch);               // display the received byte
            HSerout(ch);                // Echo back
            index++;                    // move on
            }
        }

    void HSerinit()
        {
        TRISC = 0xC0;                   // should ideally be set
        SPBRG = 129;                        // 20Mhz xtal 9600 BAUD
        TXSTA = 0x24;                   // TXEN and BRGH
        RCSTA = 0x90;                   // SPEN and CREN
        }

    void HSerout(unsigned char ch)
        {
        while(!TXIF);                   // Wait for module to finish
        TXREG = ch;                     // ready to send
        }
    unsigned char HSerin()
        {
        while(!RCIF);                   // Wait for a character
        return RCREG;                   // return character

        }

    void LCD_init()
        {
        LCD_cmd(0x20);                  // 4 bit
        LCD_cmd(0x28);                  // display shift
        LCD_cmd(0x6);                   // character mode
        LCD_cmd(0xc);                   // display on / off and cursor
        LCD_clr();                      // clear display
        }


    void LCD_hex(int value)
        {
        char data;                              // dispay hex values.
        data = value >> 4 & 0xf;                // send upper nibble
        data = HEX_Table[data];                 // hex lookup
        LCD_char(data);
        data = value & 0xf;                     // send lower nibble
        data = HEX_Table[data];                 // hex lookup
        LCD_char(data);
        }

    void LCD_goto(char line, char column)       // combines line and lineW
        {
        unsigned char data = 0x80;              // default to 1
        if(line == 2)data = 0xc0;               // change to 2
        data += column;                         // add in  column
        LCD_cmd(data);
        }

    void LCD_clr()
        {
        LCD_cmd(1);                             // Clr screen
        }

    void LCD_cur(char on)
        {
        unsigned char cur = 0xc;                // cursor off
        if(on) cur = 0xd;                       // cursor on
        LCD_cmd(cur);
        }

    void LCD_cmd(unsigned char ch)
        {
        LCD_PORT = ch >> 4 & 0xf;           // write high nibble
        LCD_RS = 0;
        pulse_E();
        LCD_PORT = ch & 0xf;                // write low nibble
        LCD_RS = 0;
        pulse_E();
        __delay_ms(5);
        }

    void LCD_charD(unsigned char ch)
        {
        ch+=0x30;
        LCD_char(ch);                       // convert to ascii
        }

    void LCD_char(unsigned char ch)
        {
        LCD_PORT = ch >> 4 & 0xf;           // High nibble
        LCD_RS = 1;
        pulse_E();
        LCD_PORT = ch & 0xf;                // low nibble
        LCD_RS = 1;
        pulse_E();
        __delay_ms(5);
        }

    void pulse_E()
        {
        LCD_E = 1;
        __delay_us(1);
        LCD_E = 0;
        }
    Hserout() Hserin() and Hserinit()... This will output / input 9600 Baud at 20Mhz...
     
  13. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    I had removed some part......

    Code (text):

     
           
     

     pulse_E(void),

    unsigned char  HSerin(void);
    void HSerout(unsigned char ch), HSerinit(void);
     
    void main(void)                     // program entry
        {
        int index = 0;
        unsigned char ch;
        ADCON1 = 0x6;                   // Analogue off
                        // line 1.
        HSerinit();
        while(1)                        // endless Loop
            {
            ch = HSerin();              // wait for a character
     
           
                            // display the received byte
            HSerout(ch);                // Echo back
            index++;                    // move on
            }
        }
     
    void HSerinit()
        {
        TRISC = 0xC0;                   // should ideally be set
        SPBRG = 129;                        // 20Mhz xtal 9600 BAUD
        TXSTA = 0x24;                   // TXEN and BRGH
        RCSTA = 0x90;                   // SPEN and CREN
        }
     
    void HSerout(unsigned char ch)
        {
        while(!TXIF);                   // Wait for module to finish
        TXREG = ch;                     // ready to send
        }
    unsigned char HSerin()
        {
        while(!RCIF);                   // Wait for a character
        return RCREG;                   // return character
     
        }
     
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    You don't need the pulse_E function.. It's for the LCD routines..
     
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, i will remove that part also now please tell how to send data to Pc via uC and from PC to uc I am using terminal software linked in previous post!!
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Code (text):

    include <pic.h>             // pic specific identifiers
    #define _XTAL_FREQ  20000000        // Xtal speed
    __CONFIG(0x3F71);               // Config bits
     
     
            // Required prototypes.. each function needs to be declared
            // if called BEFORE definition.
     

    unsigned char  HSerin(void);
    void HSerout(unsigned char ch), HSerinit(void);
     
    void main(void)                     // program entry
        {
        int index = 0;
        unsigned char ch;
        ADCON1 = 0x6;                   // Analogue off                
        HSerinit();
           __delay_ms(150);
        while(1)                        // endless Loop
            {
            ch = HSerin();              // wait for a character
            HSerout(ch);                // Echo back

            }
        }
     
    void HSerinit()
        {
        TRISC = 0xC0;                   // should ideally be set
        SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
        TXSTA = 0x24;                   // TXEN and BRGH
        RCSTA = 0x90;                   // SPEN and CREN
        }
     
    void HSerout(unsigned char ch)
        {
        while(!TXIF);                   // Wait for module to finish
        TXREG = ch;                     // ready to send
        }
    unsigned char HSerin()
        {
        while(!RCIF);                   // Wait for a character
        return RCREG;                   // return character
     
        }
     
     
    A simple echo... Any character typed in hyper terminal will be echo'd back..
     
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, this code will send data to uc then to PC again?
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Should do.... Then it will be easy to modify to use it for a controller.. Lets say you send commands from the PC.... You can then send different data back depending on the command sent..
     
  19. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,154
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Please Ritesh.... Can you back up with schematics.... A simple drawn cicuit so I can see your connections to A.. The PC and B.. The Micro
     
  21. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,


    I have connected the power supply of USB to uC board and the rx to tx and tx to rx of both!!
     

Share This Page