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

📄 interrupt_usart.c

📁 串口通讯程序
💻 C
字号:
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : interrupt_Usart.c
//* Object              : USART Interrupt Management
//*
//* 1.0 14/Dec/04 JPP   : Creation
//* 1.1 29/Aug/05 JPP   : Update AIC definion
//*----------------------------------------------------------------------------


// Include Standard LIB  files
#include "Board.h"

#include "cdc_enumerate.h"

#define USART_INTERRUPT_LEVEL		1


#define AT91C_US_ASYNC_485MODE ( AT91C_US_USMODE_RS485 + \
                        AT91C_US_NBSTOP_1_BIT + \
                        AT91C_US_PAR_NONE + \
                        AT91C_US_CHRL_8_BITS + \
                        AT91C_US_CLKS_CLOCK )

AT91PS_USART COM0;
#define USART_BAUD_RATE 		9600
void AT91F_US_Put( char *buffer);
extern struct _AT91S_CDC 	pCDC;
static char buff_rx[100];
static char buff_rx1[100];
unsigned int first =0;
void usart_input();
unsigned char com1_read_data[64];
unsigned char iskaishi = 0;
int data_length = 0;
//*------------------------- Internal Function --------------------------------

//*----------------------------------------------------------------------------
//* Function Name       : Trace_Toggel_LED
//* Object              : Toggel a LED
//*----------------------------------------------------------------------------
void Trace_Toggel_LED (unsigned int Led)
{
    if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & Led ) == Led )
    {
        AT91F_PIO_ClearOutput( AT91C_BASE_PIOA, Led );
    }
    else
    {
        AT91F_PIO_SetOutput( AT91C_BASE_PIOA, Led );
    }
}
//*------------------------- Interrupt Function -------------------------------

//*----------------------------------------------------------------------------
//* Function Name       : Usart_c_irq_handler
//* Object              : C handler interrupt function called by the interrupts
//*                       assembling routine
//*----------------------------------------------------------------------------
void Usart_c_irq_handler(void)
{
	AT91PS_USART USART_pt = COM0;
	unsigned int status;

	//* get Usart status register and active interrupt
	status = USART_pt->US_CSR ;
        status &= USART_pt->US_IMR;	
	if ( status & AT91C_US_RXBUFF)
        {
          COM0->US_RPR = (unsigned int) buff_rx;
          COM0->US_RCR = 1;
          buff_rx[1] = 0;
          AT91F_PIO_SetOutput( AT91C_BASE_PIOA, AT91C_PIO_PA7 ) ;
          //COM0->US_CR = COM0->US_CR | AT91C_US_RTSEN;

          AT91F_US_Put(buff_rx);
          while(!(USART_pt -> US_CSR & 0x1<<9));
          //for(int i=0;i<10000000;i++)
           // ;
          //COM0->US_CR = COM0->US_CR | AT91C_US_RTSDIS;
          AT91F_PIO_ClearOutput(AT91C_BASE_PIOA,AT91C_PIO_PA7);
        }
	//* Reset the satus bit for error
	//USART_pt->US_CR = AT91C_US_RSTSTA;
}
//*-------------------------- External Function -------------------------------

//*----------------------------------------------------------------------------
//* \fn    AT91F_US_Printk
//* \brief This function is used to send a string through the US channel
//*----------------------------------------------------------------------------
void AT91F_US_Put( char *buffer) // \arg pointer to a string ending by \0
{
	while(*buffer != '\0') {
		while (!AT91F_US_TxReady(COM0));
		AT91F_US_PutChar(COM0, *buffer++);
	}
}

//*----------------------------------------------------------------------------
//* Function Name       : Usart_init
//* Object              : USART initialization
//* Input Parameters    : none
//* Output Parameters   : TRUE
//*----------------------------------------------------------------------------
void Usart_init ( void )
//* Begin
{
    COM0= AT91C_BASE_US0;
    //* Define RXD and TXD as peripheral
    // Configure PIO controllers to periph mode
    AT91F_PIO_CfgPeriph(
	 AT91C_BASE_PIOA, // PIO controller base address
	 ((unsigned int) AT91C_PA5_RXD0    ) |
	 ((unsigned int) AT91C_PA6_TXD0    ) , // Peripheral A
	 0 ); // Peripheral B

    AT91F_PIO_CfgOutput( AT91C_BASE_PIOA, AT91C_PIO_PA7) ;
    AT91F_PIO_ClearOutput(AT91C_BASE_PIOA,AT91C_PIO_PA7);
    //* First, enable the clock of the PIOB
    AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<<AT91C_ID_US0 ) ;

    //* Usart Configure
    //AT91F_US_Configure (COM0, MCK,AT91C_US_ASYNC_MODE,USART_BAUD_RATE , 0);
    AT91F_US_Configure (COM0, MCK,AT91C_US_ASYNC_485MODE,USART_BAUD_RATE , 0);
    //* Enable usart
    COM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;

    //* open Usart interrupt
    AT91F_AIC_ConfigureIt (AT91C_BASE_AIC, AT91C_ID_US0, USART_INTERRUPT_LEVEL,
                           AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, Usart_c_irq_handler);
    AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US0);
    // Set the PDC
    AT91F_PDC_Open (AT91C_BASE_PDC_US0);
    COM0->US_RPR = (unsigned int) buff_rx;
    COM0->US_RCR = 1;
    first = 0;
    COM0->US_RTOR = 10;
    //* Enable USART IT error and AT91C_US_ENDRX
    //AT91F_US_EnableIt(COM0,AT91C_US_RXBUFF | AT91C_US_TIMEOUT );
    AT91F_US_EnableIt(COM0,AT91C_US_RXBUFF);


//* End
}

void usart_input()//DZ接收数据处理函数
{
  //while((*AT91C_US1_CSR & AT91C_US_TIMEOUT)!= (1<<8));
  /*判断接受数据长短*/
  int com1_read_num;
  com1_read_num = 255 - *AT91C_US1_RCR;

  AT91C_BASE_US1->US_CR = AT91C_BASE_US1->US_CR | AT91C_US_STTTO;//启动超时
  *AT91C_US1_RPR = (unsigned  int)com1_read_data ;//设置接收数组
  *AT91C_US1_RCR = 255 ;//设置接收计数器
  *AT91C_US1_PTCR = AT91C_PDC_RXTEN ;//接收使能

  //从接收数据中分拣出完整包
  int DZ_head,DZ_end,DZ_start;
  DZ_head = DZ_end = DZ_start = 0;
  for(int i = 0 ; i< com1_read_num ; i++)
  {
    if(com1_read_data[i] == '*')
    {
      DZ_head = i;
      DZ_start = 1;
    }
    else if((DZ_start == 1)&&(com1_read_data[i] == '^'))
    {
      DZ_end = i;
      DZ_start = 0;
      com1_read_num = DZ_end - DZ_head + 1;

      for(int j = 0 ;j < com1_read_num;j++)
      {
        com1_read_data[j] = com1_read_data[j+DZ_head];
      }

      //---------------对接收的包进行判断---------------
      if((com1_read_data[0] == '*')&&(com1_read_data[com1_read_num-1] == '^'))
      {
        com1_read_data[com1_read_num] = 0;
        return ;
      }

    //---------------结束-------------------------------
    }
  }
  /*清空接收数组*/
  for(int i=0;i<com1_read_num;i++)
  {
    com1_read_data[i] = 0x00;
  }
}

⌨️ 快捷键说明

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