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

📄 function.c

📁 全国电子设计大赛:智能电动车的设计.实现功能:以AT89C52单片机为核心
💻 C
字号:
//*************************************************************************************************
//  Module Name :  Function.C
//  CreateDate  :  2005-01-09
//  ModifData   :  2005-06-01
//  Description :  
//  Author      :  李远正
//  Version     :  V1.0
//*************************************************************************************************

#include <c8051f020.h>
#include "Function.H"

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

//=================================================================================================
// Temperature
//=================================================================================================
// Get the temperature of the chip
// 
unsigned int GetTemperature( void )
{
    unsigned long temp;
	unsigned int  TEMPER;

    AMX0SL = 0x0F;      // 选择温度传感器作为 ADC 输入
	ADC0CF = 0x61;      // 设置 ADC 的时钟 = SYSCLK/8, ADC 增益 = 2
	ADC0CN = 0xC1;      // 允许 ADC;允许低功耗跟踪方式
	                    // 清除转换完成中断
						// 选择 ADBUSY 作为转换启动源 
						// 清除窗口比较中断
						// 设置输出数据格式为左对齐
	AD0BUSY = 1;        // 启动 ADC 转换

	while( AD0INT==0 ) ; AD0INT = 0;     // Wait for convert...

//	temp   = (ADC0-42380)*420/65536;      // 精确到   1 度
	temp   = (ADC0-42380)*4200/65536;     // 精确到 0.1 度
	TEMPER = ( unsigned int )temp;
	return( TEMPER );
}


//=================================================================================================
// ADC
//=================================================================================================
// ADC 10 Bit ( datum: 0-1023 ) 
// 
unsigned int ADConvert0( unsigned char channel, unsigned char configure )
{
    AMX0SL = channel;
	AMX0CF = configure;

    ADC0CF = 0x60;      // 设置 ADC 的时钟 = SYSCLK/8, ADC 增益 = 1
	ADC0CN = 0xC0;      // 允许 ADC;允许低功耗跟踪方式
	                    // 清除转换完成中断
						// 选择 ADBUSY 作为转换启动源 
						// 清除窗口比较中断
						// 设置输出数据格式为右对齐
	AD0BUSY = 1;        // 启动 ADC 转换

	while( AD0INT==0 ) ; AD0INT = 0;     // Wait for convert...

	return( ADC0 );
}

//=================================================================================================
// DAC
//=================================================================================================
// DAC0 convert 12 Bit ( datum: 0-4095 )
// 
void DAConvert0( unsigned int datum )
{
//    datum <<= 6;             // 数据左对齐

    DAC0L   = datum &0xFF;   // 先写入低字节
	DAC0H   = datum >>8;     // 后写入高字节( 启动转换 )
}

//-------------------------------------------------------------------------------------------------
// DAC1 convert 12 Bit ( datum: 0-4095 )
// 
void DAConvert1( unsigned int datum )
{
//    datum <<= 6;             // 数据左对齐

    DAC1L   = datum &0xFF;   // 先写入低字节
	DAC1H   = datum >>8;     // 后写入高字节( 启动转换 )
}
/*
//=================================================================================================
// PWM Out  (16 Bit)
void PWM16( unsigned char channel,unsigned int datum )
{
    switch( channel )
	{
	    case 0: PCA0CPM0 = 0x82; PCA0CPL0 = datum &0xFF; PCA0CPH0 = datum >>8; break;
		case 1: PCA0CPM1 = 0x82; PCA0CPL1 = datum &0xFF; PCA0CPH1 = datum >>8; break;
		case 2: PCA0CPM2 = 0x82; PCA0CPL2 = datum &0xFF; PCA0CPH2 = datum >>8; break;
		case 3: PCA0CPM3 = 0x82; PCA0CPL3 = datum &0xFF; PCA0CPH3 = datum >>8; break;
		case 4: PCA0CPM4 = 0x82; PCA0CPL4 = datum &0xFF; PCA0CPH4 = datum >>8; break;
		default:break;
	}
}
*/
//-------------------------------------------------------------------------------------------------
// PWM Out  (8 Bit)
void PWM8( unsigned char channel,unsigned char datum )
{
    switch( channel )
	{
	    case 0: PCA0CPM0 = 0x02; PCA0CPH0 = datum; break;
		case 1: PCA0CPM1 = 0x02; PCA0CPH1 = datum; break;
		case 2: PCA0CPM2 = 0x02; PCA0CPH2 = datum; break;
		case 3: PCA0CPM3 = 0x02; PCA0CPH3 = datum; break;
		case 4: PCA0CPM4 = 0x02; PCA0CPH4 = datum; break;
		default:break;
	}
}

/*
//=================================================================================================
// ADC0_Init
//=================================================================================================
//
// Configure ADC0 to use Timer3 overflows as conversion source, to
// generate an interrupt on conversion complete, and to use left-justified
// output mode.  Enables ADC end of conversion interrupt. Leaves ADC disabled.
//
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
}

//=================================================================================================
// ADC0_ISR
//=================================================================================================
//
// ADC0 end-of-conversion ISR 
// Here we take the ADC0 sample, add it to a running total <accumulator>, and
// decrement our local decimation counter <int_dec>.  When <int_dec> reaches
// zero, we post the decimated result in the global variable <result>.
//
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
   }
}

*/

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

#define SYSCLK       11059200          // SYSCLK frequency in Hz

//=================================================================================================
// UART0_Init
//=================================================================================================
// Configure the UART0 using Timer2, for <baudrate> and 8-N-1.
// 
#define BAUDRATE0    115200            // Baud rate of UART0 in bps

//-------------------------------------------------------------------------------------------------
// 
void UART0_Init ( void )
{
	SCON0  = 0x50;                      // SCON0: Mode 1, 8-bit UART, enable RX. ( For USBS   )
//	SCON0  = 0x90;                      // SCON0: Mode 2, 9-bit UART, enable RX. ( For CH375S )
                                        // Bit 7-6: SM00,SM10
                                        // Bit 5  : SM20
                                        // Bit 4  : REN0
                                        // Bit 3-2: TB80,RB80
										// Bit 1-0: TI0, RI0

    CKCON |= 0x20;                      // (CKCON.5) Timer2 uses SYSCLK as time base
	PCON  |= 0x80;                      // (PCON.7) = SMOD0 = 1

	RCAP2  = -(SYSCLK/BAUDRATE0/32);    // Set Timer2 reload value for baudrate
	T2     = RCAP2;                     // initialize Timer value

    TR2    = 1;                         // (T2CON.2) = TR2, Start Timer2 

#if UART0_INT_EN  == 1                  // Enable UART0 interrupts
	ES0    = 1;                         // Enable UART0 interrupts
#endif
}
/*
//-------------------------------------------------------------------------------------------------
// SendByte
void UART0SendByte ( unsigned char datum )
{
	ES0    = 0;                         // Disable UART0 interrupts

	TI0    = 0;
	SBUF0  = datum; 
	while ( TI0 == 0 ) ;                // Waiting...

#if UART0_INT_EN  == 1                  // Enable UART0 interrupts
	ES0    = 1;                         // Enable UART0 interrupts
#endif
}

//-------------------------------------------------------------------------------------------------
// RecvByte
unsigned char UART0RecvByte ( void )
{
    unsigned char datum,count;

	ES0    = 0;                         // Disable UART0 interrupts

	while ( RI0 == 0 ) // Waiting...
	{
	    count++; if(count==255) {SCREEN = 51; break;}
	}
	    
	datum  = SBUF0;
	RI0    = 0;

#if UART0_INT_EN  == 1                  // Enable UART0 interrupts
	ES0    = 1;                         // Enable UART0 interrupts
#endif

	return( datum );
}
  */
//-------------------------------------------------------------------------------------------------
// SendString 
/*
void UART0SendString ( unsigned char *p )
{
	ES0    = 0;                         // Disable UART0 interrupts 

	while( *p != '\0' )
	{
	    SBUF0  = *p++; 
	    while ( TI0 == 0 ) ;            // Waiting...
        TI0    = 0;
	}

#if UART0_INT_EN  == 1                  // Enable UART0 interrupts
	ES0    = 1;                         // Enable UART0 interrupts
#endif
}
*/
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// interrupt
#if UART0_INT_EN     == 1    // Enable UART0 interrupts
void UART0_ISR ( void ) interrupt 4 
{
    RI0    = 0;                         // Clear Flag of RI0 
	ES0    = 0;                         // Disable UART0 interrupts 



	ES0    = 1;                         // Enable UART0 interrupts
}
#endif

//=================================================================================================
// UART1_Init
//=================================================================================================
// Configure the UART0 using Timer4, for <baudrate> and 8-N-1.
// 
#define BAUDRATE1    115200            // Baud rate of UART1 in bps

//-------------------------------------------------------------------------------------------------
// 
void UART1_Init ( void )
{
    SCON1  = 0x50;                      // SCON1: Mode 1, 8-bit UART, enable RX.
                                        // Bit 7-6: SM01,SM11
                                        // Bit 5  : SM21
                                        // Bit 4  : REN1
                                        // Bit 3-2: TB81,RB81
										// Bit 1-0: TI1, RI1

    CKCON |= 0x40;                      // (CKCON.6) Timer4 uses SYSCLK as time base
	PCON  |= 0x10;                      // (PCON.4) = SMOD1 = 1

    RCAP4  = -(SYSCLK/BAUDRATE1/32);    // Set Timer4 reload value for baudrate
	T4     = RCAP4;                     // initialize Timer value

    T4CON |= 0x04;                      // (T4CON.2) = TR4, Start Timer4 

#if UART1_INT_EN  == 1                  // Enable UART1 interrupts
	EIE2  |= 0x40;                      // (EIE2.6)  = ES1, Enable UART1 interrupts
#endif
}

//-------------------------------------------------------------------------------------------------
// SendByte
void UART1SendByte ( unsigned char datum )
{
	EIE2   &= 0xBF;                     // (EIE2.6)  = ES1 = 0, Disable UART1 interrupts

	SCON1 &= 0xFD;  // TI1 = 0;
	SBUF1   = datum; 
	while ( (SCON1 | 0xFD) == 0xFD ) ;  // Waiting...
    SCON1 &= 0xFD;  // TI1 = 0;

#if UART1_INT_EN    == 1                // Enable UART1 interrupts
	EIE2  |= 0x40;                      // (EIE2.6)  = ES1, Enable UART1 interrupts
#endif
}
/*
//-------------------------------------------------------------------------------------------------
// RecvByte
unsigned char UART1RecvByte ( void )
{
    unsigned char datum,count;

	EIE2   &= 0xBF;                     // (EIE2.6)  = ES1 = 0, Disable UART1 interrupts

//	while ( (SCON1 | 0xFE) == 0xFE ) ;  // Waiting...
	while ( (SCON1 | 0xFE) == 0xFE ) // Waiting...
	{
	    count++; if(count==255) {SCREEN = 51; break;}
	}
	  
	datum    = SBUF1;
	SCON1   &= 0xFE;  // RI1 = 0;

#if UART1_INT_EN     == 1               // Enable UART1 interrupts
	EIE2  |= 0x40;                      // (EIE2.6)  = ES1, Enable UART1 interrupts
#endif

	return( datum );
}

//-------------------------------------------------------------------------------------------------
// SendString 
void UART1SendString ( unsigned char *p )
{
	EIE2   &= 0xBF;                     // (EIE2.6)  = ES1 = 0, Disable UART1 interrupts

	while( *p != '\0' )
	{
	    SBUF1   = *p++; 
	    while ( (SCON1 | 0xFD) == 0xFD ); // Waiting...
        SCON1 &= 0xFD;  // TI1 = 0;
	}

#if UART1_INT_EN     == 1               // Enable UART1 interrupts
	EIE2  |= 0x40;                      // (EIE2.6)  = ES1, Enable UART1 interrupts
#endif
}
*/
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// interrupt
#if UART1_INT_EN     == 1    // Enable UART1 interrupts
void UART1_ISR ( void ) interrupt 20 
{
    SCON1  &= 0xFE;          // (SCON1.0) = RI1 = 0, Clear Flag of RI1 
	EIE2   &= 0xBF;          // (EIE2.6)  = ES1 = 0, Disable UART1 interrupts

//	USBStatus = SBUF1;

	EIE2  |= 0x40;           // (EIE2.6)  = ES1, Enable UART1 interrupts
}
#endif


⌨️ 快捷键说明

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