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

📄 serial.c

📁 51单片机实时多任务操作心痛实例
💻 C
字号:
/******************************************************************************/
/*                                                                            */
/*       SERIAL.C:  Interrupt Controlled Serial Interface for RTX-51 tiny     */
/*                                                                            */
/******************************************************************************/


#include <reg52.h>                    /* special function register 8052       */
#include <rtx51tny.h>                 /* RTX-51 tiny functions & defines      */
#include <stdio.h>
#include <serial.h>
#include <multi_task.h> 

unsigned char uart_index=0;							//串口接收数组索引值
unsigned char xdata uart_receive[ARRAY_LEN];		//串口接收数组



/******************************************************************************/
/*       serial_init: initialize serial interface                             */
/******************************************************************************/
void serial_init (void)  {
    ES = 0;							// 禁止串口中断
    SCON = 0x50;					// 0101,0000  位数据位, 无奇偶校验
    T2CON = 0x34;					// 0011,0100, 由T2作为波特率发生器
    RCAP2H = 0xFF;					// 时钟11.0592MHz,  38400 波特率
    RCAP2L = 0xF7;
    ES = 1;	
//  SCON  = 0x50;                      /* mode 1: 8-bit UART, enable receiver   */
  //TMOD |= 0x20;                      /* timer 1 mode 2: 8-Bit reload          */
  //TH1   = 0xfd;                      /* reload value 2400 baud                */
  //TR1   = 1;                         /* timer 1 run                           */
  //ES    = 1;                         /* enable serial port interrupt          */
}




/******************************************************************************/
/*       serial:  serial receiver / transmitter interrupt                     */
/******************************************************************************/
serial () interrupt 4 using 2  {     /* use registerbank 2 for interrupt      */

  if(RI){
	RI=0;
	uart_receive[uart_index]=SBUF;

	if((uart_receive[uart_index-1]==0xc3)&&(uart_receive[uart_index]==0x3c)){
		os_send_signal (GET_KEY); 
		//os_send_signal (DTIME); 
	}
	//	Serial_Respond();

	if(uart_receive[uart_index]!=0xAA)
		uart_index++;
	else uart_index=1;	
	
	}
}
/******************************************************************************/

/******************************************************************************/
void Clear_Array(unsigned char *p)			//数组清零
{
    unsigned char i;
	uart_index=0;	
    for(i=0;i<ARRAY_LEN;i++)
    *p++=0;
}

/******************************************************************************/
/*       Send_Byte:  										                  */
/******************************************************************************/
  //ES    = 1;                         /* enable serial port interrupt        */
void Send_Byte(unsigned char one_byte)		// 发送一个字节
{
    TI = 0;							// 清零串口发送中断标志
    SBUF = one_byte;
    while (TI == 0);
    TI = 0;							// 清零串口发送中断标志
}
/******************************************************************************/

/******************************************************************************/
void Send_String(unsigned char *p)			//发送一组字符串
{	

    while(*p){
	Send_Byte(*p++);

	}
}

/******************************************************************************/

/******************************************************************************/  
char Hex_To_Num(char   hex1char)   
  /*   
        'a'   or   'A'   ->   10,   '1'   ->   1   
        If   hex1char   not   in   '0'-'9','A'-'F','a'-'f'   return   -1   
  */   
  {   
      if(hex1char >= '0' && hex1char <= '9') return hex1char - '0';   
      if(hex1char >= 'A' && hex1char <= 'F') return hex1char - 'A' + 10;   
      if(hex1char >= 'a' && hex1char <= 'f') return hex1char - 'a' + 10;   
      return -1;   
  }  


/******************************************************************************/

/******************************************************************************/ 
char Send_Hex_String(unsigned   char   *hex_str)   
  /*   
        "ba 08 7d 40 12 34 56 78"   ->   
		{0xba,   0x08,   0x7d,   0x40,   0x12,   0x34,   0x56,   0x78}   
  */   
  {   
	int h1,   h2;   
	unsigned   char   *sh=hex_str;   

	while(*sh!=0)   
      {   
	    if(*sh==' ')sh++;
		if((h1   =   Hex_To_Num(*sh++))   <   0)   return   -1;   
		if((h2   =   Hex_To_Num(*sh++))   <   0)   return   -1;  

		Send_Byte(h1   <<   4   |   h2);   
      }   
      return   1;   
  }   


/******************************************************************************/

/******************************************************************************/
void SETPANEL(unsigned int Color,unsigned int BColor)
////////显示颜色设置
{
	Send_Byte(0xAA);
	Send_Byte(0x40);
	Send_Byte((char)(Color>>8));
	Send_Byte((char)(Color&0xff));
	Send_Byte((char)(BColor>>8));
	Send_Byte((char)(BColor&0xff));
	Send_Hex_String("CC 33 C3 3C");
}

/******************************************************************************/

/******************************************************************************/
void CLEARDEVICE()
////////清屏
{

	Send_Hex_String("AA 52CC 33 C3 3C");
}

/******************************************************************************/

/******************************************************************************/
void ASC8S(unsigned int X,unsigned int Y,char *S )
////////8*8点阵ASCII字符串显示
{

	Send_Byte(0xAA);
	Send_Byte(0x53);
	Send_Byte((char)(X>>8));
	Send_Byte((char)(X&0xff));
	Send_Byte((char)(Y>>8));
	Send_Byte((char)(Y&0xff));
	Send_String(S);
	Send_Hex_String("CC 33 C3 3C");
}

/******************************************************************************/

/******************************************************************************/
void GB231224(unsigned int X,unsigned int Y,char *S )
////////12*12点阵GB2312字符串显示
{

	Send_Byte(0xAA);
	Send_Byte(0x6F);
	Send_Byte((char)(X>>8));
	Send_Byte((char)(X&0xff));
	Send_Byte((char)(Y>>8));
	Send_Byte((char)(Y&0xff));
	Send_String(S);
	Send_Hex_String("CC 33 C3 3C");
}

/******************************************************************************/

/******************************************************************************/
void GB231232(unsigned int X,unsigned int Y,char *S )
////////24*24点阵GB2312字符串显示
{

	Send_Byte(0xAA);
	Send_Byte(0x55);
	Send_Byte((char)(X>>8));
	Send_Byte((char)(X&0xff));
	Send_Byte((char)(Y>>8));
	Send_Byte((char)(Y&0xff));
	Send_String(S);
	Send_Hex_String("CC 33 C3 3C");
}
/******************************************************************************/
/*																*/
/******************************************************************************/
void STRING(unsigned int X,unsigned int Y,
			unsigned char Lib_ID,unsigned char C_Mode,unsigned char C_Dots,
			unsigned int Color,unsigned int BColor,char *S )
////////任意点阵字符串显示
{

	Send_Byte(0xAA);
	Send_Byte(0x98);
	Send_Byte((char)(X>>8));
	Send_Byte((char)(X&0xff));
	Send_Byte((char)(Y>>8));
	Send_Byte((char)(Y&0xff));
	Send_Byte(Lib_ID);
	Send_Byte(C_Mode);
	Send_Byte(C_Dots);
	Send_Byte((char)(Color>>8));
	Send_Byte((char)(Color&0xff));
	Send_Byte((char)(BColor>>8));
	Send_Byte((char)(BColor&0xff));
	Send_String(S);
	Send_Hex_String("CC 33 C3 3C");
}
/******************************************************************************/
/*									区域去反							*/
/******************************************************************************/
void CPLSCR(unsigned int X0,unsigned int Y0,unsigned int X1,unsigned int Y1)
//区域去反
{
	Send_Byte(0xAA);
	Send_Byte(0x5C);

	Send_Byte((char)(X0>>8));
	Send_Byte((char)(X0&0xff));
	Send_Byte((char)(Y0>>8));
	Send_Byte((char)(Y0&0xff));

	Send_Byte((char)(X1>>8));
	Send_Byte((char)(X1&0xff));
	Send_Byte((char)(Y1>>8));
	Send_Byte((char)(Y1&0xff));
	Send_Hex_String("CC 33 C3 3C");
}


/******************************************************************************/
/*									显示时间						*/
/******************************************************************************/
void D_Time(struct time *ptime)
//显示时间
{
		   
	Send_Hex_String("AA 55 00 D0 00 28");
	Send_Byte((*ptime).hour/10+0x30);
	Send_Byte((*ptime).hour%10+0x30);Send_Byte(':');
	Send_Byte((*ptime).min/10+0x30);
	Send_Byte((*ptime).min%10+0x30);Send_Byte(':');
	Send_Byte((*ptime).sec/10+0x30);
	Send_Byte((*ptime).sec%10+0x30);
	Send_Hex_String("CC 33 C3 3C");

}

/******************************************************************************/
/*									面板出初始化					*/
/******************************************************************************/
void panel_init()
{
	 
	SETPANEL(0xf800,0xffff);
	CLEARDEVICE();
	GB231224(0,8," ");
//	   ASC8S(0,0,"123456789012345678901234567890123456789012345678901234567890");
//	GB231224(0,8,"1234567890123456789012345678901234567890");
	  ASC8S(0,32," YEAR MONTH  DAY  WEEK  SET  FM ON  SLEEP ");
	//GB231224(0,40,"  09   04    23   4       22:08");ASC8S(37*8,40,"PM");


	GB231232(1*8,40,"09");
	GB231232(6*8,40,"04");
	GB231232(13*8-4,40,"25");
	GB231232(19*8,40,"6");
	GB231232(26*8,40,"04");
	ASC8S(24*8,40,"PM");

	STRING(8*8,9*8,0x21,0xc2,0x07,0xf800,0xffff,"3");
	STRING(27*8,9*8,0x21,0xc2,0x07,0xf800,0xffff,"+60");
	ASC8S(2*8,100,"EFFECT");
	ASC8S(2*8,112,"INPUT");
	ASC8S(12*8,108,"STB");
	ASC8S(19*8,11*8,"BASS");
	ASC8S(18*8,12*8,"TREBLE");
	ASC8S(18*8,13*8,"BALANCE");
	ASC8S(18*8,14*8,"VOLUME");
	ASC8S(39*8,14*8,"dB");

	GB231232(480-32*4,272-32*4,"1 2 3 A");
	GB231232(480-32*4,272-32*3,"4 5 6 B");
	GB231232(480-32*4,272-32*2,"7 8 9 C");
	GB231232(480-32*4,272-32*1,"* 0 # D");
	GB231232(0,272-32*1,"F1F2F3F4F5");


}



  struct KEY_POS{					/*按键位置结构体         	*/
	unsigned int x;					/*横坐标位置        		*/
	unsigned int y;					/*纵坐标位置     		  	*/
	char key;						/*键值		        	 	*/
};

  struct KEY_POS idata key_pos;//={0,0,-1}
/******************************************************************************/
/*								获得键值						*/
/******************************************************************************/
void Get_Key(void) _task_ GET_KEY	
{
  unsigned char idata aa,bb;
  while(1){
	if(uart_receive[2])
		key_pos.x=uart_receive[3]+256;
	else 
		key_pos.x=uart_receive[3];
	if(uart_receive[4])
		key_pos.y=uart_receive[5]+256;
	else 
		key_pos.y=uart_receive[5];

	if(key_pos.x<5*32&key_pos.y>239){
		aa=(uart_receive[3]&0xe0);
		key_pos.key=(aa>>5)|16;
		//os_send_signal (POSITION); 		     // KEY_PROCESS
		//os_send_signal (DIS_KEY);
		os_send_signal (KEY_PROCESS);

		CPLSCR(aa,240,aa+32,272);

		os_wait (K_TMO, 10, 0);          /* wait interval:  1 second             */
		CPLSCR(aa,240,aa+32,272);
	}

	else if(key_pos.x>351 && key_pos.y>143){
		aa=(uart_receive[3]&0x60);
		aa>>=5;
		aa+=1;
		aa&=0x03;
		bb=(uart_receive[5]&0x60);
		bb>>=5;
		key_pos.key=aa+bb*4;
		//os_send_signal (POSITION); 		     // KEY_PROCESS
		//os_send_signal (DIS_KEY);
		os_send_signal (KEY_PROCESS);

		CPLSCR(352+32*aa,144+32*bb,384+32*aa,176+32*bb);

		os_wait (K_TMO, 10, 0);          /* wait interval:  1 second             */
		CPLSCR(352+32*aa,144+32*bb,384+32*aa,176+32*bb);

	}
	else {
		key_pos.key=-1;
		//os_send_signal (POSITION); 		     // KEY_PROCESS
		//os_send_signal (DIS_KEY);
		os_send_signal (KEY_PROCESS);
	}

	//Clear_Array(uart_receive);
	os_wait (K_SIG, 0, 0);
  }
}

/******************************************************************************/

/******************************************************************************/
void Dis_Key (void) _task_ DIS_KEY 
{	
  while(1){
	os_wait (K_SIG, 0, 0);
	}
}
/******************************************************************************/
/*									*/
/******************************************************************************/
void Dis_Position (void) _task_ POSITION {

	while(1) {
		os_wait (K_SIG, 0, 0);
	}
}


/******************************************************************************/
/*        Task 7 'blinking': runs if current time is outside start & end time */
/******************************************************************************/
  unsigned char idata blinking_time;
  bit	idata blinking_time_en;
void blinking (void) _task_ BLINKING  {        /* blink yellow light          */
	unsigned char key;
	blinking_time=0;
	blinking_time_en=0;

	key=key_pos.key;
  while (1)  {                                 /* endless loop                */
	blinking_time++;
	switch(key){
		case 0: break;
		case 1: break;
		case 2: break;
		case 3: break;
		case 4: break;
		case 5: break;
		case 6: break;
		case 7: break;
		case 8: break;
		case 9: break;
		case 10: break;
		case 11: break;
		case 12: break;
		case 13: break;
		case 14: break;
		case 15: break;
		case 16: CPLSCR(8,40,40,72);break;
		case 17: CPLSCR(48,40,80,72);break;
		case 18: break;
		case 19: break;
		}
		if((blinking_time&0x01)&&(blinking_time<13))
			os_wait (K_TMO, 20, 0);
		else {
			os_wait (K_TMO, 20, 0);
			if((blinking_time>11)||blinking_time_en)
			os_delete_task(BLINKING);
			}


  }
}
/******************************************************************************/
/*								按键处理							*/
/******************************************************************************/
void key_process(void) _task_ KEY_PROCESS
//串口响应
{ 
  char xdata sbuff[ARRAY_LEN];
  while(1){
	if(key_pos.key>0){
	Send_Hex_String("AA 55 00 F0 00 D0");
	Send_Byte(key_pos.key/10+0x30);
	Send_Byte(key_pos.key%10+0x30);
	Send_Hex_String("CC 33 C3 3C");
	}
	else{
		sprintf(sbuff,"位置:(%3d,%3d)",key_pos.x,key_pos.y);
		Send_Hex_String("AA 55 00 00 00 B0");
		Send_String(sbuff);
		Send_Hex_String("CC 33 C3 3C");
	}

	switch(key_pos.key){
		case 0: break;
		case 1: break;
		case 2: break;
		case 3: break;
		case 4: break;
		case 5: break;
		case 6: break;
		case 7: break;
		case 8: break;
		case 9: break;
		case 10: break;
		case 11: break;
		case 12: break;
		case 13: break;
		case 14: break;
		case 15: break;
		case 16: 

				break;
		case 17: 

				break;
		case 18: 

				break;
		case 19: 

				break;
		}
				blinking_time_en=1;
				os_create_task(BLINKING);
	  os_wait (K_SIG, 2, 0);
  }
}


⌨️ 快捷键说明

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