PIC16F877A and Serial Port code

Thread Starter

betoelbarato

Joined Nov 29, 2007
3
hello I have a questoin about this code if someone can help me..


this is the code I have for my pic

#include <system.h>
#define PIC16F877A
#pragma CLOCK_FREQ 4000000

char data1;

void main ()
{

trisd=00000000b; //set port d as output
trisc=10000000b; //set port rc7 as input since the receiver
//rx is there

portc=0; //set port c to 0
portd=0; //same for d
intcon=11000000; //because I am using interrupts
sprg=27; //I am using a boud rate in computer and
//high speed formula in pic data sheet
//9600=4000000(16(x+1)), x=27.04 so i put 27

while (1) //loop so that the microcontroller keeps checking
{ //the signal that comes from the computer
portd=0; / /portd = 0 so that if the data1 is not 1100001 then the
//port d will not be on
txsta=00100100b; // to enable enterrupts and to use baoud rate
//high speed
rcsta=10010000b; //to enable the serial port asynchronous receiver

pie1=00100000b; // for using interrupts

//the rcif flag
// is set when reception is complete
data1=rcreg; //read rcreg where the signal bits are stored

if (data1==1100001) //or if the ascii of letter 'a' is received
{ //then the rd7 = high
portd=10000000;
}
}
}


and the plan that i have is to send a signal whith the computer using hyper terminal
so lets say i send the letter a and while a keep pressing it the computer is going to send the signals to the pic and while the pic is recieving the letter a portd 7 is going to be high or an led concected to it will be on and if i stop pressing the letter a the port d is not going to be high and the led will be off

so please tell me what i need to fix in my code because i know there are things wrrong maybe the while loop is not located correctly or a i do not know , I read the pic data sheet and i tryed to follow the steps to program the usart as it said but i do not know if its right

thank you i'll appreciate your help
 

eeboy

Joined Sep 27, 2007
90
A few things:

txsta=00100100b; // to enable enterrupts and to use baoud rate
1) You don't need to continuously enable RCV interrupts. Enable it once prior to entering the while loop.

portd=0; / /portd = 0 so that if the data1 is not 1100001 then the
2) You will never see the LED light even if your code is written correctly. You have PORTD=0; at the beginning of the loop. You should change the if statement to an if/else similar...

if(data1==1100001b){portd=1000000b;}else{portd=0;}

data1=rcreg; //read rcreg where the signal bits are stored
3) Only read the RCREG when the int is set (RCIF).
 

eeboy

Joined Sep 27, 2007
90
Got a little distracted while I was replying... regarding item 2....

My solution still isn't what you are probably looking for. The LED pulse might be so short that you can't see it. Depending on your clock frequency. This while loop is going to be executing on the order of microseconds. So, when a byte is received it will set the PORTD pin but on the very next cycle of the loop the PORTD pin will be cleared until the next byte is received. Compare the cycle time of the loop to the baud rate and you will see how often this "blip" will occur.

Make sense?
 
Top