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
    oh what about just making one Apple appear while thw switch is pressed/off? isit possible?
     
  2. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Yes, thats what the second software trigger is for.
    But I dont know if the main code works yet, as I cant test it, due to simulator issues.

    Wilksey
     
  3. micheadache

    micheadache New Member

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

    i just tried with
    Code (text):

    if((!RE3) && (!m_Switched))    
    {   m_Switched = true;
         RB0 = 1;
        printf("Apple \r\n");
        printf("Tart \r\n");
    }  else if(RE3)  {  
        m_Switched = false;
        RB0 = 0;}
     
    and it somewhat works! i wonder why it didnt yesterday.
     
    Last edited: Feb 23, 2010
  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

    found out something weird.

    my current code is
    Code (text):

    if((!RE3) && (!m_Switched))    
         {  m_Switched = true;
         RB0 = 1;
        printf("/// \r\n");
         }  else if((RE3) && (m_Switched))    
         {  m_Switched = false;
        RB0 = 0;
        printf("Ntu \r\n");     }
     
    , why does the output turn out to be as the one shown in the attached file..
    Does the above codes only allow capital letters to be shown or?
    '
    *problem solved. i restarted the TeraTerm and i have a proper output combi of capital & small letters now*

    Now for the actuall stuffs i guess...... =/

    As i guess u have read the dpdf file, i am currently tring out the very first step, which is to send "///", and the LED will turn oranger indicating that it is in AT mode. i am setting up the TeraTerm for COM1 (together with the rs232 connection) and HyperTerminal for the COM14 (external bluetooth which looks something like this).

    I am hoping to make a start by being able to transmit "///" and making the bluetooth into going into AT mode.

    On further exploration, i was thinking of the logic of how the final setup will look like, is it possible if we explore along the line of
    Code (text):

    if(porta == 0x01)        
    {         if(portb = 0x01)   /*assigns content of portB RB0 to 0x00 for turn-off Led*/
               {      portb = 0x00;    }
             else{ portb = 0x01; } /*assigns content of portB RB0 to 0x01 for turn-on Led*/
    }
    Its something i found from this site. As there many steps to follow and there's 4 Leds on my pre-made board, i guess this is one of the more feasible method the run through the steps and ensuring that each one of them has been done? i am trying to come up with the logic first and its already making me confused.lol
     

    Attached Files:

    Last edited: Feb 24, 2010
  6. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    It looks as though you are getting some corrupted data somewhere.

    The link you provided looks like a toggle momentary switch.

    The code doesn't affect the output, it should output as you typed.

    Try adding a "\0" to the end of the strings to terminate them.

    Wilksey
     
  7. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    i went home after i last editted the reply yesterday and just reached the office now. i wonder whats our time difference..haha

    hmm do u mean printf("/// \r\n\0"); ?
    so both \0 and the m_switch are inserted to sort of teminate it?
     
  8. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    how do i code it up if i want to do the following.

    initially, for the first step,check if any of the leds are lit, if not, light up RB0 and send "///" when pressing the button. (at the same time entering AT mode)
    of course, next i wud have to let go the button.
    consecutively, lets say when the button pressed, check if RB0 is lit, if it is, off it & on RB1, and sending another AT command.
    next if RB0 = 0 & RB1 = 1, then off RB0 & RB1 and on RB2. so on and so forth.

    am kidna confused of how to write the code. afraid that it will screw up the rest.
     
  9. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    I think we are GMT+0.

    You can do an "if (RB0) // led lit" for example on the LED's to check if they are lit.

    You will need to check PORTB against a mask for ex if (PORTB == 0b00000011) would check RB0 and RB1 were lit.

    Or, alternatively, set up a count variable, each time the button is pressed, increment then check the value with a switch statement.

    Wilksey
     
  10. micheadache

    micheadache New Member

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

    if((!RE3) && (!m_Switched))    {
            if (RB0=0)
    {   m_Switched = true;
         RB0 = 1;
        printf("/// \r\n\0");   //) goes into AT mode
    }
     
    this doesnt seem to be working?
    heh.
     
  11. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    if(RB0 == 0)
    {

    etc

    Wilksey
     
  12. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    oh i think i shud b at GMT+8, Singapore.

    oh yes!! how i can have forgotten the double"=". silly me.

    Hmm oh yes, u just reminded me of switch-case. that may be more efficient but shud i reference the count to the count number of the button pressed or the numbers of leds that have been lit before? 0001, 0010, 0011, 0100, etc...
     
    Last edited: Feb 24, 2010
  13. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    can i continue the code by writing
    Code (text):

    if((!RE3) && (!m_Switched))    
    {   m_Switched = true; 
         if (RB0==0)            {
            RB0 = 1;
            printf("/// \r\n\0");   }   //goes into AT mode

        else if(PORTB==0b00000001)      {
            RB0=0;
            RB1=1;
            printf("AT*AGND \r\n\0")    }
             else if(PORTB==0b00000010)     {
            PORTB==0b00000011;
            printf("AT*AGND \r\n\0");   }

    }  else if((RE3) && (m_Switched))    
    {   m_Switched = false;
        RB0 = 0;
    }
    and so on...
    Nope, i dont think it's feasible..i tried it..

    Hmm how do i start the count rolling?
     
    Last edited: Feb 25, 2010
  14. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Hmmm, thats a bit long winded.
    Set up a global int called m_Count = 0;
    Code (text):

    m_Switched = true;
    m_Count++;
    switch(m_Count)
    {
    case 1:
    //AT Command 1
    break;
    case 2:
    //AT Command 2
    break;
    }
     
    This is part of what you want to do I believe?

    Wilksey
     
  15. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    ohhh so its the m_count that we need to put in there...i was thinking how will the uC understand that m_count refers to the number of times we have pressed the button?

    However, i have tried changing the overall codes to
    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;
    #define true 1
    #define false 0
    void main()
    {
        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++;

        switch(m_Count){
        case 1:
            RB0 = 1;
            printf("/// \r\n\0");
        case 2:
            RB1=1;
                       }               


    }                          
                }
    }

    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
    }
     
    but there's no output available to it, it's stuck to led RB1=1
     
    Last edited: Feb 25, 2010
  16. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    eek, you forgot your breaks!

    Look at my code after each case statement I have included a break, but you havent, therefore your code falls right through to RB1=1 no matter what the case.

    Dont forget to reset the count when you have reached sufficient level.

    Also, dont forget to initialise your variable.

    Wilksey
     
    Last edited: Feb 25, 2010
  17. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    oops! i've changed that portion to
    Code (text):

    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;          }
     
    but to no avail.........i was thinking if its due to the m_Count too as i still wonder how the uC knows that 1 button press == 1 count



    Btw, i just consulted my supervisor if i'm on the right track and he said that i'm lacking of the receiver part of the uC! hmm, as we are now able to send Apple etc from uC to PC, as shown from the results in HyperTerminal, but we have not set it up for the uC to receive any infos =/ If this is not set, there'll be no response seen no matter wat we typed in the final product.

    Remember that u once taught me
    Code (text):

    void putch(unsigned char byte)
    {   /* output one byte */
        while(!TXIF)    /* set when register is empty */
            continue;
        TXREG = byte;
    }
    is the receiving part similar to this?

    I think we need to use RCREG, but now if we're gonna use TXREG and RCREG, are we still allowed to use printf?
    i think the logic for the
    Tx: uC send to PC and shown in HT
    Rx: PC's HTerminal send to uC, uC receive & it sends back response, finally shown in HT.

    but how should be the confusing codes be written =(
     
    Last edited: Feb 25, 2010
  18. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Like this:
    And yes, you are still allowed to use printf
    Code (text):

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

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    To echo the reply back to the UART use the getch echo like this:
    Code (text):

    unsigned char getche(void)
    {
        unsigned char c;
        putch(c = getch());
        return c;
    }
     
    Wilksey
     
  20. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    oh u just need to return RCREG; for the receiving portion? Hmm btw, what's the continue used for?

    hmm its Saturday over here today, can only try those codes agn on monday when i get back to office =/
     
  21. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Yes, continue means if you are not ready to receive a character then carry on without attempting to receive the char from the UART receive buffer.

    Yes, I am out most weekends away from my computer, so I mainly respond on a weekday also, with technical / code responses, general responses if i'm at my computer checking emails I will reply.

    Hope you have a good weekend.

    Wilksey
     

Share This Page