Tuesday, March 29, 2011

Testing ADC LPC2368 Keil complier








/****************************************************/
/* Examples Program For "CP-JR ARM7 LPC2368"        */
/* Target MCU  : Philips ARM7-LPC2368               */
/*         : X-TAL : 12.00 MHz                  */
/*             : PLL Setup = MSEL(12),NSEL(1) */
/* Keil Editor : uVision3 V3.53a                    */
/* Compiler    : Keil Realview MDK V3.20        */
/* Create By   : Musa                               */
/* Last Update : 13/March/2011                      */
/* Function    : DAC testing                        */
/****************************************************/
#include "LPC23xx.h"
#define  LED1       0x02000000   // P3.25(0000 00x0 0000 0000 0000 0000 0000 0000)
#define  LED2       0x04000000   // P3.26(0000 0x00 0000 0000 0000 0000 0000 0000)
#define  LED1_ON()  FIO3CLR = LED1     // LED1 Pin = 0 (ON LED) http://www.etteam.com
#define  LED1_OFF() FIO3SET = LED1 // LED1 Pin = 1 (OFF LED)
#define  LED2_ON()  FIO3CLR = LED2 // LED2 Pin = 0 (ON LED)
#define  LED2_OFF() FIO3SET = LED2 // LED2 Pin = 1 (OFF LED)

void delay_ms(long int ms);
void delay_us();



int main(void)
{
  unsigned short table_sine[64]={
  0x07FF, 0x08C8, 0x098E, 0x0A51,
  0x0B0F, 0x0BC4, 0x0C71, 0x0D12,
  0x0DA7, 0x0E2E, 0x0EA5, 0x0F0D,
  0x0F63, 0x0FA6, 0x0FD7, 0x0FF5,
  0x0FFF, 0x0FF5, 0x0FD7, 0x0FA6,
  0x0F63, 0x0F0D, 0x0EA5, 0x0E2E,
  0x0DA7, 0x0D12, 0x0C71, 0x0BC4,
  0x0B0F, 0x0A51, 0x098E, 0x08C8,
  0x07FF, 0x0736, 0x0670, 0x05AD,
  0x04EF, 0x043A, 0x038D, 0x02EC,
  0x0257, 0x01D0, 0x0159, 0x00F1,
  0x009B, 0x0058, 0x0027, 0x0009,
  0x0000, 0x0009, 0x0027, 0x0058,
  0x009B, 0x00F1, 0x0159, 0x01D0,
  0x0257, 0x02EC, 0x038D, 0x043A,
  0x04EF, 0x05AD, 0x0670, 0x0736};


  int i,j,k,l;
  int count;
  unsigned int value,value1,value2;


PCONP &=0xFFF81FFF;              //Set up power/clock control bit manual
PCONP |=0x00081000;
PCLKSEL0 = 0x03000000;   // Set bit 25:24='11' for the periphiral clock of ADC. This is fastest conversion.
AD0CR = 0x012B0001; // bit 31:28=0, EDGE-bit 27=0, Start-bit 26:24=001, 23:22=00, 21=1, 20=0, 16=0, CLKDIV(4)=0x04, SEL=0x01  
PINSEL1 = 0x00004000;

PINSEL7  &= 0xFFC3FFFF;   // P3[26:25] = GPIO Function(xxxx xxxx xx00 00xx xxxx xxxx xxxx xxxx)
PINMODE7 &= 0xFFC3FFFF; // Enable Puul-Up on P3[26:25]
FIO3DIR  |= LED1;   // Set GPIO-3[26:25] = Output(xxxx x11x xxxx xxxx xxxx xxxx xxxx xxxx)
FIO3DIR  |= LED2;

// Config DAC
PCLKSEL0 |=0x00400000;  //Enable peripheral clock DAC
PINSEL1 |=0x00200000;  //Set P0.23 Turn to ADC
PINMODE1 |=0x00200000;

// Set port 4 as input for put botton switch P4.28 and P4.29.
PINSEL9 = 0x00000000;
LED1_ON(); /*http://etteam.com*/
LED2_ON();
k=0;
l=0;
while(1)
{
do
{
value=AD0DR0;
}
while((value & 0x80000000) == 0);
value=(value>>6) & 0x03FF;
for (j=0;j<=value;j++)
{
delay_us();
}
DACR = (table_sine[i]<<4);
i++;
i &= 0x3F;

if(((FIO4PIN & 0x10000000) == 0)&&(k==0))
{
LED1_OFF();
delay_ms(200);
LED1_ON();
value1=value;
k=1;
}
else if (((FIO4PIN & 0x10000000)==0)&&(k==1)&&(l==0))
{
LED2_OFF();
delay_ms(200);
LED2_ON();
value2=value;
l==1;
}
else if ((FIO4PIN & 0x20000000) == 0)
{
LED1_OFF();
LED2_OFF();
delay_ms(200);
LED1_ON();
LED2_ON();

while(1)
{

for (count=0;count<=5;count++)
{
 for (i=0;i<=63;i++)
 {
DACR = (table_sine[i]<<4);
for (j=0;j<=value1;j++)
{
delay_us();
}
 }
}

for (count=0;count<=5;count++)
{
 for (i=0;i<=63;i++)
 {
DACR = (table_sine[i]<<4);
for (j=0;j<=value2;j++)
{
delay_us();
}
 }
}
}
}
}
}

void delay_ms(long int ms)
{
long int i,j;
for(i=0;i<ms;i++)
for(j=0;j<3999;j++);
}

void delay_us()
{
return;
}

No comments:

Post a Comment