
/* =========================================================================== */
/* Project:        PWM */
/* File:           PWMMain.c */
/* Organization:   STMicroelectronics */
/* Author:         STMicroelectronics  (initial version generated by RBuilder) */
/* Date:           11/16/2004 */
/* =========================================================================== */

#include "ST7lib_config.h"
#include <stdio.h>

#define _btst(A, B)		((A) & (0x01<<(B)))
//#endif

/*---------------------------------------------------------------------------*/
/*Function:       main()*/
/*Purpose:        main routine*/
/* Date:           11/16/2004 */
/*---------------------------------------------------------------------------*/
//at 0x00 hreg PADR    ;
void main ( void ) {

//PADR = 0x80;
unsigned char val_adc, old_adc = 0, Pfaible;
unsigned int PWMValue = 0x0000;

PADDR = 0x9F;				// configures port A[0..4] and PA7 as output
PAOR = 0x9F;
PADR = 0x9F;


ADCCSR = 0x21;			// enables the A/D peripheral on PB1 
BREAKCR = 0;
BREAKEN = 0;
ATCSR = 0x10;        //PWM config freq
ATCSR2 = 0xC3;
ATRH = 0x0F;
ATRL = 0xB5;
PWM0CSR = 0x02;      //0x00 = Not inverted ; 0x02 = inverted                    
DCR0H = 0x0F;
DCR0L = 0xB5;  
PWMCR = 0x01;       //Output enable

while(1)
  { 
	while(!_btst(ADCCSR, 7));	// waits for the end of the conversion
	val_adc = ADCDRH;		// reads the A/D value and writes it on the LEDs    
	//PADR = val_adc ^0x9F;
	if( (val_adc >= (old_adc + 5)) || (val_adc <= (old_adc - 5))){
	 old_adc = val_adc;
	 if (val_adc >= 0xF5){    
	  DCR0H = 0x0f;
	  DCR0L = 0xff;
	  ATCSR2 = 0xC3;
	 } else if(val_adc <= 0x0A){   
	  DCR0H = 0x0F;
	  DCR0L = 0xB5; 
	  ATCSR2 = 0xC3;
	 } else {
     PWMValue = ((unsigned int)74 * val_adc)/(unsigned int)244;   
     Pfaible =  PWMValue;
	  DCR0H = 0x0F;
	  DCR0L = 0xB5 +Pfaible;
	  ATCSR2 = 0xC3;
 	 }    
	}
}   
//#endif
}/*end of main*/



