amitrana3348
Newbie level 4
hii all
am currently working on a project which makes use of mma7260 accelerometer
I want to find out the vibration frequency using accelerometer to monitor
vibrations
the hardware used here is accelerometer mma7260 with PIC18F452
compiler is mikroC pro for PIC
The zero offset voltage out of mma7260 is typically 1.65 Volts,
i want to detect that how many times it goes above 1.75 volts,
or below 1.55 volts in 1 second, which will give me the frequency
of vibrations, below is my code with comments, am not figuring out
where am going wrong, I have selected maximum speed for A-D conversion
and i was using PIC18 timer to make the calculations but the result
is showing 1 on LCD continuously instead of frequency
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB1_bit;
sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB1_bit;
// End LCD module connections
unsigned long getadcvalue(unsigned char);
void main(void)
{
int value;
unsigned char disp[15],digi[15];
unsigned char count,frequency[5];
int acc;
TRISA = 0xFF; // make port A an input
ADCON1 = 0x81;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
lcd_out(1,1,"Hello World");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
while(1)
{
again:
T0CON = 0x06; // timer0, 16-bit mode, prescalar fosc / 4 / 128
TMR0H = 0x67; // some initial count to count upto 1 second
TMR0L = 0x69; // some initial count
T0CON.TMR0ON = 1;
while(INTCON.TMR0IF == 0) // repeat this loop until timer overflows (1 second)
{
value = getadcvalue(2); // get adc value read from adc input 2, Vref is 3.3V since mma7260 o/p is max 3.3V
value = value / 31; // get the voltage from converted value, say for 1.6Volts, it will show 16
Delay_ms(10);
IntToStr(value,disp); //this disp here indicates the voltage as 12 for 1.2 volt just for our reference
Lcd_Out(2,1,disp); //
//Delay_ms(10);
if(value>17||value<14) // check wheather the value is > 17 means 1.7Volt or less than 14 i.e. 1.4V
{
count++; // if more or less increment counter once
ByteToStr(count,frequency);
Lcd_out(1,1,frequency);
while(value>17||value<14) // wait till the value is more than 17 or less than 14
{}
}
else
Lcd_Out(1,1,"cle"); // if nothing such happens in then display "CLE" to indicate clear
}
T0CON.TMR0ON = 0; // turn off timer after 1 second
INTCON.TMR0IF = 0; // disable timer interrupt
goto again; // again goto initial stage
}
}
unsigned long getadcvalue(unsigned char channel)
{
unsigned int b;
unsigned int c;
if(channel == 0)
{ ADCON0 = 0x81; }
if(channel == 1)
{ ADCON0 = 0x89; }
if(channel == 2)
{ ADCON0 = 0x91; }
Delay_ms(5);
ADCON0.GO = 1;
while(ADCON0.DONE == 1)
{}
b = ADRESH<<8;
c = b|ADRESL;
return c;
}
am currently working on a project which makes use of mma7260 accelerometer
I want to find out the vibration frequency using accelerometer to monitor
vibrations
the hardware used here is accelerometer mma7260 with PIC18F452
compiler is mikroC pro for PIC
The zero offset voltage out of mma7260 is typically 1.65 Volts,
i want to detect that how many times it goes above 1.75 volts,
or below 1.55 volts in 1 second, which will give me the frequency
of vibrations, below is my code with comments, am not figuring out
where am going wrong, I have selected maximum speed for A-D conversion
and i was using PIC18 timer to make the calculations but the result
is showing 1 on LCD continuously instead of frequency
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB1_bit;
sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB1_bit;
// End LCD module connections
unsigned long getadcvalue(unsigned char);
void main(void)
{
int value;
unsigned char disp[15],digi[15];
unsigned char count,frequency[5];
int acc;
TRISA = 0xFF; // make port A an input
ADCON1 = 0x81;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
lcd_out(1,1,"Hello World");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
while(1)
{
again:
T0CON = 0x06; // timer0, 16-bit mode, prescalar fosc / 4 / 128
TMR0H = 0x67; // some initial count to count upto 1 second
TMR0L = 0x69; // some initial count
T0CON.TMR0ON = 1;
while(INTCON.TMR0IF == 0) // repeat this loop until timer overflows (1 second)
{
value = getadcvalue(2); // get adc value read from adc input 2, Vref is 3.3V since mma7260 o/p is max 3.3V
value = value / 31; // get the voltage from converted value, say for 1.6Volts, it will show 16
Delay_ms(10);
IntToStr(value,disp); //this disp here indicates the voltage as 12 for 1.2 volt just for our reference
Lcd_Out(2,1,disp); //
//Delay_ms(10);
if(value>17||value<14) // check wheather the value is > 17 means 1.7Volt or less than 14 i.e. 1.4V
{
count++; // if more or less increment counter once
ByteToStr(count,frequency);
Lcd_out(1,1,frequency);
while(value>17||value<14) // wait till the value is more than 17 or less than 14
{}
}
else
Lcd_Out(1,1,"cle"); // if nothing such happens in then display "CLE" to indicate clear
}
T0CON.TMR0ON = 0; // turn off timer after 1 second
INTCON.TMR0IF = 0; // disable timer interrupt
goto again; // again goto initial stage
}
}
unsigned long getadcvalue(unsigned char channel)
{
unsigned int b;
unsigned int c;
if(channel == 0)
{ ADCON0 = 0x81; }
if(channel == 1)
{ ADCON0 = 0x89; }
if(channel == 2)
{ ADCON0 = 0x91; }
Delay_ms(5);
ADCON0.GO = 1;
while(ADCON0.DONE == 1)
{}
b = ADRESH<<8;
c = b|ADRESL;
return c;
}