pic uart not receiving

Discussion in 'Embedded Systems and Microcontrollers' started by Andy206, Dec 30, 2010.

  1. Andy206

    Thread Starter New Member

    Nov 17, 2010
    Hi everyone,

    I'm trying to get my pic16f690 to talk rs232... using the usual max232 with 4 caps, really not worth posting a diagram i think.
    It's actually working quite well, in that i can transmit from the pic just fine, going out to my hyperterminal without problems. Problem is on the receiving end, it just doesn't want to receive anything! I've checked with a logic probe on the actual pin of the pic (pin 12), i'm definitely getting data there. someone else seems to have had problems with logic levels before, checked that as well - my high is 4.2V which I'd think should more than do it... just in case i put a 2.2k to Vdd, no difference.
    Code is attached below, it's the demo that came with mplab... I've modified it to use interrupts and spit out all sorts of debug info, all i find is that FERR is set but not OERR, and i'm not getting an interrupt - RCREG is always 0. I went back to the basic version below, same thing. I've tried using 2400 baud instead, no difference either. I'm really stuck... hoping for someone to spot some silly beginner's mistake...

    Thanks heaps!

    Code ( (Unknown Language)):
    2. #ifndef _SERIAL_H_
    3. #define _SERIAL_H_
    4. #define BAUD 9600      
    5. #define FOSC 4000000L
    6. #define NINE 0     /* Use 9bit communication? FALSE=8bit */
    7. #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
    8. #define HIGH_SPEED 1
    9. #if NINE == 1
    10. #define NINE_BITS 0x40
    11. #else
    12. #define NINE_BITS 0
    13. #endif
    14. #if HIGH_SPEED == 1
    15. #define SPEED 0x4
    16. #else
    17. #define SPEED 0
    18. #endif
    20. #define RX_PIN TRISB5
    21. #define TX_PIN TRISB7
    22. /* Serial initialization */
    23. #define init_comms()\
    24. RX_PIN = 1; \
    25. TX_PIN = 1;    \
    26. SPBRG = DIVIDER;      \
    27. RCSTA = (NINE_BITS|0x90); \
    28. TXSTA = (SPEED|NINE_BITS|0x20)
    29. void putch(unsigned char);
    30. unsigned char getch(void);
    31. unsigned char getche(void);
    32. #endif
    Code ( (Unknown Language)):
    2. #include <htc.h>
    3. #include <stdio.h>
    4. #include "usart.h"
    5. /* Serial initialization */
    6. void
    7. putch(unsigned char byte)
    8. {
    9. /* output one byte */
    10. while(!TXIF) /* set when register is empty */
    11. continue;
    12. TXREG = byte;
    13. }
    14. unsigned char
    15. getch() {
    16. /* retrieve one byte */
    17. while(!RCIF) /* set when register is not empty */
    18. continue;
    19. return RCREG;
    20. }
    21. unsigned char
    22. getche(void)
    23. {
    24. unsigned char c;
    25. putch(c = getch());
    26. return c;
    27. }
    Code ( (Unknown Language)):
    2. #include <stdio.h>
    3. #include <htc.h>
    4. #include "usart.h"
    5. /* A simple demonstration of serial communications which
    6.  * incorporates the on-board hardware USART of the Microchip
    7.  * PIC16Fxxx series of devices. */
    9. void main(void){
    10.  unsigned char input;
    11.  INTCON=0; // purpose of disabling the interrupts.
    12.  init_comms(); // set up the USART - settings defined in usart.h
    13.  // Output a message to prompt the user for a keypress
    14.  printf("\rPress a key and I will echo it back:\n");
    15.  while(1){
    16.   input = getch(); // read a response from the user
    17.   printf("\rI detected [%c]",input); // echo it back
    18.  }
    19. }
  2. Andy206

    Thread Starter New Member

    Nov 17, 2010
    After 8h+ of experimenting I finally found the problem: I have to initialise ANSEL/ANSELH to 0! d'oh! knew it'd be something simple... bit surprised though it's not in the demo software but hey, it's working now.
    Hope it maybe saves someone else some time at some point.
  3. Mosaic


    Aug 2, 2010
    The Usart via the max232 shifts voltage levels. Is there a way to do this via serial usb without shifting levels?

    I need some advice on serial via usb. I think i need a s'ware driver on the windows side and then do I use standard Usart protocols for comms?
  4. Andy206

    Thread Starter New Member

    Nov 17, 2010
    correct... that's why you need something like the max232. rs232 doesn't use ttl levels, that's just just how it works... officially you want +12 for H and -12 for L. However, the standard does require the receiver to understand everything down to +3/-3V, so sometimes you can trick it a bit by using +5/-5 or something like that. In any case, you'd still have to get -5V.

    If you want to use usb instead you'd need a chip with a usb interface, different story.

    Serial usb is the same as a "normal" serial port, you don't get any other benefits out of that (other than being able to use it on newer computers which don't generally have a serial port anymore). And yes, you need a driver, which will emulate the com port to windows (or whatever else you're using).

    So really, there's no difference at all between using a "normal" serial port or a usb serial port.

    And just a note on the side, you should probably open a new thread for a new topic... doesn't really have anything to do with the initial problem in this thread.