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

📄 serial.c

📁 矿工定位系统单端
💻 C
字号:
#include <stdio.h>
#include <W77c32.h>
#include <string.h>
#include<intrins.h>
#include "serial.h"
//sbit R485=P1^4;
/*
#define CTS    P1_0
#define RTS    P1_1
#define DSR    P1_4
#define DTR    P1_5

#define LED0   P1_6
#define LED1   P1_7
//#define DTR1    P1_6
//#define RTS1    P1_7
//#define CTS1    P3_4
//#define DSR1    P3_5
#define RING   P3_2
*/
typedef unsigned short u16  ;
typedef unsigned char  u8 ;
#define CLOCK  22118400L
//***********************串口初始化波特率定义*****原有********************
#define OSC_FREQ                22118400L
#define BAUD_115200             256 - (OSC_FREQ/192L)/115200L
#define BAUD_57600              256 - (OSC_FREQ/192L)/57600L
#define BAUD_38400              256 - (OSC_FREQ/192L)/38400L
#define BAUD_28800              256 - (OSC_FREQ/192L)/28800L
#define BAUD_19200              256 - (OSC_FREQ/192L)/19200L
#define BAUD_14400              256 - (OSC_FREQ/192L)/14400L
#define BAUD_9600               256 - (OSC_FREQ/192L)/9600L
#define BAUD_4800               256 - (OSC_FREQ/192L)/4800L
#define BAUD_2400               256 - (OSC_FREQ/192L)/2400L
#define BAUD_1200               256 - (OSC_FREQ/192L)/1200L

//====================Timer=============================================
//====================UART0=============================================
unsigned char data RX1buf[5],RXNUM=0,RecvBuff[40],RxPoint=0,RxLen=0;
bit RxFlag,Sflag=0;

#pragma OT(4,speed)

void ISR0(void) interrupt 0
{
	
}

void ISR2(void) interrupt 2
{
	
}

void ISR3(void) interrupt 3
{
	
}

void ISR5(void) interrupt 5
{
	
}

void ISR6(void) interrupt 6
{
	
}

void ISR8(void) interrupt 8
{
	
}

void ISR9(void) interrupt 9
{
	
}
void ISR10(void) interrupt 10
{
	
}

void ISR11(void) interrupt 11
{
	
}

void ISR12(void) interrupt 12
{
	
}


//=========================================================================
//uart1 interrupt
//
//
/*=========================================================================
void ub1ISR(void) interrupt 7
{
	if(RI_1)
	{
		RI_1=0;

	    RX1buf[RXNUM]=SBUF1;
		RXNUM++;
		if(RXNUM==5)RXNUM=0;

	}
	if(TI_1)
	{
		TI_1=0;
	
	}     
}
*/

//=========================================================================
//
//
//
//=========================================================================
// ===========================================================================
// TH1 = BPS = 256 -	( CLOCK/(384*BAUD) )
// Timer1, BAUD = 2400, TH1 = 0xE5(229), CLK = 25M HZ;
// Timer1, BAUD = 9600, TH1 = 0xF9(249), CLK = 25M HZ;
// Timer1, BAUD = 9600, TH1 = 0xFD(253), CLK = 11.059M HZ;
void UartInit(void)
{
	//u8 c;	
	IE   = 0 ;    // Disable all interrupts  
	//==============
	// Set Baurd Rate  
	// TH1 = BSP = 256 -  CLK / (384*Baurd)
	SCON=0x50;		//Set UART to mode 1
	SCON1=0x50;    //Set UART1 to mode 1
	//c=TMOD&0x0f;
	//c|=0x20;
	TMOD=0X21;//c; 		   //Set timer 1 to mode 2(8-bit autoload)
	PCON|=0x80;    //double baurd rate
	// BAUD_9600 ;
	TH1=BAUD_19200;//0xfb;		//TH1 = 256 -  CLK / (384*Baurd) CLK=18.432M//COM1
    TL1=TH1;

	RCAP2L=0x70;//0xc0;//0xe0;;//0X70;//0xc0;//0xe0;//0XB8;//0xfdc0---1200//B8 9600//0xfb; //ffb8--9600 //fee0--2400//ff70--4800   //RCAP2HL=65536-f0sc/(32*bps)  70 ---4800
	RCAP2H=0xff;//0xfd;//0xfe;;      //0xfd;//0xfe;//0xff;  


	TR1=1;   		//Start timer 1
	TR2=1;          //Start timer 2
	
	
	TI = 0;
	RI = 0;
	TI_1=0;
	RI_1=0;
	
	// enable interrupt register
	ES1=1;
	ES=1;
	//IE = 0xd0;    //(IE.7)EA=1  (IE.6)ES1=0  (IE.4)ES=1
} // end UartInit
//========================================================================
//
//
//

//====================================================================================
//
//
//====================================================================================

void InitREG()
{
	TA=0xaa;
	TA=0x55;
	WDCON=0x80;   //disable watchdog
	_nop_();
	_nop_();
	_nop_();
	TA=0xaa;
	TA=0x55;
	CKCON=0XC1;//0xF9;   //0xf9; timer1 clk=4; timer0 clk=4
	_nop_();
	_nop_();
	_nop_();
	
	PMR=0x41;     //4 clk 
	
	EIE=0x00;
	IE=0;
	SADEN=0;
	SADEN1=0;
    T2CON=0x30;          //the timer2 is used for baud rate of serial port 0
}




//========================================================================


/*--------------------------------------------------------------------
Start serial communications
--------------------------------------------------------------------*/
void Comm_On(void)
{
	
	
 ES1=1;     //Enable interrupt
	//	 LED1=0;
}

/*--------------------------------------------------------------------
Shutdown serial communications
--------------------------------------------------------------------*/
void Comm_Off(void)
{
	

   ES1=0;    //Disable interrupt    
}



/*--------------------------------------------------------------------
This routine sets the serial baud rate; will accept funny baud rates.
Setting the speed requires .       

Input:   <bd> : New baud rate 
--------------------------------------------------------------------*/
/*void  SetSpeed(long bd)
{
	unsigned char c;
	
	c=256-CLOCK/(64L*bd);
	TH1 = c;
}*/

/*-------------------------------------------------------------------------
Output a single character to the serial port

-------------------------------------------------------------------------*
void PutChar(unsigned char idata c)
{

	SBUF=c; 
	while (!TI );
	TI=0;
}

/*-------------------------------------------------------------------------
Output a single character to the serial port
-------------------------------------------------------------------------*/
void PutChar1(char c)
{
   // R485=1;
	SBUF1=c;
    while (!TI_1);
    TI_1=0;
}
//***************************************************************




⌨️ 快捷键说明

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