PIC16F628, MAX232 and Hi Tech C

Discussion in 'Embedded Systems and Microcontrollers' started by nerdegutta, Sep 27, 2011.

  1. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Hi.

    Been struggling for awhile with this code:

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <htc.h>
    4. #include <conio.h>
    5. #include "usart.h"
    6.  
    7.  
    8. #define _XTAL_FREQ 4000000
    9.  
    10. /* Configuration */
    11. __CONFIG    (WDTDIS & PWRTEN & MCLREN & BOREN & LVPDIS &  UNPROTECT & INTIO);
    12.  
    13. void main(void)
    14. {
    15.     unsigned char input;
    16.  
    17.     INTCON=0;    // purpose of disabling the interrupts.
    18.  
    19.     init_comms();    // set up the USART - settings defined in usart.h
    20.  
    21.     // Output a message to prompt the user for a keypress    
    22.     printf("\rPress a key and I will echo it back:\n\r");
    23.  
    24. while(1)
    25. {
    26.         input = getchar();    // read a response from the user
    27.          printf("\n\rI detected [%c]",input);    // echo it back
    28. } // end while
    29. } // end main
    30.  
    I've used the attached schematic, and I found the code in the Hi Tech C sample directory.

    It's connected to Hyperterminal, and when I push the reset button, I get a message on the screen saying:"Press a key and I will echo it back:".

    When I press any key, it doesn't respond. When I press the reset button, it starts over.

    I've tried with getchar, getch, getche, all found in the headerfiles, and no errors while compiling.

    I've tested the cable, and it seems to work ok.

    What could cause this?

    I'm using MPLAB 8.63, Hi Tech C v9.80
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    If you get data from your PIC. You know at least that something are correct. Like the baud rate generator. And that at least the transmitter is enabled correct. Your MAX232 may also be good at least for the transmitter part. Have you done some debugging to see if the register concerning receiving are correct? Also remember the KISS rule. Instead of sending a message like detected.... using the printf function. Just put the received char into the transmitter at register level.
    You can do this by polling the RCIF interrupt flag bit of the PIR1. The
    PIR1 register is set whenever the EUSART receiver is enabled and there is an unread character in the receive FIFO. Then read the RCREG and put the content into the TXREG. If you type one char at the time in hyperterm this will work as simple test​
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    One thing come mind Rottetryne. Can you open the dissambly Listing. And post a cut out of the putch function
     
  4. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Do you mean this:

    Code ( (Unknown Language)):
    1.  
    2. 1:                 #include <htc.h>
    3. 2:                 #include <stdio.h>
    4. 3:                 #include "usart.h"
    5. 4:                
    6. 5:                 void
    7. 6:                 putch(unsigned char byte)
    8. 7:                 {
    9.    7BF    00F0     MOVWF 0x70
    10. 8:                     /* output one byte */
    11. 9:                     while(!TXIF)    /* set when register is empty */
    12.    7C0    1283     BCF 0x3, 0x5
    13.    7C1    1E0C     BTFSS 0xc, 0x4
    14.    7C2    2FC0     GOTO 0x7c0
    15. 10:                        continue;
    16. 11:                    TXREG = byte;
    17.    7C3    0870     MOVF 0x70, W
    18.    7C4    0099     MOVWF 0x19
    19. 12:                }
    20.    7C5    0008     RETURN
    21. 13:                
    22. 14:                unsigned char
    23. 15:                getch() {
    24. 16:                    /* retrieve one byte */
    25. 17:                    while(!RCIF)    /* set when register is not empty */
    26.    7B5    1283     BCF 0x3, 0x5
    27.    7B6    1E8C     BTFSS 0xc, 0x5
    28.    7B7    2FB5     GOTO 0x7b5
    29. 18:                        continue;
    30. 19:                    return RCREG;    
    31.    7B8    081A     MOVF 0x1a, W
    32. 20:                }
    33.    7B9    0008     RETURN
    34. 21:                
    35. 22:                unsigned char
    36. 23:                getche(void)
    37. 24:                {
    38. 25:                    unsigned char c;
    39. 26:                    putch(c = getch());
    40.    7BA    27B5     CALL 0x7b5
    41.    7BB    00F1     MOVWF 0x71
    42.    7BC    27BF     CALL 0x7bf
    43. 27:                    return c;
    44.    7BD    0871     MOVF 0x71, W
    45. 28:                }
    46.    7BE    0008     RETURN
    47.  
    Before I got new glasses, I was more like Harry Potter. LOL
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    As far as I can see you have the correct code for putch and getch. Then you use the printf function in HI-Tech C. You must provide your own putch function. And by so far it looks OK. Did you test a more simple version working directly on the register level. Do you have a scope. So you can test the output from max232 chip. It could be something wrong so you do not receive anything on the PIC side
     
  6. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    If instead of connecting them to the PIC, you connect the wires going to RX and TX together, anything you type into hyperterminal should get echoed back. If not the problem is not the PIC.
     
  7. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Nope, no scope. :(

    When I measure with my digital voltmeter:

    From PC, RX (pin 2) is -8.98v. When I press reset button, it jumps down to -7.99v. When I press a key in HyperTerminal, nothing.
    From PC, TX (pin 3) is -11.25v. When I press reset, nothing. Nothing from HyperTerm.

    From PIC, RB1 (pin 7) is 5.38v, nothing happens when I press reset or in HyperTerm.
    From PIC, RB2 (pin 8) is 5.39v, when I press reset, it jumps down to 4.65v, nothing from HyperTerm.
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    That was a good one...:rolleyes:
     
  9. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Do I dare connect them?
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    A digital multimeter are way to slow to catch the RS232 signal changes. Have you turned off the flow controll in Hyper Term
     
  11. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    I know its too slow, but it's what I have...

    I thing I've tried nearly all settings in HyperTerminal.:(
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You do have protective goggles I guess...:p Just joking. This will be safe it would be like connecting one output to an input not on the same gate on a logic circuit. On the Max232 you connect pin 12 and 11 together. But disconnect the pic first
     
  13. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Ok, I will try tomorrow.

    Thanks.
     
  14. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Dunno if it's any use but this works fine on the 16F628A, it's in assembler. Probably best to ground pins 11 and 12 on the PIC so they aren't floating. Just type in numbers and see if anything changes.
    http://www.marksphotos.info/fg/
     
  15. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    I see you have:
    Code ( (Unknown Language)):
    1.  
    2. movlw b'11110010'       ; RB7-RB4 and RB1(RX)=input, others output
    3.  
    in your code. I guess the important here is the RB1 pin...?

    Could I be missing something like this...?

    I can't check, 'cause I'm at work, but I think I remember something like

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #define RX_PIN TRISB1
    4. #define TX_PIN TRISB2
    5.  
    6.  
    in the uart.h file. Perhaps the pin configuration for the RX_PIN is set the wrong way. Need to check later...
     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This can be somewhat confusing but the data sheet (16f628A) say
     
  17. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    OK. If I remember correct, both are set as 1 in the uart.h - file.
     
  18. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The datasheet must be inaccurate, because that code is working fine with RB2 as an output. Maybe it also works with it as an input, I haven't tried, I just copied some sample code from somewhere.
     
  19. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,515
    785
    Lucky me, when it comes to computers, cards and laptop, I'm a bit of a hoarder... :eek:

    I found a laptop with XP, startet HyperTerminal, and everything was working.

    I guess this means that the com port is fired - Um that's fried, but fired works also... :)

    Thanks for you effort.
     
  20. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    DoH! So that was the error. Anyway since you are so good at finding things. USB to RS232 dongles work OK in most cases. At least for simple work of the type you are doing now. It may take some googling if you do not have a driver
     
Loading...