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

Thread Starter

kurian2z5

Joined Jan 8, 2009
1
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
Rich (BB code):
//Compiled for PIC16F877A
#include <stdio.h>
#include <htc.h>
#include "usart.h"

/* A simple demonstration of serial communications which
 * incorporates the on-board hardware USART of the Microchip
 * PIC16Fxxx series of devices. */

void main(void)
{
    unsigned char input;

    INTCON=0;    // purpose of disabling the interrupts.

    init_comms();    // set up the USART - settings defined in usart.h
    TRISD = 0;    // make all PORTD bits output
    PORTD = 0;    // initialize all bits to OFF
    
    printf("A-H to turn on, a-h to turn off:\n\r");
    while(1)
    {
        input = getch();
        printf("[%c]", input);
        
        if ((input >= 'A') && (input <='H'))
        {
            switch(input)
            {
                case 'A':
                    RD0 = 1;
                    break;
                case 'B':
                    RD1 = 1;
                    break;
                case 'C':
                    RD2 = 1;
                    break;
                case 'D':
                    RD3 = 1;
                    break;
                case 'E':
                    RD4 = 1;
                    break;
                case 'F':
                    RD5 = 1;
                    break;
                case 'G':
                    RD6 = 1;
                    break;
                case 'H':
                    RD7 = 1;
            }
            printf(" on.\n\r");
        }
        else if ((input >= 'a') && (input <='h'))
        {
            switch(input)
            {
                case 'a':
                    RD0 = 0;
                    break;
                case 'b':
                    RD1 = 0;
                    break;
                case 'c':
                    RD2 = 0;
                    break;
                case 'd':
                    RD3 = 0;
                    break;
                case 'e':
                    RD4 = 0;
                    break;
                case 'f':
                    RD5 = 0;
                    break;
                case 'g':
                    RD6 = 0;
                    break;
                case 'h':
                    RD7 = 0;
            }
            printf(" off.\n\r");
        }
        else
        {
            printf(" unrecognized.\n\r");
        }
    }
}
usart.h
Rich (BB code):
#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 19200
#define FOSC 20000000L
#define NINE 0     /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#if defined(_16F87) || defined(_16F88)
    #define RX_PIN TRISB2
    #define TX_PIN TRISB5
#else
    #define RX_PIN TRISC7
    #define TX_PIN TRISC6
#endif

/* Serial initialization */
#define init_comms()\
    RX_PIN = 1;    \
    TX_PIN = 1;          \
    SPBRG = DIVIDER;         \
    RCSTA = (NINE_BITS|0x90);    \
    TXSTA = (SPEED|NINE_BITS|0x20)

void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);

#endif
usart.c
Rich (BB code):
#include <htc.h>
#include <stdio.h>
#include "usart.h"

void 
putch(unsigned char byte) 
{
    /* output one byte */
    while(!TXIF)    /* set when register is empty */
        continue;
    TXREG = byte;
}

unsigned char 
getch() {
    /* retrieve one byte */
    while(!RCIF)    /* set when register is not empty */
        continue;
    return RCREG;    
}

unsigned char
getche(void)
{
    unsigned char c;
    putch(c = getch());
    return c;
}
 
Top