⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jgb_kzb_v1.0.c

📁 TMS320F2812串行口通信程序-采用循环缓冲区方式
💻 C
字号:

// main function       
void main(void)
{
	unsigned int index,temp;
	unsigned char array[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
							   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
	SystemInit();
	ParameterInit();
	delay(1000);		// delay 1s for system stable 
	while(1)
	{
		if(SCIA_State.INQUIREEN == 1)
		{	// if inquire CPLD message is enable
			scia_inquire_mesg[1] = scia_inquire_addr;
			scia_inquire_mesg[2] = 0x02;
			scia_inquire_mesg[3] = scia_inquire_mesg[0]+scia_inquire_mesg[1]+scia_inquire_mesg[2];
			SCIA_TX(scia_inquire_mesg,4);
			SCIA_State.INQUIREEN = 0;	// disable the inquire,waiting for answer
		}
		if(SCIA_State.MESGSTATE == 1)
		{	// if a full CPLD message have been received
			// get the complete message
			index = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
			array[0] = SCIARxBuffer[index];		// get the start flag of the frame
			SCIARxBuffer[index] = 0;			// only clear the 0xA5 flag is enough
			index = (index+1)%MAX_SCIA_BUFFER_SIZE;
			array[1] = SCIARxBuffer[index];	// get the length field of the frame
			SCIARxBuf_Front = index; 
			temp = index;			 			
			for(index = 0;index < array[1];index++)
			{
				temp = (temp+1)%MAX_SCIA_BUFFER_SIZE;
				array[index+2] = SCIARxBuffer[temp];
			}
			SCIARxBuf_Front = (SCIARxBuf_Front+index)%MAX_SCIA_BUFFER_SIZE;
			// accumulation and sum checking
			for(temp = 0,index = 0;index <= array[1];index++)
				temp += array[index];
			temp &= 0x00FF;
			// here index = 15 or 3
			if((array[index] == temp)&&(array[2] == scia_inquire_addr))
			{
				SCIA_State.INQUIREEN = 1;
				SCIA_State.RECVMESGFLAG = 1;
				scia_inquire_addr++;
				if(scia_inquire_addr > 9)	scia_inquire_addr = 1;
				if(scia_cmesg_counter != 0)  scia_cmesg_counter++;
			} 
			SCIA_State.MESGSTATE = 0;	// clear the flag
		}
		if(SCIA_State.RECVMESGFLAG == 1)
		{	// if the CPLD message is right 
			if(array[1] == 14)
			{ // copy the data from the array[16]
				scia_position_mesg[scia_pmesg_counter].Addr = array[2];
				scia_position_mesg[scia_pmesg_counter].PMesg[0] = array[3];
				scia_position_mesg[scia_pmesg_counter].PMesg[1] = array[4];
				scia_position_mesg[scia_pmesg_counter].PMesg[2] = array[5];
				scia_position_mesg[scia_pmesg_counter].PMesg[3] = array[6];
				scia_position_mesg[scia_pmesg_counter].PMesg[4] = array[7];
				scia_position_mesg[scia_pmesg_counter].PMesg[5] = array[8];
				scia_position_mesg[scia_pmesg_counter].PMesg[6] = array[9];
				scia_position_mesg[scia_pmesg_counter].PMesg[7] = array[10];
				scia_position_mesg[scia_pmesg_counter].PMesg[8] = array[11];
				scia_position_mesg[scia_pmesg_counter].PMesg[9] = array[12];
				scia_position_mesg[scia_pmesg_counter].PMesg[10] = array[13];
				scia_position_mesg[scia_pmesg_counter].PMesg[11] = array[14];
				scia_pmesg_counter ++;
				if(scia_cmesg_counter == 0)	scia_cmesg_counter = 1;
			}
			if(scia_cmesg_counter == 9)
			{ // if a round search is completed
				unsigned char *str,*strptr;
				// allocate a dynamic memory space
				str = (unsigned char *)malloc(scia_pmesg_counter*13+3);
				*str = 0xA5;										// the start flag of frame
				*(str+1) = scia_pmesg_counter*13 + 1;// the length field of the frame
				// copy the position message to send by SCI-B
				strptr = (unsigned char *)scia_position_mesg;
				for(index = 0;index < (scia_pmesg_counter*13);index++)
				{
					*(str+index+2) =*(strptr+index);
					*(strptr+index) = 0;	
				}
				// calculate the accumulation sum
				temp = scia_pmesg_counter*13+2;
				*(str+temp) = 0;
				for(index = 0;index < temp;index++)
					*(str+temp) += *(str+index);
				*(str+temp) &= 0x00FF; 
				// send the position message by SCI-by
				SCIB_TX(str,temp+1);
				// free the memory space allocated above
				free(str);
				scia_cmesg_counter = 0;
				scia_pmesg_counter = 0;	 
			}
			SCIA_State.RECVMESGFLAG = 0;			
		}
	}
}	
// Parameter Initialization
void ParameterInit(void)
{
	unsigned int index;
	// SCI parameter initialization
	SCIATxBuf_Front = 0;
	SCIATxBuf_Rear = 1;
	SCIARxBuf_Front = 0;
	SCIARxBuf_Rear = 1;
	SCIBTxBuf_Front = 0;
	SCIBTxBuf_Rear = 1;
	SCIBRxBuf_Front = 0;
	SCIBRxBuf_Rear = 1;
	for(index=0;index<MAX_SCIA_BUFFER_SIZE;index++)
	{
		SCIATxBuffer[index] = 0;
		SCIARxBuffer[index] = 0;
	}
	SCIA_State.INQUIREEN = 1;	// enable inquire the CPLD board
	SCIA_State.MESGSTATE = 0;	// No CPLD data received
	SCIA_State.RECVMESGFLAG = 0;
	SCIA_State.RXBUFEMPTY = 1;	// not used
	SCIA_State.RXBUFFULL = 0;	// not used
	SCIA_State.TXBUFEMPTY  = 1;	// not used
	SCIA_State.TXBUFFULL = 0;	// not used
	SCIA_State.rsvd = 0;		// reserved
	scia_cmesg_counter = 0;
	scia_pmesg_counter = 0;
	scia_inquire_addr = 1;
	for(index= 0;index<9;index++)
	{
		scia_position_mesg[index].Addr = 0x00;
		scia_position_mesg[index].PMesg[0] = 0x00;
		scia_position_mesg[index].PMesg[1] = 0x00;
		scia_position_mesg[index].PMesg[2] = 0x00;
		scia_position_mesg[index].PMesg[3] = 0x00;
		scia_position_mesg[index].PMesg[4] = 0x00;
		scia_position_mesg[index].PMesg[5] = 0x00;
		scia_position_mesg[index].PMesg[6] = 0x00;
		scia_position_mesg[index].PMesg[7] = 0x00;
		scia_position_mesg[index].PMesg[8] = 0x00;
		scia_position_mesg[index].PMesg[9] = 0x00;
		scia_position_mesg[index].PMesg[10] = 0x00;
		scia_position_mesg[index].PMesg[11] = 0x00;
	}
}

// 1ms delay routine 
void delay(unsigned int n)
{
	unsigned int i=0;
	while(n--)					
	{	
		for(i=0;i<12440;i++);
	}
}

// SCI-A transmit routine
void SCIA_TX(unsigned char *Str,unsigned int Len)
{
	unsigned int index,temp,counter=1;
	temp = (SCIATxBuf_Rear+1)%MAX_SCIA_BUFFER_SIZE;
	while(temp != SCIATxBuf_Front)
	{	// get the free space size
		counter++;
		temp = (temp+1)%MAX_SCIA_BUFFER_SIZE;
	}
	if(counter>=Len)
	{	// if there is enough free space
		for(index=0;index<Len;index++)
		{
			SCIATxBuffer[SCIATxBuf_Rear] = Str[index];
			SCIATxBuf_Rear = (SCIATxBuf_Rear+1)%MAX_SCIA_BUFFER_SIZE;
		}
	}
	else
	{	//if there is not enough free space
		// add the processing error code 
	}
}

// SCI-B transmit routine
void SCIB_TX(unsigned char *Str,unsigned int Len)
{
	unsigned int index,temp,counter=1;
	temp = (SCIBTxBuf_Rear+1)%MAX_SCIB_BUFFER_SIZE;
	while(temp != SCIBTxBuf_Front)
	{	// get the free space size
		counter++;
		temp = (temp+1)%MAX_SCIB_BUFFER_SIZE;
	}
	if(counter>=Len)
	{	// if there is enough free space
		for(index=0;index<Len;index++)
		{
			SCIBTxBuffer[SCIBTxBuf_Rear] = Str[index];
			SCIBTxBuf_Rear = (SCIBTxBuf_Rear+1)%MAX_SCIB_BUFFER_SIZE;
		}
	}
	else
	{	//if there is not enough free space
		// add the processing error code 
	}
}

// Timer0 interrupt service routine happens every 2ms
// here the timer0 is used for sytem time slot allocation
interrupt void CPU_Timer0_ISR(void)
{
    unsigned int index,temp,counter=0;
	CpuTimer0.InterruptCount++;

	// SCI-A tranmit section
	temp = SciaRegs.SCIFFTX.bit.TXFFST;	// get SCI-A TXFIFO state
	if(temp <= 6)						// TXFFST<=6,start the transmit operation
	{
		index = (SCIATxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
		while(index != SCIATxBuf_Rear)		
		{	// get the number of data in buffer
			counter++;
			index = (index+1)%MAX_SCIA_BUFFER_SIZE;
		}
		//  choose the minimum value as the result
		counter = ((16-temp) <= counter) ? (16-temp) : counter;	
		for(index = 1;index<=counter;index++)
		{
			temp = (SCIATxBuf_Front+index)%MAX_SCIA_BUFFER_SIZE;
			SciaRegs.SCITXBUF = SCIATxBuffer[temp];
			SCIATxBuffer[temp] = 0;
		}
		SCIATxBuf_Front = (SCIATxBuf_Front+counter)%MAX_SCIA_BUFFER_SIZE;
		counter = 0;		
	}
	
	// SCI-A receive section
	temp = SciaRegs.SCIFFRX.bit.RXFIFST;	// get SCI-A RXFIFO state
	if(temp > 0)		// if the RXFIFO receive data
	{
		counter = 1;
		index = (SCIARxBuf_Rear+1)%MAX_SCIA_BUFFER_SIZE;
		while(index != SCIARxBuf_Front)
		{	// get the free space
			counter++;
			index = (index+1)%MAX_SCIA_BUFFER_SIZE;
		}
		if(counter >= temp)
		{	// if there is enough space
			for(index = 0;index < temp;index++)
			{
				SCIARxBuffer[SCIARxBuf_Rear] = SciaRegs.SCIRXBUF.all;
				SCIARxBuf_Rear = (SCIARxBuf_Rear+1)%MAX_SCIA_BUFFER_SIZE;
			}
		}
		else
		{	// if there is not enough space
			// add the processing error code 
		}
		counter = 0;
	}

	// SCI-B tranmit section
	temp = ScibRegs.SCIFFTX.bit.TXFFST;	// Get SCI-A FIFOTX state
	if(temp <= 6)						// TXFFST<=6,start the operation
	{
		index = (SCIBTxBuf_Front+1)%MAX_SCIB_BUFFER_SIZE;
		while(index != SCIBTxBuf_Rear)		
		{	// get the number of data in buffer
			counter++;
			index = (index+1)%MAX_SCIB_BUFFER_SIZE;
		}
		// choose the minimum value as the result
		counter = ((16-temp) <= counter) ? (16-temp) : counter;	
		for(index = 1;index <= counter;index++)
		{
			temp = (SCIBTxBuf_Front+index)%MAX_SCIB_BUFFER_SIZE;
			ScibRegs.SCITXBUF = SCIBTxBuffer[temp];
			SCIBTxBuffer[temp] = 0;
		}
		SCIBTxBuf_Front = (SCIBTxBuf_Front+counter)%MAX_SCIB_BUFFER_SIZE;
		counter = 0;		
	}
	
	// SCI-A command parsing section
	if(CpuTimer0.InterruptCount>=3)
	{ // parsing operation happens every 6ms
		CpuTimer0.InterruptCount = 0;
		index = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
		while(index != SCIARxBuf_Rear)
		{	// search the SCI-A RX buffer until the end 
			if(SCIARxBuffer[index] == 0xA5)
			{ // if found the flag of frame start : 0xA5 
				temp = (index +1)%MAX_SCIA_BUFFER_SIZE;	// the length field of the frame
				if(SCIARxBuffer[temp] == 0x00)
					// if the length field is 0,the frame is not completely received
					break;	
				else
				{ // get the following data number in buffer
					// here the index is unusual now
					index = (temp+1)%MAX_SCIA_BUFFER_SIZE;
					while(index != SCIARxBuf_Rear)		
					{	// get the number of data in Rxbuffer
						counter++;
						index = (index+1)%MAX_SCIA_BUFFER_SIZE;
					}
					if(counter>=SCIARxBuffer[temp])
						// if a full message have been received	
						SCIA_State.MESGSTATE = 1;
					else
						// if not ,break out the cycle
						break;
					counter = 0;
				}  
			}
			else
			{	// clear the unuseful data
				SCIARxBuffer[index] = 0x00;
				SCIARxBuf_Front = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
				index = (SCIARxBuf_Front+1)%MAX_SCIA_BUFFER_SIZE;
			}
		}
	}
   	// 响应中断并允许系统接收更多的中断
   	PieCtrl.PIEACK.all = PIEACK_GROUP1;
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -