[PIC] Will this program work? (Also need HW buying advice)

Discussion in 'Embedded Systems and Microcontrollers' started by kurian2z5, Jan 8, 2009.

  1. kurian2z5

    Thread Starter New Member

    Jan 8, 2009
    1
    0
    I don't own any hardware yet, but I just wrote my first program in Hi-Tech C and compiled it for a PIC16F877A (which I'm planning on getting). Can some one tell me if this will work before I actually buy the hardware and try to flash it?

    The goal is simple: The PIC receives RS232 character input from a PC. If it receives capital letters A-H, then the corresponding output PORTD pin RD0-RD7 should be activated. If it receives small letters a-h, the corresponding PORTD output pin RD0-RD7 should be disabled.

    The program is compiling successfully. The board I'm getting has a 20MHz crystal, so I've changed the value in the header file.

    main.c
    Code ( (Unknown Language)):
    1. //Compiled for PIC16F877A
    2. #include <stdio.h>
    3. #include <htc.h>
    4. #include "usart.h"
    5.  
    6. /* A simple demonstration of serial communications which
    7.  * incorporates the on-board hardware USART of the Microchip
    8.  * PIC16Fxxx series of devices. */
    9.  
    10. void main(void)
    11. {
    12.     unsigned char input;
    13.  
    14.     INTCON=0;    // purpose of disabling the interrupts.
    15.  
    16.     init_comms();    // set up the USART - settings defined in usart.h
    17.     TRISD = 0;    // make all PORTD bits output
    18.     PORTD = 0;    // initialize all bits to OFF
    19.    
    20.     printf("A-H to turn on, a-h to turn off:\n\r");
    21.     while(1)
    22.     {
    23.         input = getch();
    24.         printf("[%c]", input);
    25.        
    26.         if ((input >= 'A') && (input <='H'))
    27.         {
    28.             switch(input)
    29.             {
    30.                 case 'A':
    31.                     RD0 = 1;
    32.                     break;
    33.                 case 'B':
    34.                     RD1 = 1;
    35.                     break;
    36.                 case 'C':
    37.                     RD2 = 1;
    38.                     break;
    39.                 case 'D':
    40.                     RD3 = 1;
    41.                     break;
    42.                 case 'E':
    43.                     RD4 = 1;
    44.                     break;
    45.                 case 'F':
    46.                     RD5 = 1;
    47.                     break;
    48.                 case 'G':
    49.                     RD6 = 1;
    50.                     break;
    51.                 case 'H':
    52.                     RD7 = 1;
    53.             }
    54.             printf(" on.\n\r");
    55.         }
    56.         else if ((input >= 'a') && (input <='h'))
    57.         {
    58.             switch(input)
    59.             {
    60.                 case 'a':
    61.                     RD0 = 0;
    62.                     break;
    63.                 case 'b':
    64.                     RD1 = 0;
    65.                     break;
    66.                 case 'c':
    67.                     RD2 = 0;
    68.                     break;
    69.                 case 'd':
    70.                     RD3 = 0;
    71.                     break;
    72.                 case 'e':
    73.                     RD4 = 0;
    74.                     break;
    75.                 case 'f':
    76.                     RD5 = 0;
    77.                     break;
    78.                 case 'g':
    79.                     RD6 = 0;
    80.                     break;
    81.                 case 'h':
    82.                     RD7 = 0;
    83.             }
    84.             printf(" off.\n\r");
    85.         }
    86.         else
    87.         {
    88.             printf(" unrecognized.\n\r");
    89.         }
    90.     }
    91. }
    92.  
    usart.h
    Code ( (Unknown Language)):
    1. #ifndef _SERIAL_H_
    2. #define _SERIAL_H_
    3.  
    4. #define BAUD 19200
    5. #define FOSC 20000000L
    6. #define NINE 0     /* Use 9bit communication? FALSE=8bit */
    7.  
    8. #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
    9. #define HIGH_SPEED 1
    10.  
    11. #if NINE == 1
    12. #define NINE_BITS 0x40
    13. #else
    14. #define NINE_BITS 0
    15. #endif
    16.  
    17. #if HIGH_SPEED == 1
    18. #define SPEED 0x4
    19. #else
    20. #define SPEED 0
    21. #endif
    22.  
    23. #if defined(_16F87) || defined(_16F88)
    24.     #define RX_PIN TRISB2
    25.     #define TX_PIN TRISB5
    26. #else
    27.     #define RX_PIN TRISC7
    28.     #define TX_PIN TRISC6
    29. #endif
    30.  
    31. /* Serial initialization */
    32. #define init_comms()\
    33.     RX_PIN = 1;    \
    34.     TX_PIN = 1;          \
    35.     SPBRG = DIVIDER;         \
    36.     RCSTA = (NINE_BITS|0x90);    \
    37.     TXSTA = (SPEED|NINE_BITS|0x20)
    38.  
    39. void putch(unsigned char);
    40. unsigned char getch(void);
    41. unsigned char getche(void);
    42.  
    43. #endif
    44.  
    usart.c
    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. #include <stdio.h>
    3. #include "usart.h"
    4.  
    5. void
    6. putch(unsigned char byte)
    7. {
    8.     /* output one byte */
    9.     while(!TXIF)    /* set when register is empty */
    10.         continue;
    11.     TXREG = byte;
    12. }
    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.  
    22. unsigned char
    23. getche(void)
    24. {
    25.     unsigned char c;
    26.     putch(c = getch());
    27.     return c;
    28. }
    29.  
    30.  
     
Loading...