main.c

来自「飞思卡尔S12系列Serial_Communications_Interface」· C语言 代码 · 共 158 行

C
158
字号
/* ======================================================================
 * Project:  SCI_Example   												*
 * file: 	 main.c														*
 * -------------------------------------------------------------------- *
 * Description:															*
 *		Open hyperterminal to establish the dimmer intensity	 		*
 *																		*
 * -------------------------------------------------------------------- *
 * Author:  Luis F. Reynoso Covarrubias								 	*
 *			Motorola Mexico FAE Team 									*
 * 																		*
 * Last Modification:  23/Dec/02										*
 * =====================================================================*/

#define FBUS 8000000					// Define the Bus frequency
#define SCALE (unsigned char) (FBUS/((unsigned long) 100* (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__  
 #define SCI_FBUS FBUS
 #define BDL9600 	(unsigned char) ((unsigned long)SCI_FBUS/(unsigned long)((unsigned long)16*(unsigned long)9600))


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

unsigned char *pSciBuffer0;
const unsigned char OK[]="Dimmer OK!";
const unsigned char error1[]="Del 0 al 100 solamente!";
const unsigned char error2[]="Solo Numeros!";
const unsigned char question[]="Dimmer Intensity(0/100)?";
unsigned char SciRxBuffer[50];
unsigned char *pSciRxBuffer;
unsigned char SciRxCounter;
Bool SciRxDone;

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


void main(void)
{ 
char reception[5]={0};
unsigned int counter=0;
unsigned int error=0;
volatile unsigned int valor=0;

	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
	
		
  	SCI0BDH = 0;							/* Set BaudRate =9600*/
	SCI0BDL	= BDL9600;
	  
	SCI0CR1 = 0x00;							/*SCI0CR2 = 0x0C;  8 bit, TE and RE set */
 	SCI0CR2 = 0x0C;
    
    do
	{
		SCI0SR1;
	    SCI0DRL = 0xFF; 						//Clear any Transmition complete flag spurious
		
		pSciBuffer0=question;   				//Initialize the Sci Buffer to the message
		
		while(*pSciBuffer0)
		{
			SCI0DRL = *pSciBuffer0++;   			//Load new byte into Data Register and at the same time clear TC Flag
			TCIE_0 = ~TCIE_0; 						//Enable Transmit Complete Interrupt
			while(!(SCI0SR1 & 0x40));
		}
		
		SCI0SR1;
		SCI0DRL = 0xFF; 						//Clear the last Transmition complete flag	
		counter=0;								//
		error=0;								// Reset internal variables
		valor=0;								//
		do 										// Receive until an error or Enter
		 {
			 while (!(SCI0SR1 & 0x20));
			 reception[counter]=SCI0DRL;		// Set ReceptionArray Element	
			 if (counter>3)																				// Error if more than 3 numbers
			 {error=1; reception[counter]=0x0D; pSciBuffer0=error1;}
			 if ((reception[counter]<0x30 || reception[counter]>0x39) && reception[counter]!=0x0D)		//Error if not 0-9
			 {error=2; reception[counter]=0x0D; pSciBuffer0=error2;}
			 
			 counter++;
		 }
		 while(reception[counter-1]!=0x0D);

		valor= ((unsigned int)reception[counter-2]-0x30);						// Convert String to Integer
		 if (counter>2)
		 valor+=((unsigned int)reception[counter-3]-0x30)*(unsigned int)10;
		 if (counter==4)
		 valor+=((unsigned int)reception[counter-4]-0x30)*(unsigned int)100;

		 	if (valor>100 && error==0)					// Error If the number is greater than 100
		 	{error=1; pSciBuffer0=error1;}
		 	
		 if (error==0)
		 {pSciBuffer0=OK;   							//Initialize the Sci Buffer to the message
		 PWMDTY7=valor;									// Set dimmer duty
		 }
		
		/*Transmit the resulting String (error or Succesful)*/
		while(*pSciBuffer0){
		
		SCI0DRL = *pSciBuffer0++;   //Load new byte into Data Register and at the same time clear TC Flag
		TCIE_0 = ~TCIE_0; //Enable Transmit Complete Interrupt
		while(!(SCI0SR1 & 0x40));
		}
		/* Transmit Carriage Return and Line Feed */
		SCI0DRL = 0x0A;   //Load new byte into Data Register and at the same time clear TC Flag
		TCIE_0 = ~TCIE_0; //Enable Transmit Complete Interrupt
		while(!(SCI0SR1 & 0x40));
		SCI0DRL = 0x0D;   //Load new byte into Data Register and at the same time clear TC Flag
		TCIE_0 = ~TCIE_0; //Enable Transmit Complete Interrupt
		while(!(SCI0SR1 & 0x40));
		
	}
	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 = 100;							// Period of 100 for PWMCH7 
	 PWMDTY7 = PWMDuty;						// Set the duty for PWMCH7
} //PWMConfig

⌨️ 快捷键说明

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