lin_init.h

来自「芯科原厂所有c8051fxx程序的例子。」· C头文件 代码 · 共 138 行

H
138
字号
// Copyright (c) 2006 SILICON LABORATORIES, INC.
//
// FILE NAME   : LIN_Init.h
// TARGET MCU  : C8051F52xA-53xA
// DESCRIPTION : Header file for LIN_Init.c
//

#ifndef _LIN_INIT_H_
#define _LIN_INIT_H_


//-----------------------------------------------------------------------------
// Typedefs
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------

// The Internal Oscillator frequency is 24.5 MHz.
#define  IOSC_FREQ      24500000

// Determine the divider necessary to generate SYSCLK.
#if (SYSCLK <= IOSC_FREQ / 128)
#define  IFCN     0x00
#elif (SYSCLK <= IOSC_FREQ / 64)
#define  IFCN     0x01
#elif (SYSCLK <= IOSC_FREQ / 32)
#define  IFCN     0x02
#elif (SYSCLK <= IOSC_FREQ / 16)
#define  IFCN     0x03
#elif (SYSCLK <= IOSC_FREQ / 8)
#define  IFCN     0x04
#elif (SYSCLK <= IOSC_FREQ / 4)
#define  IFCN     0x05
#elif (SYSCLK <= IOSC_FREQ / 2)
#define  IFCN     0x06
#elif (SYSCLK <= IOSC_FREQ / 1)
#define  IFCN     0x07
#endif

// Disable AUTOBAUD if device is in master mode.
#if (LIN_MODE == LIN_MASTER)
#undef   AUTOBAUD
#define  AUTOBAUD    FALSE
#endif

// If device is in slave mode and autobaud is enabled, it will determine the
// LIN bit rate using the autobaud method.
// LIN_BIT_RATE must be set to 20000 in autobaud mode.
#if ((LIN_MODE == LIN_SLAVE) && (AUTOBAUD == TRUE))
#undef   LIN_BIT_RATE
#define  LIN_BIT_RATE            20000
#endif

// Determine the multiplier value to load into LINMUL.
#define  LIN_MULTIPLIER          ((20000uL / LIN_BIT_RATE) - 1)

// First, obtain LIN_PRESCALAR_LOOKUP using LIN_MULTIPLIER.
#define  LIN_PRESCALAR_LOOKUP    (SYSCLK / ((LIN_MULTIPLIER + 1) * LIN_BIT_RATE * 200))

// Determine the prescalar value to load into LINMUL.
// This is done by taking the natural log of LIN_PRESCALAR_LOOKUP, dividing by
// the natural log of two, and subtracting one. The preprocessor cannot perform
// the natural log function, so this uses a table.
#if ((LIN_PRESCALAR_LOOKUP >= 0) && (LIN_PRESCALAR_LOOKUP < 4))
#define  LIN_PRESCALAR      0
#elif ((LIN_PRESCALAR_LOOKUP >= 4) && (LIN_PRESCALAR_LOOKUP < 8))
#define  LIN_PRESCALAR      1
#elif ((LIN_PRESCALAR_LOOKUP >= 8) && (LIN_PRESCALAR_LOOKUP < 16))
#define  LIN_PRESCALAR      2
#else
#define  LIN_PRESCALAR      3
#endif

// Determine the divider value to load into LINDIV.
#define  LIN_DIVIDER    (SYSCLK / ( (0x01 << (LIN_PRESCALAR + 1)) * (LIN_MULTIPLIER + 1) * LIN_BIT_RATE))

// Determine the values of T0M and SCA needed to overflow Timer 0 at
// frequency FREQUENCY_SYSTEM_TICK.
#if (LIN_MODE == LIN_MASTER)
#if ((SYSCLK / FREQUENCY_SYSTEM_TICK) <= 0xFFFF)
#define  T0M   0x04     // Timer 0 uses system clock
#define  SCA   0x00     // N/A
#define  TIMER0_RELOAD  (0xFFFF - (SYSCLK / FREQUENCY_SYSTEM_TICK))

#elif ((SYSCLK / FREQUENCY_SYSTEM_TICK / 4) <= 0xFFFF)
#define  T0M   0x00     // Timer 0 uses clock defined by [SCA1 : SCA0]
#define  SCA   0x01     // Timer 0 uses SYSCLK / 4
#define  TIMER0_RELOAD  (0xFFFF - (SYSCLK / FREQUENCY_SYSTEM_TICK / 4))

#elif ((SYSCLK / FREQUENCY_SYSTEM_TICK / 12) <= 0xFFFF)
#define  T0M   0x00     // Timer 0 uses clock defined by [SCA1 : SCA0]
#define  SCA   0x00     // Timer 0 uses SYSCLK / 12
#define  TIMER0_RELOAD  (0xFFFF - (SYSCLK / FREQUENCY_SYSTEM_TICK / 12))

#elif ((SYSCLK / FREQUENCY_SYSTEM_TICK / 48) <= 0xFFFF)
#define  T0M   0x00     // Timer 0 uses clock defined by [SCA1 : SCA0]
#define  SCA   0x02     // Timer 0 uses SYSCLK / 48
#define  TIMER0_RELOAD  (0xFFFF - (SYSCLK / FREQUENCY_SYSTEM_TICK / 48))

#else
#error   The period of timebase SYSTEM_TICK is too long to be reached at SYSCLK.
#endif

#endif   // LIN_MODE == LIN_MASTER

//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
l_bool   l_sys_init (void);
void     LIN_Init (void);
void     LIN_Bit_Rate_Init (void);
void     Sysclk_Init (void);
void     Port_Init (void);
void     Timer0_Init (void);


//-----------------------------------------------------------------------------
// Extern Variable Declarations
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Additional SFR Definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Error Messages
//-----------------------------------------------------------------------------
#if (LIN_MODE != LIN_MASTER) && (LIN_MODE != LIN_SLAVE)
#error LIN device must be configured as either LIN_MASTER or LIN_SLAVE.
#endif


#endif	// _LIN_INIT_H_

⌨️ 快捷键说明

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