//*******************************
// Function prototypes
//*******************************
void LoPriISR(void);
void ShutDownTimer1(void);
//*******************************
// Pragmas
//******************************
#pragma code lowVector=0x18
void atLowVector(void)
{
_asm GOTO LoPriISR _endasm
}
#pragma code
//*****************************************************
// Include files
//*****************************************************
#include <usart.h>
#include <p18cxxx.h> /* for TRISB and PORTB declarations */
#include <p18f252.h> /* for TRISB and PORTB declarations */
/* Set configuration bits for use with ICD2 / PICDEM2 PLUS Demo Board:
* - set HS oscillator
* - disable watchdog timer
* - disable low voltage programming */
#include <usart.h>
#include <stdio.h>
#include <delays.h>
#include <timers.h>
#include <string.h>
#include <i2c.h>
//*******************************
// Definitions and equates
//******************************
#define DELAY_20CHAR (65536 - 17400) // 1.74mSec = 20char @ 115200 bauds
#define DATA_MAX 100 // Maximun number of bytes that can be received from the Comm Port
#define CHAR_EOF ',' // Character END OF FIELD
#define CHAR_EOT 0 // Character END OF TRANSMISSION
//*******************************
// Global variables
//*******************************
unsigned char DataIn[DATA_MAX]; // General Data Buffer
unsigned char DataInPtr; // Keep track of the number of bytes received from Comm Port
unsigned char DataInEov; // This flag announces a data overflow
unsigned char DataInCrc; // This variable holds the CRC result
// ****************************************************************************************************
// main()
// ****************************************************************************************************
void main()
{
while(1)
{
ClrWdt(); // Feed the dog....!!!!!!!!!
// TIMER1 (USART time out) *******************************************************************
if (PIR1bits.TMR1IF) // Interrupt caused by TIMER1
{
ShutDownTimer1();
}
}
}
//***************************************************************************************************
//***** L O W P R I O R I T Y *****************************************************************
//***************************************************************************************************
#pragma interruptlow LoPriISR // Low-priority interrupt service routine
void LoPriISR(void)
{
unsigned char i,c,iLen;
if (PIR1bits.RCIF) // Check if USART receive interrupt flag is set
{
// USART ************************************************************************************
c = ReadUSART(); // Get the character received from the USART
if (!(DataInPtr > 0)) // If this is the first byte received?
{
OpenTimer1( TIMER_INT_OFF & // This timer expires after a 20 char delay
T1_16BIT_RW & // from the serial port
T1_SOURCE_INT &
T1_PS_1_1 &
T1_OSC1EN_OFF &
T1_SYNC_EXT_OFF );
WriteTimer1(DELAY_20CHAR); // Reload Timer1
}
DataIn[DataInPtr] = c; // Save character into buffer
DataInPtr++; // Increment pointer
if (!(DataInPtr < DATA_MAX)) // Maximun number of characters received
{
//... do what you have to do
ShutDownTimer1();
}
else if (c == CHAR_EOF) // END OF FILD character received
{
//... do what you have to do
ShutDownTimer1();
}
else if (c == CHAR_EOT) // END OF TRANSMISSION character received
{
//... do what you have to do
ShutDownTimer1();
}
else
{
PIR1bits.TMR1IF = 0; // Clear Timer0 int flag
PIR1bits.RCIF = 0; // Clear USART receive interrupt flag
WriteTimer1(DELAY_20CHAR); // Reload Timer1
}
}
}
//***************************************************************************************************
//***************************************************************************************************
//***************************************************************************************************
void ShutDownTimer1()
{
CloseTimer1(); // Close Timer0
PIR1bits.TMR1IF = 0; // Clear Timer0 interrupt flag
DataInPtr = 0; // Clear DataIn data pointer
}
评论0