stc12c5a 단편기 이중 직렬 구동

1903 단어 51
쓸데없는 말은 그만하고 코드를 바로 붙여라
#include "stc12c5a60s2.h"
#ifndef __UART2_H__
#define __UART2_H__

typedef unsigned char uchar;
typedef unsigned int  uint;

#define UART1 0
#define UART2 1

#define SYS_CLK  11059200L //Íⲿ¾§ÕñÓõÄ11.0592MHz,½éÒâÓÃÕâ¸ö£¬»î×Å18.432MHz
#define BAUD2 115200
#define BAUD1 9600

#define NONE_PARITY 0  //УÑéλΪnone
#define S2RI 0x01
#define S2TI 0x02
#define S2RB8 0x04
#define S2TB8 0x08
extern uchar angle[8];
extern uchar sign;
extern uchar uart1_temp;

void uart1_init();
void uart2_init();
void send_data(uint uart,uchar dat);
void send_uart1_string(char *s);
void send_uart2_string(char *s);

#endif

#include "uart2.h"
bit busy1;
bit busy2;
uchar temp;
uchar  angle[8]={0xaa,0x01,0x02,0x03,0x04,0x05,0x06,0x55}; //½Ç¶È´æ´¢Çø Êý¾Ý½á¹¹²Î¿¼¸ÃĿ¼ÏÂÊÖ²á
uchar  cnt = 0;
uchar  sign = 1;
uchar uart1_temp;
void uart2_init()
{
#ifdef NONE_PARITY   //ÎÞУÑé
	S2CON = 0x50;
#endif
	busy2 = 0;
	BRT = -(SYS_CLK/32/BAUD2);
	AUXR = 0x14;
	IE2 = 0x01;
	//EA = 1;
}

void uart1_init()
{
	SCON = 0x50;
	busy1= 0;
	TMOD = 0x20;
	TH1  = 0xFD;
	TL1  = TH1;
	PCON =0x00;
	TR1  = 1;
	ES   = 1;
	//EA   = 1;
}

void send_data(uint uart,uchar dat)
{
	if(uart)
	{
   while(busy2);
	 busy2 = 1;
	 S2BUF = dat;
	}
	if(uart==0)
	{
	 while(busy1);
		busy1 = 1;
		SBUF = dat;
	}
}

void send_uart1_string(char*s)
{
    while(*s)
	  {
	   send_data(0,*s++);
	  }
}

void send_uart2_string(char*s)
{
   while(*s)
	 {
	  send_data(1,*s++);
	 }
}

void uart2() interrupt 8 using 1
{
   if(S2CON&S2RI)
	 {
	   S2CON&=~S2RI;
		 angle[cnt] = S2BUF;
		 cnt++;
		 if(cnt==8)
		 {
			 cnt = 0;
			 sign=1;
		 }
	 }
	 if(S2CON&S2TI)
	 {
	   S2CON&=~S2TI;
		 busy2=0;
	 }
}	

void uart1() interrupt 4 using 1
{
    if(RI)
		{
		   RI = 0;
			 uart1_temp = SBUF;
		}
		if(TI)
		{
		   TI = 0;
			 busy1=0;
		}
}

프로그램은 직렬 1과 직렬 2의 드라이버를 썼다
직접 사용 가능

좋은 웹페이지 즐겨찾기