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

📄 uart0.c

📁 MSP430电能测量程序,用的是电力线载波通讯.即PLC
💻 C
字号:
#include "uart0.h"
//#include <stdlib.h>
#include "device.h"
#include <ctype.h>
#include "parameter.h"

unsigned char UART_RX_Buffer[UART_RX_Buffer_Size];
unsigned int UART_RX_Bytes;  
unsigned int UART_Status;

//--------------------------------------------------------------
// Extracts the hexadecimal number out of the received string
//--------------------------------------------------------------
int GetHexNumber(unsigned char * num)
{
	int	val = 0;
    int chval;
	unsigned char * ptr = num;
	while (*ptr > 0)
	{
		chval = *ptr - '0';
		if (chval >10)
		{
			chval -= 'A' - '0' -10;
		}
		val = (val<<4) + chval;
		ptr++;
	}
	return(val);
}

//--------------------------------------------------------------
// Extracts the hexadecimal number out of the received string
//--------------------------------------------------------------
long GetLongHexNumber(unsigned char * num)
{
	long	val = 0;
	int chval;
	unsigned char * ptr = num;
	while (*ptr > 0)
	{
		chval = *ptr - '0';
		if (chval >10)
		{
			chval -= 'A' - '0' -10;
		}
		val = (val<<4) + chval;
		ptr++;
	}
	return(val);
}

//--------------------------------------------------------------
// Extracts the decimal number out of the received string
//--------------------------------------------------------------
//unsigned int GetNumber(unsigned char * num);
int GetNumber(unsigned char * num)
{
	int	val = 0;
	int chval;
	unsigned char * ptr = num;
	while (*ptr > 0)
	{
		chval = *ptr - '0';
		if ((chval >=0) && (chval<=9))
		{		
			val = (val * 10) + chval;
		}
		ptr++;
	}
	return(val);
}

//--------------------------------------------------------------
// Extracts the BCD number out of the received string
//--------------------------------------------------------------
int GetBCDNumber(unsigned char * num)
{
	int	val = 0;
	int chval;
	unsigned char * ptr = num;
	while (*ptr > 0)
	{
		chval = *ptr - '0';
		if ((chval >=0) && (chval<=9))
		{		
			val = (val<<4) + chval;
		}
		ptr++;
	}
	return(val);
}


//------------------------------------------------------------------------------ 
//		URX0_ISR    ; UART0 Receive Interrupt Routine
//------------------------------------------------------------------------------ 
#ifdef __IAR_SYSTEMS_ICC__
#if __VER__ < 200
interrupt[UART0RX_VECTOR] void URX0_ISR(void)
#else
#pragma vector=UART0RX_VECTOR
__interrupt void URX0_ISR( void )
#endif
#endif

#ifdef __CROSSWORKS__
void URX0_ISR(void)   __interrupt[UART0RX_VECTOR] 
#endif
{	
	char	RxChar;
	
	P1OUT &= ~0x02;		// Turn off P1.1 to turn on User1 LED
	do
	{
	RxChar = RXBUF0;
	if((UART_Status & LineReceived) == 0)		// ignore if last Buffer has not been processed
	{
		switch	(RxChar)
		{
			
		case 0x00:		//ignore NUL
			break;
			
		case 0x08:		// Backspace
			if (UART_RX_Bytes >0)
			{	
				UART_RX_Bytes--;		// Dec index if buffer not empty
				UART_RX_Buffer[UART_RX_Bytes] = 0;	// Erase previous char	
			}	
			break;		
		
		case 0x0A:		//ignore LF
			break;
		
		case 0x0D:		//Line Received
			UART_Status |= LineReceived;
			LPM3_EXIT;				// Exit Low Power Mode 3 
			RxChar = 0;
			UART_RX_Buffer[UART_RX_Bytes] = RxChar;
			break;

		case 0x1B:		//<ESC> -> Clear Buffer
			UART_RX_Bytes = 0;	//Clear RX Buffer
			break;
		
		case '-':		//'-' for Calibration
		case '+':		//'+' for Calibration
		
//?		case 0x30:		//'0' special single char received
			UART_Status |= LineReceived;
			LPM3_EXIT;				// Exit Low Power Mode 3 
			//break;

		default:	// Byte received -> move to Buffer
			if ((RxChar >= 'a') && (RxChar <= 'z'))
				{UART_RX_Buffer[UART_RX_Bytes] = RxChar-0x20;}	// Convert to upper-case
			else
				{UART_RX_Buffer[UART_RX_Bytes] = RxChar;}
			
			if (UART_RX_Bytes < UART_RX_Buffer_Size)
			{	
				UART_RX_Bytes++;	// Inc index if buffer not full
			}
			break;
		}
	}
	}	while(U0IFG & URXIFG0);	// While incoming character pending

	return;
}

#ifdef withRS485
//==========================================================================================
// Function:		SelectRS485Direction()
//
// Description: 	This function selects the data flow direction of the RS485 connection.
//					Choices are Talk or Listen.
//
// Revision History:
// 10/15/03	HEM		New function.
// 01/29/03 HEM		Wait a while on first character of talk.  RS485 needs to see falling
//                  edge of start bit, so first we need to let it go high for a while before
//					before starting to talk.
//==========================================================================================
#include "comms_uart.h"
void SelectRS485Direction(unsigned char RS485Dir)
{

	unsigned char OldRS485Dir;
	OldRS485Dir = (P2OUT >>2) & 3;

	P2OUT = (P2OUT & ~(3<<2)) | (RS485Dir<<2);		// Send direction control signal via GPIO pins P2.2 and P2.3

	if ((RS485Dir == RS485_TALK)  && (RS485Dir != OldRS485Dir))	// If this is the first character of an outgoing transmission
	{
		int	k;
		for (k=0; k<50; k++)      // Wait a while so RS485 sees the high idle state
		{
			_NOP();
		}
	}

}
#endif

⌨️ 快捷键说明

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