5回答

0收藏

[求助] MMA7361 倾角传感器的使用

51单片机 51单片机 6073 人阅读 | 5 人回复 | 2013-08-06

本人用的是STC89c52RC的开发板,买了个MMA7361倾角传感器,不知怎么使用,如何测试。先前在淘宝买的时候店家有送程序,不过使用keil编译,显示有错误,无法生成hex文件。本人菜鸟,不知如何改程序。还请高手指点一下,同时也希望指点出传感器输出了些什么,需要从哪里才能看出输出数据。

程序:
//-------------------------------------------------------------------
// MMA7361 Demo code, show the 3 axis date through UART periodically
// Hardware connection: X_out           PC2
//   Y_out         PC1
//          Z_out         PC0
//   MMA7361_En PC3
//   UART_Tx  PD1
// Software UART baudrate 115200 when main frequency is 4M
//-------------------------------------------------------------------
            
#include "iom168.h"
#include "inAVR.h"
#include "stdio.h"
#include "stdbool.h"
#define Max_Axis 3
#define X_Channel 0 //PC2
#define Y_Channel       1 //PC1
#define Z_Channel 2 //PC0
#define MMA7361_EN 0x08 //PC3
const unsigned char Channel_Tbl[Max_Axis]={0x42,0x41,0x40};     //Z,Y,X
const unsigned char Axis_Name[Max_Axis]={'X','Y','Z'};
unsigned char Temp, Temp1, ADC_Done,ShowDebug_Cnt;              //Temporary saving cells
unsigned int Axis_Data[Max_Axis];
unsigned long SumReg = 0;     //Storage variable
static char SampCnt;                                        //ADC samples counter
#include "functions.c"
int main(void)
{
  unsigned char i;
  IO_Ports_Init();    //Initialize IO ports
  Timers_Init();    //Initialize timers
  ADC_Init();     //Initialize ADC
  PRR = 0x82;                                   //Reduce consumption 86 (don't stop SPI)
  CLKPR = 0x80;     //Increase Fcpu
  CLKPR = 0x00;
  TCCR2B = 0x06;
  __disable_interrupt();
  __watchdog_reset();
    WDTCSR = 0x18;    //Enable watchdog, timeout 256ms
    WDTCSR = 0x0D;
  __enable_interrupt();    //Enable interrupts
  
  MMA7361_Init();
  while(1)
  {
    __watchdog_reset();    //Reset watchdog timer
      
    if(ShowDebug_Cnt==0)      
    {
     ShowDebug_Cnt=30;//13;      //Get data periodically
     for(i=0;i<Max_Axis;i++)     //Get Axis data
     Axis_Data[i]=Get_ADC(i,10);
    for(i=0;i<Max_Axis;i++)    //Show Axis data through UART
    {
      SW_UART_Tx(Axis_Name[i]);
     Put_Axis(Axis_Data[i]);
      SW_UART_Tx(' ');
    }
     SW_UART_Tx(' ');     //Next Line
     SW_UART_Tx(0x0d);
   }
  }
  
  
}
//SIGNAL(SIG_OVERFLOW2)
#pragma vector=TIMER2_OVF_vect
__interrupt void TMR2irq( void )
{
  if(ShowDebug_Cnt!=0)
   ShowDebug_Cnt--;
}
//SIGNAL(SIG_ADC)
#pragma vector=ADC_vect
__interrupt void ADCirq( void )
{
  SumReg += ADC;         //Add result to common sum
  if(--SampCnt == 0)        //Check number of samples
  {
ADC_Done=1;        //Measuring is complete
ADCSRA = 0x80;               //Stop ADC
  }
  else
  {
ADCSRA |= 0x40;        //Start next ADC cycle
  }
}
//SIGNAL(__vector_default)
//#pragma vector=default

分享到:
回复

使用道具 举报

回答|共 5 个

倒序浏览

沙发

Earduino

发表于 2013-8-6 21:17:18 | 只看该作者

同问,也在搞这个
板凳

zhaojunlin-1847508

发表于 2013-8-6 22:06:07 | 只看该作者

给你的是AVR的程序,你的单片机是51的,头文件和定义都不一样,肯定不能编译了啊~~~~~
地板

梦在远方-403512

发表于 2013-8-6 22:19:56 | 只看该作者

给力啊,源代码都贴出来了
5#

minehe

发表于 2013-8-6 22:55:08 来自手机 | 只看该作者

zhaojunlin123 发表于 2013-8-6 22:06:07
给你的是AVR的程序,你的单片机是51的,头文件和定义都不一样,肯定不能编译了啊~~~~~

那你有没有这个传感器的51程序
6#

minehe

发表于 2013-8-6 22:57:18 来自手机 | 只看该作者

zhaojunlin123 发表于 2013-8-6 22:06:07
给你的是AVR的程序,你的单片机是51的,头文件和定义都不一样,肯定不能编译了啊~~~~~

我表示我2b了
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条