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

📄 uart_interrupt.c

📁 Lpc2103的小程序
💻 C
字号:
/****************************************Copyright (c)**************************************************
**
**                                   北天星国际有限公司
**                                 http://www.po-star.com



**文件名称:UART_Interrupt.C
**功能:串口中断.
**说明:JP9连接上。
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/




#define UART_BPS  9600
#include "..\inc\config.h"
#include  <intrinsic.h>

/*********************UART0初始化****************************************/
void UART0_Init ()		
{
   U16 Fdiv;

   U0LCR = 0x83;		            // DLAB = 1,可设置波特率
   Fdiv = (Fpclk / 16)/ UART_BPS;           // 设置波特率
   U0DLM = Fdiv / 256;							
   U0DLL = Fdiv % 256;	
   U0LCR = 0x03;
}

/***************************从串口0接收数据*********************************/
char UART0_GetChar ( void )
{
  U8 Rcv_Data;

  while((U0LSR&0x01)==0);                  //等待有效数据
  Rcv_Data=U0RBR;                          //读取数据
  return(Rcv_Data);
}

/***************************从串口0接收字符串*********************************/
void UART0_GetString ( U8 *s,U32 n )
{
  for(;n>0;n++)
  {
    *s++=UART0_GetChar();
  }
}

/***************************向串口0发送数据*********************************/

void UART0_PutChar ( U32 data)
{
  U0THR = data;			    // 发送数据
  while( (U0LSR&0x40)==0 );	    // 等待数据发送完毕
}

/***************************向串口0发送字符串*********************************/
void  UART0_PutString(char *str)
{
  while(*str !='\0')
  {
    UART0_PutChar(*str++);
  }
}

/*****************************PLL初始化**************************************/
void PLL_Init(void)
{
  /* 设置系统各部分时钟 */
	   PLLCON = 1;
	#if ((Fcclk / 4) / Fpclk) == 1
		VPBDIV = 0;
	#endif
	#if ((Fcclk / 4) / Fpclk) == 2
		VPBDIV = 2;
	#endif
	#if ((Fcclk / 4) / Fpclk) == 4
		VPBDIV = 1;
	#endif
	#if (Fcco / Fcclk) == 2
		PLLCFG = ((Fcclk / Fosc) - 1) | (0 << 5);
	#endif
	#if (Fcco / Fcclk) == 4
		PLLCFG = ((Fcclk / Fosc) - 1) | (1 << 5);
	#endif
	#if (Fcco / Fcclk) == 8
		PLLCFG = ((Fcclk / Fosc) - 1) | (2 << 5);
	#endif
	#if (Fcco / Fcclk) == 16
		PLLCFG = ((Fcclk / Fosc) - 1) | (3 << 5);
	#endif
		PLLFEED = 0xaa;
		PLLFEED = 0x55;
		while((PLLSTAT & (1 << 10)) == 0);
		PLLCON = 3;
		PLLFEED = 0xaa;
		PLLFEED = 0x55;	
}

/********************************延时**************************/
void  Delay(U32  dly)
{  U16  i;

   for(; dly>0; dly--)
      for(i=0; i<500; i++);
}

/******************************中断********************************/

// IRQ 中断句柄
#pragma vector=0x18
__irq __arm void IRQ_ISR_Handler (void)
{ void (*interrupt_function)();
  unsigned int vector;
  vector = VICVectAddr;                  // 获取中断向量
  interrupt_function = (void(*)())vector;
  (*interrupt_function)();               // 调用向量中断函数
 }

/***********************串口0中断服务程序****************************/
void UART0Interrupt(void)
{
  char b;
  VICVectAddr=0;
  b=U0RBR;      //中断服务程序必须读出U0RBR的数据否则无法退出服务程序。

  //发生串口中断以后,在串口上打印字符

  UART0_PutString(" \nPolar Star LPC210X EK Interrupt Test\n");

}


/********************UART0中断初始化*********************************/
void InitEINT0Interrupt(void)
{
  VICIntSelect = 0x00000000;                   // IRQ on UART0 line.
  VICVectAddr0 = (unsigned int)&UART0Interrupt;
  VICVectCntl0 = 0x20|6 ;                      // 允许UART0向量中断
  VICIntEnable =1<<6;                          // UART0 开中断
  U0IER=0X01;
  __enable_interrupt();                        // 开中断
}

/**************************************************************
**
**名称:main.c
**功能:发生一次串口中断,就在串口上显示一次字符
**
***************************************************************/
void main( void )
{
  PINSEL0 = 0x00000005;			       // 设置I/O连接到UART0
  PINSEL1 = 0x00000000;
  PLL_Init();
  UART0_Init();	//UART0初始化
  InitEINT0Interrupt();
  while(1);

}

⌨️ 快捷键说明

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