智能车制作
标题:
用128采集陀螺仪和加速度仪的零偏值,可是返回的始终是0,看很久也没看出是什么问题
[打印本页]
作者:
cah506798484
时间:
2014-1-24 21:38
标题:
用128采集陀螺仪和加速度仪的零偏值,可是返回的始终是0,看很久也没看出是什么问题
#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();
}
}
作者:
cah506798484
时间:
2014-1-24 21:59
稍微有点思路了,用单步测试时有在第一次读到变量的值,[attach]55180[/attach],后面那个变量没有被分配,这个我待会看看什么问题,之后读出来的值就始终是0,所以问题我想出在AD设置上面
作者:
cah506798484
时间:
2014-1-24 22:22
[attach]55182[/attach]还是没搞懂。。。只有当i=0的时候才有读数,当i++后,两个读数值始终为0。。。菜鸟希望求解答。。。自己检查了很长时间没找到问题所在。。。
作者:
cah506798484
时间:
2014-1-24 22:51
好吧,那些值本身就是不对的,并不是读取的,而且上面的程序也有很多毛病,根本问题应该就是AD寄存器设置不正确,导致没能读出数字量
作者:
cah506798484
时间:
2014-1-25 10:11
新的一天,继续来解决问题
作者:
cah506798484
时间:
2014-1-25 20:58
还是不行啊。。。。求大神发个能用的参考一下啊
作者:
机遇
时间:
2014-1-25 21:03
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;
}
}数据都没累加 还有i能等于20嘛
作者:
cah506798484
时间:
2014-1-25 21:12
机遇 发表于 2014-1-25 21:03
for (i = 0; i < 20; i++)
{
while (!ATD0STAT2L_CCF0);
嗯,这里是错了,~现在修改到#include <hidef.h> #include "derivative.h"
unsigned int g_iAdGravityAngle = 0, g_iAdGyroscopeAngleSpeed = 0;
void ATD_Init(void){
ATD0CTL0 = 0x01; //AN1反转
ATD0CTL1 = 0x40; //分辨率选择12位
ATD0CTL2 = 0x40; //AD快速清零,禁止外部触发,禁止中断
ATD0CTL3 = 0x90; //右对齐,转换序列长度为2
ATD0CTL4 = 0x01; //采样时间选择为4个AD时钟周期一次
// PRS=后五位的值
//f(AD转换时间频率)=f(总线频率)/(2*(PRS+1))
ATD0CTL5 = 0x30; //禁止特殊通道转换,连续转换,多通道采样
ATD0DIEN = 0x00; //禁止数字输入功能
}
void SCI0_Init(void){
SCI0BD = 52;
SCI0CR1 = 0x00;
SCI0CR2 = 0x0c;
}
void capture_ATD(void) {
while (!ATD0STAT2_CCF0);
g_iAdGravityAngle = ATD0DR0;
while (!ATD0STAT2_CCF1);
g_iAdGyroscopeAngleSpeed = ATD0DR1;
}
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();
while(!SCI0SR1_TDRE);
SCI0DRL = data1;
delay();
while(!SCI0SR1_TDRE);
data3 = (unsigned char)(g_iAdGyroscopeAngleSpeed & 0x00ff);
data4 = (unsigned char)(g_iAdGyroscopeAngleSpeed >> 8);
SCI0DRL = data4;
delay();
while(!SCI0SR1_TDRE);
SCI0DRL = data3;
delay();
}
}
作者:
cah506798484
时间:
2014-1-25 21:59
好吧。。。换了一块芯片。。。读的出来了0 0。。。唉,大概是芯片问题吧
作者:
余晖。。
时间:
2015-4-10 19:01
e 我的数据也是0
float OutData[4] = { 0 };
unsigned char databuf[10] = {0};
unsigned short CRC_CHECK(unsigned char *Buf, unsigned char CRC_CNT)
{
unsigned short CRC_Temp;
unsigned char i,j;
CRC_Temp = 0xffff;
for (i=0;i<CRC_CNT; i++){
CRC_Temp ^= Buf[i];
for (j=0;j<8;j++) {
if (CRC_Temp & 0x01)
CRC_Temp = (CRC_Temp >>1 ) ^ 0xa001;
else
CRC_Temp = CRC_Temp >> 1;
}
}
return(CRC_Temp);
}
void OutPut_Data(void)
{
int temp[4] = {0};
unsigned int temp1[4] = {0};
//unsigned char databuf[10] = {0};
unsigned char i;
unsigned short CRC16 = 0;
for(i=0;i<4;i++)
{
temp[i] = (int)OutData[i];
temp1[i] = (unsigned int)temp[i];
}
for(i=0;i<4;i++)
{
databuf[i*2] = (unsigned char)(temp1[i]%256);
databuf[i*2+1] = (unsigned char)(temp1[i]/256);
}
CRC16 = CRC_CHECK(databuf,8);
databuf[8] = CRC16%256;
databuf[9] = CRC16/256;
for(i=0;i<10;i++)
uart_putchar(databuf[i]);
}
我用的这种 我的是128
欢迎光临 智能车制作 (http://dns.znczz.com/)
Powered by Discuz! X3.2