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

📄 comm.c

📁 keil c 开发的典型动态LED显示屏控制器,cpu为51系列
💻 C
字号:
/*------------------------------------------------------------------------*/
/* for M8 cpu              */
/* 2006/4/27   by chenchungen                 */
/*------------------------------------------------------------------------*/
#include <intrins.h>
#include "PortDef.h"
#include "comm.h"


#define RCAP2L_VALUE ((65536-(400000L/32/96))%256)  /* 40M 9600 */
#define RCAP2H_VALUE ((65536-(400000L/32/96))/256) 

#define T_SIZE			0x40
#define T_SIZE_MASK 	(T_SIZE-1) 
#define R_SIZE			0x40
#define R_SIZE_MASK 	(R_SIZE-1)
#define MAX_CMD_LEN 	0x0c


extern unsigned int  DownTime;
extern bit CutDown;
extern bit Close;
extern bit Hold;
extern bit Great;
extern bit Test;

unsigned char xdata comm_t_buf[T_SIZE];
unsigned char xdata comm_r_buf[R_SIZE];
unsigned char comm_r_head = 0;		 
unsigned char comm_r_tail = 0;
unsigned char comm_t_head = 0;
unsigned char comm_t_tail = 0;
bit comm_now_send = 0;

unsigned char xdata cmd_frame[MAX_CMD_LEN];
unsigned char cmd_frame_ptr = 0;
unsigned char error = 0;

void sercomm() interrupt 4	
{							
  if( TI0 ){
    TI0 = 0;
    if( comm_t_head != comm_t_tail ){
      SBUF0 = comm_t_buf[ comm_t_head ];
      comm_t_head ++;
      comm_t_head &= T_SIZE_MASK;
     }
    else  {
	  comm_now_send = 0;
	  TXLED = !TXLED;
	 }
   }
  if( RI0 ){
    RI0 = 0;
	if( ((comm_r_tail+1)&R_SIZE_MASK) != comm_r_head ){
      comm_r_buf[ comm_r_tail ] = SBUF0;
      comm_r_tail ++;
      comm_r_tail &= R_SIZE_MASK;
     }
   }
}

unsigned char get_char_rbuf(void)
{
  BYTE ch;
  EA = 0;
  ch = comm_r_buf[comm_r_head];
  comm_r_head++;
  comm_r_head &= R_SIZE_MASK;
  EA = 1;
  return ch;
}

void treat_rbuf(void)
{
  BYTE ch;
  
  if( comm_r_head != comm_r_tail ){
    ch = get_char_rbuf();
  	if( ch == 0x0d ){
	  RXLED = !RXLED;
 	  if( error == 0 ) treat_comm_cmd();
	  cmd_frame_ptr = 0;
	  error = 0;
      return;
     }
	else {
      cmd_frame[cmd_frame_ptr] = ch;
      cmd_frame_ptr ++;
      if( cmd_frame_ptr > MAX_CMD_LEN ) error = 1;
      return;
	 }
   }
}

void put_char_tbuf( BYTE ch)
{
  EA = 0;
  if( comm_t_head != ((comm_t_tail+1)&T_SIZE_MASK) ){
    comm_t_buf[comm_t_tail] = ch;
    comm_t_tail ++;
    comm_t_tail &= T_SIZE_MASK;
   }
  EA = 1;
  return;
}

void send_char(void)
{
  unsigned char ch;
  EA = 0;
  if( comm_t_head != comm_t_tail ){
    if( !comm_now_send ){
      EA = 1;
      ch = comm_t_buf[comm_t_head];
      comm_t_head++;
      comm_t_head &= T_SIZE_MASK;
      comm_now_send = 1;
      SBUF0 = ch;
	  return;
     }
   }
  EA = 1;
}

void treat_comm_cmd( void )
{
  BYTE ch, len, i;

  if (cmd_frame_ptr<1)	return;
  ch = cmd_frame[0];
  switch( ch ) {
	case 'B': if( cmd_frame_ptr<5 || cmd_frame[1]!=':') return;
	          len=0;
			  for( ch=2; ch<cmd_frame_ptr; ch++) {
			    if( cmd_frame[ch]==':' ){
				  len=ch-2;
				  break;
				 }
			   }
			  if( len==0 ) return;
			  			  
	          put_char_tbuf('D');
			  put_char_tbuf(':');
			  for( ch=0; ch<cmd_frame_ptr; ch++)  put_char_tbuf(cmd_frame[ch]);
			  put_char_tbuf(0x0d);

			  ch = cmd_frame_ptr-len-3;
			  
			  DownTime = 0;
			  for(i=0; i<ch; i++){
				DownTime = DownTime*10+cmd_frame[3+len+i]-0x30;
			   }

			  CutDown = 1;
		      break;
	case 'C': if( cmd_frame_ptr<3 || cmd_frame[1]!=':') return;
		      len = cmd_frame_ptr-2;
			  
			  if( len==0 ) return;
			  			  
			  put_char_tbuf('D');
			  put_char_tbuf(':');
			  for( ch=0; ch<cmd_frame_ptr; ch++)  put_char_tbuf(cmd_frame[ch]);
			  put_char_tbuf(0x0d);
			  Close = 1;
              break;
	case 'H': if( cmd_frame_ptr<3 || cmd_frame[1]!=':') return;
		      len = cmd_frame_ptr-2;

			  if( len==0 ) return;
			  			  
			  put_char_tbuf('D');
			  put_char_tbuf(':');
			  for( ch=0; ch<cmd_frame_ptr; ch++)  put_char_tbuf(cmd_frame[ch]);
			  put_char_tbuf(0x0d);
			  Hold = 1;
              break;
	case 'E': if( cmd_frame_ptr<3 || cmd_frame[1]!=':') return;
		      len = cmd_frame_ptr-2;

			  if( len==0 ) return;
			  			  
			  put_char_tbuf('D');
			  put_char_tbuf(':');
			  for( ch=0; ch<cmd_frame_ptr; ch++)  put_char_tbuf(cmd_frame[ch]);
			  put_char_tbuf(0x0d);
			  Great = 1;
              break;
	 case 'T': if( cmd_frame_ptr<3 || cmd_frame[1]!=':') return;
		      len = cmd_frame_ptr-2;

			  if( len==0 ) return;
			  if(cmd_frame[2]==0x31)  Test = 1;
			  else				 	  Test = 0;
              break;
	 default: break;
    }
}

⌨️ 快捷键说明

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