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

📄 main.c

📁 采用C8051F020单片机的串口通信应用资料
💻 C
字号:
#include "TestSerial.h"                 // SFR declarations
#include "serial.h"
#include "serial1.h"
#include "SoftUart.h"
#define	TIMER0			1000    //1000us
#define	TIMER0_COUNT	SYSCLK/1000*TIMER0/1000	

#include <stdio.h>

sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
sfr16 TMR3     = 0x94;                 // Timer3 counter
sfr16 ADC0     = 0xbe;                 // ADC0 data
sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 T2       = 0xcc;                 // Timer2
sfr16 RCAP4    = 0xe4;                 // Timer4 capture/reload
sfr16 T4       = 0xf4;                 // Timer4
sfr16 DAC0     = 0xd2;                 // DAC0 data
sfr16 DAC1     = 0xd5;                 // DAC1 data


#define SAMPLE_RATE  500             	// Sample frequency in Hz
#define INT_DEC      256               // integrate and decimate ratio



void SYSCLK_Init (void);
void PORT_Init (void);
void ADC0_Init (void);
void Timer0_Init (int counts);
void Timer3_Init (int counts);

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

long result;                           // ADC0 decimated value

bit Flag1ms,Flag100ms,Flag1s,Flag10s;

extern bit FlagRecComm,FlagRecComm1;
extern bit FlagRecComm2,FlagRecComm3;

bit	RecDataFlag,RecDataStart,EnableSendFlag;
bit SendGPSFlag,GPSDataValid,GPSWorkStatus;
bit SendTempFlag;
unsigned char TimerCount,TimerCount10;
unsigned char Count1ms,Count100ms,Count1s;

xdata unsigned char DataBuf1[255];
xdata unsigned char DataBuf2[255];

void SYSCLK_Init (void)
{
   int i;                              // delay counter
   OSCXCN = 0x67;                      // start external oscillator with
                                       // 11.059MHz crystal
   for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
}
// Configure the Crossbar and GPIO ports
void PORT_Init (void)
{

   XBR0    = 0x2C;                     // Enable UART0
   XBR1    = 0x00;
   XBR2    = 0x44;                     // Enable crossbar and weak pull-ups,Enable UART1
   P0MDOUT |= 0x85;                    // enable TX0 TX1 as a push-pull output
   P1MDOUT |= 0x02;   //P1.1 output
   P3MDOUT = 0x73;
}
void ADC0_Init (void)
{
   ADC0CN = 0x05;                      // ADC0 disabled; normal tracking
                                       // mode; ADC0 conversions are initiated 
                                       // on overflow of Timer3; ADC0 data is
                                       // left-justified
   REF0CN = 0x07;                      // enable temp sensor, on-chip VREF,
                                       // and VREF output buffer
   AMX0SL = 0x0f;                      // Select TEMP sens as ADC mux output
   ADC0CF = (SYSCLK/2500000) << 3;     // ADC conversion clock = 2.5MHz
   ADC0CF |= 0x01;                     // PGA gain = 2

   EIE2 |= 0x02;                       // enable ADC interrupts

	DAC0CN = 0x80;	// DAC0 Control Register
	DAC0L = 0x00;	// DAC0 Low Byte Register
	DAC0H = 0x00;	// DAC0 High Byte Register

	DAC1CN = 0x80;	// DAC1 Control Register
	DAC1L = 0xFF;	// DAC1 Low Byte Register
	DAC1H = 0xFF;	// DAC1 High Byte Register
}

void Timer3_Init (int counts)
{
   TMR3CN = 0x02;                      // Stop Timer3; Clear TF3;
                                       // use SYSCLK as timebase
   TMR3RL  = -counts;                  // Init reload values
   TMR3    = 0xffff;                   // set to reload immediately
   EIE2   &= ~0x01;                    // disable Timer3 interrupts
   TMR3CN |= 0x04;                     // start Timer3
}

void Timer0_Init (int counts)
{
   CKCON|=0x8;
   TMOD|=0x1;    //16Bit
   TH0=(65536-counts)/256;
   TL0=(65536-counts)%256;
   IE|= 0x2; 
   TR0=1;
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

unsigned char Month,Date,Hour,Minute,second;

void Timer0_ISR (void) interrupt 1
{
    TH0=(65536-TIMER0_COUNT)/256;
    TL0=(65536-TIMER0_COUNT)%256;
	Flag1ms=1;
	if (Count1ms) Count1ms--;
	TimerCount--;
	if 	(!TimerCount)
	{
		TimerCount=100;
		TimerCount10--;
		Flag100ms=1;
		if (Count100ms) Count100ms--;
		if 	(!TimerCount10)
		{
			TimerCount10=10;
			Flag1s=1;
			if (Count1s) Count1s--;
			second=(second+1)%60;
			if (!second)
			{
				Minute=(Minute+1)%60;
				if (!Minute)
				{
					Hour=(Hour+1)%24;
					if (!Hour)
					{
						Date=(Date+1)%31;
						if (!Date)
						{
							Month=(Month+1)%12;
						}
					}
				}
				
			}
		}
	}
}

void ADC0_ISR (void) interrupt 15
{
   static unsigned int_dec=INT_DEC;    // integrate/decimate counter
                                       // we post a new result when
                                       // int_dec = 0
   static long accumulator=0L;         // here's where we integrate the
                                       // ADC samples             

   AD0INT = 0;									// clear ADC conversion complete
                                       // indicator

	accumulator += ADC0;                // read ADC value and add to running
                                       // total
   int_dec--;                          // update decimation counter

   if (int_dec == 0) {                 // if zero, then post result
      int_dec = INT_DEC;               // reset counter
      result = accumulator >> 8;
      accumulator = 0L;                // reset accumulator
   }
}
void Delay1ms(unsigned char T)
{
	Count1ms=T;
	while (Count1ms);
}

void main (void) {
   long temperature;           // temperature in hundredths of a degree C
   int temp_int, temp_frac;    // integer and fractional portions of temperature
   char ch;

   WDTCN = 0xde;               // disable watchdog timer
   WDTCN = 0xad;
   TimerCount=100;
   TimerCount10=10;

   Count1ms=1;
   Count100ms=1;
   Count1s=1;
   SYSCLK_Init ();              // initialize oscillator
   PORT_Init ();                // initialize crossbar and GPIO
   
   OpenComm();                  // initialize UART0
   OpenComm1();                 // initialize UART1

   SW_UART_INIT0();				// initialize UART2
   SW_UART_ENABLE0();
   SW_UART_INIT1();				// initialize UART3
   SW_UART_ENABLE1();


   ClearCommRecBuffer();
   ClearCommRecBuffer1();
   ClearCommRecBuffer2();
   ClearCommRecBuffer3();

   Timer3_Init (SYSCLK/SAMPLE_RATE);   // initialize Timer3 to overflow at sample rate
   Timer0_Init (TIMER0_COUNT);
   ADC0_Init ();                       // init ADC
   AD0EN = 1;                          // enable ADC
   EA = 1;                             // Enable global interrupts
   Delay1ms(2);
   SendCommBuffer("Init OK\r\n",9);
   SendCommString("at\r");
   GetCommCharWait(&ch,10);	
   SendCommBuffer1("A",1);
   SendCommString1("A");
   SendCommChar1('A');
   GetCommCharWait1(&ch,1);

   SendCommChar2('a');
   SendCommBuffer2("t\r",2);
   SendCommBuffer2("at\r",2);

   SendCommChar3('a');
   SendCommBuffer3("t\r",2);
   SendCommBuffer3("at\r",2);


   while (1) 
   	{
	  if (Flag1ms)
	  {
	  	Flag1ms=0;
	  }
	  if (Flag100ms)
	  {
	  	Flag100ms=0;
	  }
	  if (Flag1s)
	  {
	  	  Flag1s=0;
	   	  EIE2 &= ~0x02; // Disenable ADC interrupts
	   	  temperature = result;
	   	  EIE2 |= 0x02; // enable ADC interrupts
	  }
	  if (SendTempFlag)
	  {
	      temperature = temperature - 42380;
	      temperature = (temperature * 100L) / 156;
	      temp_int = temperature / 100;
	      temp_frac = temperature - (temp_int * 100);
		  SendCommBuffer("T=",2);
	   	  SendCommChar(0x30+temp_int/100);
	   	  SendCommChar(0x30+temp_int%100/10);
	   	  SendCommChar(0x30+temp_int%10);
	   	  SendCommChar('.');
	   	  SendCommChar(0x30+temp_frac%100/10);
	   	  SendCommChar(0x30+temp_frac%10);
		  SendCommBuffer("\r\n",2);
		  SendTempFlag=0;
	  }
	  if (FlagRecComm)
	  {
	  	  if (GetCommChar(&ch))
		  {
			SendCommChar(ch);
		  }     
	  }
	  if (FlagRecComm1)
	  {
	  	  if (GetCommChar1(&ch))
		  {
			SendCommChar1(ch);
		  }     
	  }
	  if (FlagRecComm2)
	  {
	  	  if (GetCommChar2(&ch))
		  {
			SendCommChar2(ch);
		  }     
	  }
	  if (FlagRecComm3)
	  {
	  	  if (GetCommChar3(&ch))
		  {
			SendCommChar3(ch);
		  }     
	  }
	}
}

⌨️ 快捷键说明

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