Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[help] PIC16f877a INTERFACES WITH PC

Status
Not open for further replies.

sfchew7

Junior Member level 2
Joined
Sep 29, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,423
I have done some research about it but it is still not functioning. I wish to send something from my circuits to the computer . For example, when the led turns on, the computer will receive a signal/message.something like that. Can someone guide me how to solve the problem ?
I have connected PIC16F877a with ,max 232 IC circuits, serial ports.

here is my coding of the PIC16F877A

#include "16f877a.h"
#fuses XT, NOPROTECT, NoWDT, put, brownout
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

void main()
{
int incode;
while(1)
{
incode = getc(); // Read character from UART
printf( " ASCII = %d \n\r " ,incode); // Display it on
}
}

I tested it with hyper terminal but nothing is appear.
help.Can someone guide me how to solve the problem?
thanks
 

Hi what you are asking is not clear write in detail
what are all your requiremnets.
you want serial port interfacing code
or GPIO accessiable code what you want.

---------- Post added at 10:13 ---------- Previous post was at 10:08 ----------

#ifndef _SERIAL_H_
#define _SERIAL_H_
#define BAUD 9600
#define FOSC 4000000L
#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);
void sendstring(unsigned char *s);
#endif


void
putch(unsigned char byte)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG = byte;
}
void
fputch(float byte)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG =( int) 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 sendstring(unsigned char *s)
{
while(*s)
putch(*s++);
}

void init_ports(void)
{

ADCON1 = 0x06; // all digital outputs
PORTA = 0;
TRISA = 0xFC; // inputs except RA1,RA0 p to 4017

PORTB = 0;
TRISB = 0; // out

PORTC = 0x01; // b0 = 1 for 7seg
TRISC = 0x81; // B7 = i/p for USART, RC0 T1CKI-Timer1 i/p.

PORTD = 0;
TRISD = 0; // out

PORTE = 0;
TRISE = 0x07; // out
}


main()
{

unsigned char s;
INTCON=0; // purpose of disabling the interrupts.


init_ports();

init_comms();

sendstring("serial test prog");// send a string to hyperterminal
putch('A'); //to print a charechet on hyperterminal
s=getche(); //read a char from keyboard and send back
putch(s);
}


/// test with this code it will works for serial data send and receive.
 

Hi what you are asking is not clear write in detail
what are all your requiremnets.
you want serial port interfacing code
or GPIO accessiable code what you want.

---------- Post added at 10:13 ---------- Previous post was at 10:08 ----------

#ifndef _SERIAL_H_
#define _SERIAL_H_
#define BAUD 9600
#define FOSC 4000000L
#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);
void sendstring(unsigned char *s);
#endif


void
putch(unsigned char byte)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG = byte;
}
void
fputch(float byte)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG =( int) 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 sendstring(unsigned char *s)
{
while(*s)
putch(*s++);
}

void init_ports(void)
{

ADCON1 = 0x06; // all digital outputs
PORTA = 0;
TRISA = 0xFC; // inputs except RA1,RA0 p to 4017

PORTB = 0;
TRISB = 0; // out

PORTC = 0x01; // b0 = 1 for 7seg
TRISC = 0x81; // B7 = i/p for USART, RC0 T1CKI-Timer1 i/p.

PORTD = 0;
TRISD = 0; // out

PORTE = 0;
TRISE = 0x07; // out
}


main()
{

unsigned char s;
INTCON=0; // purpose of disabling the interrupts.


init_ports();

init_comms();

sendstring("serial test prog");// send a string to hyperterminal
putch('A'); //to print a charechet on hyperterminal
s=getche(); //read a char from keyboard and send back
putch(s);
}


/// test with this code it will works for serial data send and receive.

thanks
is it mean when I programmed this code to my PIC and run hyperterminal
then my hyperterminal will auto show me the message without typing any charcter?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top