Serial coms with a pc


Closed Thread
Results 1 to 14 of 14
  1. #1

    Question Serial coms with a pc

    Hi there

    I have built a circuit with a pic16f627 and a so232acp chip. The idea is to read an input and display it on hyper terminal and toggle two leds if I send a command from the pc, I am using the on board usart.
    The transmitting from the pic work 100% but I can not seem to get it responding to the commands from the pc. Can anyone please help; here is the testing code I use.

    INCLUDE "ANSI.INC"

    CMCON = %00000111 ;Disable Comparator module's
    OPTION_REG = %11010111 ;Set PIC options
    INTCON = 0 ;Disable interrupts
    TRISB = %00000000 ;RB7...RB0 are outputs.
    TRISA = 255 ;SET PORTA AS INPUTS

    DEFINE HSER_RCSTA 90h ;Set receive register to receiver enabled
    DEFINE HSER_TXSTA 20h ;Set transmit regester to transmitter enabled
    DEFINE HSER_BAUD 2400 ;Set baud rate
    DEFINE HSER_SPBRG 25 ;Set spbrg regester




    I VAR BYTE
    I = 0
    PORTB.6 = 1

    loop:
    I =0
    @ ClearScr ; Clear Screen
    HSEROUT ["Value of input 1:",DEC PORTA.0,10,13]

    HSEROUT ["Please select a command",10,13]
    HSEROUT [9,"1 = Flash leds",10,13]


    HSERIN 5000,LOOP,[I]

    IF I = 1 THEN
    PORTB.7 = 1
    PORTB.6 = 0
    PAUSE 1000
    PORTB.7 = 0
    PORTB.6 = 1
    GOTO LOOP

    ELSE

    GOTO LOOP

    ENDIF

    End

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947

    Default

    Try checking for the condition

    If i = "1" then

    since the keypressed from the pc is a printable ascii character.

    Jerson

  3. #3
    skimask's Avatar
    skimask Guest

    Default

    Code:
    INCLUDE  "ANSI.INC"
    cmcon=7:option_reg=$d7:intcon=0:trisb=0:trisa=$ff
    DEFINE HSER_RCSTA 90h	;Set receive register to receiver enabled
    DEFINE HSER_TXSTA 20h	;Set transmit regester to transmitter enabled
    DEFINE HSER_BAUD 2400	;Set baud rate
    DEFINE HSER_SPBRG 25	;Set spbrg regester
    temp var byte:I VAR BYTE:I = 0
    loop:
    I =0
    @ ClearScr         ; Clear Screen
    hserout ["Value of input 1:",dec porta.0,10,13]
    hserout ["Please select a command",10,13]
    hserout [9,"1 = Flash 1 LED",10,13]:hserout [9,"2 = Flash 2 LED",10,13]
    hserout [9,"3 = Flash Both LED",10,13]:hserin 5000,loop,[I]
    IF I = "1" THEN
    for temp = 1 to 10:PORTB.7 = 1:pause 1000:portb.7=0:pause 1000:next temp
    endif
    if i = "2" then
    for temp = 1 to 10:portb.6=1:pause 1000:portb.6=0:pause 1000:next temp
    endif
    if i="3" then
    for temp = 1 to 10:portb.6=1:portb.7=1:pause 1000:portb.6=0
    portb.7=0:pause 1000:next temp
    endif
    GOTO LOOP
    End
    Try that out...

    EDIT: DOH! Jerson beat me to it!

  4. #4

    Default

    Thank you for the reply, but stil nothing.
    I am starting to think my hardware is faulty. I have written a program that acording to me could ecco what is sended to the pic. do you agree with the program, if it is correct i definitly need to start looking at my hardware.
    Thank you again for al the help

    INCLUDE "ANSI.INC"

    CMCON = %00000111 ;Disable Comparator module's
    OPTION_REG = %11010111 ;Set PIC options
    INTCON = 0 ;Disable interrupts
    TRISB = %00000000 ;RB7...RB0 are outputs.
    TRISA = 255 ;SET PORTA AS INPUTS

    DEFINE HSER_RCSTA 90h ;Set receive register to receiver enabled
    DEFINE HSER_TXSTA 20h ;Set transmit regester to transmitter enabled
    DEFINE HSER_BAUD 2400 ;Set baud rate
    DEFINE HSER_SPBRG 25 ;Set spbrg regester

    I VAR BYTE
    I = 20

    loop:
    @ ClearScr ; Clear Screen
    HSEROUT ["Value of I:",I,10,13]
    HSERIN 5000,LOOP,[I]
    GOTO loop
    End

  5. #5
    skimask's Avatar
    skimask Guest

    Default

    Loopback with lower case to upper case translation (shows the PIC is actually working and that the PC isn't just receiving the same thing it sent out, i.e. local echo):

    Code:
    include "modedefs.bas"
    cmcon=7:option_reg=$d7:trisb=0:trisa=$ff:i var byte
    main:
    serin portb.1,n2400,I:if (i > 96) and (i < 123) then i = i - 32
    serout portb.2,n2400,[I]:goto main
    And I just noticed something in the code in your last post. Take out the clear screen and if anything happens...

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

    Default

    i just copy/paste your code and, yes indeed there's few mistake.

    first..
    Code:
    DEFINE HSER_RCSTA 90h ;Set receive register to receiver enabled
    DEFINE HSER_TXSTA 20h ;Set transmit regester to transmitter enabled
    DEFINE HSER_BAUD 2400 ;Set baud rate
    DEFINE HSER_SPBRG 25 ;Set spbrg regester
    In red, you overwrite PBP calcs. This result of a bad SPBRG setting for a TXSTA=20. Also, as you're using HSERIN, i would suggest you to use HSER_CLROERR feature to clear all possible overflow error. The correct declaration should be
    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 25  ' 9600 Baud @ 4MHz, 0.16%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    Even with this, you have configure the RX pin as output so change TRISB to
    Code:
    TRISB = %00000010 ' RB1(RX) as input, others to Output
    that sort few error, but unless you want to have a full-filled of "Value of I:" screen, i'll suggest you to remove the timeout delay and it's jump label
    Code:
    HSERIN [i]
    This way, the program will sit there and wait for a user entry.


    EVEN with the above correction done, you may have some problem if you're using the internal OSC, if your "Value of I" message is messy, use an external one OR try with a lower baudrate. Here, it doesn't work @9600 baud with the internal OSC. The output looks like
    VX• ˁ'‘
    i don't know for others... but i can't understand what the above ask me


    the full corrected code, with config fuse setting for MPASM and a external 4MHz crystal, but without the ANSI feature looks like
    Code:
        ;INCLUDE "ANSI.INC"
        @ __CONFIG _XT_OSC  & _MCLRE_ON  &  _LVP_OFF & _WDT_OFF & _PWRTE_ON  & _BODEN_ON  
        CMCON = %00000111 ;Disable Comparator module's
        OPTION_REG = %11010111 ;Set PIC options
        INTCON = 0 ;Disable interrupts
        TRISB = %00000010 ' RB1(RX) as input, others to Output
        TRISA = 255 ;SET PORTA AS INPUTS
        
        DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
        DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
        DEFINE HSER_SPBRG 25  ' 9600 Baud @ 4MHz, 0.16%
        DEFINE HSER_CLROERR 1 ' Clear overflow automatically
        
        
        I VAR BYTE
        I = 20
    
    loop:
        ;@ ClearScr ; Clear Screen
        HSEROUT ["Value of I:",I,10,13]
        HSERIN [I]
        GOTO loop
        END
    
    More information on config fuse and possible compilation warning messages at the following link
    http://www.picbasic.co.uk/forum/showthread.php?t=543

    hth
    Last edited by mister_e; - 17th June 2007 at 20:14.
    Steve

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

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Well, I'm always looking for "comments" on my programs.

    I guess "commenting" them out qualifies.

    ANSI.INC

    .
    DT

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

    Default

    don't take it personal.. i hate Hyperterminal, sure it has to work with.
    Steve

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

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    And the best part is ... you don't have to use Hyperterminal.

    Any terminal program that can emulate ANSI will do.

    Even a DOS window with ANSI.SYS loaded. Yuck!

    .
    DT

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

    Default

    Deuh... but MisteEZCOM one don't do it

    Anyhow, proof it's working...

    I prefer RealTerm... but... erm, MisterEZCOM is not bad to...

    Code:
        @ ForColor yellow               ; Set foreground (text) color to yellow
        @ BackColor blue                ; Set background color to blue
    
    loop:
        @ ClearScr ; Clear Screen
        HSEROUT ["Value of I:",I,10,13]
        HSERIN [i]
        GOTO loop
        END
    Feel better now ?
    Attached Images Attached Images  
    Last edited by mister_e; - 17th June 2007 at 23:33.
    Steve

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

  11. #11
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Feel better now ?
    Well yes, actually, I do.

    Thank you!

    .
    DT

  12. #12
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    P.S.

    Where do I get this MisterEZCOM?

    Must have missed that thread.
    .
    DT

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

    Default

    It's not released yet, so you didn't missed it
    Steve

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

  14. #14

    Thumbs up

    Thank you all

Similar Threads

  1. PIC18F4680 to PC via MAX232 (RS232 serial) no output
    By opticsteam1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th April 2008, 20:39
  2. interfacing to the pc serial port
    By kelangfei in forum General
    Replies: 4
    Last Post: - 7th October 2007, 22:35
  3. PC serial port funny
    By nicjo in forum Serial
    Replies: 13
    Last Post: - 6th February 2007, 05:34
  4. Replies: 2
    Last Post: - 28th April 2006, 12:10
  5. Interrupt and Serial communication to PC.
    By obaskirt in forum mel PIC BASIC
    Replies: 2
    Last Post: - 17th June 2005, 20:01

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