/*********************************************************************
*
* Board Support Packet of Microchip Chips
*
*
*********************************************************************
* FileName: BSP_DEBUG.C
* Dependencies: BSP_DEF.H
* BSP_FIFO.H
* BSP_INT.H
* BSP_UART.H
* P24FJ128GA006.H
* Processor: PIC24F
* Complier: Microchip C30 v2.01 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* This software is owned by Microchip Technology Inc. ("Microchip")
* and is supplied to you for use exclusively as described in the
* associated software agreement. This software is protected by
* software and other intellectual property laws. Any use in
* violation of the software license may subject the user to criminal
* sanctions as well as civil liability. Copyright 2006 Microchip
* Technology Inc. All rights reserved.
*
* This software is provided "AS IS." MICROCHIP DISCLAIMS ALL
* WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
* TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* INFRINGEMENT. Microchip shall in no event be liable for special,
* incidental, or consequential damages.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Robin Chen Dec/06 Original (Rev. 1.0)
********************************************************************/
#include "..IncludeBSP.H"
#include <p18f66j60.h>
/******************************************************************************
* UART BAUD & BAUD RATE TABLE
******************************************************************************/
rom WORD UART_BAUD_TBL[8][2] =
{
{UART_BAUD_1200 , UART_BAUD_RATE_1200 },
{UART_BAUD_2400 , UART_BAUD_RATE_2400 },
{UART_BAUD_4800 , UART_BAUD_RATE_4800 },
{UART_BAUD_9600 , UART_BAUD_RATE_9600 },
{UART_BAUD_19200 , UART_BAUD_RATE_19200 },
{UART_BAUD_38400 , UART_BAUD_RATE_38400 },
{UART_BAUD_57600 , UART_BAUD_RATE_57600 },
{UART_BAUD_115200, UART_BAUD_RATE_115200 }
};
/******************************************************************************
* COM port related FIFO struction define, Tx & Rx Buffer
******************************************************************************/
#pragma udata SEC_RXBUF // locate 0xA00
BYTE UARTRxBuf[512]; // Receive FIFO buffer
#pragma udata SEC_TXBUF // locate 0xC00
BYTE UARTTxBuf[512]; // Transfer FIFO buffer
#pragma udata
PBYTE RxFifo_Start;
PBYTE RxFifo_End;
PBYTE RxFifo_Read;
PBYTE RxFifo_Write;
PBYTE TxFifo_Start;
PBYTE TxFifo_End;
PBYTE TxFifo_Read;
PBYTE TxFifo_Write;
#pragma code // Return to default code section
//*******************************ADconvert
unsigned char i,j,CN;
union adres
{
unsigned int y1;
unsigned char adre[2];
}adresult;
BYTE Uart_Rx[128];
BYTE Uart_Rx_Len=0;
BYTE Uart_Frame=0;
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BSP_STATUS UART_Init(BYTE Baud)
{
RCSTA1bits.SPEN = 0;// disable uart commutation
//calculate baud rate
SPBRGH1 = (BYTE)((UART_BAUD_TBL[Baud][1] & 0xFF00 )>>8);
SPBRG1 = (BYTE)(UART_BAUD_TBL[Baud][1] & 0x00FF );
//enable uart
RCSTA1bits.SPEN = 1;//Enable uart commutation
TxFifo_Start = (PBYTE)UARTRxBuf;
TxFifo_End = (PBYTE)UARTRxBuf + sizeof(UARTRxBuf);
TxFifo_Read = (PBYTE)UARTRxBuf;
TxFifo_Write = (PBYTE)UARTRxBuf;
RxFifo_Start = (PBYTE)UARTTxBuf;
RxFifo_End = (PBYTE)UARTTxBuf + sizeof(UARTTxBuf);
RxFifo_Read = (PBYTE)UARTTxBuf;
RxFifo_Write = (PBYTE)UARTTxBuf;
return STATUS_SUCCESS;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BSP_STATUS UART_Read(PBYTE pBuffer,WORD wCount )
{
while( wCount-- > 0 )
{
// read from the FIFO, and pointe to the next
*pBuffer++ = *RxFifo_Read;
INTCONbits.GIEL = 0; // disable low level priority interrupt
RxFifo_Read++;
INTCONbits.GIEL = 1; // enable low level priority interrupt
// check if it is the bottom of FIFO
if( RxFifo_Read >= RxFifo_End )
{
INTCONbits.GIEL = 0; // disable low level priority interrupt
RxFifo_Read = RxFifo_Start;
INTCONbits.GIEL = 1; // enable low level priority interrupt
}
}
return STATUS_SUCCESS;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BSP_STATUS UART_Write(PBYTE pBuffer,WORD wCount )
{
while( wCount-- > 0 )
{
// write to the FIFO, and pointe to the next
*TxFifo_Write = *pBuffer++;
INTCONbits.GIEL = 0; // disable low level priority interrupt
TxFifo_Write++;
INTCONbits.GIEL = 1; // enable low level priority interrupt
// check if it is the bottom of FIFO
if( TxFifo_Write >= TxFifo_End )
{
INTCONbits.GIEL = 0; // disable low level priority interrupt
TxFifo_Write = TxFifo_Start;
INTCONbits.GIEL = 1; // enable low level priority interrupt
}
}
PIE1bits.TX1IE = 1; // enable
return STATUS_SUCCESS;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
WORD UART_BlankNum(void)
{
WORD wBlankNum;
INTCONbits.GIEL = 0; // disable low level priority interrupt
if( TxFifo_Write == TxFifo_Read )
wBlankNum = TxFifo_End - TxFifo_Start - 1;
else if( TxFifo_Write > TxFifo_Read )
wBlankNum = TxFifo_End - TxFifo_Start + TxFifo_Read - TxFifo_Write - 1;
else
wBlankNum = TxFifo_Read - TxFifo_Write - 1;
INTCONbits.GIEL = 1; // enable low level priority interrupt
return wBlankNum;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
WORD UART_HoldNum(void)
{
WORD wHoldNum;
INTCONbits.GIEL = 0; // disable low level priority interrupt
if( RxFifo_Write == RxFifo_Read ) //如果BUFFER中没有包,或在写帧数据,就不要发包
wHoldNum = 0;
else if( RxFifo_Write > RxFifo_Read )
wHoldNum = RxFifo_Write - RxFifo_Read;
else
wHoldNum = RxFifo_End - RxFifo_Start + RxFifo_Write - RxFifo_Read;
INTCONbits.GIEL = 1; // enable low level priority interrupt
return wHoldNum;
}
/*********************************************************************
* Function: void _ISR _U1RXInterrupt(void)
*
* PreCondition: None
*
* Input: None
*
* Output:
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
#pragma interruptlow UART_Handler
void UART_Handler(void)
{
BYTE data;
BYTE i;
// TX interrupter handler
if(PIR1bits.TX1IF)
{
if( TxFifo_Write == TxFifo_Read )
{
PIE1bits.TX1IE = 0; // disable
}
else
{
// read from the FIFO, and pointe to the next
TXREG1 = *TxFifo_Read++;
// check if it is the bottom of FIFO
if( TxFifo_Read >= TxFifo_End )
TxFifo_Read = TxFifo_Start;
}
}
// RX interrupter handler
if(PIR1bits.RC1IF)
{
data = RCREG1;
////////////////////////////
//RCSTA1bits.SPEN = 0;// disable uart commutation
Uart_Rx[Uart_Rx_Len]=data;
if(Uart_Rx[0]==0x02)
{
Uart_Rx_Len++;
if( (Uart_Rx_Len == Uart_Rx[3]+5) && (Uart_Rx_Len>=0x05) ) //一个桢类型,两个命令,一个数据长度,一个校验位
{
for( i=0; i < Uart_Rx_Len;i++)
{
*RxFifo_Write++ = Uart_Rx;
if((RxFifo_Write)>= RxFifo_End )
RxFifo_Write = RxFifo_Start;
}
Uart_Rx[0]=0;
Uart_Rx_Len = 0;
}
}
else
{
Uart_Rx_Len=0;
Uart_Rx[0]=0;
}
//RCSTA1bits.SPEN = 1;//Enable uart commutation
///////////////////////////
//*RxFifo_Write++ = data;
// check if it is the bottom of FIFO
//if( RxFifo_Write >= RxFifo_End )
//RxFifo_Write = RxFifo_Start;
}
}
#pragma code LowVector=0x18
void LowVector (void)
{
_asm goto UART_Handler _endasm
}
以上的这样会死-----------------------------------------------------------------------------------------没办法啊,去掉缓冲环后,实验证明就不死了,,,,,如下
*******************************************************************************************************
/*********************************************************************
*
* Board Support Packet of Microchip Chips
*
*
*********************************************************************
* FileName: BSP_DEBUG.C
* Dependencies: BSP_DEF.H
* BSP_FIFO.H
* BSP_INT.H
* BSP_UART.H
* P24FJ128GA006.H
* Processor: PIC24F
* Complier: Microchip C30 v2.01 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* This software is owned by Microchip Technology Inc. ("Microchip")
* and is supplied to you for use exclusively as described in the
* associated software agreement. This software is protected by
* software and other intellectual property laws. Any use in
* violation of the software license may subject the user to criminal
* sanctions as well as civil liability. Copyright 2006 Microchip
* Technology Inc. All rights reserved.
*
* This software is provided "AS IS." MICROCHIP DISCLAIMS ALL
* WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
* TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* INFRINGEMENT. Microchip shall in no event be liable for special,
* incidental, or consequential damages.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Robin Chen Dec/06 Original (Rev. 1.0)
********************************************************************/
#include "..IncludeBSP.H"
#include <p18f66j60.h>
#include "..IncludeCompiler.h"
/******************************************************************************
* UART BAUD & BAUD RATE TABLE
******************************************************************************/
rom WORD UART_BAUD_TBL[8][2] =
{
{UART_BAUD_1200 , UART_BAUD_RATE_1200 },
{UART_BAUD_2400 , UART_BAUD_RATE_2400 },
{UART_BAUD_4800 , UART_BAUD_RATE_4800 },
{UART_BAUD_9600 , UART_BAUD_RATE_9600 },
{UART_BAUD_19200 , UART_BAUD_RATE_19200 },
{UART_BAUD_38400 , UART_BAUD_RATE_38400 },
{UART_BAUD_57600 , UART_BAUD_RATE_57600 },
{UART_BAUD_115200, UART_BAUD_RATE_115200 }
};
/******************************************************************************
* COM port related FIFO struction define, Tx & Rx Buffer
******************************************************************************/
#pragma udata SEC_RXBUF // locate 0xA00
BYTE UARTRxBuf[256]; // Receive FIFO buffer
#pragma udata SEC_TXBUF // locate 0xC00
BYTE UARTTxBuf[256]; // Transfer FIFO buffer
#pragma udata
PBYTE RxFifo_Start;
PBYTE RxFifo_End;
PBYTE RxFifo_Read;
PBYTE RxFifo_Write;
PBYTE TxFifo_Start;
PBYTE TxFifo_End;
PBYTE TxFifo_Read;
PBYTE TxFifo_Write;
#pragma code // Return to default code section
BOOL LED_TEST;
//*******************************ADconvert
unsigned char i,j,CN;
union adres
{
unsigned int y1;
unsigned char adre[2];
}adresult;
BYTE Uart_Rx[128];
BYTE Uart_Tx[128];
BYTE Uart_Rx_Len=0;
BYTE Uart_Frame=0;
BYTE UART_Rx_Lensave;
BYTE Tx_Len;
BYTE Tx_Now;
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BSP_STATUS UART_Init(BYTE Baud)
{
RCSTA1bits.SPEN = 0;// disable uart commutation
//calculate baud rate
SPBRGH1 = (BYTE)((UART_BAUD_TBL[Baud][1] & 0xFF00 )>>8);
SPBRG1 = (BYTE)(UART_BAUD_TBL[Baud][1] & 0x00FF );
//enable uart
RCSTA1bits.SPEN = 1;//Enable uart commutation
TxFifo_Start = (PBYTE)UARTRxBuf;
TxFifo_End = (PBYTE)UARTRxBuf + sizeof(UARTRxBuf);
TxFifo_Read = (PBYTE)UARTRxBuf;
TxFifo_Write = (PBYTE)UARTRxBuf;
RxFifo_Start = (PBYTE)UARTTxBuf;
RxFifo_End = (PBYTE)UARTTxBuf + sizeof(UARTTxBuf);
RxFifo_Read = (PBYTE)UARTTxBuf;
RxFifo_Write = (PBYTE)UARTTxBuf;
return STATUS_SUCCESS;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BSP_STATUS UART_Read(PBYTE pBuffer,WORD wCount )
{
unsigned char i=0;
while( wCount-- > 0 )
{
// read from the FIFO, and pointe to the next
/*
*pBuffer++ = *RxFifo_Read;
INTCONbits.GIEL = 0; // disable low level priority interrupt
RxFifo_Read++;
INTCONbits.GIEL = 1; // enable low level priority interrupt
// check if it is the bottom of FIFO
if( RxFifo_Read >= RxFifo_End )
{
INTCONbits.GIEL = 0; // disable low level priority interrupt
RxFifo_Read = RxFifo_Start;
INTCONbits.GIEL = 1; // enable low level priority interrupt
}
*/
*pBuffer++ =Uart_Rx[i++] ;
}
return STATUS_SUCCESS;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BSP_STATUS UART_Write(PBYTE pBuffer,WORD wCount )
{
unsigned char i=0;
Tx_Len = wCount;
Tx_Now = 0;
LED_TEST=!LED_TEST;
BUTTON_IO = LED_TEST;
while( wCount-- > 0 )
{
/*
// write to the FIFO, and pointe to the next
*TxFifo_Write = *pBuffer++;
INTCONbits.GIEL = 0; // disable low level priority interrupt
TxFifo_Write++;
INTCONbits.GIEL = 1; // enable low level priority interrupt
// check if it is the bottom of FIFO
if( TxFifo_Write >= TxFifo_End )
{
INTCONbits.GIEL = 0; // disable low level priority interrupt
TxFifo_Write = TxFifo_Start;
INTCONbits.GIEL = 1; // enable low level priority interrupt
}
*/
INTCONbits.GIEL = 1;
Uart_Tx[i++]=*pBuffer++;
}
PIE1bits.TX1IE = 1; // enable
return STATUS_SUCCESS;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
WORD UART_BlankNum(void)
{
WORD wBlankNum;
INTCONbits.GIEL = 0; // disable low level priority interrupt
if( TxFifo_Write == TxFifo_Read )
wBlankNum = TxFifo_End - TxFifo_Start - 1;
else if( TxFifo_Write > TxFifo_Read )
wBlankNum = TxFifo_End - TxFifo_Start + TxFifo_Read - TxFifo_Write - 1;
else
wBlankNum = TxFifo_Read - TxFifo_Write - 1;
INTCONbits.GIEL = 1; // enable low level priority interrupt
return wBlankNum;
}
/*********************************************************************
* Function:
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
WORD UART_HoldNum(void)
{
WORD wHoldNum;
/*
INTCONbits.GIEL = 0; // disable low level priority interrupt
if( RxFifo_Write == RxFifo_Read ) //如果BUFFER中没有包,或在写帧数据,就不要发包
wHoldNum = 0;
else if( RxFifo_Write > RxFifo_Read )
wHoldNum = RxFifo_Write - RxFifo_Read;
else
wHoldNum = RxFifo_End - RxFifo_Start + RxFifo_Write - RxFifo_Read;
INTCONbits.GIEL = 1; // enable low level priority interrupt
*/
wHoldNum = UART_Rx_Lensave;
UART_Rx_Lensave = 0;
return wHoldNum;
}
/*********************************************************************
* Function: void _ISR _U1RXInterrupt(void)
*
* PreCondition: None
*
* Input: None
*
* Output:
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
#pragma interruptlow UART_Handler
void UART_Handler(void)
{
BYTE data;
BYTE i;
// TX interrupter handler
if(PIR1bits.TX1IF)
{
if((Tx_Len !=0)&&(Tx_Now<Tx_Len))
{
TXREG1 = Uart_Tx[Tx_Now];
Tx_Now++;
}
else
{
PIE1bits.TX1IE = 0;
Tx_Len = 0;
}
/*
if( TxFifo_Write == TxFifo_Read )
{
PIE1bits.TX1IE = 0; // disable
}
else
{
// read from the FIFO, and pointe to the next
TXREG1 = *TxFifo_Read++;
// check if it is the bottom of FIFO
if( TxFifo_Read >= TxFifo_End )
TxFifo_Read = TxFifo_Start;
}
*/
}
// RX interrupter handler
if(PIR1bits.RC1IF)
{
data = RCREG1;
////////////////////////////
//RCSTA1bits.SPEN = 0;// disable uart commutation
Uart_Rx[Uart_Rx_Len]=data;
if((Uart_Rx[0]==0x02)&&(Uart_Rx_Len<=127)) //单包大小最大127。新增加+++++++++++
{
Uart_Rx_Len++;
if( (Uart_Rx_Len == Uart_Rx[3]+5) && (Uart_Rx_Len>=0x05) ) //一个桢类型,两个命令,一个数据长度,一个校验位
{ /*
for( i=0; i < Uart_Rx_Len;i++)
{
*RxFifo_Write++ = Uart_Rx;
if((RxFifo_Write)>= RxFifo_End )
RxFifo_Write = RxFifo_Start;
}
*/
UART_Rx_Lensave=Uart_Rx_Len;
Uart_Rx[0]=0;
Uart_Rx_Len = 0;
}
}
else
{
Uart_Rx_Len=0;
Uart_Rx[0]=0;
}
//RCSTA1bits.SPEN = 1;//Enable uart commutation
///////////////////////////
//*RxFifo_Write++ = data;
// check if it is the bottom of FIFO
//if( RxFifo_Write >= RxFifo_End )
//RxFifo_Write = RxFifo_Start;
}
}
#pragma code LowVector=0x18
void LowVector (void)
{
_asm goto UART_Handler _endasm
}
*************************
哪个能发现原因??????
// $Id: 18f66j60.lkr,v 1.2 2005/06/28 19:04:35 nairnj Exp $
// File: 18f66j60.lkr
// Sample linker script for the PIC18F66J60 processor
LIBPATH .
FILES c018i.o
FILES clib.lib
FILES p18f66j60.lib
CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
CODEPAGE NAME=page START=0x2A END=0xFFF7
CODEPAGE NAME=config START=0xFFF8 END=0xFFFD PROTECTED
CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
ACCESSBANK NAME=accessram START=0x0 END=0x5F
DATABANK NAME=gpr0 START=0x60 END=0xFF
DATABANK NAME=gpr1 START=0x100 END=0x1FF
DATABANK NAME=gpr2 START=0x200 END=0x2FF
DATABANK NAME=gpr3 START=0x300 END=0x3FF
DATABANK NAME=gpr4 START=0x400 END=0x4FF
DATABANK NAME=gpr5 START=0x500 END=0x5FF
DATABANK NAME=gpr6 START=0x600 END=0x6FF
DATABANK NAME=gpr7 START=0x700 END=0x7FF
DATABANK NAME=ram_buf START=0x800 END=0x9FF PROTECTED
DATABANK NAME=ram_rxbuf START=0xA00 END=0xBFF PROTECTED
DATABANK NAME=ram_txbuf START=0xC00 END=0xDFF PROTECTED
DATABANK NAME=gpr14 START=0xE00 END=0xE7F
DATABANK NAME=sfr14 START=0xE80 END=0xEFF PROTECTED
DATABANK NAME=gpr15 START=0xF00 END=0xF5F
ACCESSBANK NAME=accesssfr START=0xF60 END=0xFFF PROTECTED
SECTION NAME=CONFIG ROM=config
SECTION NAME=SEC_BUF RAM=ram_buf
SECTION NAME=SEC_RXBUF RAM=ram_rxbuf
SECTION NAME=SEC_TXBUF RAM=ram_txbuf
STACK SIZE=0x100 RAM=gpr7
一周热门 更多>