/* =========================================================================== */
/* Project:        LiteAutoReloadTimer */
/* File:           LiteAutoReloadTimermain.c */
/* Organization:   STMICROELECTRONICS */
/* Author:         VQT  (initial version generated by RBuilder) */
/* Date:           3/16/2006 */
/* =========================================================================== */

/*
This example shows how to use the ADC and the PWM of the ST7LiteUS with the REva board.

It reads the value of the potentiometer and the more it is close to the middle 
value (50%), the more it outputs on the PWM.

On the REva board, you'll need to set the enable jumpers called "POT", "D0", and "D2".

The PWM outputs on D0, and D2 gets the same signal as POT.

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

/* --------------------------------------------------------------------------- */
/* Function:       PeriphInit */
/* Purpose:        Periph configuration at RESET */
/* Date:           3/16/2006 */
/* --------------------------------------------------------------------------- */
void PeriphInit ( void ) {
   /* Lite Auto Reload Timer initialization */
   LART_Init(LART_COUNTER_CLK_FCPU);

   /* A/D Converter initialization */
   ADC_Init(ADC_DEFAULT);
   ADC_Select_Channel(2);
   
   /* I/O Port initialization */
   IO_Init();
   IO_Output(IO_PUSH_PULL, IO_PORT_A, 0xfb);
   IO_ByteWrite(IO_PORT_A, 0xff); 
}/* end of  PeriphInit */

/* --------------------------------------------------------------------------- */
/* Function:       main() */
/* Purpose:        main routine */
/* Date:           3/16/2006 */
/* --------------------------------------------------------------------------- */
void main ( void ) {

   /* Configure the internal peripherals */
   PeriphInit();

   while ( 1 ){
        int Conv_Data1;
   
         ADC_Select_Channel(2);                    /* Select channel  */
         ADC_Enable();                             /* Start conversion ADON bit is set */
         while ( !ADC_Test_Conversn_Complete() ) ; /* Wait till conversion completes */
         Conv_Data1 = ADC_Conversn_Read();         /* Read converted value */
         
         if ( Conv_Data1 >= 512 ) {
           LART_PWM_Mode(0xB00, LART_POLARITY_0, 0xE00 + Conv_Data1 - 512);     /* Generate PWM at PWM0 pin */        
         } else {
           LART_PWM_Mode(0xB00, LART_POLARITY_0, 0xE00 + 511 - Conv_Data1);     /* Generate PWM at PWM0 pin */        
         }
         ADC_Disable();                          
   } 
}/* end of main */


