16F877a rs232 hitech c

Thread Starter

varunme

Joined Sep 29, 2011
60
Is the code below correct ? , not working in 16F877a

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

#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 20000000
#endif

__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT);

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




 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;
}





void main(void){

 	 TRISD=0;
	 PORTD=0;
     unsigned char input;

     INTCON=0;    // purpose of disabling the interrupts.
 
     init_comms();    // set up the USART - settings defined in usart.h
 
     while(1){

		char rxbyte;
        
		rxbyte= getch();
		switch(rxbyte)
		{
			case '1':
			PORTD=0xFF;
			putch("hhh");
			__delay_ms(20);
			break;

			case 'b':
			PORTD=0x00;
			putch("hhh");
			__delay_ms(20);
			break;
		
			default :
			break;


	
		}

     }
 }
 

nerdegutta

Joined Dec 15, 2009
2,684
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?
 

Thread Starter

varunme

Joined Sep 29, 2011
60
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
 

t06afre

Joined May 11, 2009
5,934
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
 

Thread Starter

varunme

Joined Sep 29, 2011
60
usart.h

Rich (BB code):
#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600      
#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
yes , once i complete editing , i will inform u
 
Last edited:

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
while(1){
 
char rxbyte;
 
rxbyte= getch();
switch(rxbyte)
{
case '1':
PORTD=0xFF;
putch("hhh");
__delay_ms(20);
break;
 
case 'b':
PORTD=0x00;
putch("hhh");
__delay_ms(20);
break;
 
default :
break;
}
In the data sheet you will find this
The heart of the receiver is the receive (serial) shift register
(RSR). After sampling the STOP bit, the received
data in the RSR is transferred to the RCREG register (if
it is empty). If the transfer is complete, flag bit RCIF
(PIR1<5>) is set. The actual interrupt can be enabled/
disabled by setting/clearing enable bit RCIE
(PIE1<5>). Flag bit RCIF is a read only bit, which is
cleared by the hardware. It is cleared when the RCREG​
register has been read and is empty.
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.​
 

Thread Starter

varunme

Joined Sep 29, 2011
60
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 ?
 
Top