msp430單片機(jī)模擬IIC總線讀取MMA7660三軸加速度傳感器
//-----------主函數(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 //設(shè)置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 //從機(jī)地址
#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
////////////////////////////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\
關(guān)鍵詞:
msp430單片機(jī)模擬IIC總線MMA7660三軸加速度傳感
相關(guān)推薦
技術(shù)專區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線
- 開關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車電子
- 轉(zhuǎn)換器
- 電源管理
- 信號放大器
評論