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

📄 dg128b.h

📁 智能车控制头文件
💻 H
字号:
/*
**==============================================================================
** DG128B.H:                            -- by Dr. ZhuoQing 2005,12,12
**
**==============================================================================
*/
#ifndef __DG128B__
#define __DG128B__
//------------------------------------------------------------------------------
#ifdef DG128B_GLOBALS   
    #define DG128B_EXT
#else
    #define DG128B_EXT extern
#endif // DG128B_GLOBALS    
//------------------------------------------------------------------------------
#include "dg128bport.h"

//==============================================================================
//                  MODULE DEFINE
//------------------------------------------------------------------------------
#define MISC_EN                 1               // Enable miscellanences functions
#define REALTIME_EN             1               // Enable time base function
#define SCI0_EN                 1               // Enable SCI0 
#define SCI0_RECEINT_EN         1               // Enable SCI0 Receive buffer
#define SCI1_EN                 0               // Enable SCI0 
#define SCI1_RECEINT_EN         0               // Enable SCI0 Receive buffer
#define PUTCHAR_EN              0               // Enable 
#define ECLK_EN					1				// Enable BUSCLK = ECLK output 
#define ADC_EN					0				// Enable ADC
#define ADC_INT_EN				0				// Enable ADC interrupt
#define IRQ_EN					0				// Enable IRQ
#define I2CB_EN					0				// Enable I2C Bus
#define EEPROM_EN				1				// Enable program EEPROM
#define FLASH_EN				0				// Enable program flash
#define SPI0_EN					1				// Enable SPI0
#define SPI1_EN					1				// Enable SPI1 

//------------------------------------------------------------------------------
//					OSC CLOCK
//------------------------------------------------------------------------------
#define OSC_CLOCK			16000000L
#define REF_CLOCK			1000000L
#define BUS_CLOCK			25000000L
#define REFDV_VALUE			(OSC_CLOCK / REF_CLOCK) - 1
#define SYNR_VALUE			(BUS_CLOCK / REF_CLOCK) - 1

#define FDIV8_VALUE			0x40
#if OSC_CLOCK > 12800000
	#define FCLKDIV_VALUE 	(OSC_CLOCK / 200000 / 8) + FDIV8_VALUE - 1
#else
	#define FCLKDIV_VALUE	(OSC_CLOCK / 200000 - 1)
#endif // 

//------------------------------------------------------------------------------
#define ___INITRM      (*(volatile unsigned char *) 0x0010)
#define ___INITRG      (*(volatile unsigned char *) 0x0011)
#define ___INITEE      (*(volatile unsigned char *) 0x0012)
//------------------------------------------------------------------------------
	
//==============================================================================
//                  INTERFACE OF FUNCTION
//------------------------------------------------------------------------------
void DG128BInit(void);

//==============================================================================
//                  MISCELLIANCE FUNCTIONS
//------------------------------------------------------------------------------
#if MISC_EN
void Delay(unsigned int nLoop);
void DelayMs(unsigned int nLoop);
#define DELAY_LOOP          1132
//------------------------------------------------------------------------------
void StopHere(void);
#endif // MISC_EN

//==============================================================================
//                  REALTIME ENABLE
//------------------------------------------------------------------------------
#if REALTIME_EN
void RealTimeInit(void);
DG128B_EXT  unsigned int g_nRealTimeCount;

void WaitTime(unsigned int nTime);              // 1.024ms 
#endif // REALTIME_EN

//==============================================================================
//                  SCI0 FUNCTION
//------------------------------------------------------------------------------
#if SCI0_EN
#define SCI0_BAUD				115200
void SCI0Init(void);
//------------------------------------------------------------------------------
#if SCI0_RECEINT_EN
#define SCI0_RECE_BUFFER         32
DG128B_EXT unsigned char g_ucSCI0ReceBuffer[SCI0_RECE_BUFFER];
DG128B_EXT unsigned char g_ucSCI0ReceHead, g_ucSCI0ReceTail;
#endif // SCI0_RECEINT_EN
//------------------------------------------------------------------------------
#define SEND_RECE_LOOP          0xfff0
void SendChar0(unsigned char ucChar);
unsigned char ReceChar0(unsigned char * pucChar);

#if SCI0_RECEINT_EN
    #define CAN_RECE0  (g_ucSCI0ReceHead != g_ucSCI0ReceTail)
#else
    #define CAN_RECE0  (SCI0SR1 & 0x20)
#endif // SCI0_RECEINT_EN
//------------------------------------------------------------------------------

#if PUTCHAR_EN
char putchar(char c);
#endif PUTCHAR_EN    
//------------------------------------------------------------------------------
#endif // SCI0_EN

//==============================================================================
//                  SCI1 FUNCTION
//------------------------------------------------------------------------------
#if SCI1_EN
#define SCI1_BAUD				115200
void SCI1Init(void);
//------------------------------------------------------------------------------
#if SCI1_RECEINT_EN
#define SCI1_RECE_BUFFER         32
DG128B_EXT unsigned char g_ucSCI1ReceBuffer[SCI1_RECE_BUFFER];
DG128B_EXT unsigned char g_ucSCI1ReceHead, g_ucSCI1ReceTail;
#endif // SCI0_RECEINT_EN
//------------------------------------------------------------------------------
#define SEND_RECE_LOOP          0xfff0
void SendChar1(unsigned char ucChar);
unsigned char ReceChar1(unsigned char * pucChar);

#if SCI1_RECEINT_EN
    #define CAN_RECE1  (g_ucSCI1ReceHead != g_ucSCI1ReceTail)
#else
    #define CAN_RECE1  (SCI1SR1 & 0x20)
#endif // SCI1_RECEINT_EN
//------------------------------------------------------------------------------
#endif // SCI1_EN

//==============================================================================
//					ADC MODULE
//------------------------------------------------------------------------------
#if ADC_EN
//------------------------------------------------------------------------------
void ADCInit(void);
unsigned int ADCSample(unsigned char ucChannel);
#define ADC_SAMPLE_LOOP			5000
//------------------------------------------------------------------------------
#endif // ADC_EN

//==============================================================================
//					IRQ MODULE
//------------------------------------------------------------------------------
#if IRQ_EN
//------------------------------------------------------------------------------
void IRQInit(void);
//------------------------------------------------------------------------------
#endif // IRQ_EN

//==============================================================================
//					I2C MODULE
// In most time, IIC bus action as master mode
//------------------------------------------------------------------------------
#if I2CB_EN
void I2CBInit(void);
//------------------------------------------------------------------------------
#define I2CB_START_AGAIN		IBCR_RSTA = 1   // Generate a repeat start
#define I2CB_IBAL_CLEAR			IBSR_IBAL = 1   // Clear the arbitration lost
#define I2CB_IBIF_CLEAR			IBSR_IBIF = 1   // Clear the I-Bus interrup flag
//------------------------------------------------------------------------------
#define I2CB_CHECK_LOOP			0x1fff0

unsigned char I2CBShiftOutByte(unsigned char ucByte);
unsigned char I2CBShiftInByte(unsigned char * pucByte); // Only read one byte

//------------------------------------------------------------------------------
#endif // I2CB_EN

//==============================================================================
//				EEPROM PROGRAM MODULE
//------------------------------------------------------------------------------
#if EEPROM_EN
void EEPromInit(void);
//------------------------------------------------------------------------------
#define EEPROM_ERASE_SECT			4           // 4 byte
#define EEPROM_START				0x800       // EEProm initial as the eeprom to 0x800
#define EEPROM_LENGTH				0x800		

//------------------------------------------------------------------------------
#define EEPROM_PROTECT_OFFSET		0x7fd
#define EEPROM_BUS_CLOCK			BUS_CLOCK

//------------------------------------------------------------------------------
#define ECMD_ERASEVERIFY			0x5
#define ECMD_WORDPROGRAM			0x20
#define ECMD_SECTORERASE			0x40
#define ECMD_MASSERASE				0x41
#define ECMD_SECTORMODIFY			0x60

//------------------------------------------------------------------------------
#define EEPROM_BYTE(x)			*((unsigned char *)(EEPROM_START + x))
#define EEPROM_WORD(x)			*((unsigned int *)(EEPROM_START + x))
#define EEPROM_LONG(x)			*((unsigned long *)(EEPROM_START + x))
#define EEPROM_FLOAT(x)			*((float *)(EEPROM_START + x))

//------------------------------------------------------------------------------
#define EEPROM_CHECK_LOOP			0xfff0
unsigned char EEPromWriteWord(unsigned int nAddress, unsigned int nWord);
unsigned char EEPromEraseAll(void);
unsigned char EEPromEraseSector(unsigned int nAddress);
unsigned char EEPromBlankCheck(void);
unsigned char EEPromSectorModify(unsigned int nAddress, unsigned int nWord);
//------------------------------------------------------------------------------                                                
unsigned char EEPromWriteByte(unsigned int nAddress, unsigned char ucByte);
unsigned char EEPromWriteDim(unsigned int nAddress, unsigned char * pucBuffer, unsigned int nNumber);
unsigned char EEPromUpdateWord(unsigned int nAddress, unsigned int nWord);
unsigned char EEPromUpdateWord2(unsigned int nAddress, unsigned int nWord1, unsigned int nWord2);
//------------------------------------------------------------------------------
void EEPromReadDim(unsigned int nAddress, unsigned int nWord, unsigned char * pucBuffer);
//------------------------------------------------------------------------------
#endif // EEPROM_EN
//------------------------------------------------------------------------------

//==============================================================================
//				FLASH PROGRAM MODULE
//------------------------------------------------------------------------------
#if FLASH_EN
void FlashInit(void);
//------------------------------------------------------------------------------
DG128B_EXT unsigned char g_ucFlashClkFlag;      // 0 : Should initialize the CLK
#define FLASH_BUS_CLOCK			BUS_CLOCK
//------------------------------------------------------------------------------
#define FLASH_START				0x0
#define FLASH_BYTE(x)			*((unsigned char * far)(FLASH_START + x))
#define FLASH_WORD(x)			*((unsigned int * far)(FLASH_START + x))
#define FLASH_LONG(x)			*((unsigned long * far)(FLASH_START + x))
#define FLASH_FLOAT(x)			*((float * far)(FLASH_START + x))
//------------------------------------------------------------------------------
//==============================================================================
//------------------------------------------------------------------------------
#define FLASH_PROGRAM_LOOP					0x20000L
#define FLASH_ERASE_LOOP					0x20000L

#define FLASHCMD_WRITEWORD				0x20
#define FLASHCMD_ERASESEG				0x40
#define FLASHCMD_ERASEBLOCK				0x41
#define FLASHCMD_ERASEBLANK				0x05	// Erase block and check blank.

//------------------------------------------------------------------------------
#pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this module. Placement will be in NON_BANKED area. */
//------------------------------------------------------------------------------
DG128B_EXT unsigned int g_nFlashAddress, g_nFlashWord;
unsigned char FlashProgramWord(void);
unsigned char FlashEraseSegment(void);
//unsigned char FlashEraseBlock(void);
void FlashSetPage(unsigned char ucPage);
//------------------------------------------------------------------------------
#define FLASH_FUN_LENGTH		0x60            // Keep the flash operation
DG128B_EXT unsigned char g_ucFlashProgramWord[FLASH_FUN_LENGTH];
DG128B_EXT unsigned char g_ucFlashEraseSegment[FLASH_FUN_LENGTH];
#define FLASH_PROGRAM_WORD		((unsigned char __near (*)(void))((unsigned int)g_ucFlashProgramWord))
#define FLASH_ERASE_SEGMENT		((unsigned char __near (*)(void))((unsigned int)g_ucFlashEraseSegment))
//------------------------------------------------------------------------------
#define FLASH_SECTOR_LENGTH 	512
//------------------------------------------------------------------------------
unsigned char FlashWriteWord(unsigned long lnAddress, unsigned int nWord);
unsigned char FlashEraseSector(unsigned long lnAddress);
DG128B_EXT unsigned char g_ucFlashPage, g_ucFlashFCNFG;
//------------------------------------------------------------------------------
#pragma CODE_SEG DEFAULT               /* Change code section to DEFAULT. */
//------------------------------------------------------------------------------
#endif // FLASH_EN
//------------------------------------------------------------------------------

//==============================================================================
//				SPI0,1 MODULE SECTION
//------------------------------------------------------------------------------
#if SPI0_EN
//------------------------------------------------------------------------------
void SPI0Init(void);

#define SPI0_SENDRECE_LOOP			0xfff0
unsigned char SPI0InOutByte(unsigned char ucSend, unsigned char * pucRece);
				                                // return 0 : OK; Other : Overtime
#define SPI0InByte(x)		(void)SPI0InOutByte(0x0, x)
#define SPI0OutByte(x)		(void)SPI0InOutByte(x, NULL)				                                
//------------------------------------------------------------------------------
unsigned char 	SPI0InOutInt8(unsigned char ucSend);
unsigned int 	SPI0InOutInt16(unsigned int nSend);
unsigned long 	SPI0InOutInt24(unsigned long lnSend);
unsigned long   SPI0InOutInt32(unsigned long lnSend);
//------------------------------------------------------------------------------
unsigned char SPI0OutInt8(unsigned char ucByte);
unsigned char SPI0InInt8(void);
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
#endif // SPI0_EN

//==============================================================================
//------------------------------------------------------------------------------
#if SPI1_EN
//------------------------------------------------------------------------------
void SPI1Init(void);
//------------------------------------------------------------------------------
#endif // SPI1_EN
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//                  END OF THE FILE  DG128B.H
#endif // __DG128B__

⌨️ 快捷键说明

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