Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F452 and acclerometer

Status
Not open for further replies.

amitrana3348

Newbie level 4
Joined
Jan 28, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
india
Activity points
1,346
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;
}
 

Hello
I'm sorry I'm not solving your problem, just want to ask you several questions because i'm new in mikroC
1-For pic18f452 do i have to initialize ADCON0 in the the program
2-If so why did you use ADCON0.GO then ADCON0.DONE as different bits while in Hitech GODONE is one bit
3-How do i get an output on a certain bit of PORTB for example (RB7) do i have to define something at the top of the code

Thank you
 

1. ADCON0 is initialized because i need to make use of on chip ADC

2. ADCON.GO and ADCON.DONE are the same bits you can use any one name of it, it simply means that if you make this bit 1, A-D conversion starts, and if this bit becomes 0 it means A-D conversion has completed

3. To get an output at a certain bit of PORTB, or any other port, you have to set the PORT to be used as an input or out put and this can be done by setting proper values in the TRIS register of particular port,
example
If PORTB to be used as output, you will enter TRISB=0x00 at the start of your program and for input you will make TRISB=0xFF
 

Ok thank you for replying, but i'm still a bit confused in the ADCON0 because i know that it must be initialized but in MikroC's example they initialized ADCON1 only. The next thing is that i mean how to use only one bit from a port
for example in Hitech ( if (RB7=0) ) so what is the equivalent for it in MikroC.
Thank you. :)
 

i'm also using mma 7260 accelerometer for vibration detection in which involves in my 4th year project.
what i have to do is get data to hyperterminal.
can you tell me, your accelerometer shows 1.65 volts, when it is at rest?
 
  • Like
Reactions: inad

    inad

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top