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

pic16f886 uart + AT problem

Discussion in 'Microcontrollers' started by micheadache, Jan 28, 2010.

  1. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    urgh i am still trying the 2 codes out...something wrong with my pickit2 at the moment.

    btw, i just thought of smth, do we need to expand the TXREG and RCREG code segments in future?
    like lets say if i should get a certain response from the AT module if i transmitted something? maybeeee, when i press the switch and it transmits the word "AT", i should get a reply of OK from the bluetooth or something?

    oh i remembered u once said that i need to initialize the m_Count. am i right to initialize it to m_Count =0? cos i dont think it worked last week
     
  2. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    Hmmm
    Code (text):

        #include<pic.h>
        #include<htc.h>
        #include<conio.h>
        #include<stdio.h>
        #define XTAL_FREQ 8MHZ

    void init_rs232();
    //void putChar( unsigned char ch);

    #define MHZ *1000L
    #define XTAL_FREQ 8MHZ
    #define DelayUs(x) { unsigned char _dcnt; \
    _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
    while(--_dcnt != 0) \
    continue; }

    int m_Count;
    //m_Count=0;
    #define true 1
    #define false 0
    void main()
    {
        //TRISC = 0b10000000; no need to set TRISC as its the lowest priority if we've already set the TXEN, etc
        OSCCON= 0x75; //8Mhz High Freq Clock, can be fine tuned using OSCTUNE
        TRISE = 0x08;
        TRISB = 0xF0;
        ADCON1 = 0x07;
        init_rs232();
        static bit m_Switched = false;
       
        while(1){

    //Global or main function variable.  
    if((!RE3) && (!m_Switched))    
    {   m_Switched = true; 
    /*   ???    //m_Count=0
        m_Count++;
     
        switch(m_Count){
        case 1:
            RB0 = 1;
            printf("/// \r\n\0");       // AT mode
            break;
        case 2:
            RB1=1;
            printf("AT*AGND \r\n\0");   // search fr new device
            break;
        case 3:
            RB0=1;
            RB1=1;
            break;      }              
    }*/
            if (RB0==0)             {
            RB0 = 1;
            printf("/// \r\n\0");   }   //goes into AT mode

    }  else if((RE3) && (m_Switched))  
    {   m_Switched = false;
        RB0 = 0;
    }      
                /*      //int click=0;
                        switch(!RE3){
                            case1:
                                PORTB=0b00000001;
                                printf("/// \r\n\0");
                                    }           */
                               
                }
    }

    void init_rs232()
    {
        BRGH = 1;   // high baud rate to avoid baud rate error & bcos 16bit achieve slow baud rates fr fast osc freq
        BRG16 = 0;
        SPBRG = 8;      // Fosc=8Mhz, ,16, BR=57600
        TX9  = 0;   // 9bits=1: 8-bit=0
        SYNC = 0;   // asynchronous
        SPEN = 1;   // enable serial port pins
        TXEN = 1;   // enable the transmitter & (automatically)TXIF=1
        CREN = 1;   // enable reception
        SREN = 0;   // no effect
        TXIE = 0;   // disable tx interrupts
        RCIE = 0;   // disable rx interrupts
        RX9  = 0;   // ninebits?1:0,,,8- or 9-bit reception
    }

    void putch(unsigned char byte)
    {   /* output one byte */
        while(!TXIF)    /* set when register is empty */
            continue;
        TXREG = byte;
    }

    unsigned char getch()
    {   /* retrieve one byte */
        while(!RCIF)    /* set when register is not empty */
            continue;
        return RCREG;
    }

    unsigned char getche(void)
    {   /*echo @ HT*/
        unsigned char c;
        putch(c = getch());
        return c;
    }
    the above codes cant seem to be working (i'm not sure if its receiving anything but there's no echo seen in the HT) =// did i insert the codes at the correct place?
     
    Last edited: Feb 28, 2010
  3. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Erm,

    I dont see you calling getche anywhere in your code, unless I have missed anything, it is 2.30 am here.

    You initialise the variable when you define it, so change int m_Count;
    to:
    Code (text):

    int m_Count = 0;
     
    There is a lot of unused code, which can make it difficult to read sometimes, it looks as if you have a } in the wrong place, but without formatting the code it isn't easy to be sure.

    Do you get any compiler errors or warnings such as "code unreachable" ?

    Wilksey
     
  4. dave

    Dave New Member

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


     
  5. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore

    ohhhh its 10.58am here now. such a big time difference there.

    oh! cos i saw ur codes as "unsigned char", thus am not sure if i'm in a dilemma of how to call them..
    lemme try in inserting
    Code (text):
     void getche(unsigned char c)
    Nope there isnt any errors shown upon compilation. guess its clearer to see it here as it can be easily differentiated between the unused and the used codes =/
     
  6. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    argh...oh man...am slightly confused now..hmm about the switch case..
    its now
    Code (text):
     if((!RE3) && (!m_Switched))    
    {   m_Switched = true; 
        m_Count++;
     
        switch(m_Count){
        case 1:
            RB0 = 1;
            printf("/// \r\n\0");       // AT mode
            break;
        case 2:
            RB1=1;
            printf("AT*AGND \r\n\0");   // search fr new device
            break;
        case 3:
            RB0=1;
            RB1=1;
            break;      }              
    }
    in the void main() but still no results

    lemme tidy up my codes and i show u the final one i am having right now
     
    Last edited: Feb 28, 2010
  7. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    Code (text):

        #include<pic.h>
        #include<htc.h>
        #include<conio.h>
        #include<stdio.h>

    #define MHZ *1000L
    #define XTAL_FREQ 8MHZ
    #define DelayUs(x) { unsigned char _dcnt; \
    _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
    while(--_dcnt != 0) \
    continue; }

    void init_rs232();
    //void putChar( unsigned char ch);
    //void getche (unsigned char c);

    int m_Count=0;
    #define true 1
    #define false 0
    void main()
    {
        OSCCON= 0x75; //8Mhz High Freq Clock, fine tuned using OSCTUNE
        TRISE = 0x08;
        TRISB = 0xF0;
        ADCON1 = 0x07;
        init_rs232();
        static bit m_Switched = false;
       
        while(1){
    getch();
    getche();

    //Global or main function variable.  
    if((!RE3) && (!m_Switched))    
    {   m_Switched = true; 
    /*  m_Count++;
     
        switch(m_Count){
        case 1:
            RB0 = 1;
            printf("/// \r\n\0");       // AT mode
            break;
        case 2:
            RB1=1;
            printf("AT*AGND \r\n\0");   // search fr new device
            break;
        case 3:
            RB0=1;
            RB1=1;
            break;      }              
    }*/
            if (!RB0)               {
            RB0 = 1;
            printf("/// \r\n\0");   }   //  should go into AT mode

    }  else if((RE3) && (m_Switched))  
    {   m_Switched = false;
        RB0 = 0;
    }      
                                       
                }
    }

    void init_rs232()
    {
        BRGH = 1;   // high baud rate to avoid baud rate error & bcos 16bit achieve slow baud rates fr fast osc freq
        BRG16 = 0;
        SPBRG = 8;              // Fosc=8Mhz, ,16, BR=57600

        TX9  = 0;                   // 9bits=1: 8-bit=0
        SYNC = 0;                   // asynchronous
        SPEN = 1;                   // enable serial port pins
        TXEN = 1;                   // enable the transmitter & (automatically)TXIF=1
        CREN = 1;   // enable reception
        SREN = 0;                   // no effect
        TXIE = 0;                   // disable tx interrupts
        RCIE = 0;                   // disable rx interrupts
        RX9  = 0;                   // ninebits?1:0,,,8- or 9-bit reception
    }

    void putch(unsigned char byte)
    {   /* output one byte */
        while(!TXIF)    /* set when register is empty */
            continue;
        TXREG = byte;
    }

    unsigned char getch()
    {   /* retrieve one byte */
        while(!RCIF)    /* set when register is not empty */
            continue;
        return RCREG;
    }

    unsigned char getche(void)
    {   /*echo @ HT*/
        unsigned char c;
        putch(c = getch());
        return c;
    }
     
     
    Last edited: Mar 1, 2010
  8. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    i happen to find this site. i tried it out as it looks simple but nothing was shown too.

    arhhhhh!
     
  9. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    You are calling getch and getche, you can only call one, also, you are not storing the return value.

    So, by calling getch you are clearing the buffer, but not putting it anywhere, thus clearing the buffer, so when you call getche the buffer is cleared previously by getch.

    Try removing getch(); from your code and leave getche(); in place.

    You can do a simple test by using this:

    Code (text):

    while(1)
    {
        getche();
    }
     
    This should eliminate any hardware / switch issues.

    Wilksey
     
    Last edited: Mar 1, 2010
  10. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    Ohhh...sort of understanding it..
    hmm i followed ur advice and tried
    Code (text):

    void main()
    {
        OSCCON= 0x75; //8Mhz High Freq Clock, fine tuned using OSCTUNE
        TRISE = 0x08;
        TRISB = 0xF0;
        ADCON1 = 0x07;
        init_rs232();
        static bit m_Switched = false;

    while(1){

    getche();

    //Global or main function variable.  
    if((!RE3) && (!m_Switched))    
    {   m_Switched = true; 
    /*  m_Count++;
     
        switch(m_Count){
        case 1:
            RB0 = 1;
            printf("/// \r\n\0");       // AT mode
            break;
        case 2:
            RB1=1;
            printf("AT*AGND \r\n\0");   // search fr new device
            break;
        case 3:
            RB0=1;
            RB1=1;
            break;      }              
    }*/
            if (!RB0)       {
            RB0 = 1;
            printf("/// \r\n\0");   }   //  should go into AT mode

    }  else if((RE3) && (m_Switched))  
    {   m_Switched = false;
        RB0 = 0;
    }      
                                       
                }
    }

     
    but still to no avail =(( and the switch still doesnt work too =/
    and even when i use back the if else instead of the swtich case as shown above, the led and switch doesnt work too (is it due to the getche() ?
     
    Last edited: Mar 1, 2010
  11. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    What happens if you take out all of the switch code?

    But just leave getche in place?

    Wilksey
     
  12. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    hmmmm same thing happens. as in there's no response at the HT window too
     
    Last edited: Mar 1, 2010
  13. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    I guess I should have said you need to post something to the module to get a response such as "OK", can you issue a standard ATI command and get information back from the module?

    Calling getche on its own will just wait for a never ending response, unless it data is comming through regardless?

    For ex:

    Code (text):

    while(1)
    {
        printf("ATI\r\n");
        getche();
    }
     
    Wilksey
     
  14. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    Hmm so i need to type in an AT command first prior to getting any echo?
    hmm but my supervisor mentioned that we're (should be) able to somehow get a double "aa" or something when we, for eg, type "a" on te keyboard, sending it to the pic through HT.

    Hmm will try it when i reach office in 3hr
     
  15. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Well,
    If connecting to HT then it should echo back anything typed, so if you type "a" in hyperterminal, "a" should appear, unless you have "echo typed characters locally" enabled on HT, in which case you will get "aa" on the HT screen.

    I thought you were interfacing with the BT module, not HT.

    In which case, you cam take out the printf statement in my prev post.

    I'll see if I can simulate the code here.

    Wilksey
     
  16. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Ok, I have just simulated the code, and it works as it should.

    If this doesn't work, I would suggest checking your 23
     
  17. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Ok, I have just simulated the code, and it works as it should.

    If this doesn't work, I would suggest checking your 232 connection, one thing you could do is download an application called Moxa Data Scope, or terraterm, and see the raw hex data comming through the port, you should be able to tell if there is anything funny happening.

    Anyway, here is the code that works for me:
    Code (text):

        #include<pic.h>
        #include<htc.h>
        #include<conio.h>
        #include<stdio.h>
        __CONFIG(WDTDIS & INTIO & MCLRDIS);

    void init_rs232();
    #define true 1
    #define false 0

    void main()
    {
        OSCCON = 0x75;
        TRISA0 = 1;
        TRISB = 0x00;
        init_rs232();
        static bit m_Switched = false;  //Global or main function variable.

            while(1)
            {
                getche();
            /*  if (RA0) //&& (!m_Switched))
                {
                    m_Switched = true;
                    RB0 = 1;
                    printf("Apple\r\n");
                }
                else if(!RA0)
                {
                    m_Switched = false;
                }*/
            }
    }

    void init_rs232()
    {
        BRGH = 1;    
        BRG16 = 0;
       
        SPBRG = 51; /* set baud rate: Fosc=8Mhz, 16, BR=9600 */
        TX9  = 0;   /* ninebits?1:0,,,8- or 9-bit transmission */
        SYNC = 0;   /* asynchronous */
        SPEN = 1;   /* enable serial port pins */
        TXEN = 1;   /* enable the transmitter & (automatically)TXIF=1 */
        CREN = 1;   /* enable reception */
        SREN = 0;   /* no effect */
        TXIE = 0;   /* disable tx interrupts */
        RCIE = 0;   /* disable rx interrupts */
    }

    void  putch(unsigned char byte)
    {
        /* output one byte */
        while(!TXIF)    /* set when register is empty */
            continue;
        TXREG = byte;
    }

    unsigned char getch() {
        /* retrieve one byte */
        while(!RCIF)    /* set when register is not empty */
            continue;
        return RCREG;  
    }

    unsigned char getche(void)
    {
        unsigned char c;
        putch(c = getch());
        return c;

     
    Wilksey
     
  18. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    Oh so
    Code (text):

    while(1)
    {
        printf("ATI\r\n");
        getche();
    }
    is the way to do it when BT is connected eh?

    Hmm cos i was thinking to do it step by step from getting a result out of HT itself before trying to get the BT out. yup for the past 2 days, i've been trying that similiar code of writing in only the "getche();"

    the 232 should still be working as i used back the "/// output" experiment to ensure that all foundations are still working well.

    and yes, i've checked "echo typed characters locally" enabled but one 1 "a" appear. [this echo thing's making me confused about the simple HT now =/]

    urgh, i really hope i can, at least, get the echo out today. afterwhich, i would want to try out that "OK" is shown when i type AT in HT. then i can go back to the switch problem =/ i'll try to use ur codes to countercheck with mine too.
    codings driving me crazy.
     
    Last edited: Mar 2, 2010
  19. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Did you check the input to the PIC as well as the output?

    It's the input that is mainly effected by the level shifting i.e. RX pin.

    If you have turned on "echo" but have only 1 'a' displayed, and printf etc works fine (test, before the while(1) loop add a "printf("Testing RS232\r\r");"), then there is something iffy with your transmit to PIC receive circuitry.

    Hyperterminal itself is simple, it's a "dumb terminal".

    Unless there is something majorly wrong with your code, then it isn't a coding issue.

    It will be more likely a timing / hardware issue, which is why I suggest using Terraterm Pro or Moxa Datascope utility instead of Hyperterminal, or something that can display the HEX representation of what is comming out of the MAX232 circuitry.

    At this stage, it's all about debugging!

    One thing you could do, is connect RX to TX on the 232 circuit (TTL side), temporarily. bypass the PIC, and check that the characters are echoed back through hardware.

    Can you isolate power to the pic from the 232 converter? Might be a useful exercise.

    With the HEX representation idea, you send a uppercase A for example which is ASCII 65, HEX 41, you should see RXing a code 41, if you see something like FE, FF, 00, C1 etc, that means there is data corruption, and the levels are not shifting correctly.

    If you see nothing at all, there is a major issue with either the PIC or the 232, in which case, conduct the loopback test (TX to RX on TTL side of the 232 circuit), eliminate the issues / potential issues 1 by 1!

    Good luck, i'm sure you will be able to find the problem.

    Wilksey
     

Share This Page