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

📄 main.c

📁 该代码在KEIL平台基础上实现LPC2132与MAX485进行RS485通信
💻 C
字号:
#include <LPC213x.h>

typedef unsigned char  uint8;                   /* defined for unsigned 8-bits integer variable 	无符号8位整型变量  */
typedef signed   char  int8;                    /* defined for signed 8-bits integer variable		有符号8位整型变量  */
typedef unsigned short uint16;                  /* defined for unsigned 16-bits integer variable 	无符号16位整型变量 */
typedef signed   short int16;                   /* defined for signed 16-bits integer variable 		有符号16位整型变量 */
typedef unsigned int   uint32;                  /* defined for unsigned 32-bits integer variable 	无符号32位整型变量 */
typedef signed   int   int32;                   /* defined for signed 32-bits integer variable 		有符号32位整型变量 */
typedef float          fp32;                    /* single precision floating point variable (32bits) 单精度浮点数(32位长度) */
typedef double         fp64;                    /* double precision floating point variable (64bits) 双精度浮点数(64位长度) */

#define Fosc            12000000                    //Crystal frequence,10MHz~25MHz,should be the same as actual status. 
						    //应当与实际一至晶振频率,10MHz~25MHz,应当与实际一至
#define Fcclk           (Fosc * 4)                //System frequence,should be (1~32)multiples of Fosc,and should be equal or less  than 60MHz. 
						    //系统频率,必须为Fosc的整数倍(1~32),且<=60MHZ
#define Fcco            (Fcclk * 4)                 //CCO frequence,should be 2、4、8、16 multiples of Fcclk, ranged from 156MHz to 320MHz. 
						    //CCO频率,必须为Fcclk的2、4、8、16倍,范围为156MHz~320MHz
#define Fpclk           (Fcclk / 4) * 1             //VPB clock frequence , must be 1、2、4 multiples of (Fcclk / 4).
						    //VPB时钟频率,只能为(Fcclk / 4)的1、2、4倍


/*
*********************************************************************************************************
** 函数名称 :DelayNS()
** 函数功能 :长软件延时。
** 入口参数 :dly	延时参数,值越大,延时越久
** 出口参数 :无
*********************************************************************************************************
*/
void DelayNS (uint32 dly)
{
	uint32 i;
	
	for ( ; dly>0; dly--)
		for (i=0; i<50000; i++);
}
#define	UART_BPS	115200				// 串口通讯波特率
/*
*********************************************************************************************************
** 函数名称 :UART0_Init()
** 函数功能 :串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率115200。
** 入口参数 :无
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_Init (void)
{
	uint16 Fdiv;
	
	U0LCR = 0x83;						// DLAB=1,允许设置波特率
	Fdiv  = (Fpclk / 16) / UART_BPS;	// 设置波特率
	U0DLM = Fdiv / 256;
	U0DLL = Fdiv % 256;
	U0LCR = 0x03;
}
/*
*********************************************************************************************************
** 函数名称 :UART0_GetByte()
** 函数功能 :从串口接收1字节数据,使用查询方式接收。
** 入口参数 :无
** 出口参数 :接收到的数据
*********************************************************************************************************
*/
uint8 UART0_GetByte (void)
{
	uint8 rcv_dat;
	IO0CLR |=1<<31;
	while ((U0LSR & 0x01) == 0);
	rcv_dat = U0RBR;
	
	return (rcv_dat);	
}
/*
*********************************************************************************************************
** 函数名称 :UART0_GetStr()
** 函数功能 :从串口接收
** 入口参数 :	s	指向接收数据数组的指针
**				n	接收的个数
** 出口参数 :	无
*********************************************************************************************************
*/
void UART0_GetStr (uint8 *s, uint32 n)
{
	for ( ; n>0; n--)
	{
		*s++ = UART0_GetByte();
	}
}
/*
*********************************************************************************************************
** 函数名称 :UART0_SendByte()
** 函数功能 :向串口发送字节数据,并等待发送完毕,查询方式。
** 入口参数 :dat	要发送的数据
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_SendByte (uint8 dat)
{
	 IO0SET |=1<<31;
	U0THR = dat;
	while ((U0LSR & 0x40) == 0);		// 等待数据发送完毕
}
/*
*********************************************************************************************************
** 函数名称 :UART0_SendStr()
** 函数功能 :向串口发送一字符串
** 入口参数 :str	要发送的字符串的指针
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_SendStr (uint8 const *str)
{
	while (1)
	{
		if (*str == '\0')	break;		// 遇到结束符,退出
		UART0_SendByte(*str++);			// 发送数据
	}
}

/*
*********************************************************************************************************
** 函数名称 :main()
** 函数功能 :从串口UART0接收字符串"Hello EasyARM2131!",并发送回上位机显示。
** 调试说明 :需要PC串口显示终端软件如EasyARM.exe。
*********************************************************************************************************
*/
int main (void)
{
	uint8 snd[2];
	uint8 RCV[2];
	memset(RCV,0,2);
    IO0DIR |=1<<31;
	IO0CLR |=1<<31;
    PINSEL0  |=0x05;    /* Select the pins for Uart 选择管脚为UART0 */

	
	UART0_Init();						// 串口初始化
   snd[0]='a';
   snd[1]='b';
	//UART0_GetStr(snd,18);				// 从串口接收字符串
	//DelayNS(10);
   while (1)
   {	
	//UART0_SendStr(snd);					// 向串口发送字符串
	DelayNS(10);
	UART0_GetStr(RCV,2);
	DelayNS(10);
	}
	
    return 0;
}
/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

/*
*******************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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