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
    But you have mention 20mhz only in code not 4mhz....
    and the SPBRG value is 129 for 20mhz ....so, what is the problem in it??


    can you expand this i am not getting it, is this portc7???
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Whoops.. a bit of Intel in there.... Yes I mean RC6 and RC7...

    I'm doing too much.

    You changed the crystal setting not me.... When I wrote the code it was for a 4Mhz like I said....
     
  3. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Yes, connected with USB module RX and TX....

    ok, here is the code you posted
    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
     
        }
    so, i have to make it at 20mhz or 4mhz b'coz as you said __CONFIG(0x3F71); for 4mhz but in code 20mhz is mention...plus is thi s fine
    TRISC = 0xC0; ?? or it should be changed to 0b10000000
     
  4. dave

    Dave New Member

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


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK

    I have always set both tris bits, This is how they recommend in the datasheet... But!! I know it works so You need to sort your hardware out..
     
  6. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    that mean this is correct TRISC = 0xC0;, i am saying b'coz many people on forum are pointing this...
    and can you tell me about hardware any suggestion as i have alreday posted the schematic...
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    It's correct..... Have you got a different USB -> TTL convertor.
     
  8. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    No, i am using that only and as i said when i short the RX and TX of USB module it work fine i.e. echo back the character.....
    then what is the problem in hardware should i connect low value capacitor to rx and tx from gnd for filtering??
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Have you tried Rx to Rx and Tx to Tx . Put a 220 ohm resistor in series first, as it doesn't look like a ft232 chip or similar .
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    I don't think you two read my post so I'll say it agin I complied this with XC8 and it doesn't work it complies fine it even debug ok but the Hex doesn't match for some reason. It's not making a Hex with all the goodies in it. I didn't have time to play with it much. But that maybe happening here.
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Burt.... I wrote the software on hitech.. I can compile and run the software... I have It working at work..

    Did you change the config settings? If you try the code as is you need a 4Mhz crystal.
     
  12. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    I under stand you wrote it in hitech XC8 should work fine using the code and it does but the Hex is useless for some reason the code test fine in Mpsim. I just saying maybe the OP changed to XC8 as the compiler
    and its not doing something right. Mine is not for some reason.
     
  13. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    No, i am also using hi tech C...
    OK, i was using it at 20mhz and its setting was also 20Mhz
    are you saying taht i should use it at 4MHz then tell what changes is to be done in SBRG register also...
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I just compiled this in XC8.... It works as expected.

    Code (C):

    #include <xc.h>             // pic specific identifiers
    #define _XTAL_FREQ  20000000        // Xtal speed
    #pragma config CONFIG = 0xFF72
     
            // 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 = 0b10000000;                 // 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
     
        }
     
     
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, In Mp Lab showing error......

    don't know the problem in it??

    Code (text):
    #include <htc.h>                // pic specific identifiers
    #define _XTAL_FREQ  20000000        // Xtal speed
    #pragma config CONFIG = 0xFF72
    Make: The target "C:\Users\Abc\Documents\mplab\uarttest.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\uarttest.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"
    Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe" -ouarttest.C.cof -muarttest.C.map --summary=default --output=default uarttest.p1 --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"
    HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.83
    Copyright (C) 2011 Microchip Technology Inc.
    (1273) Omniscient Code Generation not available in Lite mode (warning)
    Error [715] C:\Users\Abc\Documents\mplab\uarttest.C; 3. bad pragma "x"
    Error [715] C:\Users\Abc\Documents\mplab\uarttest.C; 3. bad pragma "0"
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    No No No!!! The code was for XC8 to prove to Burt.... You need the old code "__CONFIG(0x3F72);"

    I said, and I quote
    This was aimed solely for Burt...
     
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I am not getting what might be the problem why real term is showing rx data....??
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    What version is your MPLAB? Can you simulate the process in MPLAB?
     
  19. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I am using 8.87, how to do...??
     
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, it is working but few error are there i don't know them...
    it is working now thanks...
    but i found some problem it display 'a' also but some times other default character , why??


    Code (text):

    unsigned char  HSerin(void);
     void HSerout(unsigned char ch),
     HSerinit(void);

     void main(void)                        // program entry  
       {    
       int index = 0;  
       unsigned char ch = 'a';         // <- LOOK HERE.
       ADCON1 = 0x6;                    // Analogue off    
       HSerinit();      
      __delay_ms(150);    
     while(1)                        // endless Loop      
      {           HSerout(ch);                // Echo back    
      }
     
     }


     void HSerinit()    
     {    
       TRISC = 0b10000000;                    // 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  
      }


    this work fine..
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      283.6 KB
      Views:
      97
    • 2.jpg
      2.jpg
      File size:
      291.7 KB
      Views:
      96
    • 3.jpg
      3.jpg
      File size:
      285.8 KB
      Views:
      105
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Debbugger/settings/USART I/O... USART enable checked... Output to window checked.. Create a text file with the USART input stimulus... and attach it...
     

Share This Page