/*
* File : EX09_02.c
* Purpose : ADC (Simultaneous Sampling mode)
: 4 Channels, Auto-Sample Start
: Tad Conversion Start Simultaneous Sampling Code
* Author : Prajin Palangsantikul
* Company : AppSoftTech Co.,Ltd.
* WWW : www.appsofttech.com
* Date : 18/07/08
* Ref. :
*/
//----------------------------------------------------:Includes
#include
#include
#include
#define TRUE 1
// LED
#define TRIS_LED TRISF
#define EL_FINT1 _LATF0
#define EL_BINT2 _LATF1
#define ER_FINT1 _LATF2
#define ER_BINT2 _LATF3
//----------------------------------------------------:Config fuses
_FOSC(CSW_FSCM_OFF & XT_PLL4); // Sw Disabled, Mon Disabled, XT w/PLL 4x
_FWDT(WDT_OFF); // Watchdog timer off
//----------------------------------------------------:Global variables
unsigned int ADC_Value[5];
//----------------------------------------------------:Library
#include "LIB_Uart1.C" // UART1 Module Library
//----------------------------------------------------:Delay MS
// Delay 1 ms (XT w/PLL 4x)
void Delay_MS(unsigned int ms)
{
unsigned int i;
for (; ms>0; ms--)
for (i=0; i<728; i++)
Nop(); // delay 1 mch cycle
}
//----------------------------------------------------:Initialize ADC 10bit
void ADC10_Init(void)
{
unsigned int config1, config2, config3;
unsigned int configport, configscan;
unsigned int channel;
CloseADC10(); // Turn off A/D
// Configure ADCON1 register
config1 = ADC_MODULE_OFF & // A/D Converter off
ADC_IDLE_CONTINUE & // A/D Operate in Idle mode
ADC_FORMAT_INTG & // A/D data format integer
ADC_CLK_AUTO & // sampling/conversion (Auto convert)
ADC_SAMPLE_SIMULTANEOUS & // Simultaneous sampling
ADC_AUTO_SAMPLING_ON & // Auto sampling Select
ADC_SAMP_ON; // sample/hold amplifiers are sampling
// Configure ADCON2 register
config2 = ADC_VREF_AVDD_AVSS & // Vref+ is AVdd and Vref- is AVss
ADC_SCAN_OFF & // Do notScan Input
ADC_CONVERT_CH_0ABC & // A/D channels utilised
ADC_SAMPLES_PER_INT_5 & // interrupt at 5th sample
ADC_ALT_BUF_OFF & // Buffer 16-word buffer
ADC_ALT_INPUT_OFF; // use MUXA only
// Configure ADCON2 register
config3 = ADC_SAMPLE_TIME_3 & // A/D Auto Sample Time 3 Tad
ADC_CONV_CLK_SYSTEM & // Clock Source Clock derived from system clock
ADC_CONV_CLK_3Tcy2; // A/D Conversion Clock Select bits
// Configure ADCHS register
channel = ADC_CH0_POS_SAMPLEA_AN3 & // A/D Chan 0 pos i/p sel for SAMPLE A is AN3
ADC_CH0_NEG_SAMPLEA_NVREF & // A/D Chan 0 neg i/p sel for SAMPLE A is -Vref
ADC_CHX_POS_SAMPLEA_AN0AN1AN2 & // A/D Chan A B C pos i/p sel for SAMPLE A are AN0, 1 and 2
ADC_CHX_POS_SAMPLEA_AN3AN4AN5 & // A/D Chan A B C pos i/p sel for SAMPLE A are AN3, 4 and 5
ADC_CHX_NEG_SAMPLEA_NVREF; // A/D CHA, CHB, CHC neg input is VREF-
// RB0, RB1, RB2, RB3& RB4 = analog
configport = 0xFFF0; // ADPCFG register
// Configure ADCSSL register
configscan = SCAN_NONE; // Skip AN0-AN15 for Input Scan
// configures the ADC
//OpenADC10(config1, config2, config3, configport, configscan);
OpenADC10(config1, config2, config3, configport, configscan);
// sets the positive and negative inputs for the sample multiplexers
SetChanADC10(channel);
}
//----------------------------------------------------:Main
int main(void)
{
unsigned int count, *adcptr;
unsigned int Duty; // Duty Cycle Buffer
unsigned int m , x , c ,y ,y1,y2,y3;
ADC10_Init(); // Initialize ADC 10bit
TRIS_LED = 0; // PORTE Output
_ADON = 1; // turn ADC ON
for (;;) { // Loop forever
adcptr = &ADCBUF0; // Initialize ADCBUF pointer
_ADIF = 0; // Clear interrupt
while (!_ADIF); // Convert done?
for (count=0; count<5; count++) {
ADC_Value[count] = *adcptr++;
}
Delay_MS(100);
c=1024;
m=-10;
x= ADC_Value[1];
init_mcpwm();
if (ADC_Value[1] >= 250)
{
y=(-0.5*(ADC_Value[1]))+1024 ;
y2=(y2-y)*0.1 ;
SetDCMCPWM(1,y2,0);
EL_FINT1 = 0; //_LATF0
EL_BINT2 = 1; //_LATF1
ER_FINT1 = 0; //_LATF2
ER_BINT2 = 1; //_LATF3
}
if (ADC_Value[1] <= 100)
{
y=(-0.5*(ADC_Value[1]))+1024 ;
y1=(y1-y)*0.1 ;
SetDCMCPWM(1,y1,0);
EL_FINT1 = 1; //_LATF0
EL_BINT2 = 0; //_LATF1
ER_FINT1 = 1; //_LATF2
ER_BINT2 = 0; //_LATF3
}
if (ADC_Value[1] < 50)
{
SetDCMCPWM(1,0,0);
EL_BINT2 = 0; //_LATF2
ER_BINT2 = 0; //_LATF4
EL_FINT1 = 0; //_LATF1
ER_FINT1 = 0; //_LATF3
}
if(ADC_Value[3] <= 100)
{
SetDCMCPWM(1,800,0);
EL_BINT2 = 0; //_LATF1
ER_BINT2 = 0; //_LATF3
EL_FINT1 = 0; //_LATF0
ER_FINT1 = 1; //_LATF2
}
if(ADC_Value[2] <= 100)
{
SetDCMCPWM(1,800,0);
EL_BINT2 = 0; //_LATF1
ER_BINT2 = 0; //_LATF3
EL_FINT1 = 1; //_LATF0
ER_FINT1 = 0; //_LATF2
}
}
return 0;
}
void init_mcpwm(void)
{
CloseMCPWM(); // Disable MCPWM Before New Config
// Config MCPWM Interrupt Control
ConfigIntMCPWM(PWM_INT_DIS & // Disable PWM Interrupt
PWM_INT_PR6 & // PWM Interrupt Priority = 6
PWM_FLTA_DIS_INT & // Disable Fault-A Interrupt
PWM_FLTA_INT_PR7); // Fault-A Interrupt Priority = 7
SetMCPWMFaultA(PWM_OVA1H_INACTIVE & // Enable Fault-A Control PWM1H = OFF
PWM_FLTA_MODE_LATCH & // Fault-A Mode = Latch
PWM_FLTA1_EN); // Enable Fault-A CH1
OpenMCPWM(512, // PTPER = Period = 57.6 KHz
0, // SEVTCMP = Special Time(Not Used)
// PTCON
PWM_EN & // Enable PWM Function
PWM_IDLE_STOP & // Disable PWM in IDLE Mode
PWM_OP_SCALE1 & // PWM Post Scale = 1
PWM_IPCLK_SCALE1 & // PWM Input Clock Prescale = 1
PWM_MOD_FREE , // PWM = Free Running
// PWMCON1
PWM_MOD1_IND & // PWM1 = Free Mode
PWM_PEN1H, // PWM1 High
// PWMCON2
PWM_SEVOPS1 & // Special Even Post Scaler = 1:1
PWM_OSYNC_PWM & // Overide Sync. With PWM Clock
PWM_UEN); // Enable PWM Update
}