I want to generate the pulse based on analog input giving to ADC and convert ADC value to PWM signal using LPC2138 ARM processor. I am able to dubug in Keil software in which it works perfectly as I want. but in Proteus it is not working
#include<LPC214x.h> // Define LPC2148 Header File
#include <stdio.h>
void initPWM(void);
void fanstart(void);
void Delay();
#define DONE 0x80000000
#define START 0x01000000
#define PRESET 0x00230600
#define PLOCK 0x00000400
#define PWMPRESCALE 60 //60 PCLK cycles to increment TC by 1 i.e 1 Micro-second
unsigned long Val;
void main ()
{
initPWM();
VPBDIV = 0x02; //pclk @ 30MHz
//Serial_Init ();
PINSEL1 = 0x01 << 24; //P0.28 configure as ADC0.1
// Welcome ();
AD0CR = PRESET | 0x02;
AD0CR |= START; //Start Conversion NOW
while (1)
{
do
{
Val = AD0GDR;
} while ((Val & DONE) == 0);
Val = ((AD0GDR >> 8) & 0x0FF);
if(Val>=0x08) //check condition of adc input and goto function fan()
fanstart();
else
{
PWMMR1=0;
PWMLER =(1<<1);
}
Delay();
}
}
void Delay ()
{
unsigned int i,j;
for (i=0;i<50;i++)
for (j=0;j<500;j++);
}
void fanstart(void)
{
//initClocks(); //Initialize CPU and Peripheral Clocks @ 60Mhz
//IO0DIR = 0x1; This is not needed!
//Also by default all pins are configured as Inputs after MCU Reset.
PWMMR1=0;
PWMLER =(1<<1);
if( Val>=0x08 && Val<=0x45 ) // Check adc value range and change the pulse width
{
PWMMR1 = 1000;
PWMLER = (1<<1);
Val=0x00; //Update Latch Enable bit for PWMMR1
}
else if(Val>0x45 && Val<=0x8B ) // Check adc value range and change the pulse width
{
PWMMR1 = 1250;
PWMLER = (1<<1);
Val=0x00;
}
else if( Val>0x8B && Val <=0xD1 ) //Check adc value range and change the pulse width
{
PWMMR1 = 1500;
PWMLER = (1<<1);
Val=0x00;
}
else if( Val>0xD1 && Val <=0xFF) // Check P0.4
{
PWMMR1 = 1750;
PWMLER = (1<<1);
Val=0x00;
}
}
//return 0; //normally this wont execute ever
void initPWM(void)
{
/*Assuming that PLL0 has been setup with CCLK = 60Mhz and PCLK also = 60Mhz.*/
/*This is a per the Setup & Init Sequence given in the tutorial*/
PINSEL0 = (1<<1); // Select PWM1 output for Pin0.0
PWMPCR = 0x00; //Select Single Edge PWM - by default its single Edged so this line can be removed
PWMPR = PWMPRESCALE-1; // 1 micro-second resolution
PWMMR0 = 20000; // 20ms = 20k us - period duration
// PWMMR1 = 1000; // 1ms - pulse duration i.e width
PWMMCR = (1<<1); // Reset PWMTC on PWMMR0 match
PWMLER = (1<<1) | (1<<0); // update MR0 and MR1
PWMPCR = (1<<9); // enable PWM output
PWMTCR = (1<<1) ; //Reset PWM TC & PR
//Now , the final moment - enable everything
PWMTCR = (1<<0) | (1<<3); // enable counters and PWM Mode
//PWM Generation goes active now!!
//Now you can get the PWM output at Pin P0.0!
}