📄 lin_init.c
字号:
// Copyright (c) 2006 SILICON LABORATORIES, INC.
//
// FILE NAME : LIN_Init.c
// TARGET MCU : C8051F52xA-53xA
// DESCRIPTION : Initialization routines for LIN API
//
// Initializes the device.
//
//-----------------------------------------------------------------------------
// Header Files
//-----------------------------------------------------------------------------
#include <c8051F520.h>
#include "LIN.h"
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// l_sys_init ()
//-----------------------------------------------------------------------------
//
// Initializes the system hardware, including the system clock, port I/O,
// LIN hardware and timers.
//
l_bool l_sys_init (void)
{
// Disable Watchdog Timer
PCA0MD = 0x00;
Sysclk_Init ();
Port_Init ();
LIN_Init ();
#if (LIN_MODE == LIN_MASTER)
Timer0_Init ();
#endif
// Enable interrupts.
EA = 1;
return (1);
}
//-----------------------------------------------------------------------------
// Sysclk_Init ()
//-----------------------------------------------------------------------------
//
// Configures the system clock to frequency SYSCLK.
//
void Sysclk_Init (void)
{
// Configure the SYSCLK for Internal Oscillator divided by the largest
// possible divider (see LIN_Init.h for calculation of IFCN).
OSCICN |= IFCN;
}
//-----------------------------------------------------------------------------
// Port_Init ()
//-----------------------------------------------------------------------------
//
// Configures the system clock to frequency SYSCLK.
//
void Port_Init (void)
{
// Configure LIN TX (P0.1) as push-pull output.
P0MDOUT = 0x02;
// Skip P0.0.
P0SKIP |= 0x01;
// Enable LIN on the crossbar.
XBR0 = 0x04;
// Enable the crossbar.
XBR1 = 0x40;
}
//-----------------------------------------------------------------------------
// LIN_Init ()
//-----------------------------------------------------------------------------
//
// Initializes the LIN hardware for master or slave mode.
//
void LIN_Init (void)
{
#if (LIN_MODE == LIN_MASTER)
// Configure the device for LIN Master, manual baud rate.
LINCF = 0xC0;
#elif (LIN_MODE == LIN_SLAVE)
#if (AUTOBAUD == TRUE)
// Configure the device for LIN Slave, auto baud rate.
LINCF = 0xA0;
#else
// Configure the device for LIN Slave, manual baud rate.
LINCF = 0x80;
#endif
#endif
}
//-----------------------------------------------------------------------------
// LIN_Bit_Rate_Init ()
//-----------------------------------------------------------------------------
//
// Initializes the bit rate of the LIN hardware.
//
void LIN_Bit_Rate_Init (void)
{
// Load the divider, multiplier, and prescalar for LIN_BIT_RATE.
// Load the least significant byte of the divider.
_LIN_IREG_WRITE (LINDIV, (l_u8) LIN_DIVIDER);
// Load the multiplier, prescalar, and most significant bit of the divider.
_LIN_IREG_WRITE (LINMUL, (l_u8) ((LIN_PRESCALAR << 6) | (LIN_MULTIPLIER << 1) | (LIN_DIVIDER >> 8)));
}
#if (LIN_MODE == LIN_MASTER)
//-----------------------------------------------------------------------------
// Timer0_Init ()
//-----------------------------------------------------------------------------
//
// Configures Timer0 to overflow every system tick. This is the timebase used
// by the LIN master to control timing of the schedule table. LIN slave does not
// implement this function.
//
// The system tick frequency is defined as FREQUENCY_SYSTEM_TICK in LIN.h.
//
void Timer0_Init (void)
{
// Configure Timer0 for 16-bit operation.
TMOD |= 0x01;
// Set Timer0 timebase.
CKCON |= (T0M | SCA);
// Set Timer0 to overflow immediately.
T0 = 0xFFFF;
// Enable Timer0 interrupts.
ET0 = 1;
// Start Timer0
TR0 = 1;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -