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.

USART_PIC protocol problem

Status
Not open for further replies.

bbgil

Full Member level 2
Joined
Mar 11, 2006
Messages
135
Helped
13
Reputation
26
Reaction score
9
Trophy points
1,298
Activity points
2,321
pir1.rcif usart_read()

Hi. i want to implement a simple USART_PIC protocol. my idea is to have USART data to have a synch bit, data and stop bit. i have implemented a USART interrupt but no idea on how the data can be captured to control my main program. I'm using 16F877a and MikroC. Any help here will be very much appreciated.
Code:

unsigned short readPos,buffPos,a;
unsigned char buffer[]={0x00, 0x00, 0x00};



void interrupt ()
{

// Received Interrupt
if (PIR1.RCIF)
{
// Read Byte from Usart Buffer
a=Usart_Read();
// Save it to Memory Buffer
buffer[buffPos]=a;

buffPos++;
// End of Buffer ? Then Set it to 0
if (buffPos==3)
{
buffPos=0;
// Make sure that this is the end of the string
buffer[buffPos]=0x00;
}

// Write Info Back to USART
Usart_write (a);
PIR1.RCIF=0; // Clear Interrupt

}
}


void main() {
TRISA =0;
PORTA =0;
TRISD = 0xFF;
TRISB=0;
TRISC =0xF0;
PORTC=0xF0;
PORTB =0;
buffPos=0; // Initialize Buffer Position
Usart_Init (9600);


// ENABLE INTERRUPTS
PIE1.RCIE = 1; // RECEIVE INTERRUPT
INTCON = 0xC0; // GLOBAL and PERIPHERAL INTERRUPTS

while (1)

{ if (buffer???){PORTC.f0 =~PORTC.f0; // my problems starts here } ;
} else
{ PORTC.f2 =~PORTC.f2;} }
} // endless loop
}
 

You could have flag, that states that you have data in buffer. Another concern is that you write 0x00 to the first location if you get more than 3 bytes of data.

So code in main could look like this...

Code:
if (received==1)
{
    PORTC.f0 =1;
    received=0;
}
else
{
   PORTC.f0 =0;
}

This is only to blink one led when received flag is raised.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top