/ / Name: microcontroller serial output the MMA7361 angle value / / MCU: stc12c5a60s2 / / Crystal: 11.0592m / / X axis -> (ADC0) the P1.0 / / Y-axis -> (ADC1), P1.1, / / Z-axis -> (ADC1) P1.2 / / Use the pc serial Assistant displays, select the character display / / Reference macro crystalline Website routine

forestevanescentElectronics - Devices

Nov 2, 2013 (3 years and 7 months ago)

161 views

/ / Name: microcontroller serial output the MMA7361 angle value

/ / MCU: stc12c5a60s2

/ / Crystal: 11.0592m

/ / X axis
-
> (ADC0) the P1.0

/ / Y
-
axis
-
> (ADC1), P1.1,

/ / Z
-
axis
-
> (ADC1) P1.2

/ / Use the pc serial Assistant displays, select the character d
isplay

/ / Reference macro crystalline Website routine

# Include "reg51.h

# Include "intrins.h

# Include <math.h> / / the Keil library

# The include <stdio.h> / / the Keil library


/ / # Define FOSC 18432000L

# Define FOSC 11059200L / / crystal 11.0592M

#
Define BAUD 9600


typedef for an unsigned char BYTE;

typedef for unsigned int the WORD;


# Define uchar, an unsigned char

# Define uint, an unsigned int


/ * The Declare the SFR associated with the the ADC * /

sfr AUXR1 = 0XA2;

sfr ADC_CONTR = 0xBC; / / AD
C control register

sfr ADC_RES = 0xBD; / / ADC high 8
-
bit result register

sfr ADC_RESL = 0xBE;

sfr ADC_LOW2 = 0xBE; / / ADC low 2
-
bit result register

sfr P1ASF = 0x9D; / / P1 secondary function control register


/ * Define ADC operation const for ADC_CONTR

* /

# Define ADC_POWER 0x80 /
/ ADC power control bit

# The define ADC_FLAG 0x10 / / ADC complete flag

# Define ADC_START 0x08 / / ADC start control bit

# Define ADC_SPEEDLL 0x00 / / 420 clocks

# Define ADC_SPEEDL 0x20 / / 280 clocks

# Define ADC_SPEEDH

0x40 / / 140 clocks

# Define ADC_SPEEDHH 0x60 / / 70 clocks


void InitUart ();

void InitADC ();

void SendData (BYTE dat);

int GetADCResult (BYTE ch);

void Delay (WORD);

uchar, ge, shi bai, qian wan,; / / display variable


void conversion (uint temp_data)

{





wan = temp_data/10000 +0 x30;





temp_data = temp_data% 10 000; / / modulo arithmetic

qian = temp_data/1000 +0 x30;





temp_data = temp_data% 1000; / / modulo arithmetic





bai = temp_data/100 +0 x30;





temp_data = temp_data% 100; / / modulo ope
rator





shi = temp_data/10 +0 x30;





temp_data = temp_data% 10; / / modulo operator





ge = temp_data +0 the x30;

}


/ *
----------------------------

Get the ADC result

----------------------------

* /

int GetADCResult (BYTE ch)

{Int AD10bitResult;





ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;





samples the 1
-

(); / / Must the wait before inquiry





samples the 1
-

();





samples the 1
-

();





samples the 1
-

();





while (! (ADC_CONTR & ADC_FLAG)) ;/ / Wait complete flag





ADC_CO
NTR & = ~ ADC_FLAG; / / Close ADC






AD10bitResult = ADC_RES;





AD10bitResult << = 8;





AD10bitResult | = ADC_RESL;





return AD10bitResult; / / Return 10BIT ADC result

}

/ *
----------------------------

Initial the UART

----------------------------

* /

void InitUart ()

{





Special Function Register SCON = 0x5a; / / 8
-
bit the data, no parity
-
bit





TMOD = 0x20; / / T1 as 8
-
bit auto the reload





TH1 = TL1 =
-

(FOSC/12/32/BAUD); / / Set Uart baudrate





Of TR1 = 1; / / T1 start running

}


/ *
---
-------------------------

Initial the ADC sfr

----------------------------

* /

void InitADC ()

{





P1ASF = 0xff; / / Open 8 channels ADC function


AUXR1 = AUXR1 | 0X04; / / ad value of left
-
aligned





ADC_RES = 0; / / Clear previous result





ADC_CONTR

= ADC_POWER | ADC_SPEEDLL;





Delay (2); / / ADC power
-
on and the delay

}


/ *
----------------------------

Send the one byte data to the PC

Input: dat (UART data)

The Output:
-

----------------------------

* /

void SendData (BYTE dat)

{





while (! TI)
; / / Wait for the previous the data is sent





TI = 0; / / Clear TI flag





SBUF = dat; / / Send current the data

}


/ *
----------------------------

Software, the delay function

----------------------------

* /

void Delay (WORD)

{





The WORD the x;






while (n
-
)





{









x = 5000;









while (x
-
);





}

}


/ /
************************************************

***************************
***


void main ()

{Int x, y, z;

a float Q, T
-
, K, Roll, Pitch;





InitUart (); / / Init UART, use to show

ADC result





InitADC (); / / Init ADC sfr





while (1)





{

x = GetADCResult (0);

y = GetADCResult (1);

z = GetADCResult (2);

/ / 0
-
5v corresponds to 0
-
1024

/ / 3.3v / 2 = 1.65v (0g time value)

/ / 1.65v
-
> = 338 (the AD value of decimal 338, hexadeci
mal, 0X0152)

x
-
= 0X0152; / /

y
-
= 0X0152;

z
-
= 0X0152;





Q = x;





T = y;





K = z
-
;





Q =
-
Q;





Roll = (float) (((atan2 (K, Q) * 180) / 3.14159265) +180);





Pitch = (float) (((atan2 (K, T) * 180) / 3.14159265) +180);





conversion (Roll); / / roll angle 0
-
360 degrees



/ / Conversion (Pitch); / / pitch angle of 0
-
360 degrees





SendData (bai);

SendData (shi);

SendData (ge);


SendData (0x0d);

SendData (0x0a) ;/ / line feed, carriage return






Delay (30);





}

}