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

📄 uart_comm.c

📁 自己的小作,能简化繁琐的通讯帧格式的检测工作,在常量数组 PROTOCOL_ARRAY 中定义一个或多个通讯帧格式,能同时将多个有效帧检测出来,采样队列的方式,高效.
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 Copyright (C) R&D Institute of Holley Group Co., Ltd.
 Module:
 FileName:  
 Author:jerry chen              
 Version :1.0          
 Date:2005-12-16
 Description:
 ------------------------------------------------------------------------------
 Modification History:
 <No.>     	<time>        		<author>     		<version >   	<contents> 
  1、    	2005-12-24    		Jerry.chen    	    1.0             build this moudle   
********************************************************************************/
/*-----------------------------------------------------------------------------*/

    /********************************/
    /*           文件包含            */
    /********************************/
    #include  "config.h" 

    /********************************/
    /*           宏定义             */
    /********************************/
    FRAME_FORMAT_TYPE   const PROTOCOL_ARRAY[ PROTOCOL_MAX ]={
        {     
            {3},
            {{0,0,68, 0,  0},
             {0,7,68, 0,  0}, 
             {1,8,16,11,120},  
             {0,0, 0, 0,  0},  
             {0,0, 0, 0,  0},  
             {0,0, 0, 0,  0},  
            }
        }      
    };







    /********************************/
    /*          变量定义/声明        */
    /********************************/
    volatile    uint8 uart0_dlm,uart0_dll;
    volatile    UART_OP         uart0_option;
    volatile    COMM_STATUS     uart0_rec;
    volatile    COMM_STATUS     uart0_trs;
    volatile    COMM_QUEUE_TYPE uart0_rec_que;
    
    volatile    rec_queue1[UART0_REC_MAX];
    
    volatile    trs_buff1[UART0_TRS_MAX];

    volatile    uint8           uart0_dlm,uart0_dll;
    
    volatile    MULTI_FRAME_DET_TYPE	multi_frame_det;

/****************************************************************************
* 名    称:UART0_SendByte()
* 功    能:向串口发送字节数据,并等待发送完毕。
* 入口参数:data		要发送的数据
* 出口参数:无
****************************************************************************/
void  UART0_SendByte(uint8 data)
{  
    U0THR = data;				    	// 发送数据
    
    while( (U0LSR&0x40)==0 );	    	// 等待数据发送完毕
}

/****************************************************************************
* 名    称:UART0_SendStr()
* 功    能:向串口发送一字符串
* 入口参数:srt		要发送的字符串的指针
* 出口参数:无
****************************************************************************/
void  UART0_SendStr(uint8 *str,uint8 send_num)
{ 
    uint8 i; 
    
    for(i=0;i<send_num;i++)
    {
        UART0_SendByte(*str++);	    	// 发送数据
    }
}
/*************************************************************
//  name:		;
//  function: 		;
//  hardware use:	none ;
//  pra in:		;
//  pra out:		;
//									;
//									;
//									;
//  note:		;
//  author:		jerry chen .
*************************************************************/
uint8 fmt_uart0 (void)
{
    uint8 lcr_op,i;
    ////////////////////////////////////////////////////////////
    //接收队列 和 接受结构初始化
    uart0_rec_que.head = 0;
    uart0_rec_que.trail = 1;
    uart0_rec_que.que_long = 0;
    uart0_rec.lsr_status.pe_flag = 0;
    uart0_rec.lsr_status.oe_flag = 0;
    uart0_rec.lsr_status.fe_flag = 0;
    uart0_rec.lsr_status.foe_flag = 0;
    uart0_rec.rtx_num = 0;
    ////////////////////////////////////////////////////////////
    //帧格式检测结构初始化
    multi_frame_det.detected_positon = 0;
    multi_frame_det.valid_que_trail  = 1;
    
    for(i=0;i<PROTOCOL_MAX;i++)
    {
    	multi_frame_det.frame_det[i].level = 0;
    	multi_frame_det.frame_det[i].frame_start = 0;
    	multi_frame_det.frame_det[i].valid = 0;
    }
    
    ////////////////////////////////////////////////////////////
    //串口控制寄存器初始化
    PINSEL0 = (PINSEL0&0xFFFFFFF0) | 0x05; 	
    
    IRcvStr(CAT1025, UART0_OP_ADDR, &(uint8)uart0_option.baudrate_gene, 5);		

    if( (uart0_option.data_bit>8) || (uart0_option.data_bit<5) )
    {
        uart0_option.baudrate_gene= 16;        
        uart0_option.data_bit   = 8;        
        uart0_option.stop_bit   = 2;      
        uart0_option.bit_check  = 2;  
        
        ISendStr(CAT1025, UART0_OP_ADDR, &(uint8)uart0_option.baudrate_gene, 5);    
    }
    
    lcr_op = 0;
    
    switch(uart0_option.data_bit)
    {
        case 5: lcr_op &= 0xfc;                 break;
        case 6: lcr_op &= 0xfd; lcr_op |= 0x01; break;
        case 7: lcr_op &= 0xfe; lcr_op |= 0x02; break;
        case 8: lcr_op |= 0x03;                 break;
        default:lcr_op |= 0x03;                 break;
    }

    switch(uart0_option.stop_bit)
    {
        case 2: lcr_op &= 0xfb; break;
        case 3: lcr_op |= 0x04; break;
        case 4: lcr_op |= 0x04; break;
        default:lcr_op |= 0x04; break;
    }

    switch(uart0_option.bit_check)
    {
        case 0: lcr_op &= 0xc7;                 break;
        case 1: lcr_op &= 0xcf; lcr_op |= 0x08; break;
        case 2: lcr_op &= 0xdf; lcr_op |= 0x18; break;
        default:lcr_op &= 0xc7;                 break;
    }

    U0LCR = lcr_op | 0x80;
    U0DLM = (Fpclk / (uart0_option.baudrate_gene*4800))/ 256;							
	U0DLL = (Fpclk / (uart0_option.baudrate_gene*4800))% 256;		
    U0FCR = 0x00;
    U0LCR = lcr_op & 0x7f;

//    U0IER = 0x01;
    U0IER = 0x03;


    
    return(0);
}
/*************************************************************
//  name:		;
//  function: 		;
//  hardware use:	none ;
//  pra in:		;
//  pra out:		;
//									;
//									;
//									;
//  note:		;
//  author:		jerry chen .
*************************************************************/
uint8 que_reposition( uint8 old_position ,uint8 que_max )
{
    return((old_position>=que_max)?(old_position-que_max):(old_position));
}

/*************************************************************
//  name:		;
//  function: 		;
//  hardware use:	none ;
//  pra in:		;
//  pra out:		;
//									;
//									;
//									;
//  note:		;
//  author:		jerry chen .
*************************************************************/
void __irq uart0_isr (void)
{
    uint8 lsr_temp,u0iir_temp;

    u0iir_temp = U0IIR;
 
    if( (u0iir_temp&0x0e) == 0x04 )
    {
        
        uart0_rec_que.que_long++;
        uart0_rec_que.head++;
        uart0_rec.rtx_num++;
        if(uart0_rec_que.head>=COM_QUE_MAX)
        {
            uart0_rec_que.head = 0;
        }
        
        if(uart0_rec_que.que_long>=COM_QUE_MAX)
        {
            uart0_rec.lsr_status.foe_flag = 1;
        }
        else
        {
            uart0_rec_que.queue[uart0_rec_que.head] = U0RBR; 
            led3_change;
            
            if( (U0LSR & 0x02) == 0x02 ) 
            {
                uart0_rec.lsr_status.oe_flag = 1;   
            }
            if( (U0LSR & 0x04) == 0x04 )
            {
                uart0_rec.lsr_status.pe_flag = 1;
            }
            if( (U0LSR & 0x08) == 0x08 )
            {
                uart0_rec.lsr_status.fe_flag = 1;
            }
        } 
    }  
                         
    if( (u0iir_temp&0x0e) == 0x02 )
    {
        if ( uart0_trs.comm_cnt < uart0_trs.rtx_num )
        {
            U0THR = (uart0_trs.tx_buff)[uart0_trs.comm_cnt++];
        }
        else
        {
             uart0_trs.lsr_status.tx_finish_flag = 1;
        }
    }
    VICVectAddr = 0;					// 向量中断结束 
}

/*************************************************************
//  name:		;
//  function: 	以查询方式发送队列尾部的8字节数据	;
//  hardware use:	none ;
//  pra in:		;
//  pra out:		;
//									;
//									;
//									;
//  note:		;
//  author:		jerry chen .
*************************************************************/
void uart0_tx_test (void)
{
    uint8 i;
    
    for(i=0;i<8;i++)
    {
        if( uart0_rec_que.que_long > 0 ) 
        {
            uart0_rec_que.que_long--;
            UART0_SendByte(uart0_rec_que.queue[uart0_rec_que.trail]);	
            uart0_rec_que.trail ++;
            if(uart0_rec_que.trail >=COM_QUE_MAX)
            {
                uart0_rec_que.trail = 0;

⌨️ 快捷键说明

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