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

📄 system.#1

📁 该程序是基于c8051f的红外线发射率的测量程序
💻 #1
📖 第 1 页 / 共 2 页
字号:
//----------------------------------------------------------------------------------------------------------------------------------------------------
//文件名:system.c
//作用:用于硬件系统的初始化,以及对存储器,AD,485等的操作
//
//-----------------------------------------------------------------------------------------------------------------------------------------------------

#include<c8051F120.h>
#include <GAL.h> 
#define AIADDR           0x01  //AI表地址
#define W_CONTROLBYTE    0xA0  //24c01的写地址 A0,A1,A2均为0
#define R_CONTROLBYTE    0xA1  //24c01的读地址 A0,A1,A2均为0
#define FS_SUM           0x00  //文件系统存储sum的地址
#define FS_POINTER       0x01  //存储当前的项目号地址
#define FS_START_ADDR    0x02  //数据起始地址    
//液晶控制口
sbit STB=P3^2;   
sbit Busy=P3^1;

sbit TR=P3^0;   //控制485总线的方向
sbit SDA=P0^7;//			*************	I2C		**************
sbit SCL=P0^6;//			*************	总线	**************

bit nop=0;  //延时用
unsigned char time=0;
unsigned int emissivity_AD;
unsigned int voltage_AD;
unsigned int current_AD;

unsigned char xdata AIBUF[10];//518P表的回送数据缓冲寄存器
unsigned char xdata AIDATNUM;//518P表接收数据计数器
bit AIOVERFLG;//518P转换完成标志
bit TIMEFLG;//时间超过标志
unsigned char xdata AITIME;//518P转换时间记录
unsigned char xdata ERRORNUM;//518P错误次数
extern Cstatusbar xdata statusbar;
 
//************************************************************************************************************************************************
//延时us
//************************************************************************************************************************************************
//delay function
void delay1s(void)
{
	unsigned char i,j,k;
	for(i=0;i<214;i++)
	for(j=0;j<255;j++)
	for(k=0;k<125;k++)
	{}
}
void delay100us(void)
{
	unsigned char i,j,k;
	for(i=0;i<200;i++)
	for(j=0;j<1;j++)
	for(k=0;k<1;k++)
	{}
}
void delay200ms(void)
{
	unsigned char i,j,k;
	for(i=0;i<43;i++)
	for(j=0;j<255;j++)
	for(k=0;k<255;k++)
	{}
}
void delay5ms(void)
{
	unsigned char i,j,k;
	for(i=0;i<1;i++)
	for(j=0;j<200;j++)
	for(k=0;k<100;k++)
	{}
}
void delay4us(void)
{
	unsigned char i,j,k;
	for(i=0;i<2;i++)
	for(j=0;j<1;j++)
	for(k=0;k<1;k++)
	{}
}
void delay8us(void)
{
	unsigned char i;
    for(i=0;i<4;i++)
	 delay4us();
}
//**************************************************************************************************************************************************
//初始化系统硬件,并显示系统logo,系统
//***************************************************************************************************************************************************
void sys_init(void)
{
    WDTCN=0xDE;//先关看门狗,初始化完成之后再开
    WDTCN=0xAD;//
  	//*****************************************************************	
	SFRPAGE=CONFIG_PAGE;  //选择sfr页
	//OSC Select*******************************************************	
	OSCICN=0xC3;//使用内部频率24.5MHz,不分频
 	//*****************************************************************
    //io 定义
    XBR0=0X04; //select UART0
	XBR2=0x44; //select UART1 and enable cross switch
    P0MDOUT=0x45;  //Tx0,Tx1,SCL推挽
	P2MDOUT=0xff;  //与 lcd通信数据端口,全部推挽
    P3MDOUT=0x35;  //T/R,STB,CLK,LD 推挽
    SFRPAGE=LEGACY_PAGE;
	//*****************************************************************
	//RSTSRC init******************************************************	
	RSTSRC=0x06;//选择时钟丢失监测器和VDD监视器为复位源。
	//*****************************************************************	
	//电压基准**********************************************************
	REF0CN=0x03;//内部基准,2.4V,且采用偏置电压
    //配置时钟和UART
    CKCON=0x00;//define T0 clock is sysclk/12, T1 clock is sysclk/12
   	TMR2CF=0x08;//T2 clock is SYSCLK
	TMR2H=0xFF;//UART0 BR is 9600
	TMR2L=0x61;
 	RCAP2H=0xFF;
 	RCAP2L=0x61;
 
   	SFRPAGE=LEGACY_PAGE;
	//TMR4CF=0x00;//T4 clock is SYSCLK/12	
	TMOD=0x21;//Timer0 is in mode 1; Timer1 is in mode 2
	SCON0=0x40;//UART0 is in the mode 1 and REN0 is close, then UART0 can send but receive data
	SSTA0=0x05;//UART0 use T2 to be the TX and RX BR generator
	SFRPAGE=UART1_PAGE;
	SCON1=0x50;//UART1 is in the 8 bit mode and can receive data
	SFRPAGE=LEGACY_PAGE;
	TL0=0x40;
	TH0=0xB0;//这样T0就在10ms内中断一次
	TH1=0x96;//UART1 BR is 9600
	TL1=0x96;
	TR1=1;//open the UART1 BR
	TR2=1;//open the UART0 BR	

	//设置AD的转换
	ADC0CF=0x58;//加大SAR时钟,1MHz
    ADC0CN=0x80;  //使能adc0,选择查询模式
	REF0CN&=0x0f;//AD0VRS=0,ADC0参考电压为VREFA
	REF0CN|=0x07;//TEMPE=1,内部温度传感器工作
	//
   	//interrupt define*************************************************
	IP=0x10;//UART0 is the highest level int
	//IE=0x07;//INT0 and T0 and INT1 are active
	IE=0x12;//uart0 and T0 are active
	//EIE1=0x04;//允许ADC0窗口比较中断
	EIE1=0x00;
	EIE2=0x40; //允许UART1中断
	EIP2=0x40; //UART1中断优先级置高,注意此位一定要置高,否则不能进中断
	TR0=1;//开始清狗
	EA=1;//open the interrupt
	//*****************************************************************
}

void SYSINIT(void)
{
	//*****************************************************************	
	SFRPAGE=CONFIG_PAGE;
	//OSC Select*******************************************************	
	OSCICN=0xC3;//使用内部频率24.5MHz,不分频
	//*****************************************************************
	//I/O define*******************************************************
	XBR0 = 0x04;//select UART0
	XBR1 = 0x14;//select INT0 and INT1
	XBR2 = 0x04;//选择弱上拉,并且选择UART1
	XBR2 = 0x44;//使能交叉开关
	//*****************************************************************	
	P0MDOUT = 0x45; // Output configuration for P0 
    P1MDOUT = 0x80; // Output configuration for P1 
    P2MDOUT = 0xFF; // Output configuration for P2 
    P3MDOUT = 0x61; // Output configuration for P3 	
	SFRPAGE=LEGACY_PAGE;
	//*****************************************************************
	//RSTSRC init******************************************************	
	RSTSRC=0x06;//选择时钟丢失监测器和VDD监视器为复位源。
	//*****************************************************************	
	//电压基准**********************************************************
	REF0CN=0x03;//内部基准,2.4V,且采用偏置电压
	//*****************************************************************	
	//TCON*************************************************************
	TCON=0x05;//INT0 and INT1 are active in edge mode
	//*****************************************************************
	//UART and Timer n config******************************************
	CKCON=0x00;//define T0 clock is sysclk/12, T1 clock is sysclk/12
	TMR2CF=0x08;//T2 clock is SYSCLK
	TMR2H=0xFF;//UART0 BR is 9600
	TMR2L=0x61;
	RCAP2H=0xFF;
	RCAP2L=0x61;
	SFRPAGE=TMR3_PAGE;
	TMR3CF=0x00;//T3 clock is SYSCLK/12
	TMR3H=0x00;
	TMR3L=0x00;
	RCAP3H=0x00;
	RCAP3L=0x00;
	SFRPAGE=LEGACY_PAGE;
	//TMR4CF=0x00;//T4 clock is SYSCLK/12	
	TMOD=0x21;//Timer0 is in mode 1; Timer1 is in mode 2
	SCON0=0x40;//UART0 is in the mode 1 and REN0 is close, then UART0 can send but receive data
	SSTA0=0x05;//UART0 use T2 to be the TX and RX BR generator
	SFRPAGE=UART1_PAGE;
	SCON1=0x40;//UART1 is in the 8 bit mode and can't receive data
	SFRPAGE=LEGACY_PAGE;
	TL0=0x7F;
	TH0=0x60;//这样T0就在12ms内中断一次
	TH1=0x96;//UART1 BR is 9600
	TL1=0x96;
	TR1=1;//open the UART1 BR
	TR2=1;//open the UART0 BR	
	//*****************************************************************
	ADC0LTL=0xA0;
	ADC0LTH=0x02;//上限为4mA报警
	ADC0CF=0x58;//加大SAR时钟,1MHz
	ADC0CN=0xC0;//开ADC0并且设置为转换才跟踪方式,设置为向AD0BUSY写1判断AD0INT为1方式启动ADC0
	//*********************************************************************
	//interrupt define*************************************************
	IP=0x10;//UART0 is the highest level int
	//IE=0x07;//INT0 and T0 and INT1 are active
	IE=0x03;//INT0 and T0 are active
	//EIE1=0x04;//允许ADC0窗口比较中断
	EIE1=0x00;
	EIE2=0x03;//允许ADC0中断和T3中断
	TR0=1;//开始清狗
	EA=1;//open the interrupt
	//*****************************************************************
//	BEEP=0;//close the beep
//	MOTORSTOP;//关电机
	//TPFCOFF;//关二位四通阀	
	//*****************************************************************
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////	以下是关于I2C总线的	///////
////////////////////////////////////////////////	延时6us			////////////
void delay6us(){
	nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;
	nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;
	nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;
	nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;
	nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;
	nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;nop=0;
}
////////////////////////////////////////////////	延时任意us		////////////
void delayus(unsigned int us){
	unsigned int i;
	for(i=0;i<us;i++);
}
////////////////////////////////////////////////	启动I2C总线		///////////
void i2cstart(){
	SCL=0;
	delay6us();
	SDA=1;
	delay6us();
	SCL=1;
	delay6us();
	SDA=0;
	delay6us();
	SCL=0;
	delay6us();
}
////////////////////////////////////////////////	关闭I2C总线		///////////
void i2cstop(){
	SCL=0;
	delay6us();
	SDA=0;
	delay6us();
	SCL=1;
	delay6us();
	SDA=1;
	delay6us();
	SCL=0;
	delay6us();
}

////////////////////////////////////////////////	发送非应答位	//////////
void mnack(){
	SDA=1;
	delay6us();
	SCL=1;
	delay6us();
	SCL=0;
	delay6us();
	SDA=0;
}
////////////////////////////////////////////////	应答位检查子程序	//////
unsigned char checkack(){
	unsigned char ack;
	SDA=1;
	delay6us();
	SCL=1;
	delay6us();
	ack=SDA;
	delay6us();
	SCL=0;
	delay6us();
	return(ack);
}
///////////////////////////////////////////////		向FLASH写1个字节	///////
void write1byte(unsigned char input){
	unsigned char i,temp;
	temp = input;
	for(i=0;i<8;i++){
		SCL = 0;
		delay6us();
		temp<<=1;
		SDA = CY;//temp左移溢出的位
		delay6us();
		SCL = 1;
		delay6us();
	}
	SCL = 0;
	delay6us();						
	SDA = 1;
}
///////////////////////////////////////////////		从FLASH中读1个字节	/////////
unsigned char read1byte(){
	unsigned char i,temp;
	temp = 0;
	SCL = 0;
	delay6us();
	SDA = 1;
	delay6us();
	for(i=0;i<8;i++){
		SCL = 1;
		delay6us();
		temp=(temp<<=1) | SDA;
		SCL = 0;
		delay6us();
	}							
	SDA = 1;
	delay6us();	
	delayus(2000); 					
	return(temp);
}
//*****************************************************************************************************************************************************
//从存储器芯片指定地址处读出一字节
//*****************************************************************************************************************************************************
unsigned char write_byte(unsigned char dat,unsigned int addr)

⌨️ 快捷键说明

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