常驻嘉宾
- 积分
- 3064
- 威望
- 1392
- 贡献
- 1084
- 兑换币
- 707
- 注册时间
- 2014-1-1
- 在线时间
- 294 小时
|
#include <hidef.h>
#include "derivative.h"
unsigned int g_iAdGravityAngle, g_iAdGyroscopeAngleSpeed;
void ATD_Init(void)
{
ATD0DIEN = 0x00; //禁止数字输入功能
ATD0CTL1 = 0x40; //分辨率选择12位
ATD0CTL2 = 0x40; //AD快速清零,禁止外部触发,禁止中断
ATD0CTL3 = 0x90; //右对齐,转换序列长度为2
ATD0CTL4 = 0x03; //采样时间选择为4个AD时钟周期一次
// PRS=后五位的值
//f(AD转换时间频率)=f(总线频率)/(2*(PRS+1))
ATD0CTL5 = 0x30; //禁止特殊通道转换,连续转换,多通道采样
}
void SCI0_Init(void){
SCI0BD = 52;
SCI0CR1 = 0x00;
SCI0CR2 = 0x0c;
}
void capture_ATD(void)
{
unsigned int iAdGravityAngle = 0, iAdGyroscopeAngleSpeed = 0;
unsigned int i;
for (i = 0; i < 20; i++)
{
while (!ATD0STAT2L_CCF0);
iAdGravityAngle = ATD0DR0;
while (!ATD0STAT2L_CCF1);
iAdGyroscopeAngleSpeed = ATD0DR1;
if(i == 20)
{
g_iAdGravityAngle = iAdGravityAngle/20;
g_iAdGyroscopeAngleSpeed = iAdGyroscopeAngleSpeed/20;
}
}
}
void delay(void){
unsigned int i, j;
for (i = 0; i < 500; i++)
for (j = 0; j < 500; j++);
}
void main(void) {
unsigned char data1, data2, data3, data4;
ATD_Init();
SCI0_Init();
for (;;) {
capture_ATD();
while(!SCI0SR1_TDRE);
data1 = (unsigned char)(g_iAdGravityAngle & 0x00ff);
data2 = (unsigned char)(g_iAdGravityAngle >> 8);
SCI0DRL = data2;
delay();
SCI0DRL = data1;
delay();
while(!SCI0SR1_TDRE);
data3 = (unsigned char)(g_iAdGyroscopeAngleSpeed & 0x00ff);
data4 = (unsigned char)(g_iAdGyroscopeAngleSpeed >> 8);
SCI0DRL = data4;
delay();
SCI0DRL = data3;
delay();
}
}
|
|