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

PIC18F and USART

Discussion in 'Microcontrollers' started by adrianvon, Oct 24, 2013.

  1. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Problem solved :) It was from the PIC configuration settings. Thanks for your help.
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Pray Tell!!! Show me.
     
  3. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    I tested all the hardware as below:

    First I shorted pins 2 and 3 of the USB to Serial cable (i.e. RX and TX). I then used PuTTY to check if data is being transmitted and received correctly. This way i confirmed that the cable and the driver were in order.

    I then shorted pins 11 and 12 of the MAX232 IC, hence bypassing the PIC. Again I used PuTTY to test for correct data and it also worked.

    The rest was to check if either the PIC was damaged or the coding was incorrect. I changed the PIC and all ok, which concluded that the problem was the code.

    To be honest I just copied and paste the PIC config from a forum, which was as follow:

    #pragma config FOSC = HS // 20MHz Crystal, (HS oscillator)
    #pragma config PBADEN = OFF // PORTB<4:0> pins are configured as digital I/O on Reset
    #pragma config WDT = OFF // watch dog timer off
    #pragma config LVP = OFF // Low voltage program off

    Tomorrow I will go through the config setting from MPLAB to understand them better.

    Again thanks for your help and patient :)
     
  4. dave

    Dave New Member

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


     
  5. tunedwolf

    tunedwolf Well-Known Member

    Joined:
    Aug 9, 2006
    Messages:
    1,080
    Likes:
    62
    Location:
    UK

    Glad to see you made progress :)
     
  6. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    Looks like you didn't change anything. Must've been a bad contact somewhere, which you repaired while testing the hardware.
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I think your problem was a short on one of the micro pins on Tx or Rx....( I have also done this!!)

    At least your up and running.... Any questions and we'l try and answer them...
     
  8. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi again,

    The reason I wanted to use USART is to interface Visual Studio with the PIC. Now lest say i have 20 buttons on VS, does that mean I have to do 20 if conditions?

    Eg:
    Code (text):
    If(data == 'a')
    {
    Turn motor 1 clockwise
    }
    If(data == 'b')
    {
    Turn motor 1 counter clockwise
    }
    If(data == 'c')
    {
    Turn LED on
    }
    ............................
    ............................

    Or is there a neater/more efficient way to do this?
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Depends on ALL the conditions.... Some may be related... Optimising code is an artform.... Many times you'll come back to a piece of code and say "My god!!! Why did I do that"..
     
  10. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    probably better to use CASE/SWITCH?? but i dont know for sure and like mr Ian says depends on the data.
    but am a noob with C
     
  11. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    At 9600 baud, a new symbol can arrive only every 10 ms or so. Therefore, you have plenty of time amd it isn't worth working on code optimization.
     
  12. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    sorry i am rubbish at maths its my worst subject at school, could you explain how you get a symbol every 10ms at 9600 baud. i tried working it out and only got 1.2ms??? where did i go wrong :oops: sorry for the stupid question
    what i did was take 9600 bit per second and divide that by 8 as i assume a character is 8 bits (byte) then divide that by 1000 and i got 1.2
    i spose in the real world you also have to add the start and stop bit time? as well
    LG
     
  13. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    There are also start and stop bits, but you're right, it's around 1 ms. My math was wrong. Still very slow.
     
  14. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    thanks for that, i thought i had messed up!! as maths is not my strong point and i like to note down stuff like this! still gets me how much a micro can do in 1ms!! amazing stuff
     
  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi LG,
    Allow, for most RS232 serial applications, 1 start bit, 8 data bits, 1 stop bit ie: 10 bits per character.
    So 9600 baud/10 = ~ 960 characters/second
    E
     
  16. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    Math was always my strongest ... :)

    My PIC uses UART to talk to the USB adapter and it does so at 2,000,000 baud.
     

Share This Page