Get Adobe Flash player
FacebookTwitterGoogle+
English Arabic French German Italian Portuguese Russian Spanish

Did you know?

Electrocution is one of the top five causes of workplace deaths.
 

Help us stay online:

small donate

lisy300 ebase artlogoIn article Rotating office chair angular position and rotation speed measurement using gyroscope and ATmega16 we explained how to use gyroscope LISY300 and atmega16. If you are interesting how gyroscope works, read previous linked articles. In this article we will give you same project but reprogrammed for dsPIC30F4013. This is also example how to use Serial Peripheral Interface (SPI) module for dsPIC30F series of microcontrollers. More about basic SPI configuration and functions, read in article dsPIC30F4013 Serial Peripheral Interface (SPI) example.

Basics function InitSPI(), SPIWrite(data), SPIRead() and SPIClose() are used to make specific code for gyroscope. This is recipe how to program SPI for every component that supports this interface.

"gyro.h"

void InitZiroskop(void);
void SPI_Ziroskop_W(unsigned int sd_data);
signed int SPI_Ziroskop_R();

"gyro.c"

#include<p30fxxxx.h>
void InitZiroskop(void)
{
    InitSPI();
}
void SPI_Ziroskop_W(unsigned int sd_data)
{
    LATBbits.LATB2 = 0; //CS = 0
    SPIWrite(sd_data);
    LATBbits.LATB2 = 1; //CS = 1
}
signed int SPI_Ziroskop_R()
{
    unsigned char d1, d2;
    signed int gyro_ret;
    LATBbits.LATB2 = 0; //CS = 0
    d1=SPIRead();
    d2=SPIRead();
    gyro_ret=d1;
    gyro_ret<<=8;
    gyro_ret+=d2;
    gyro_ret>>=2;
    LATBbits.LATB2 = 1; //CS = 1
    return gyro_ret;
} 

"main.c"

/**********************************************************************************************
Naziv projekta  :  CityWatch
Primena projekta:  Uredjaj sa merenje koncentracije gasova i slanje podataka na udaljeni server
                   (oscilator interni 7.3728  MHz sa ukljucenim PLL4)
                                 
 * datum: 05.06.2013
 ************************************************************************************************/
#include <p30fxxxx.h>
#include <stdlib.h>
#include <stdio.h>
#include "adc.h"
#include "timer.h"
#include <math.h>
#include "uart.h"
#include "gyro.h"
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //instruction takt je isti kao i interni oscilator
_FWDT(WDT_OFF);
//-----------------------------------------------------------------
#define GyroSteadyValue_MAX 502
#define GyroSteadyValue_MIN 509
#define GyroSteadyValue_AVARAGE 506
#define GyroQuantsPerDegree 103
#define GyroQuantsPerCircle (GyroQuantsPerDegree*360)
#define GytoSpeed_INTERVAL 10
unsigned char gyro_speed_time;
signed int    gyro_adc, gyro_speed;
long          angle, circle, degrees;
float         integraled_gyro, pervious_integraled_gyro, integraled_gyro_copy;
char buf[10];
int tempRX2; //UART2
unsigned int GYRO_timer, stoperica;
//-----------------------------------------------------------------
void Delay_ms(unsigned int vreme)
//-----------------------------------------------------------------
// funkcija za kasnjenje u ms
{
    stoperica = 0;
    while(stoperica < vreme);
}
//-----------------------------------------------------------------
void __attribute__((__interrupt__)) _T1Interrupt(void) // TIMER INTERRUPT
//-----------------------------------------------------------------
// prekidna rutina Thajmera 1
{
    TMR1 = 0;               //Stop Timer
    stoperica++;            //for Delay_ms() function
    GYRO_timer++;           //get output from gyro every 10ms
    if(GYRO_timer == 10)
    {
        GYRO_timer = 0;
        gyro_adc = SPI_Ziroskop_R();
        if((gyro_adc<GyroSteadyValue_MIN)||(gyro_adc>GyroSteadyValue_MAX))
        {
            integraled_gyro=integraled_gyro+gyro_adc-GyroSteadyValue_AVARAGE;
        }
        gyro_speed_time++;
        if(gyro_speed_time==GytoSpeed_INTERVAL)
        {
            gyro_speed_time=0;
            gyro_speed=((100/GytoSpeed_INTERVAL)*(pervious_integraled_gyro-integraled_gyro))/GyroQuantsPerDegree;
            pervious_integraled_gyro=integraled_gyro;
        }
    }
    IFS0bits.T1IF = 0;      //Clear timer flag
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
void __attribute__((__interrupt__)) _U2RXInterrupt(void) //UART2 UINTERRUPT
//-----------------------------------------------------------------
{
    IFS1bits.U2RXIF = 0;
    tempRX2 = U2RXREG;
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
int main(void)  //MAIN MAIN MAIN
//-----------------------------------------------------------------
{
    InitUART2();
    Init_T1();
    InitZiroskop();
    GYRO_timer = 0;
    stoperica = 0;
   
    WriteStringUART2("**********START**********\r");
    WriteStringUART2("Gyroscope speed and position measurement example from www.Electronics-Base.com\r");
    Delay_ms(1000);
   
    while (1)
    {
          Delay_ms(150);
          integraled_gyro_copy=integraled_gyro;
          WriteStringUART2("Gyroscope ADC readout: ");
          WriteUART2dec2string(gyro_adc);
          WriteStringUART2(" Gyroscope summed integral: ");
          sprintf(buf, "%d", integraled_gyro_copy);
          WriteStringUART2(buf);
          WriteStringUART2(" Calculated Angle: ");
          angle=integraled_gyro/GyroQuantsPerDegree;
          sprintf(buf, "%ld", angle);
          WriteStringUART2(buf);
          WriteStringUART2("°");
          WriteStringUART2(" Full circles: ");
          circle=integraled_gyro_copy/GyroQuantsPerCircle;
          sprintf(buf, "%ld", circle);
          WriteStringUART2(buf);
          WriteStringUART2(" Degrees: ");
          degrees =  integraled_gyro_copy-circle*GyroQuantsPerCircle;
          degrees=degrees/GyroQuantsPerDegree;
          sprintf(buf, "%ld", degrees);
          WriteStringUART2(buf);
          WriteStringUART2(" Speed: ");
          sprintf(buf, "%d", gyro_speed);
          WriteStringUART2( buf);
          WriteStringUART2(" °/sec\r");
    }//while
    return 0;
}//main

You can download complete project for MPLAB X IDE HERE.