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. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    And as Be80be said,

    This is where you need to think for yourself. Go through the code, make sure the program controls the right pins and ports. Otherwise your chip won't do a thing.
     
  2. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    so, this is the problem....??

    TRISC = 0xC0;
    bit 7 and 6 are input....
     
  3. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    Look at the #@$%!*& datasheet, like we've been telling you to do from the start. Look under the ports section. ALL THE INFORMATION YOU'LL EVER NEED is in there.
     
  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

    OK, after changing TRISC =0b10000000; will it work??
     
  6. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    Like I just said:

     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, after changing the TRISC setting and i have checked SPBRG register it should be 129 only at 20Mhz....but still not working.
    I noticed while resetting the PIC null charter are transmitted 00, why??
     
  8. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Now what to do please tell after doing correction it is not working i have seen the data sheet.
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Post your complete code.... I suspect your connections are still faulty... The code I gave you should work.... It does for me.
     
  10. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE
    From the data sheet,

    Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be
    set in order to configure pins RC6/TX/CK and RC7/RX/DT
    as the Universal Synchronous Asynchronous Receiver
    Transmitter.

    It's all there if you simply look.

    Mike.
     
  11. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I think code are fine and the schematic is posted in previous post....



    Code (text):

    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000





    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(250);
        while(1)                        // endless Loop
            {
            //ch = HSerin();                // wait for a character
            HSerout;                // 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 = '0';                        // ready to send
        }
    /
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I though so.... As you haven't changed anything other than the xtal setting... Whats not working! As this program, as it stands, doesn't do anything!!

    Hserout should take a parameter.... "ch" in my example... So send something.

    Hserout('A'); Send character A.
     
  13. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Beleive me i have tested this code first than moved to other it is not working don't know why???



    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 = 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
     
        }
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I have just tested the code above... It is working fine.. I think your Rx and Tx are the wrong way round..
     
  15. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Ian hi that code doesn't do anything but echo back what's typed and sent out maybe the keyboard is broke or some one is for getting to type.
     
    Last edited: May 17, 2013
  16. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    That^ :D
     
  17. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    I just for the heck of it complied this with XC8 and guess what it complies fine but none of the serial stuff works and that maybe what is happening to the OP
     
  18. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I have tested USB module it is fine and i have connected the rx to tx and tx to rx but no data is transmitted...

    and i have changed setting too this b'coz urs where giving me error...

    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
     
  19. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    [MODNOTE]Deleted Off Topic[/MODNOTE]
     
    Last edited by a moderator: May 18, 2013
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    The only error with the code was for some reason you had missed the first '#' before include... The config code can be both ways..

    Remember my code was for a 4Mhz crystal (XT) originally 0x3F71... You have a 20Mhz (HS) crystal 0x3f72..

    Are you using P3.6 and P3.7 on the PIC?
     
  21. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    [MODNOTE]Deleted Off Topic[/MODNOTE]
     
    Last edited by a moderator: May 18, 2013

Share This Page