打印

PIC32MX 串口与time1并用时出现问题

[复制链接]
3582|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
thenotin|  楼主 | 2012-8-7 19:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位高手:
我用PIC32MX795F512H做测试时,发现串口与time1不能同时使用,
程序运行了一会儿,串口就停止工作了,在debugger下运行时,一开始串口和time1都能工作,但过一会儿程序就停止了,如果关掉time1,串口就能正常收发数据,请各位帮我看看程序,非常感谢!




#include <p32xxxx.h>
#include <plib.h>
#include <math.h>
// Configuration Bit settings
// SYSCLK = 60 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 15 MHz SYSCLK/FPBDIV
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
//


#pragma config FPLLMUL = MUL_15, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_4,ICESEL = ICS_PGx1,FSOSCEN = OFF




//*************************************************************************************
#define nop() asm("nop")
#define uchar unsigned char
#define uint unsigned int

//**************************************************************************************
#define GetSystemClock() (60000000ul)
#define GetPeripheralClock() (GetSystemClock()/4)


// Let compile time pre-processor calculate the PR1 (period)
#define SYS_FREQ (60000000L)
#define PB_DIV 4
#define PRESCALE 256
#define TOGGLES_PER_SEC 1
//#define T1_TICK (SYS_FREQ/PB_DIV/PRESCALE/TOGGLES_PER_SEC)
//#define T1_TICK 14648 //250ms
#define T1_TICK 586 //10ms






//define the constant

//***********************************************************************
/* CRC 高位字节值表 */
const uchar auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41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 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;


const unsigned CmdDelCount=150; //the delay of command
//define the variables


uchar sendBuf[16];

unsigned char redBuf[240];
unsigned char recount;
unsigned long U2BSTASt;
unsigned char basetime;
//*********************************************************************
//define the function
void Re_Lw(uchar slave_addr,uchar h_addr,uchar l_addr);
void Wr_Lw(uchar slave_addr,uchar h_addr,uchar l_addr,uchar h_data,uchar l_data);
void Re_Coil(uchar slave_addr,uchar h_addr,uchar l_addr);
void Wr_Coil(uchar slave_addr,uchar h_addr,uchar l_addr,uchar h_data,uchar l_data);
uint crc16(uchar *puchMsg, uint usDataLen);



void USART2B_Tx(unsigned char data);




//define the constant

void delay_ms (unsigned ms); //delay function by ms
void delay_nop (unsigned us); //delay function by us
int main(void)
{
uchar i;
unsigned int a_h,a_l;
unsigned char d_h,d_l;
// Configure the device for maximum performance but do not change the PBDIV
//SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

basetime=0;

OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK);
// set up the timer interrupt with a priority of 2
ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);




//_TRISC14=0;
//AD1PCFG=0xffff;
//PORTSetPinsCigitalOut(IOPORT_C, BIT_14);
mPORTCClearBits(BIT_14);
mPORTCSetPinsDigitalOut( BIT_14);
//UARTConfigure(UART2A, UART_ENABLE_PINS_TX_RX_ONLY);




UARTConfigure(UART2B, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART2B, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART2B, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART2B, GetPeripheralClock(), 57600);
UARTEnable(UART2B, UART_ENABLE | UART_PERIPHERAL | UART_TX | UART_RX );


// Configure UART2B RX Interrupt

INTEnable(INT_SOURCE_UART_RX(UART2B), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART2B), INT_PRIORITY_LEVEL_7);
INTSetVectorSubPriority(INT_VECTOR_UART(UART2B), INT_SUB_PRIORITY_LEVEL_0);


// enable multi-vector interrupts
//INTEnableSystemMultiVectoredInt();

// configure for multi-vectored mode
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

// enable interrupts
INTEnableInterrupts();


//DBINIT();
recount=0;




while(1)
{
// delay_ms(500);
// mPORTCToggleBits(BIT_14);

U2BSTASt=U2BSTA;
U2BSTASt=U2BSTA;
a_h=U2BSTA & 0x0000FF00;
d_h=a_h/256;
d_l=U2BSTA & 0x000000FF;
Wr_Lw(0x02,0x0F,0xA1,d_h,d_l);
//Wr_Lw(0x02,0x0F,0xA1,0x98,0x76);
delay_ms(8);

Wr_Coil(0x02,0x0E,0xB5,0xFF,0x00);//write 0xFF even write only one coil

U2BSTASt=U2BSTA;
delay_ms(8);


Wr_Lw(0x02,0x0F,0xA1,0x00,0x00);
delay_ms(8);

Wr_Coil(0x02,0x0E,0xB5,0x00,0x00);//write 0xFF even write only one coil
delay_ms(8);

}
return 0;
}

//****************************************************************************************
//the delay function by ms,Crystal oscillator is 12M
void delay_ms (unsigned int ms)
{
unsigned int i,j;
for ( i = ms; --i; )
for ( j = 2800; --j; );
}

// STEP 3. configure the Timer 1 interrupt handler

void __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void)
{
unsigned long low;
// clear the interrupt flag
mT1ClearIntFlag();

// .. things to do
// .. in this case, toggle the LED
basetime++;
if(basetime>=50)
{
basetime=0;
mPORTCToggleBits(BIT_14);
}
}



void __ISR(_UART_2B_VECTOR, ipl7) IntUart2BHandler(void)
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART2B)))
{
// Clear the RX interrupt Flag
INTClearFlag(INT_SOURCE_UART_RX(UART2B));
while(U2BSTAbits.URXDA)
{
redBuf[1]=UARTGetDataByte(UART2B);
//redBuf[recount]=UARTGetDataByte(UART2B);
recount=recount+1;
}
if(recount>=200)
{
recount=0;
}
}




// We don't care about TX interrupt

if ( INTGetFlag(INT_SOURCE_UART_TX(UART2B)) )
{
INTClearFlag(INT_SOURCE_UART_TX(UART2B));
}

}







uint crc16(uchar *puchMsg, uint usDataLen)
{
uchar uchCRCHi = 0xFF ; /* 高CRC字节初始化 */
uchar uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */
long uIndex ; /* CRC循环中的索引 */
while (usDataLen--) /* 传输消息缓冲区 */
{
uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC */
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi << 8 | uchCRCLo) ;
}//uint16 crc16(uint8 *puchMsg, uint16 usDataLen)




void Re_Lw(uchar slave_addr,uchar h_addr,uchar l_addr)
{
uint CRC_Code,temp1,temp2;
uchar CRC_H,CRC_L,i;
sendBuf[0]=slave_addr;//0x02;//station
sendBuf[1]=0x03;//fun code
sendBuf[2]=h_addr;//0x14;//addr_h
sendBuf[3]=l_addr;//0X77;//addr_l
sendBuf[4]=0x00;//read count H
sendBuf[5]=0x01;//read count L
CRC_Code=crc16(sendBuf,6);
temp1=CRC_Code & 0xFF00;
temp2=temp1/256;
CRC_H=temp2;
CRC_L=CRC_Code & 0x00FF;
sendBuf[6]=CRC_H;
sendBuf[7]=CRC_L;
for(i=0;i<=7;i++)
USART2B_Tx(sendBuf);


delay_nop(CmdDelCount);
// Dir_485=0;
i=0;
}
void Wr_Lw(uchar slave_addr,uchar h_addr,uchar l_addr,uchar h_data,uchar l_data)
{
uint CRC_Code,temp1,temp2;
uchar CRC_H,CRC_L,i;
sendBuf[0]=slave_addr;//0x02;//station
sendBuf[1]=0x06;//fun code
sendBuf[2]=h_addr;//0x14;//addr_h
sendBuf[3]=l_addr;//0X77;//addr_l
sendBuf[4]=h_data;//read count H
sendBuf[5]=l_data;//read count L
CRC_Code=crc16(sendBuf,6);
temp1=CRC_Code & 0xFF00;
temp2=temp1/256;
CRC_H=temp2;
CRC_L=CRC_Code & 0x00FF;
sendBuf[6]=CRC_H;
sendBuf[7]=CRC_L;
//Dir_485=1; //disable receiving and enable sending
delay_nop(CmdDelCount);
delay_nop(CmdDelCount);
delay_nop(CmdDelCount);
delay_nop(CmdDelCount);
for(i=0;i<=7;i++)
USART2B_Tx(sendBuf);


delay_nop(CmdDelCount);
// Dir_485=0;
i=0;
}

void Re_Coil(uchar slave_addr,uchar h_addr,uchar l_addr)
{
uint CRC_Code,temp1,temp2;
uchar CRC_H,CRC_L,i;
sendBuf[0]=slave_addr;//0x02;//station
sendBuf[1]=0x01;//fun code
sendBuf[2]=h_addr;//0x14;//addr_h
sendBuf[3]=l_addr;//0X77;//addr_l
sendBuf[4]=0x00;//read count H
sendBuf[5]=0x01;//read count L
CRC_Code=crc16(sendBuf,6);
temp1=CRC_Code & 0xFF00;
temp2=temp1/256;
CRC_H=temp2;
CRC_L=CRC_Code & 0x00FF;
sendBuf[6]=CRC_H;
sendBuf[7]=CRC_L;
for(i=0;i<=7;i++)
USART2B_Tx(sendBuf);


delay_nop(CmdDelCount);
// Dir_485=0;
i=0;

}

void Wr_Coil(uchar slave_addr,uchar h_addr,uchar l_addr,uchar h_data,uchar l_data)
{
uint CRC_Code,temp1,temp2;
uchar CRC_H,CRC_L,i;
sendBuf[0]=slave_addr;//0x02;//station
sendBuf[1]=0x05;//fun code
sendBuf[2]=h_addr;//0x14;//addr_h
sendBuf[3]=l_addr;//0X77;//addr_l
sendBuf[4]=h_data;//read count H
sendBuf[5]=l_data;//read count L
CRC_Code=crc16(sendBuf,6);
temp1=CRC_Code & 0xFF00;
temp2=temp1/256;
CRC_H=temp2;
CRC_L=CRC_Code & 0x00FF;

sendBuf[6]=CRC_H;
sendBuf[7]=CRC_L;
//Dir_485=1; //disable receiving and enable sending
delay_nop(CmdDelCount);
delay_nop(CmdDelCount);
delay_nop(CmdDelCount);
delay_nop(CmdDelCount);
for(i=0;i<=7;i++)
USART2B_Tx(sendBuf);


delay_nop(CmdDelCount);
//Dir_485=0;
i=0;
}

void delay_nop (unsigned us) //delay function by us
{
unsigned i,SetValue;
SetValue=3*us;
for ( i = SetValue; --i; );
}



void USART2B_Tx(unsigned char data)
{
//U2BSTAbits.UTXEN = 1; //enable send
delay_nop(500);
while(!U2BSTAbits.TRMT); //check TRMT,TRMT=0 indicates that the port is busy,or it is idle
U2BTXREG = data; //output the data,and auto reset the TXIF
delay_nop(20);
//while(U1STAbits.UTXBF);
while(!U2BSTAbits.TRMT);
delay_nop(50);
//U2BSTAbits.UTXEN = 0; //disable send
}
沙发
ingureeagle| | 2013-1-4 22:37 | 只看该作者
请教下···UARTConfigure()这样的库函数在哪里找啊?
是编译器的安装目录那里??

使用特权

评论回复
板凳
shanbinaa| | 2013-4-15 09:45 | 只看该作者
我用的PIC18F46K80.我是配置定时器1后,串口二总收到串口一收到的数据,但是数据长度没有变。。现在改用的别的定时器,偶尔还是有这种情况。很是纠结。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

12

帖子

1

粉丝