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

📄 pic library.h

📁 PIC18F4520 + TW8806B ucosII+net test program source
💻 H
字号:
/*==============================================================================================
//	Filename		: PIC Library.h
//	Processor		: PIC18F2420, PIC18F2520, PIC18F4420, PIC18F4520
//	Compiler		: HI-TECH PICC-18 V8.35 Compiler
==============================================================================================*/

#define delay_us(x){unsigned char dcnt; \
		dcnt = (x)/((12*1000L)/(4*1000L))|1; \
		while(--dcnt != 0) \
		continue;	}

/*==============	⒀ Delay Routine ⑿  =============================================================*/
void delay_ms(unsigned char Delay)
{
	unsigned char i;
	do{	i = 4;
		do{ delay_us(250);  }while(--i);
		}while(--Delay);
	return;
}// End of delay_ms(unsigned char cnt)

/*==============	⒀ OSD KEY ADC Conversn Data Read Routine ⑿  ======================================*/
static unsigned char OSDKey_ADC_Read(void)
{
	ADCON0	= 0B00001001;	//AN2 Channel ADC Mudule Enable
	GODONE 	= ON;		//Initiate conversion on the selected channel
	while(GODONE)continue;	//ADSCR Conversion Completed?
	ADIF 	= OFF;
	return(ADRESH);		//8Bit ADC Return
}// End of ADC_Conversn_Read

/*==============	⒀ TIMER Enable Routine ⑿  ========================================================
**	Timer Overflow interrupt = 1 / (Main Clock / (4 x (255-TMR0L) x Prescaler) )
**	1 / (4Mhz / (4x50001x2)) = 100ms
===============================================================================================*/
void TIMER0_Enable(void)
{
	TMR0H 	= 0x3C;		//100ms Timer Reload
	TMR0L 	= 0xAF;		//100ms Timer Reload
	T0CON	= 0B10000000;	//Timer 0 Prescaler ±2 and 16Bit Timer Mode
	TMR0IF	= OFF;		//Timer 0 Interrupt Clear
	TMR0IE	= ON;		//Timer 0 Overflow Interrupt Enable
	return;
}// End of TIMER0_Enable

/*==============	⒀ TIMER0 Diasble Routine ⑿  ======================================================*/
void TIMER0_Disable(void)		//TIMER 0 Diasble Routine
{
	TMR0IE	= OFF;		//Timer 0 Overflow Interrupt Disable
	T0CON	= 0B00000000;	//Timer 0 Prescaler ±2 and 16Bit Timer Mode
	return;
}// End of TIMER0_Disable

/*==============	⒀ TIMER1 Enable Routine ⑿  ========================================================
**	Timer Overflow interrupt = (Main Clock / (4 x (65535-TMR1) x Prescaler) )
**	1 / (4Mhz / (4x50001x4)) / 1000 = 200ms
===============================================================================================*/
void TIMER1_Enable(void)
{
	TMR1H 	= 0x3C;		//200ms Timer Reload
	TMR1L 	= 0xAF;		//200ms Timer Reload
	T1CON	= 0B10100001;	//Prescaler ±4 amd 16Bit Timer Enable
	TMR1IF	= OFF;		//Timer 1 Interrupt Clear
	TMR1IE	= ON;		//Timer 1 Overflow Interrupt Enable
	return;
}// End of TIMER1_Enable

/*==============	⒀ TIMER1 Disable Routine ⑿  ======================================================*/
void TIMER1_Disable(void)		//TIMER1  Diasble Routine
{
	TMR1IE	= OFF;		//Timer1 Overflow Interrupt Enable
	TMR1IF	= OFF;		//Timer Interrupt Clear	
	T1CON 	= 0B10100000;	//Prescaler ±4 amd Timer Disable
	return;
}// End of TIMER1_Disable

/*==============	⒀ SCI Enable Routine ⑿  ==========================================================
**	SCI Baud Rate = Main Clock / 16 x (SPBRG + 1)
**	8Mhz / (16 x (51 + 1)) = 9,615bps
===============================================================================================*/
void SCI_Enable(void)
{
	SPBRG	= 51;		//9,615bps Baud Rate Data
	TXSTA	= 0B00100100;	//Tx Enable = Data 8Bit, 1 Stop, No Parity
	RCSTA	= 0B10000000;	//Rx Disable = Data 8Bit, 1 Stop, No Parity
	TXIF	= OFF;		//Transmiter interrupt Flag Clear
	TXIE	= OFF;		//Transmit Complete Interrupt 
	TXEN 	= ON;		//Transmiter Enable
	RCIF	= OFF;		//Rx Complete Interrupt Flag Clear
	RCIE	= ON;		//Rx Complete Interrupt Enable
	CREN 	= ON;		//Receiver Enable
	return;
}// End of SCI_Enable

/*==============	⒀ SCI Disable Routine ⑿  =========================================================*/
void SCI_Disable(void)
{
	TXSTA	= 0B00000000;	//Tx Disable = Data 8Bit, 1 Stop, No Parity
	RCSTA	= 0B00000000;	//Disabled the Receive
	RCIE	= OFF;		//Rx Complete Interrupt Disable
	RCIF	= OFF;		//Rx Complete interrupt Flag Clear
	TXIF	= OFF;		//Transmiter interrupt Flag Clear
	TXIE	= OFF;		//Transmit Complete Interrupt 
	return;
}// End of SCI_Disable

/*==============	⒀ Timer 2 PWM Module Enable Routine ⑿  ============================================
**	PWM Period = 1 / (PR2 + 1) x 4 x (1 / Fosc) x Timer2 Prescaler
**	1 / (255 + 1) x 4 x (1 / 4000000) x 16 = 244Hz
===============================================================================================*/
void PWM_Enable(void)
{
	T2CON	= 0B00000011;	//Timer 2 OFF and Prescaler ±16
	TMR2	= 0x00;		//Timer 2 Counter Clear
	PR2	= 0xFF;		//Timer 2 Module Period Register
	TMR2IF	= OFF;		//Timer 2 Interrupt Flag Clear
	T2CON	= 0B00000111;	//Timer 2 ON and Prescaler ±16
	CCPR1L 	= 0;		//Duty = 0%
	CCP1CON	= 0B00001100;	//PWM Mode ON
	return;
}// End of PWM Enable

/*==============	⒀ Timer 2 PWM Module Disable Routine ⑿  ==========================================*/
void PWM_Disable(void)
{
	T2CON	= 0B00000011;	//Timer 2 OFF and Prescaler ±16
	TMR2	= 0x00;		//Timer 2 Counter Clear
	CCPR1L	= 0x00;		//Duty = 0%
	CCP1CON	= 0B00000000;	//PWM Mode OFF
	return;
}// End of PWM Disable

/*==============	⒀SPI Module Enable Routine ⑿  =====================================================*/
void SPI_Enable(void)
{
	SSPSTAT	= 0B10000000;	//SPI Clock Mode Select
	SSPCON1	= 0B00110010;	//SPI Port Select and Module Enable of Fosc / 64
	SSPIF	= OFF;
	return;
}// End of 	SPI_Enable

/*==============	⒀ EEPROM_Write Routine ⑿  ========================================================*/
void WriteEEP(unsigned char Add,unsigned char Data)
{
	EEADR	= Add;		//Write EEPROM Address
	EEDATA	= Data;		//Write Data
	EEPGD	= OFF;		//DATA Memory Select
	CFGS	= OFF;		//Access EEPROM
	WREN	= ON;		//EEPROM Write Enable
	GIE	= OFF;		//Disable Global Interrupts
	EECON2	= 0x55;		//Required Sequence for EEPROM update
	EECON2	= 0xAA;		//Required Sequence for EEPROM update
	WR	= ON;		//EEPROM Write Enable
	GIE	= ON;		//Enable Global Interrupts
	delay_ms(6);
	while(!EEIF)continue;	//Write Complete?
	WREN	= OFF;		//EEPROM Write Disable
	EEIF	= OFF;		//EEPROM Write Interrupt Bit Clear
	return;
}//End of EEPROM_Write

/*==============	⒀ I2C EEPROM_Read Routine ⑿  =====================================================*/
unsigned char ReadEEP(unsigned char Add)
{
	EEADR	= Add;		//EEPROM Read Address
	EEPGD	= OFF;		//Point to DATA Memory
	CFGS	= OFF;		//Access EEPROM
	RD	= ON;		//EEPROM Read Enable
	while(RD)continue;
	return EEDATA;
}//End of EEPROM_Read
/*================================================================================================*/

⌨️ 快捷键说明

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