PIC16F628 RS232 - Conditional Processing (ASM)

Discussion in 'Embedded Systems and Microcontrollers' started by BillGeek, Oct 26, 2009.

  1. BillGeek

    Thread Starter New Member

    Oct 14, 2009
    Hi All

    I was following a tutorial online regarding RS-232 communication using the MAX232 chip. (Tutorial is called "PIC16F628 alive" - see http://www.oz1bxm.dk/PIC/628uart.htm) Everything is working and I understand most of the code that was used in the tutorial. So when I throw power onto the board, I get a message saying "16F628 alive", and when I type characters, they echo back to the PC. Everything is good. :)

    Now I want to build logic based on different signals received from the connection. For example, if I send character "A" through COM, I want the LED connected to PORTA.0 to light up. If I send character "B", LED on PORTA.1 should light up.

    I understand how to build the circuit, yet I have absolutely no clue how to determine which character is sent to the PIC. From the URL above, I know I have to replace the "send" call in the main loop with my own routine. What I have tried so far:
    Code ( (Unknown Language)):
    2. ; MAIN LOOP
    3.     call message
    4. loop
    5.     call receive
    6.     call process
    7.     goto loop
    9. process
    10.     subwf 65,W
    11.     btfsc W,0
    12.     call LightRed
    13.     btfss W,0
    14.     call LightGreen
    15.     return
    17. LightRed
    18.     bsf PORTA,0
    19.     bcf PORTA,1
    20.     return;
    22. LightGreen
    23.     bsf PORTA,1
    24.     bcf PORTA,0
    25.     return;
    As the process "method" will only fire when receive actually gets something, the LED's should be lit until the next character is received.

    For some reason, none of the above works though. I can't get either one of the LED's to light up no matter what I do. Now, my understanding of my code is this:
    Subract 65 from the W register (65 = A ascii value)
    If bit 0 of the W register is set, light the RED led
    If bit 0 of the W register is not set, light the GREEN led

    Is my understanding correct? Is there something wrong in the code? I doubt that either of the chips are damaged (when I program the sample code back I get the result as in the tutorial) so I assume it must be my code that's "dodgy"...
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    If you send a 'A' from your PC you will send 1 byte containing the number 65, so you are correct so far. Then you have done the subtraction check the Z(ero) bit in the status register
  3. Danimal

    New Member

    May 7, 2008
    This might be a dumb question, but did you change the tristate value for port A in the initialization section of the code? That would be a good reason for neither LED coming on...
  4. Boo


    Oct 27, 2009
    Your code is wrong lol

    try this :

    Code ( (Unknown Language)):
    2. process
    3.           sublw 65,W
    4.           btfsc Status,Z
    5.           call LightRed
    6.           call LightGreen
    7.           return
  5. BillGeek

    Thread Starter New Member

    Oct 14, 2009
    Sorry guys, burnt out the last PIC I had, so I had to wait to grab some more...

    @danimal: Nope, I did not. :) I completely forgot to set the direction of the ports upon initialization. After changing this though, I still get only the red LED to light up.

    @boo: I read somewhere that BTFSS and BTFSC cannot be used on the W register, so I tried modifying the code to the STATUS register. (bit Z) Even after doing exactly what you did above, it still didn't work. For some strange reason, the GREEN LED keeps lighting up now, implying the Z bit is always clear???

    I think there's something fundamentally wrong with my code here... I will try changing everything to binary tonight and completely remove the decimal values out of the code. This way I will be able to keep better track of what it is I'm doing. :)

    In addition, I also think there's something wrong with the software I'm using to communicate to the PIC. It's a basic "test" program I wrote in C# (2005), but I'm using the "WriteLine" method of the SerialPort. I suspect that this doesn't push the correct data through the COM port, so I'll try using the "write" method instead, which only dumps bytes on the COM port.

    I will post back as soon as I find anything.