色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > msp430單片機模擬IIC總線讀取MMA7660三軸加速度傳感器

          msp430單片機模擬IIC總線讀取MMA7660三軸加速度傳感器

          作者: 時間:2016-11-27 來源:網(wǎng)絡 收藏
          //-----------主函數(shù)-----------------------------------------------------------
          void main(void)
          {
          OSC_Init();
          Port_Init();
          TimerA_UART_init();
          MMA7660_Init();
          __enable_interrupt();
          TimerA_UART_print("All Initialization is OK!rn");
          for (;;)
          {
          //__bis_SR_register(LPM0_bits);
          Read3axle();
          if (X_value_final>0)
          P1OUT|= BIT0;
          else if(X_value_final<0)
          P1OUT&=~ BIT0;
          else _nop();
          //------------------------------
          TimerA_UART_print("X_value---->");
          output((unsigned int )X_value,4);
          TimerA_UART_print(" ");
          //------------------------------
          TimerA_UART_print("Y_value---->");
          output((unsigned int )Y_value,4);
          TimerA_UART_print(" ");
          //------------------------------
          TimerA_UART_print("Z_value---->");
          output((unsigned int )Z_value,4);
          TimerA_UART_print("rn");
          //------------------------------
          P1OUT ^= BIT6 ;
          //delay_us(20000);
          }
          }
          //-------------------華麗的分割線-----------------------------------------------------------------
          //file2 : g2452uart.h
          #include "msp430g2452.h"
          //------------------------------------------------------------------------------
          // Hardware-related definitions
          //------------------------------------------------------------------------------
          #define UART_TXD 0x02 // TXD on P1.1 (Timer0_A.OUT0)
          #define UART_RXD 0x04 // RXD on P1.2 (Timer0_A.CCI1A)
          //------------------------------------------------------------------------------
          // Conditions for 9600 Baud SW UART, SMCLK = 1MHz
          //------------------------------------------------------------------------------
          #define UART_TBIT_DIV_2 (1000000 / (9600 * 2))
          #define UART_TBIT (1000000 / 9600)
          //------------------------------------------------------------------------------
          // Global variables used for full-duplex UART communication
          //------------------------------------------------------------------------------
          unsigned int txData; // UART internal variable for TX
          unsigned char rxBuffer; // Received UART character
          //------------------------------------------------------------------------------
          // 串口通信的接口函數(shù)說明:
          //------------------------------------------------------------------------------
          void TimerA_UART_init(void);
          //TimerA_UART_init()模擬串口通信初始化函數(shù),在main()函數(shù)中調(diào)用此函數(shù)就可以使用一下兩個函數(shù)了
          void TimerA_UART_tx(unsigned char byte);
          //TimerA_UART_tx( byte ) 通過串口發(fā)送一字節(jié)的數(shù)據(jù)
          void TimerA_UART_print(char *string);
          //TimerA_UART_print(*string) 通過串口發(fā)送任意字節(jié)的字符穿
          void output(unsigned int num , int n );
          //輸出一個數(shù)字
          //注:接收的一字節(jié)數(shù)據(jù)存在rxBuffer變量里。
          void Port1_Init()
          {
          P1OUT = 0x00; // Initialize all GPIO
          P1SEL = UART_TXD + UART_RXD; // Timer function for TXD/RXD pins
          // P1DIR = 0xFF & ~UART_RXD; // Set all pins but RXD to output
          //設置TXD為輸出模式,P1.1和P1.2兩個調(diào)試小燈也為輸出模式。RXD為輸入模式。
          P1DIR |= UART_TXD+BIT0+BIT6;
          P1DIR &=~ UART_RXD;
          P1OUT &=~ BIT0+BIT6 ; //初始化讓兩個燈都滅。
          }
          //------------------------------------------------------------------------------
          // Function configures Timer_A for full-duplex UART operation
          //------------------------------------------------------------------------------
          void TimerA_UART_init(void)
          {
          Port1_Init(); //P1端口初始化!
          TACCTL0 = OUT; // Set TXD Idle as Mark = 1
          TACCTL1 = SCS + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture, Int
          TACTL = TASSEL_2 + MC_2; // SMCLK, start in continuous mode
          }
          //------------------------------------------------------------------------------
          // Outputs one byte using the Timer_A UART
          //------------------------------------------------------------------------------
          void TimerA_UART_tx(unsigned char byte)
          {
          while (TACCTL0 & CCIE); // Ensure last char got TXd
          TACCR0 = TAR; // Current state of TA counter
          TACCR0 += UART_TBIT; // One bit time till first bit
          TACCTL0 = OUTMOD0 + CCIE; // Set TXD on EQU0, Int
          txData = byte; // Load global variable
          txData |= 0x100; // Add mark stop bit to TXData
          txData <<= 1; // Add space start bit
          }
          //------------------------------------------------------------------------------
          // Prints a string over using the Timer_A UART
          //------------------------------------------------------------------------------
          void TimerA_UART_print(char *string)
          {
          while (*string) {
          TimerA_UART_tx(*string++);
          }
          }
          void output(unsigned int num , int n )
          {
          char *p,pd[4];
          p=pd;
          pd[0]=num/1000 +48 ; //千位
          pd[1]=num/100 +48 ;// 百位
          pd[2]=num0/10 +48 ; //十位
          pd[3]=num +48;//個位
          while (n--)
          TimerA_UART_tx((*p++));
          }
          //------------------------------------------------------------------------------
          // Timer_A UART - Transmit Interrupt Handler
          //------------------------------------------------------------------------------
          #pragma vector = TIMER0_A0_VECTOR
          __interrupt void Timer_A0_ISR(void)
          {
          static unsigned char txBitCnt = 10;
          TACCR0 += UART_TBIT; // Add Offset to CCRx
          if (txBitCnt == 0) { // All bits TXed?
          TACCTL0 &= ~CCIE; // All bits TXed, disable interrupt
          txBitCnt = 10; // Re-load bit counter
          }
          else {
          if (txData & 0x01) {
          TACCTL0 &= ~OUTMOD2; // TX Mark 1
          }
          else {
          TACCTL0 |= OUTMOD2; // TX Space 0
          }
          txData >>= 1;
          txBitCnt--;
          }
          }
          //------------------------------------------------------------------------------
          // Timer_A UART - Receive Interrupt Handler
          //------------------------------------------------------------------------------
          #pragma vector = TIMER0_A1_VECTOR
          __interrupt void Timer_A1_ISR(void)
          {
          static unsigned char rxBitCnt = 8;
          static unsigned char rxData = 0;
          switch (__even_in_range(TA0IV, TA0IV_TAIFG)) { // Use calculated branching
          case TA0IV_TACCR1: // TACCR1 CCIFG - UART RX
          TACCR1 += UART_TBIT; // Add Offset to CCRx
          if (TACCTL1 & CAP) { // Capture mode = start bit edge
          TACCTL1 &= ~CAP; // Switch capture to compare mode
          TACCR1 += UART_TBIT_DIV_2; // Point CCRx to middle of D0
          }
          else {
          rxData >>= 1;
          if (TACCTL1 & SCCI) { // Get bit waiting in receive latch
          rxData |= 0x80;
          }
          rxBitCnt--;
          if (rxBitCnt == 0) { // All bits RXed?
          rxBuffer = rxData; // Store in global variable
          rxBitCnt = 8; // Re-load bit counter
          TACCTL1 |= CAP; // Switch compare to capture mode
          //__bic_SR_register_on_exit(LPM0_bits); // Clear LPM0 bits from 0(SR)
          }
          }
          break;
          }
          }
          //----------------------華麗的分割線2--------------------------------------------------------
          //file :MMA7660FC.h
          #ifndef _MMA7660FC_H
          #define _MMA7660FC_H
          #define MMA7660FC_ADD 0x4C //Please contact the factory to request a different IIC address
          #define MMA7660FC_MW_ADD 0x98 //master write address
          #define MMA7660FC_MR_ADD 0x99 //master read address
          #define SlaveAddress 0x98 //從機地址
          #define X_outAddr 0x00
          #define Y_outAddr 0x01
          #define Z_outAddr 0x02
          // IIC Register Address
          #define MMA7660_XOUT 0x00
          #define MMA7660_YOUT 0x01
          #define MMA7660_ZOUT 0x02
          #define MMA7660_TILT 0x03
          #define MMA7660_SRST 0x04
          #define MMA7660_SPCNT 0x05
          #define MMA7660_INTSU 0x06
          #define MMA7660_MODE 0x07
          #define MMA7660_SR 0x08
          #define MMA7660_PDET 0x09
          #define MMA7660_PD 0x0A
          #endif
          ////////////////////////////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\
          //以上為所有程序源碼!編譯工具CCS V5.0

          上一頁 1 2 下一頁

          評論


          技術專區(qū)

          關閉