Hi ,
today I'm grad to show my little project about how to program to read analog signal from accelerometer by LPC2148(ARM7) . so let see.
Development tools
- Keil Compliler
- ADXL3XX from paralax
- LPC2148 board by SiSy-Works
- laptop
- USB to Serial converter
Source CODE
//This code is simple.
//Sorry if it is not good code.
#include "LPC214X.H" //change "" to <>
#include "stdio.h"
#define _PCLK 30000000 // Define PCLK for configuration baudrate
#define FWD() FIO1SET = 0x00C50000;\
FIO1CLR = 0x003A0000
#define BCK() FIO1SET = 0x00CA0000;\
FIO1CLR = 0x00350000
#define LFT() FIO1SET = 0x00C90000;\
FIO1CLR = 0x003E0000
#define RGT() FIO1SET = 0x00C60000;\
FIO1CLR = 0x003B0000
#define Brake() FIO1SET = 0x00CF0000
unsigned char index=0, temp_index=0, blue_con=0;
unsigned char Buffer[64];
unsigned char rec_comp=0;
void init(void)
{
PLL0CFG=0x24; // MSEL = 4,PSEL = 2
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
PLL0CON=0x1;
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked
PLL0CON=0x3; // Connect the PLL as the clock source
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch (4 cclks in this case)
MAMTIM=0x4;
VPBDIV=0x02; // PCLK at 30 MHz
//----set up pinsel-----------//
PINSEL0 = 0x00000000; // Clear PINSEL0
PINSEL0 |= 0x00003F00; // ADC Pins P0.04-P0.06 (AD 0.6/0.7/1.0)
//PINSEL1
PINSEL1 = 0x00000000; // Clear PINSEL1
PINSEL1 |= 0x00001800; // ADC Pins P0.21-P0.22 (AD 1.6/1.7)
PINSEL1 |= 0x15000000; // ADC Pins P0.28-P0.30 (AD 0.1/0.2/0.3)
IODIR0 &= 0x8F9FFF8F; // ADC Pins as Input Pins
}
void delay_ms(long ms) // delay 1 ms per count @ CCLK 60 MHz
{
long i,j;
for (i = 0; i < j =" 0;" u0dl =" _PCLK/(16*_baudrate);" u0lcr =" 0x00000083;" u0dll =" u0dl" u0dlm =" (u0dl">>8); // U0DL for high byte
U0LCR = 0x00000003; // DLAB =0
}
void uart1_init(unsigned int _baudrate)
{
unsigned short u1dl;
u1dl = _PCLK/(16*_baudrate); // Calculate for U1DL value
PINSEL0 |= 0x00050000; // Enable rx,tx
U1LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit
U1DLL = u1dl & 0xFF; // U1DL for low byte
U1DLM = (u1dl>>8); // U1DL for high byte
U1LCR = 0x00000003; // DLAB =0
}
void uart0_putc(char c)
{
while(!(U0LSR & 0x20)); // Wait until UART0 ready to send character
U0THR = c; // Send character
}
void uart0_puts(char *p)
{
while(*p) // Point to character
{
uart0_putc(*p++); // Send character then point to next character
}
}
short int isr_u1=0;
void isr_uart1(void) __irq
{
char msg;
char temp;
isr_u1=1;
if(((msg = U1IIR) & 0x01) == 0) // Check status flag communication
{
switch (msg & 0x0E) // Filter message
{
case 0x04: while(!(U1LSR & 0x20)); // Receive Data Available
temp = U1RBR;
//uart0_putc(temp);
if (temp == '\r')
//if (temp == '1')
{
rec_comp = 1;
Buffer[index] = temp;
temp_index = index;
index = 0;
}
else
{
rec_comp = 0;
Buffer[index] = temp;
index++;
}
break;
case 0x02: break; // THRE Interrupt
default: break;
}
}
VICVectAddr = 0; // Acknowledge Interrupt
}
void init_ADC(void){
// ADCR: - AD Control Register
// *****
// Bit 21 PDN = 1 : AD Converter is Operational
// Bit 17-19 CLKS = 000 : Resolution = 10 Bits / 11 Clocks
// Bit 16 BURST = 0 : Automated AD Channel Conversion on -> Software Controled Mod off
// Bit 8-15 CLKDIV = 00000110: AD-CLK = PCLK/(CLKDIV+1) 4.5MHz
// Bit 7- 0 SEL = 00000010: ADC Chanel default = ADC_01
// Initial AD0CR (ADCR=0x01210602)
AD0CR &= 0x00000000; // Clear All Bit Control
AD0CR |= 0x000000CE; // SEL = ADC_0.1-0.3 / ADC_0.6-0.7
AD0CR |= 0x00000600; // CLKDIV=6
AD0CR |= 0x00010000; // BURST = 1 = Conversion Continue
AD0CR &= 0xFFF1FFFF; // CLKS = 000 = 10Bit/ 11 Clock Cycles
AD0CR |= 0x00200000; // PDN = 1 = Active ADC Module
AD0CR &= 0xF7FFFFFF; // EDGE = 0 = Conversion on Falling Edge
AD0CR |= 0x00000000; // START = 000 = Start Conversion Now
// Initial AD1CR (ADCR=0x01210602)
AD1CR &= 0x00000000; // Clear All Bit Control
AD1CR |= 0x000000C1; // SEL = ADC_1.0 / ADC_1.6-1.7
AD1CR |= 0x00000600; // CLKDIV=6
AD1CR |= 0x00010000; // BURST = 1 = Conversion Continue
AD1CR &= 0xFFF1FFFF; // CLKS = 000 = 10Bit/ 11 Clock Cycles
AD1CR |= 0x00200000; // PDN = 1 = Active ADC Module
AD1CR &= 0xF7FFFFFF; // EDGE = 0 = Conversion on Falling Edge
AD1CR |= 0x00000000; // START = 000 = Start Conversion Now
// Initial ADINTEN (ADINTEN=0)
AD0INTEN = 0; // Disable ADC Interrupt
AD1INTEN = 0;
}
//**********************************************/;
//* Read functions for the ADC Channel 0-7 */;
//**********************************************/;
unsigned int Analog_0(void){
unsigned int value;
//Start Read ADC 0.1 (Pin 0.28 - Sensor 0)
do // Loop Read ADC0
{
value = AD0DR1; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
unsigned int Analog_1(void){
unsigned int value;
//Start Read ADC 0.2 (Pin 0.29 - Sensor 1)
do // Loop Read ADC0
{
value = AD0DR2; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
unsigned int Analog_2(void){
unsigned int value;
// Start Read ADC 0.3 (Pin 0.30 - Sensor 2)
do // Loop Read ADC0
{
value = AD0DR3; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
unsigned int Analog_3(void){
unsigned int value;
//Start Read ADC 0.6 (Pin 0.04 - Sensor 3)
do // Loop Read ADC0
{
value = AD0DR6; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
unsigned int Analog_4(void){
unsigned int value;
//Start Read ADC 0.7 (Pin 0.05 - Sensor 4)
do // Loop Read ADC0
{
value = AD0DR7; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
unsigned int Analog_5(void){
unsigned int value;
//Start Read ADC 1.0 (Pin 0.06 - Sensor 5)
do // Loop Read ADC0
{
value = AD1DR0; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
unsigned int Analog_6(void){
unsigned int value;
//Start Read ADC 1.6 (Pin 0.21 - Sensor6)
do // Loop Read ADC0
{
value = AD1DR6; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
unsigned int Analog_7(void){
unsigned int value;
//Start Read ADC 1.7 (Pin 0.22 - Sensor 7)
do // Loop Read ADC0
{
value = AD1DR7; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
int main(void)
{ int i=0;
unsigned int x,y,z;
char buff[32];
init();
uart0_init(9600);
uart1_init(9600);
U1IER = 3; // Enable rx/tx interrupt
PINSEL0 |= (1<<18); vicvectaddr0 =" (unsigned)isr_uart1;" vicvectcntl0 =" 0x20" vicintenable =" 1" i =" 5;" x="0;y="0;z="0;" i="0;i<10;i++)" x =" x/10;" y =" y/10;" z =" z/10;" x =" %5d" y =" %5d" z=" %5d">