bbgil
Full Member level 2
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
}
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
}