|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
3D计步器的设计论文下载
9 R* p9 a4 y" q( G
6 H9 E$ t Q3 b" L
' {2 L3 T8 b! C7 K$ C* _8 F5 `0 M3D计步器部分程序:- w% _' o+ ~( M7 C* o3 @
' G4 `. U& a- [1 O5 Q" B# ^& o
# f1 K8 B0 k/ d" n0 }$ @* _' Z#include "reg51.h"
1 j) o! |" g% p* ?* `#include "intrins.h"
7 L$ X, m1 Z% f, C#include <math.h>
6 Y( F3 ^6 D9 P( B+ U7 N, [: H#include <stdio.h> 7 ~6 R% N# i; W6 V0 f+ h# l
#define FOSC 18432000L
" j& e4 \! E6 A#define FOSC 11059200L //晶振 11.0592M * }0 _( V6 F4 H; m- y% P) Z5 p, C6 _
#define BAUD 9600 typedef unsigned char BYTE; typedef unsigned int WORD; " [1 g& ]1 B4 X% F: \; I {; p: t
#define uchar unsigned char uint unsigned int2 j! T1 Q8 b! ~+ w, x8 D' H" y
$ W2 F' ]1 n( w9 @
/*Declare SFR associated with the ADC */, a3 Q$ m; Z! O) T7 a* g8 u
sfr AUXR1 = 0XA2; sfr ADC_CONTR = 0xBC; //ADC control register sfr ADC_RES = 0xBD; //ADC high 8-bit result register sfr ADC_RESL = 0xBE; # O& Q* a. F* T( t1 d2 N- e2 M+ b
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
0 G# {* {9 w7 p; Z* o! zsfr P1ASF = 0x9D; //P1 secondary function control register /*Define ADC operation const for ADC_CONTR*/ 9 |# A; w+ z$ F+ M! J2 m( X* y# X* w
#define ADC_POWER 0x80 //ADC power control bit ' V m) u7 ^, ^4 E8 O& Q3 ~+ |
#define ADC_FLAG 0x10 //ADC complete flag
$ p5 u! V7 w4 ?0 j, t#define ADC_START 0x08 //ADC start control bit
5 g) o. u" M: L2 M' y#define ADC_SPEEDLL 0x00 //420 clocks
; ~" F% {; E% D3 N#define ADC_SPEEDL 0x20 //280 clocks $ L, T( K( X7 X) x( Q5 b- \$ A* Y
#define ADC_SPEEDH 0x40 //140 clocks #define ADC_SPEEDHH 0x60 //70 clocks void InitUart(); - l% _4 E2 w: @; [
void InitADC();
7 d" V* W6 }- [3 \0 f' |void SendData(BYTE dat); ( _7 a) N- n) T' s7 c: z5 Y* E* j
int GetADCResult(BYTE ch);
- c" G7 [2 o' R" o, Kvoid Delay(WORD n);
/ p) c' K0 {1 ^) a3 g' U- {5 Uuchar ge,shi,bai,qian,wan;
" O0 y f! G7 A( J! y: }0 hVoidconversion(uint temp_data) { wan=temp_data/10000+0x30 ; temp_data=temp_data%10000; qian=temp_data/1000+0x30 ; temp_data=temp_data%1000; bai=temp_data/100+0x30 ; temp_data=temp_data%100; shi=temp_data/10+0x30 ; temp_data=temp_data%10; ge=temp_data+0x30; }2 Y6 G( c$ ~( H: Q1 z3 @
. [6 V3 z7 z4 \, Y3 i
//显示变量
2 x1 L0 h* t# [' d' j' \9 e; r+ e3 \
//取余运算 //取余运算 //取余运算 //取余运算
: t/ B8 j9 N$ E" U5 E) P1 h* r5 S5 [4 c# z6 u5 r7 e
/*---------------------------Get ADC result ----------------------------*/ ! s# r3 X6 h9 Y8 b
int GetADCResult(BYTE ch) { int AD10bitResult; ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START; _nop_(); //Must wait before inquiry _nop_(); _nop_(); _nop_();
5 ?0 u8 {" y5 w& z, n; k. `* J while (!(ADC_CONTR & ADC_FLAG));//Wait complete 0 G Q/ b B, v1 z$ [1 [6 \/ u
flag ADC_CONTR &= ~ADC_FLAG; //Close ADC AD10bitResult=ADC_RES; + W. z0 g/ I- }: o w
AD10bitResult<<=8; AD10bitResult|=ADC_RESL; return AD10bitResult;9 A* G6 m; a; V t: j D- O
3 C6 l/ n% Y$ u6 p' r0 t/ A- a" T
//Return 10BIT ADC result, w% ?7 I4 N0 A# C
) ~. x# ~$ G! B8 ` c: b' [; m} /*---------------------------Initial UART ----------------------------*/ void InitUart() { SCON = 0x5a; //8 bit data ,no parity bit TMOD = 0x20; //T1 as 8-bit auto reload TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate TR1 = 1; //T1 start running }: z& ]. x, Q5 }# K( A
/ J6 u/ D+ n9 k' K' c; Q2 e; E7 T# ^# U6 R5 [
/*---------------------------Initial ADC sfr ----------------------------*/ void InitADC() { P1ASF = 0xff;
% J {& E1 d8 J0 p7 q1 G1 Z) e0 E% }. Z \
+ t' T% P, K! o7 x//Open 8 channels ADC function0 f! ~, E2 m1 f V) H$ C
/ z+ D. r8 x* v8 k- ?9 y* S
AUXR1=AUXR1|0X04; //ad 值左对齐 8 F$ ?0 v. X! J9 q( d
ADC_RES = 0; //Clear previous result ) `* N; f0 V! n+ ^9 J& ~
ADC_CONTR = ADC_POWER | ADC_SPEEDLL; Delay(2); //ADC power-on and delay } /*---------------------------Send one byte data to PC Input: dat (UART data) Output:----------------------------*/ ( ^% S: d! [7 V& C% c, A& c
void SendData(BYTE dat) { while (!TI); TI = 0; SBUF = dat; } /*---------------------------Software delay function ----------------------------*/
- p' h/ P- K- T* Yvoid Delay(WORD n) { WORD x; while (n--) { x = 5000; while (x--); } } //***************************************************************************** * void main()
! F! g& T* t8 P' c3 N! M
( z6 g1 u7 P \1 w( v( v' r//Wait for the previous data is sent //Clear TI flag //Send current data
/ w' D) b5 S. X! g. b; T7 K* [, n0 |: z9 b+ X+ Y8 R8 {; v
{; Z3 q+ P" ^$ p4 f: T1 `
{. g, w8 B$ \9 z) |6 o…………限于本文篇幅 余下代码请从论坛下载附件…………
9 D* r. l$ [, s3 y& T* X1 `4 L& @, \ t8 ]
! J1 k, a* L' w2 e, J7 V
! |" d3 h" k5 ^# l+ L3 |: n a |
|