• STC单片机串口输出ADXL335角度值


    STC单片机串口输出ADXL335角度值:

      1 //*****************************************************
      2 //名称:单片机串口输出ADXL335角度值
      3 //使用单片机:stc12c5a60s2
      4 //供电:电压5v
      5 //晶振:11.0592m
      6 //X轴->(ADC0)P1.0  
      7 //Y轴->(ADC1)P1.1 
      8 //Z轴->(ADC1)P1.2
      9 //用pc串口助手显示时,选择字符显示 
     10 //参考宏晶网址例程
     11 //*****************************************************
     12 #include "reg51.h"
     13 #include "intrins.h"
     14 #include  <math.h>    //Keil library  
     15 #include  <stdio.h>   //Keil library
     16 
     17 //#define FOSC    18432000L
     18 #define FOSC    11059200L  //晶振11.0592M
     19 #define BAUD    9600
     20 
     21 typedef unsigned char BYTE;
     22 typedef unsigned int WORD;
     23 
     24 #define   uchar unsigned char
     25 #define   uint unsigned int    
     26 
     27 /*Declare SFR associated with the ADC */
     28 sfr AUXR1       =   0XA2;
     29 sfr ADC_CONTR   =   0xBC;           //ADC control register
     30 sfr ADC_RES     =   0xBD;           //ADC high 8-bit result register
     31 sfr ADC_RESL    =   0xBE;
     32 sfr ADC_LOW2    =   0xBE;           //ADC low 2-bit result register
     33 sfr P1ASF       =   0x9D;           //P1 secondary function control register
     34 
     35 /*Define ADC operation const for ADC_CONTR*/
     36 #define ADC_POWER   0x80            //ADC power control bit
     37 #define ADC_FLAG    0x10            //ADC complete flag
     38 #define ADC_START   0x08            //ADC start control bit
     39 #define ADC_SPEEDLL 0x00            //420 clocks
     40 #define ADC_SPEEDL  0x20            //280 clocks
     41 #define ADC_SPEEDH  0x40            //140 clocks
     42 #define ADC_SPEEDHH 0x60            //70 clocks
     43 
     44 void InitUart();
     45 void InitADC();
     46 void SendData(BYTE dat);
     47 int GetADCResult(BYTE ch);
     48 void Delay(WORD n);
     49 uchar ge,shi,bai,qian,wan;           //显示变量
     50 
     51 void conversion(uint temp_data)  
     52 {  
     53     wan=temp_data/10000+0x30 ;
     54     temp_data=temp_data%10000;   //取余运算
     55     qian=temp_data/1000+0x30 ;
     56     temp_data=temp_data%1000;    //取余运算
     57     bai=temp_data/100+0x30   ;
     58     temp_data=temp_data%100;     //取余运算
     59     shi=temp_data/10+0x30    ;
     60     temp_data=temp_data%10;      //取余运算
     61     ge=temp_data+0x30;     
     62 }
     63 
     64 /*----------------------------
     65 Get ADC result
     66 ----------------------------*/
     67 int GetADCResult(BYTE ch)
     68 {   int AD10bitResult; 
     69     ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
     70     _nop_();                        //Must wait before inquiry
     71     _nop_();
     72     _nop_();
     73     _nop_();
     74     while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
     75     ADC_CONTR &= ~ADC_FLAG;         //Close ADC
     76 
     77     AD10bitResult=ADC_RES;
     78     AD10bitResult<<=8;
     79     AD10bitResult|=ADC_RESL;
     80     return AD10bitResult;                  //Return 10BIT ADC result
     81 }
     82 /*----------------------------
     83 Initial UART
     84 ----------------------------*/
     85 void InitUart()
     86 {
     87     SCON = 0x5a;                    //8 bit data ,no parity bit
     88     TMOD = 0x20;                    //T1 as 8-bit auto reload
     89     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
     90     TR1 = 1;                        //T1 start running
     91 }
     92 
     93 /*----------------------------
     94 Initial ADC sfr
     95 ----------------------------*/
     96 void InitADC()
     97 {
     98     P1ASF = 0xff;                   //Open 8 channels ADC function
     99 
    100     AUXR1=AUXR1|0X04;              //ad值左对齐
    101     ADC_RES = 0;                    //Clear previous result
    102     ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
    103     Delay(2);                       //ADC power-on and delay
    104 }
    105 
    106 /*----------------------------
    107 Send one byte data to PC
    108 Input: dat (UART data)
    109 Output:-
    110 ----------------------------*/
    111 void SendData(BYTE dat)
    112 {
    113     while (!TI);                    //Wait for the previous data is sent
    114     TI = 0;                         //Clear TI flag
    115     SBUF = dat;                     //Send current data
    116 }
    117 
    118 /*----------------------------
    119 Software delay function
    120 ----------------------------*/
    121 void Delay(WORD n)
    122 {
    123     WORD x;
    124 
    125     while (n--)
    126     {
    127         x = 5000;
    128         while (x--);
    129     }
    130 }
    131 
    132 //******************************************************************************
    133 
    134 void main()
    135 {   int x,y,z;
    136     float Q,T,K,Roll,Pitch;
    137     InitUart();                     //Init UART, use to show ADC result
    138     InitADC();                      //Init ADC sfr
    139     while (1)
    140     {
    141         x=GetADCResult(0);
    142         y=GetADCResult(1);
    143         z=GetADCResult(2);
    144         // 0-5v 对应 0-1024  
    145         // 3.3v/2  = 1.65v(0g时候值)
    146         // 1.65v ->= 338   (AD值即0X0152)
    147         x-=0X0152; //
    148         y-=0X0152;
    149         z-=0X0152;    
    150     Q=x;
    151     T=y;
    152     K=z;
    153     Q=-Q;
    154     Roll=(float)(((atan2(K,Q)*180)/3.14159265)+180);
    155     Pitch=(float)(((atan2(K,T)*180)/3.14159265)+180);
    156     conversion(Roll);    //横滚角0-360度
    157   //conversion(Pitch); //俯仰角0-360度
    158     SendData(bai);
    159     SendData(shi);
    160     SendData(ge);
    161 
    162     SendData(0x0d); 
    163     SendData(0x0a);//换行,回车
    164 
    165     Delay(30);
    166     }
    167 }
  • 相关阅读:
    npm 默认创建项目如何自动配置
    VueJS + TypeScript 入门第一课
    实现类数组转化成数组(DOM 操作获得的返回元素值是一个类数组)
    webpack4(4.41.2) 打包出现 TypeError this.getResolve is not a function
    vue-cli 4.0.5 配置环境变量样例
    关于H5页面在微信浏览器中音视频播放的问题
    ant-design-vue 快速避坑指南
    记elementUI一个大坑
    VUE自定义(有限)库存日历插件
    node转发请求 .csv格式文件下载 中文乱码问题 + 文件上传笔记
  • 原文地址:https://www.cnblogs.com/jikexianfeng/p/6385587.html
Copyright © 2020-2023  润新知