PIC16F877 to PC via max232 HELP!!


Closed Thread
Results 1 to 16 of 16
  1. #1
    loredana's Avatar
    loredana Guest

    Default PIC16F877 to PC via max232 HELP!!

    I hope one of you can help me, I am having lots of problems and not getting anywhere fast.

    I am trying to connect a 20Mhz PIC16F877 to a PC using a MAX232 and PicBasic. I need to use the features of hserin and hserout for my project not serin and serin2.

    I think from what I read so far its not possible to connect the the PIC to the MAX then to the port directly due to some inverting thing which I really dont understand.

    I wonder if anyone could give me some working code to echo data sent from the pc back from the PIC so I can be sure to get started.

    Also if you have a diagram of the circuit that works, I would be very greatful.

    I am using Pic Basic pro 2.45 and an Epic programmer that both work ok and I have ran plenty tests to check.

    Please help.

    Loredana

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hi loredana,

    I'll suggest you to download MicroCode Studio there : http://www.rentron.com/mcstudio/mcstudio.exe

    One circuit example is already in the HELP TOPIC under ICD>Introduction>>Harware Setup. By the way i attach one example.

    With MicroCode studio you'll be able to send and receive to your PIC in the same windows task.

    I think from what I read so far its not possible to connect the the PIC to the MAX then to the port directly due to some inverting thing which I really dont understand.
    You can connect PIC directly to the PC without MAX232 if you're using SERIN/SEROUT and if you're using Inverted Mode. But if You're using the internal USART... you cannot set it by software. Hardware only, this is why you need an MAX232 or some simples inverter transistor circuit.

    I wonder if anyone could give me some working code to echo data sent from the pc back from the PIC so I can be sure to get started.
    The following will give you this @9600 baud.
    Code:
        ' Serial data echo test
        ' =====================
        '
        ' File name : Serial_echo_test.bas
        ' Company : Mister E 
        ' Programmer : Steve Monfette
        ' Date : 16/01/2005
        ' Device : PIC16F877
    
        ' PIC setting & Programming mode         
        ' -------------------------------
        '
    @ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF                  
        ' HS(20Mhz) oscillator
        ' Enable watch dog timer
        ' Enable power up timer
        ' Enable brown out detect
        ' Disable low voltage programming
        DEFINE OSC 20 ' Use 20 MHZ oscillator
    
        ' Usart Settings
        ' --------------
        '
        ' refer to datasheet section 10 for the following settings
        '
        DEFINE HSER_RCSTA 90h  ' Enable Serial PORT
                               ' Enable continuous receive
                               '
        DEFINE HSER_TXSTA 24h  ' Enable transmit
                               ' High baud rate (BRGH=1)
                               '
        DEFINE HSER_SPBRG 129  ' set USART to 9600 baud (when BRGH=1)
        DEFINE HSER_CLROERR  1 ' Enable automatic overrun error
        
        ' Variable Definition
        ' -------------------
        SerData     var   byte
            
    Start:
        ' Program begining/Main Loop
        ' ==========================
        '
        HSERIN [serdata]
        hserout [serdata]
        goto start
    Attached Images Attached Images  
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    loredana's Avatar
    loredana Guest


    Did you find this post helpful? Yes | No

    Default PIC16F877 to PC via max232 HELP!!

    Thanks mister_e,

    That worked at 9600,

    Lost me why DEFINE HSER_SPBRG 129 I cannot find any reference to that in the picbasic manual.

    Also I did get a compiler error about an op code with this:

    @ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF

    I guessed, added a rem before it then added this DEFINE OSC 20 and it compiled ok.

    All I need now is to get to the 19200 and I will be happy.

    Do I need to just change the DEFINE HSER_SPBRG 129 value to reach 19200

    Thank you also for the gif with the diagram, that helped a lot aswell.

    Lory

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Lost me why DEFINE HSER_SPBRG 129 I cannot find any reference to that in the picbasic manual.
    That's for sure... You'll only find DEFINE HSER_SPBRG in the manual. 129 comes from the datasheet. See table 10-4. All the settings are there.

    Also I did get a compiler error about an op code with this:

    @ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF
    These are the configuration fuse for programming your PIC. This line imply that you must use MPASM to compile your code. If you are using MicroCode Studio to edit your code, go to view menu then PIC basic option. Click on the assembler tab then check the Microchip MPASM option. this will work. OR you can define your configuration fuse in a different way too, see the FAQ under setting configuration fuse or something like that.

    All I need now is to get to the 19200 and I will be happy.

    Do I need to just change the DEFINE HSER_SPBRG 129 value to reach 19200
    For baudrate 19200 you'll change only the DEFINE HSER_SPBRG 129 to DEFINE HSER_SPBRG 64

    By the datasheet it suppose to work :0)
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    loredana's Avatar
    loredana Guest


    Did you find this post helpful? Yes | No

    Default PIC16F877 to PC via max232 HELP!!

    Hi Again Steve,

    It worked alright, meaning the 64.

    19,200 and singing, thanks to you.

    I am looking now at another thing, maybe you can help me here too.

    I want the program to wait untill I sent it **play# then react to the digital that follow upto a certain ending character like !

    This compiles and works
    HSERIN [WAIT("**play#")]

    Then I wanted to the processor to pic up the next 6 characters after the **play#, so I send to the PIC **play#123456. looking at other examples the code should be like this:

    HSERIN [WAIT("**play#")STR data\6]

    wait for **play# then then next 6 characters will be present in data, well I wish, I get an error with syntax

    ERROR Line 59: Expected ']'.

    Also I read in the manual STR ArrayVar\n{\c}
    Receive string of n characters optionally ended in character c

    Does this mean I dont have to specify the value of n

    HSERIN [WAIT("**play#")STR data\n\"!"]

    wait for **play# and copy the next characters upto the ! to data.


    Or am I getting totallly mixed up.

    Have you got a few examples I could try.

    Hope I am not imterupting you steve with all these questions.

    Loredana.

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    HSERIN [WAIT("**play#")STR data\6]

    wait for **play# then then next 6 characters will be present in data, well I wish, I get an error with syntax

    ERROR Line 59: Expected ']'.
    you're really close!!! just a ',' is missing + array variable definition

    Code:
        ' Variable Definition
        ' -------------------
        SerData     var   byte [6]
       
    Start:
        ' Program begining/Main Loop
        ' ==========================
        '
    
    
        HSERIN [WAIT("**play#"),STR serdata\6]
        hserout [str serdata\6]
        goto start
    the above will echo you the next 6 character next to your **play#

    Also I read in the manual STR ArrayVar\n{\c}
    Receive string of n characters optionally ended in character c

    Does this mean I dont have to specify the value of n

    HSERIN [WAIT("**play#")STR data\n\"!"]

    wait for **play# and copy the next characters upto the ! to data.
    nop ... wait for **play# and copy the next 'n' character or previous ones if the last one is '!'.

    must be write HSERIN [WAIT("**play#"), STR lala\n\"!"]

    Hope I am not imterupting you steve with all these questions.
    Since i answer to this... no. Don't be afraid. We're all here to learn/help as we can.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    loredana's Avatar
    loredana Guest


    Did you find this post helpful? Yes | No

    Default PIC16F877 to PC via max232 HELP!!

    lala, that was funny, thanks you made me laugh.

    Both these work well steve

    HSERIN [WAIT("**play#"), STR lala\10\"!"]
    hserout [str lala\10]


    Well you said it was ok to ask so I have another.

    loop:
    HSERIN [WAIT("**play#"), STR lala\10\"!"]

    ' **play#hockey!

    ' **play#tennis!

    ' **play#pool!

    ' **play#badmington!

    lala will be contain one of these, can I make them into one string to allow an if statement to branch to different code.

    I see I could do this with simple numbers like 1,2,3,4,5 etc but I want to use strings style names if possible.


    I found some code that some guy had done like this:
    IF relay = 1 THEN outr1 ' if request is for relay#1 then goto relay#1 routine

    I would like to make it like this:


    If lala\10 = "badmington" then badmington


    badmington:
    ' the code for badmington
    GOTO loop

    Is that possible Steve

    Thanks again

    Lory

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Nothing is impossible but i'll prefer use some simple number and use BRANCH or BRANCHL statement for that. Keep it simple at least you can refer to some constant.

    hockey con 0
    tennis con 1
    pool con 2
    badmington con 3


    when you do your tests...
    if lala=hockey then goto hockey

    and others. But i'll still prefer the BRANCH option

    serdata var byte
    sport var byte

    HSERIN [WAIT("**play#"), dec Serdata]
    sport=serdata
    BRANCH sport,[hockey,tennis,pool,badmington]

    For the string request... i'll not using this so... (
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default

    And another thank you from me to Mister_E for helping me get this darn RS232 PIC-PC interface going at 19,200 BAUD.

    Now if I can only get Hilgraeve's HyperTerminal to work properly. That thing looks perfect for sending text files to the PIC for storage on EEPROMs.

    Gros bisou mon minou...

    Robert
    :LOL:
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hey robert,

    Ive never get this damn sh^&^% full of cra%$% bull%$#%$#% plein de marde of this stupid hyperterm work properly... better to build your own software in VB

    this is probably the most crap software, after MPLAB i've never ever see and use since 30 years now

    for a quick link about VB stuff.
    http://www.rentron.com/sending_data.htm
    Last edited by mister_e; - 19th February 2005 at 11:22.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default

    My god, what a piece of crap!

    Full of bugs, nothing works right, and worst of all, a support forum full of threads asking for help with most of them unanswered.

    Disgusting product...

    Robert
    :beurk:
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  12. #12
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Hi!

    Useful link.

    Regards

    Luciano


    * * * * * * *
    Free Terminal program for MCU SW development :
    (No setup, only one EXE file).

    Terminal v1.9b 20040714.
    http://bray.velenje.cx/avr/terminal/

    * * * * * * *

  13. #13
    Terry Blackwell's Avatar
    Terry Blackwell Guest


    Did you find this post helpful? Yes | No

    Default max232

    yes the invert? PIC to PIC a 1 is a 1 or a 0 is a 0 but the max chip turns input 1 from your pic in to an output o so you have to invert a 1 to 0 and a 0 to 1. Hock it up to your PC RS232 port you should get something anyway. I have used Pic16f628 and it works fine I only us MPLAB MPASM when I'm programming PIC you can do a lot and have more control of your PIC.

  14. #14
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default

    Hi Terry,

    Yeah, I'd love to do Assembler as well. I was 2nd in my class when I graduated in '83. And most importantly, I actually loved writing in Assembler, it came naturally.

    But a few, ahem, years have gone by since then and I haven't touched Assembler since. I figure I'll get back into it slowly, once I get this project done. Assembler is the best way to go, total control. But I'm taking WAY longer than originally anticipated when I started this. I didn't even expect to program MCUs when I decided to make my little controller; just buttons and LEDs. Yeah, right...

    Ok, so I knew nothing of electronics when I started 18 months ago, but hey, I'm managing. LOL I'm a little slow in some areas (like 99%), but once I get something going, I keep building on it. I'll stick to BASIC Pro for now, since BASIC was the 1st language I learned, and that was, ahem, the day it came out.

    Fortran, Pascal, RPG, COBOL, name the oldies, I've done them. It's the new generation languages from C and up that I lack. I never thought I'd say that I wished I knew Assembler on the tip of my fingers again though...

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  15. #15
    sundaecone's Avatar
    sundaecone Guest


    Did you find this post helpful? Yes | No

    Default follow-up question T_T

    wow, this thread has been extremely useful, but i have a follow-up question..
    i noticed that the crystal that loredana used was a 20MHz, i just wanna know what the maximum baud rate is for this crystal and also for the 4MHz crystal?
    thanks so much!!! ^_^

  16. #16
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Based on internal USART: all will depend wich BRG setting you're using and % error you want to deal with..

    Let's say PIC16F877 @20MHZ, all the answers are in the datasheet section 10 Table 10-4. Like every PIC that have USART.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Direct PIC to PC without MAX232
    By acjacques in forum Serial
    Replies: 14
    Last Post: - 23rd October 2014, 22:32
  2. PIC18F4680 to PC via MAX232 (RS232 serial) no output
    By opticsteam1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th April 2008, 21:39
  3. PIC16F877 sends/receives data to/from a PC
    By ovidiu in forum Serial
    Replies: 1
    Last Post: - 2nd December 2006, 15:09
  4. USB interface from PIC16F877 to PC
    By headshouter in forum USB
    Replies: 0
    Last Post: - 26th February 2006, 05:58
  5. connecting pic16F877A to pc via max232
    By zikha in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 12th January 2006, 16:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts