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

📄 atd_main.c

📁 飞思卡尔S12系列Analog_to_Digital_Converter_(ATD)应用范例
💻 C
字号:
/* ======================================================================
 * Project:  ADC_Example    											*
 * file: 	 ATD_main.c													*
 * -------------------------------------------------------------------- *
 * Description:															*
*			Led Dimmer dependant of the ADC Ch0 (AN0) input (0V to 5V)		*
 *																		*
 * -------------------------------------------------------------------- *
 * Author:  Luis Fdo. Reynoso Covarrubias								*
 * 			Motorola Mexico FAE Team 									*
 * 																		*
 * Last Modification:  17/Dec/02										*
 * =====================================================================*/
 

#define FBUS 8000000					// Define the Bus frequency
#define SCALE (unsigned char) (FBUS/((unsigned long) 255* (unsigned long) 8 * (unsigned long)2 * (unsigned long)120 )) 
										/* Sets the scaled clock (FBUS/(Period*Prescale*2*Fpwm)) 
											4 @2Mhz ; 16 @8Mhz;  32 @16 MHz*/


#define __DECL__6812DP256_H__

#include <hidef.h>
#include <6812dp256.h>

void PWMSetClockSB(void);				
void PWMConfig(unsigned char PWMDuty);	


void main(void){ 

	ATD0CTL2=0xC0;   						// Normal ATD Functionality, Fast Flag, no Wait Mode, Disable External Trigger, Interrupt disabled
	ATD0CTL3=0x20;							// 4 Conversions per sequence, No FIFO, Continue Converion in Freeze
	ATD0CTL4=0x83;							// 8 Bit Resolution, 2 Clocks in 2nd Phase, ATDClock=[BusClock*0.5]/[PRS+1]  ; PRS=3, divider=8
	ATD0CTL5=0xA0;							// Right Justified Unsigned Data, Sample Only one Channel, Analog Input =AN0
	ATD0DIEN=0x00;							// Disable  Digital Input Buffer

	PWME=0x00;								// Ensures all PWM channels are disabled
	PWMSetClockSB();						// Configure Scaled Clock B for CH7
	PWMConfig(0x00);						/* Configure the PWM Module with a defined duty in 8-Bit mode, Left Aligned, 
											 	Positive Polarity, using the Scaled Clock and a Full Period (0xFF)*/
	PWME= 0x80;								// Enable the PWM Channel 7

	do
	{
		PWMDTY7 = (unsigned char) ATD0DR0;			// Set the duty for PWMCH7 = Value readed from ADC (continuously)
	} while(1);
}


/*********PWMSetClockSB******************************
*      Configure Scaled Clock B for CH7				*
*													*
*			        	 FBUS						*
*	Fpwm(Left)=		-------------------------		*
*			 		Period*Prescale*2*Scale			*
*													*
*  Fpwm(Center)= Fpwm(Left)/2;						*
****************************************************/
void PWMSetClockSB(void)
{
	PWMPRCLK = 0x30;						// Sets the Prescaled clockB at FBUS/8
	PWMSCLB = SCALE;						// Sets the scaled clockB (FBUS/(Period*Prescale*2*Fpwm))
} // PWMSetClockSB


/*********PWMConfig**********************************************
* Configure the PWM Module with a defined duty in 8-Bit mode,	*
*  Left Aligned, Positive Polarity, using the Scaled Clock and 	*
*  a Full Period (0xFF)											*
*****************************************************************/
void PWMConfig(unsigned char PWMDuty)
{
	 PWMCTL = 0x00;							// All channels are separated 8-Bit PWMs; the clock continues in Stop & Freeze mode
	 PWMCAE = 0x00;							// PWM Operates in Left Aligned Output Mode
	 PWMPOL = 0x80;							// PWM CH7 output is high at the beginning and goes low when duty is reached.
	 PWMCLK = 0x80;							// ClockSB is the clock source for PWM CH7
	 PWMPER7 = 0xFF;						// Full period for PWMCH7 
	 PWMDTY7 = PWMDuty;						// Set the duty for PWMCH7
} //PWMConfig


⌨️ 快捷键说明

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