Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <msp430g2553.h>
- #define NUM_BYTES_TX 2
- #define NUM_BYTES_RX 6
- #define ADXL_345 0x53
- int RXByteCtr, x1,y1,z1; //activare repetare start la valoarea 1
- volatile unsigned char RxBuffer[6]; //alocare 6 biti de RAM
- unsigned char *PRxData; //pointer pentru retinerea adresei RX
- unsigned char TXByteCtr, RX = 0;
- unsigned char MSData[2];
- void Setup_TX(unsigned char);
- void Setup_RX(unsigned char);
- void Transmit(unsigned char,unsigned char);
- void TransmitOne(unsigned char);
- void Receive(void);
- void Setup_UART();
- void UARTSendArray(unsigned char *TxArray, unsigned char ArrayLength);
- void UARTSendInt(unsigned int x);
- int main(void)
- {
- WDTCTL = WDTPW + WDTHOLD;
- // LED
- P1DIR |= BIT0;
- P1OUT |= BIT0;
- // UART
- BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
- DCOCTL = CALDCO_1MHZ; // Set DCO to 1MHz
- // Configurare hardware UART
- P1SEL |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
- P1SEL2 |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
- // ADXL345
- P1SEL |= BIT6 + BIT7; //Atribuire pini I2C la USCI_B0 // Assign I2C pins to USCI_B0
- P1SEL2 |= BIT6 + BIT7; //Atribuire pini I2C la USCI_B0 // Assign I2C pins to USCI_B0
- // Initializare secventa ADXL345
- //Transmitere
- Setup_TX(ADXL_345);
- Transmit(0x2D,0x00); // blocat
- while (UCB0CTL1 & UCTXSTP); // Asigurare ca conditia de stop a fost trimisa
- //Transmitere
- Setup_TX(ADXL_345);
- Transmit(0x2D,0x10);
- while (UCB0CTL1 & UCTXSTP); //Asigurare ca conditia de stop a fost trimisa
- //Transmit process
- Setup_TX(ADXL_345);
- Transmit(0x2D,0x08);
- while (UCB0CTL1 & UCTXSTP); //Asigurare ca conditia de stop a fost trimisa
- long long i;
- while(1){
- Setup_UART();
- UARTSendArray("0\n", 2);
- //bucla pt masurarea timpului de completare
- for(i=0;i<1000;i++) {
- //Transmitere
- Setup_TX(ADXL_345);
- TransmitOne(0x32); // Request Data from ADXL345 in 2g Range 10Bit resolution
- while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
- //Primire
- Setup_RX(ADXL_345);
- Receive();
- while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
- x1 = (((int)RxBuffer[1]) << 8) | RxBuffer[0];
- y1 = (((int)RxBuffer[3]) << 8) | RxBuffer[2];
- z1 = (((int)RxBuffer[5]) << 8) | RxBuffer[4];
- }
- Setup_UART();
- UARTSendArray("1\n", 2);
- }
- __bis_SR_register( LPM3_bits + GIE );
- for( ; ; ) { }
- }
- #pragma vector = USCIAB0TX_VECTOR
- __interrupt void USCIAB0TX_ISR(void)
- {
- if(RX == 1){
- RXByteCtr--;
- if (RXByteCtr)
- {
- *PRxData++ = UCB0RXBUF;
- }
- else
- {
- UCB0CTL1 |= UCTXSTP;
- *PRxData++ = UCB0RXBUF;
- __bic_SR_register_on_exit(CPUOFF);
- }}
- else{
- if (TXByteCtr)
- {
- TXByteCtr--;
- UCB0TXBUF = MSData[TXByteCtr];
- }
- else
- {
- UCB0CTL1 |= UCTXSTP;
- IFG2 &= ~UCB0TXIFG;
- __bic_SR_register_on_exit(CPUOFF);
- }
- }
- }
- void Setup_UART() {
- _DINT();
- IE2 &= ~UCB0RXIE;
- IE2 &= ~UCB0TXIE;
- UCA0CTL1 |= UCSSEL_2; // Utilizare SMCLK
- UCA0BR0 = 104; //setare rata de transfer la 9600 cu 1MHz ceas
- UCA0BR1 = 0; //setare rata de transfer la 9600 cu 1MHz ceas
- UCA0MCTL = UCBRS0; // UCBRSx = 1
- UCA0CTL1 &= ~UCSWRST; // initializare USCI
- IE2 |= UCA0RXIE; //activare USCI_A0 RX intrerupere
- }
- void Setup_TX(unsigned char Dev_ID){
- _DINT();
- RX = 0;
- IE2 &= ~UCA0RXIE; // Disable USCI_A0 RX interrupt
- IE2 &= ~UCB0RXIE;
- while (UCB0CTL1 & UCTXSTP); //asigurare conditie de stop //dezactivare intrerupere RX
- UCB0CTL1 |= UCSWRST; //activare reset SW
- UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //I2C Master, mod de sincronizare
- UCB0CTL1 = UCSSEL_2 + UCSWRST; //utilizare SMCLK, pastrare reset SW
- UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
- UCB0BR1 = 0;
- UCB0I2CSA = Dev_ID; // adresa slave = 048h
- UCB0CTL1 &= ~UCSWRST; //curatare SW reset, continuare operatie
- IE2 |= UCB0TXIE; // activare intrerupere TX
- }
- void Setup_RX(unsigned char Dev_ID){
- _DINT();
- RX = 1;
- IE2 &= ~UCA0RXIE; //dezactivare intrerupere USCI_AO RX
- IE2 &= ~UCB0TXIE;
- UCB0CTL1 |= UCSWRST; // activare SW Reset
- UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //I2C Master, mod sincronizare
- UCB0CTL1 = UCSSEL_2 + UCSWRST; //utilizare SMCLK, pastrare SW Reset
- UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
- UCB0BR1 = 0;
- UCB0I2CSA = Dev_ID; // adresa slave este 048h
- UCB0CTL1 &= ~UCSWRST; // curatare SW Reset, continuare operatie
- IE2 |= UCB0RXIE; // activare intrerupere RX
- }
- void Transmit(unsigned char Reg_ADD,unsigned char Reg_DAT){
- MSData[1]= Reg_ADD;
- MSData[0]= Reg_DAT;
- TXByteCtr = NUM_BYTES_TX; // incaracare TX numarator de biti
- while (UCB0CTL1 & UCTXSTP); // asigurare ca conditia de stop a fost trimisa
- UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start conditie
- __bis_SR_register(CPUOFF + GIE); //intrare LPM0 cu intreruperi
- }
- void TransmitOne(unsigned char Reg_ADD){
- MSData[0]= Reg_ADD;
- TXByteCtr = 1; // Load TX byte counter // incaracare TX numarator de biti
- while (UCB0CTL1 & UCTXSTP); // asigurare ca conditia de stop a fost trimisa
- UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start conditie
- __bis_SR_register(CPUOFF + GIE); //intrare LPM0 cu intreruperi
- }
- void Receive(void){
- PRxData = (unsigned char *)RxBuffer; //start buffer RX
- RXByteCtr = NUM_BYTES_RX; //incarcare RX numarator biti
- while (UCB0CTL1 & UCTXSTP); //asigurare ca conditia de stop a fost trimisa
- UCB0CTL1 |= UCTXSTT; //I2C, start conditie
- __bis_SR_register(CPUOFF + GIE); //intrare LPM0 cu intreruperi
- }
- void UARTSendArray(unsigned char *TxArray, unsigned char ArrayLength){
- while(ArrayLength--){ //bucla while pana cand StringLength==0 si post decrementare
- while(!(IFG2 & UCA0TXIFG)); //asteptare pana cand bufferul TX e gata pt date noi
- UCA0TXBUF = *TxArray; //Scrie caracterul la locatia specificata de pointer
- TxArray++; //Incrementarea pointerului TxString la punctul urmatorului caracter
- }
- IFG2 &= ~UCA0TXIFG; // Curatare USCI_A0 int flag
- }
- void UARTSendInt(unsigned int x){
- unsigned char buff[10];
- unsigned char data[10];
- unsigned char index = 0, i = 0;
- while(x > 0) {
- unsigned char val = x % 16;
- if(val < 10)
- buff[index] = 48+val;
- else
- buff[index] = 97+val-10;
- index++;
- x /= 16;
- }
- buff[index] = '\n';
- while(index > 0) {
- index--;
- data[i] = buff[index];
- i++;
- }
- if(i==0) {
- data[0] = '0';
- i++;
- }
- data[i] = '\n';
- UARTSendArray(data, i+1);
- }
Add Comment
Please, Sign In to add comment