📄 init.c
字号:
#include <head.h> // SFR declarations
/*************************************************
* VERSION: 1.0,2005.10.27
* AUTHOR: Xiong Xiaojun,BUAA
* FUNCTION: SYSCLK
**************************************************/
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 14.7456MHz crystal
for (i=0; i < 255; 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
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
// P0 PORT
// 0 1 2 3 4 5 6 7
// TX0 RX0 SCK MISO MOSI NSS TX1 RX1
void PORT_Init (void)
{
// P0.0 = UART TX0 (Push-Pull Output)
// P0.1 = UART RX0 (Open-Drain Output/Input)
// P0.2 = SMBus SDA (Open-Drain Output/Input)
// P0.3 = SMBus SCL (Open-Drain Output/Input)
// P0.4 = T0 (Open-Drain Output/Input)
// P0.5 = T1 (Open-Drain Output/Input)
// P0.6 = T4 (Open-Drain Output/Input)
XBR0 = 0x05; // XBAR0: Initial Reset Value
XBR1 = 0x0A; // XBAR1: Initial Reset Value
XBR2 = 0x48; // XBAR2: Initial Reset Value
P0MDOUT = 0x00; // Output configuration for P0
P1MDOUT = 0xff;
P2MDOUT = 0xff;
P3MDOUT = 0x38;
}
/*************************************************
* VERSION: 1.0,2005.10.27
* AUTHOR: Xiong Xiaojun,BUAA
* FUNCTION: Serial initialize
**************************************************/
// SCON
// SM0 SM1 SM2 REN TB8 RB8 TI RI
// TCON
// TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
// TMOD
// GATE1 C/T M1 M0 GATE0 C/T M1 M0
// M1 M0 = 01 16 BIT Timer
// M1 M0 = 10 AutoLoad 8 bit
// T2CON
// TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2
void Serial_Init(void)
{
SCON0 = 0x50; // Serial Port Control Register
PCON = 0x00; // Power Control Register
}
//************1ms定时器的初始化*********************
/*************************************************
* VERSION: 1.0,2003.4.14
* AUTHOR: Xiong Xiaojun,BUAA
* FUNCTION: 1ms Timer initialize
**************************************************/
void Timer_Init(void)
{
CKCON = 0x00; // Clock Control Register
TH0 = 0x00; // Timer 0 High Byte
TL0 = 0x00; // Timer 0 Low Byte
TH1 = 0x00; // Timer 1 High Byte
TL1 = 0x00; // Timer 1 Low Byte
TMOD = 0x55; // Timer Mode Register
TCON = 0x00; // Timer Control Register
RCAP2H = 0xFF; // Timer 2 Capture Register High Byte
RCAP2L = 0xD9; // Timer 2 Capture Register Low Byte
TH2 = 0x00; // Timer 2 High Byte
TL2 = 0x00; // Timer 2 Low Byte
T2CON = 0x34; // Timer 2 Control Register
TMR3RLL = 0xB0; // Timer 3 Reload Register Low Byte
TMR3RLH = 0x3C; // Timer 3 Reload Register High Byte
TMR3H = 0x3C; // Timer 3 High Byte
TMR3L = 0xB0; // Timer 3 Low Byte
TMR3CN = 0x00; // Timer 3 Control Register
RCAP4H = 0x00; // Timer 4 Capture Register High Byte
RCAP4L = 0x00; // Timer 4 Capture Register Low Byte
TH4 = 0x00; // Timer 4 High Byte
TL4 = 0x00; // Timer 4 Low Byte
T4CON = 0x08; // Timer 4 Control Register
}
void AD0_Init(void)
{
REF0CN = 0x07; // Reference Control Register
//----------------------------------------------------------------
// ADC Configuration
//----------------------------------------------------------------
AMX0CF = 0x60; // AMUX Configuration Register
AMX0SL = 0x08; // AMUX Channel Select Register
ADC0CF = 0x58; // ADC Configuration Register
ADC0CN = 0x80; // ADC Control Register
}
void SMB_Init(void)
{
SMB0CN = 0x44; // SMBus Control Register
SMB0ADR = 0x00; // SMBus Address Register
SMB0CR = 0xc4; // SMBus Clock Rate Register 100kbps
EIE1 |= 0x02; //Extended Interrupt Enable 1
}
/*************************************************
* VERSION: 1.0,2003.4.14
* AUTHOR: Xiong Xiaojun,BUAA
* FUNCTION: Parameter initialize
**************************************************/
void Para_Init(void)
{
TimeCount = 0;
Count = 0;
LED = 1; // led off
ENABLE = 0; // disable
ZEROFLAG = 1;
READFLAG = 1;
PRINTFLAG = 1;
HumidityCount = 0;
HumidityFreq = 0;
TempCount = 0;
Readoverflag = 0;
StartReadflag = 0;
RWflag = I2CREAD; // read
// RWflag = I2CWRITE; // write
TempValue = 0;
JQOverFlag = 0;
}
void Init(void)
{
SYSCLK_Init ();
PORT_Init ();
Serial_Init();
Timer_Init();
AD0_Init();
Para_Init();
SMB_Init();
delay(6000);
Lcd_Init();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -