16F877a rs232 hitech c

Discussion in 'Embedded Systems and Microcontrollers' started by varunme, Oct 6, 2011.

  1. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    Is the code below correct ? , not working in 16F877a

    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4. #include <stdio.h>
    5.  #include <htc.h>
    6.  #include "usart.h"
    7.  
    8. #ifndef _XTAL_FREQ
    9. // Unless specified elsewhere, 4MHz system frequency is assumed
    10. #define _XTAL_FREQ 20000000
    11. #endif
    12.  
    13. __CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT);
    14.  
    15.  
    16.  /* A simple demonstration of serial communications which
    17.   * incorporates the on-board hardware USART of the Microchip
    18.   * PIC16Fxxx series of devices. */
    19.  
    20.  
    21.  
    22.  
    23.  void putch(unsigned char byte)
    24.  {
    25.     /* output one byte */
    26.     while(!TXIF)    /* set when register is empty */
    27.         continue;
    28.     TXREG = byte;
    29. }
    30.  
    31. unsigned char
    32. getch() {
    33.     /* retrieve one byte */
    34.     while(!RCIF)    /* set when register is not empty */
    35.         continue;
    36.     return RCREG;  
    37. }
    38.  
    39. unsigned char
    40. getche(void)
    41. {
    42.     unsigned char c;
    43.     putch(c = getch());
    44.     return c;
    45. }
    46.  
    47.  
    48.  
    49.  
    50.  
    51. void main(void){
    52.  
    53.      TRISD=0;
    54.      PORTD=0;
    55.      unsigned char input;
    56.  
    57.      INTCON=0;    // purpose of disabling the interrupts.
    58.  
    59.      init_comms();    // set up the USART - settings defined in usart.h
    60.  
    61.      while(1){
    62.  
    63.         char rxbyte;
    64.        
    65.         rxbyte= getch();
    66.         switch(rxbyte)
    67.         {
    68.             case '1':
    69.             PORTD=0xFF;
    70.             putch("hhh");
    71.             __delay_ms(20);
    72.             break;
    73.  
    74.             case 'b':
    75.             PORTD=0x00;
    76.             putch("hhh");
    77.             __delay_ms(20);
    78.             break;
    79.        
    80.             default :
    81.             break;
    82.  
    83.  
    84.    
    85.         }
    86.  
    87.      }
    88.  }
    89.  
    90.  
    91.  
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    What IDE and compiler are you using?

    What do you want it to do?

    Do you get any error messages when compiling?

    Perhaps you need the "usart.h" - file in the project directory?
     
  3. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    Hitech C ,I want to enable portD while sending '1' and disable by sending 'b',
    no error messages while compiling
    Usart.h in same directory
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Do you use MPLAB also?
     
  5. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    yes , using MPLAB ide and compiling with hitech c
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Microchip do have a lot of micros. your program will not work unless you modify the usart.h file to fit your PIC. Read section 10 of the data sheet. Then modify the code so it fits your chip. Also note that the serial example is made a 4 MHz clock speed. You use 20 MHz as clock speed. Do your changes and feel free to post your modified usart.h if you need advice
    In order to be able to program, you must also be able to debug. Take a look at the first post here if you need some help in debugging with MPLAB
    http://forum.allaboutcircuits.com/showthread.php?t=44852
     
    varunme likes this.
  7. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    with this 20Mhz , it worked with just echoing the input text , with the same micro
     
  8. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    usart.h

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #ifndef _SERIAL_H_
    4. #define _SERIAL_H_
    5.  
    6. #define BAUD 9600      
    7. #define FOSC 20000000L
    8. #define NINE 0     /* Use 9bit communication? FALSE=8bit */
    9.  
    10. #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
    11. #define HIGH_SPEED 1
    12.  
    13. #if NINE == 1
    14. #define NINE_BITS 0x40
    15. #else
    16. #define NINE_BITS 0
    17. #endif
    18.  
    19. #if HIGH_SPEED == 1
    20. #define SPEED 0x4
    21. #else
    22. #define SPEED 0
    23. #endif
    24.  
    25. #if defined(_16F87) || defined(_16F88)
    26.     #define RX_PIN TRISB2
    27.     #define TX_PIN TRISB5
    28. #else
    29.     #define RX_PIN TRISC7
    30.     #define TX_PIN TRISC6
    31. #endif
    32.  
    33. /* Serial initialization */
    34. #define init_comms()\
    35.     RX_PIN = 1;    \
    36.     TX_PIN = 1;          \
    37.     SPBRG = DIVIDER;         \
    38.     RCSTA = (NINE_BITS|0x90);    \
    39.     TXSTA = (SPEED|NINE_BITS|0x20)
    40.  
    41. void putch(unsigned char);
    42. unsigned char getch(void);
    43. unsigned char getche(void);
    44.  
    45. #endif
    46.  
    47.  
    48.  
    yes , once i complete editing , i will inform u
     
    Last edited: Oct 6, 2011
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Your usart.h seems to have the correct values. And since your are able to do the echo test. Your setup should be correct.
    I noticed in your code you have this
    Code ( (Unknown Language)):
    1.  
    2. while(1){
    3.  
    4. char rxbyte;
    5.  
    6. rxbyte= getch();
    7. switch(rxbyte)
    8. {
    9. case '1':
    10. PORTD=0xFF;
    11. putch("hhh");
    12. __delay_ms(20);
    13. break;
    14.  
    15. case 'b':
    16. PORTD=0x00;
    17. putch("hhh");
    18. __delay_ms(20);
    19. break;
    20.  
    21. default :
    22. break;
    23. }
    24.  
    25.  
    In the data sheet you will find this
    Your code is somewhat dodgy. As it may create a deadlock situation. It is much better to check (poll) (PIE1<5>). The RCIF Flag bit. If it set you can take some action else do nothing regarding serial port read. Then you are more experienced perhaps you can create a interrupt service routine. Handling this event. But for your simple testing this is not needed. ​
     
    varunme likes this.
  10. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    how can i transfer data from one register to another (example from RSR to RCREG) ? , can it by direct transfer (eg: RSR=RCREG)? , or by using temp variable ? , can show one example ?
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    In C "RSR=RCREG;" would be correct.
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Which version of HI-Tech C do you use? You can use check on bit level also
     
  13. varunme

    Thread Starter Member

    Sep 29, 2011
    59
    0
    I am using hitech 9.80 for pic 16
     
Loading...