Sunteți pe pagina 1din 5

#include <targets\lpc213x.

h>
#include <math.h>
#include <ctl_api.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>

#define AD06 ((1<<9)|(1<<8)) //Selecting AD0.6 function for


P0.4
#define ADINTEN6 (1<<6) //Interrupt Enable for Channel 6
#define SEL_AD06 (1<<6) //Select Channel 6 for A/D
Conversion
#define CLKDIV (15-1) //4Mhz ADC clock
(ADC_CLOCK=PCLK/CLKDIV) where "CLKDIV-1" is actually used , in our case PCLK=60mhz

#define BURST_MODE_OFF (0<<16) //1 for on and 0 for off


#define START_NOW ((0<<26)|(0<<25)|(1<<24)) //001 for starting the conversion
immediately
#define PowerUP (1<<21) //The A/D converter is operational
#define VREF 3.3 //Reference Voltage at VREF Pin

//ISR Initialisation

static void timer0ISR(void);


void timer1ISR(void);
void AD0ISR(void);

void initClocks(void); // Setup PLL and Clock Frequency


void timer0_init(void);
void timer1_init(void);
void ADC_init(void);
void adc_start();
void pwm_init();
void UART0_init(void);
void U0Write(char data);
void Send_String(char* StringPtr);

// Globals declaration
int result=0,timer1_counter = 0;
int voltage=0;
float duty;
bool bADCDataReady = false ;

uint32_t adcAcc = 0 ;
uint32_t adcCount = 0 ;
char String[20],timerready = false;

int main(void)
{
initClocks();
pwm_init();
UART0_init();
ADC_init();
timer0_init();
timer1_init();
while(1)
{
if( bADCDataReady == true )
{
bADCDataReady = false ;
if(timerready == true)
{
sprintf(String,"adc value= %d\n\r\n",voltage);
Send_String(String);
timerready = false;
}
}
}
}

void initClocks(void)
{
PLLCON = 0x01; //Enable PLL
PLLCFG = 0x22; //Multiplier and divider setup
PLLFEED = 0xAA; //Feed sequence
PLLFEED = 0x55;

while(!(PLLSTAT & 0x0400)); //is locked?

PLLCON = 0x03; //Connect PLL after PLL is locked


PLLFEED = 0xAA; //Feed sequence
PLLFEED = 0x55;
APBDIV = 0x01; // PCLK is same as CCLK i.e.60 MHz
}

void timer0_init(void)
{
volatile unsigned int x;
T0TCR = 0; // Reset timer 0
T0PR = 0 ; // Set the timer 0 prescale counter
T0MR0 = 6000; // Set time 0 match register to generate an interrupt
every 100 us(for 40khz) (CCLK is 60mhz)
T0MCR = 3; // Generate interrupt and reset counter on match
T0TCR = 1; // Start timer 0

// setup CTL stuff for Timer 0


ctl_set_isr(4, 0, CTL_ISR_TRIGGER_FIXED, timer0ISR, 0);
ctl_unmask_isr(4);

// enable global interrupts


ctl_global_interrupts_enable();
IO0DIR=0X80000000;
}

void timer1_init(void)
{
T1TCR = 0; // Reset timer 1
T1PR = 0 ; // Set the timer 1 prescale counter
T1MR0 = 30000; // Set time 1 match register to generate an interrupt
every 500 us(for 2khz) (CCLK is 60mhz)
T1MCR = 3; // Generate interrupt and reset counter on match
T1TCR = 1; // Start timer 1

// setup CTL stuff for Timer 0


ctl_set_isr(5, 4, CTL_ISR_TRIGGER_FIXED, timer1ISR, 0);
ctl_unmask_isr(5);

// enable global interrupts


ctl_global_interrupts_enable();
//IO0DIR=0X80000000;
}

void ADC_init(void)
{
PINSEL0 = (PINSEL0 | 0x0300) ; //select AD0.6 for P0.4
ctl_set_isr(18, 5, CTL_ISR_TRIGGER_FIXED, AD0ISR, 0);
ctl_unmask_isr(18);
ctl_global_interrupts_enable();
unsigned long AD0CR_setup = (CLKDIV<<8) | BURST_MODE_OFF | PowerUP;
AD0CR = AD0CR_setup | SEL_AD06;
AD0CR |= START_NOW; //Start new Conversion
}

void pwm_init()
{
PINSEL1 = (PINSEL1 & ~(1 << 11)) | (1 << 10); //(PINSEL0 & ~(1 << 14)) | (1 <<
15); //0x8000;// // Select PWM3 output for P0.1
PWMPCR = 0x0; //Select Single Edge PWM - by
default its single Edged so this line can be removed
PWMPR = 0; // 1 micro-second resolution
PWMMR0 = 1500; //25us PWM pulse (ie., 40khz
frequency)
PWMMR5 = 0; // 0.5ms - pulse duration i.e width
(Brigtness level)
PWMMCR = (1<<1); // Reset PWMTC on PWMMR0 match
PWMLER = (1<<0)|(1<<5); // update MR0 and MR5
PWMPCR = (1<<13); // enable PWM output
PWMTCR = (1<<1) ; //Reset PWM TC & PR
PWMTCR = (1<<0) | (1<<3); // enable counters and PWM Mode
}

//ADC Conversion

void adc_start()
{
AD0CR |= START_NOW; //Start new Conversion
AD0INTEN = ADINTEN6;
}
//---------------------- Timer 0 ISR every 25 us -----------------//

static void timer0ISR(void)


{
if(voltage > 675)
PWMMR5= 675;
else
PWMMR5= voltage;
PWMLER = (1<<5);
adc_start();
T0IR = 0xFF; // Clear the timer 0
interrupt
}

void AD0ISR(void) //AD0 Interrupt Function


{
unsigned long AD0GDR_Read = AD0GDR;
int result = (AD0GDR_Read>>6) & 0x3FF; //Extract Conversion
Result
adcAcc = adcAcc + result ; //Convert result to
volts.
++adcCount ;
if( adcCount >= 32)
{
voltage = ((float)adcAcc)/32.0;
bADCDataReady = true ;
adcAcc = 0 ;
adcCount = 0 ;
}
AD0INTEN = ADINTEN6;
AD0INTEN=0;
VICVectAddr = 0x0; //Signal that ISR has
finished
}

void timer1ISR(void)
{
++timer1_counter;
if(timer1_counter>2000)
{
timerready = true;
timer1_counter = 0;
}
VICVectAddr = 0x0;
T1IR = 0xFF; // Clear the timer 1
interrupt
}

void UART0_init(void)
{
PINSEL0 = (PINSEL0 & ~(1 << 1)) | (1 << 0);
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop
bit | DLAB set to 1 */
U0DLL = 110;//135;
U0DLM = 1;
U0FDR = 0xF1; /* MULVAL=15(bits - 7:4) ,
DIVADDVAL=0(bits - 3:0)*/
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL
and DIVADDVAL
}

void U0Write(char data)


{
while (!(U0LSR & (1<<5))); // wait till the THR is empty
// now we can write to the Tx FIFO
U0THR = data;
}

void Send_String(char* StringPtr)


{
while(*StringPtr != 0x00)
{
U0Write(*StringPtr);
StringPtr++;
}
}

S-ar putea să vă placă și