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
    hi experts around, i am currently using PIC16f886, pickit2, MPlab for a project.
    i editted the codes i obtained from the sample codes taken from the HiTechC folders.
    but how do i show that the connectionis working?
    can i know where will the "A" be displayed at? how do we actually do that?

    alternatively, i found another shorter & simpler codes done by a fellow expert in this forum, http://www.electro-tech-online.com/threads/hi-tech-c.87896/ through his link of "My UART code". however, i didnt get anything as the output

    pls help me as this project is really crucial to me and am rather clueless/confused of my next step. solving this current problem of mine is an intermediate step to go to the next step - configuring AT commands for the p16f886 and rs232 and BT module
     
  2. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    hmm any one who can help me out with this project?
     
  3. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Dont forget when interfacing with a RS232 / PC interface you will need a MAX232 converter chip (or something which can do 232 level shifting).

    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

    i actually have a DIY max232 on hand.
    no idea of the codings and how to communicate between p16f886 and pc via HT. thats the first task that i have to complete before moving on to the next stage (progamming the UART, program the receiving portion, program the AT command to enable the p16f886 to be bluetooth compatible)

    hope i can get some help here..would really appreciate it.
     
  6. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    any available help around?
     
  7. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    a DIY max232? the old transistor trick by chance?

    So, what exactly are you trying to achieve?

    Are you asking for code to read / write from the UART on the PIC to a PC?

    Wilksey
     
  8. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    yup, a DIY max232. i got it from my supervisor. seems to be working well when i used it to try the bluetooth communication.

    that aside, i am currently trying to transmit data to PC (viewed via TeraTerm or Hyper Terminal), via microp (P16F886).
    i initialised all the EUSART platform and sended 0x41 and 0x31, 'A' and '1' on e ASCII table respectively. I tried probing it with a oscillope but the 1s and 0s are there correctly. Hoever, the words dont seem to be coming out well in the PC. nto sure wats the problem, is it due to the oscillator or baud rate? i am using Fosc 8Mhz, BR 9600, SPBRG 51.

    This is just my first task =( following which, i have to configure my bluetooth modules with the use of AT commands thru the 16F886 too.
     
  9. Wilksey

    Wilksey Member

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

    The BRGH would have to be set to 1 for SPBRG to be 51, else it is 12 for BRGH = 0. Something to check.

    You would be better off posting your current code, along with the name of the compiler you are using.

    Sounds like a fairly straight forward request, so long as your DIY 232 chip has the correct voltage shifts.

    Put the code in the [ code ] [ /code ] tags to retain formatting.

    Also, if you can list the AT commands needed to configure the BT module, may be able to incorporate some of that into the code.

    Wilksey
     
  10. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    yup, i have set BRGH to 1 and BRG16 = 0

    however, now, i am trying to firgure out if the diy232 works, cos everything seems right.

    will include my codes in for your reference once i tried out the diy 232?

    its fairly simple u mean? oh gosh, am clueless
     
  11. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    am i right to assume using 8Mhz, 9600Baud rate?

    the codes that am currently using is the following.
    its rather simple i guess. using MpLab PICKIT2.

    Code (text):

        #include<pic.h>
        #include<htc.h>
        #include<conio.h>
        #include<stdio.h>
       
    void init_rs232();

    void main()
    {

        TRISE = 0x08;
        TRISB = 0x00;
        init_rs232();

            while(1){
                if(RE3==0){
                RB0=1;
                if(TRMT)
                    TXREG = 0x41; //sending 'A'
                }
                else{
                    RB0=0;
                    if(TRMT)
                    TXREG = 0x70; //sending 'p'
                }
            }
    }

    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 */
        }
     
    i think i should take one step at a time and solve this first before going into the AT commands. am really worried as there's a deadline i need to meet for submission. have many more to go. urgh
     
    Last edited: Feb 4, 2010
  12. AllVol

    AllVol New Member

    Joined:
    Nov 22, 2005
    Messages:
    956
    Likes:
    9
    Location:
    Tennesee, USA
    Since you are using Pickit2, you can use its version of terminal to see the output of your chip on screen, provided you have all set up right.

    Go into tools on the Pic2 site and click on USART tool. Since you are using the 16F866, I assume you are using a breadboard or something. Figure out a way of connecting the USART TX and RX pins to the Clk and Data of ICSP. The diagram on the USART screen shows the connection. Make sure the baud rate on the USART tool screen is set, and connect. You should see something to go on. Going this route, you will eliminate the use of R232 and see what is happening. Build in steps.

    Your screen might look something like my attachment:
     

    Attached Files:

  13. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    i am actually using the PicKit2 Demo board from Microchip and i dont think i am allowed to eliminate the use of rs232, as we're supposed to use it to program the bluetooth module into the 16F886 later on.
     
  14. AllVol

    AllVol New Member

    Joined:
    Nov 22, 2005
    Messages:
    956
    Likes:
    9
    Location:
    Tennesee, USA
    Okay, I see. I did not realize there were stipulations. You still might be able to use the USART tool to help you get started. Luck.
     
  15. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    I have just compiled and ran your code on a Simulator, with 8mhz clock it constantly sends out "A" across the UART.

    What exactly do you get out of your PIC when plugged into the PC? anything at all, or garbage data or another character than expected?

    You may also want to draw a simple circuit diagram of your connections.

    Wilksey
     
  16. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    any idea how to check if i am using the right Fosc, BaudRate, SPBRG?
     
  17. micheadache

    micheadache New Member

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

    good to have u back here!
    u got A as the output at ur side?!?!
    i get a continous of other alphabets or digits but never A. i am using Hyper Terminal to see the output


    i am actually given this board for my project, pickit 28pin board, MpLab, Pickit2, HyperTerminal & sometimes TeraTerm to see the output into the PC, max232 & rs232 connecting to RC6(Tx), RC7(Rx), Vdd and Gnd of the board
     
    Last edited: Feb 4, 2010
  18. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    The Baud Rate and Fosc and SPBRG are correct as it will output "A" at 9600 at 8MHz, but it is all in the PIC datasheets, along with calcluations etc.

    Once you get a stream of "A"s on your terminal you know it is working.
     
  19. Wilksey

    Wilksey Member

    Joined:
    Aug 16, 2009
    Messages:
    553
    Likes:
    20
    Location:
    Somerset, UK
    Yes I get a stream of "A" output on my side using Hyperterminal.

    You could try adding this to your code, below the #include statements:
    Code (text):

        __CONFIG(WDTDIS & XT);
     
    It sounds like a configuration issue or a 232 level converter issue.

    Wilksey
     
  20. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    yes, i have been trying using 9600, 8Mhz,, calculated and referred to the 16f886 datasheet on the values for SPBRG, etc...

    Hmm its weird cos i get nothing out of the program after inserting that line below my #include statements and my LED doesnt light up anymore too.
     
  21. micheadache

    micheadache New Member

    Joined:
    Jan 28, 2010
    Messages:
    54
    Likes:
    0
    Location:
    singapore
    oh no, my LED doesnt light up now, even if i commented out "__CONFIG(WDTDIS & XT);"
     

Share This Page