Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////
- /*****************************************************
- This program was produced by the
- CodeWizardAVR V1.25.9 Professional
- Automatic Program Generator
- © Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
- http://www.hpinfotech.com
- Project :
- Version :
- Date : 4/10/2011
- Author : F4CG
- Company : F4CG
- Comments:
- Chip type : ATmega16L
- Program type : Application
- Clock frequency : 1.000000 MHz
- Memory model : Small
- External SRAM size : 0
- Data Stack size : 256
- *****************************************************/
- #include <mega16.h>
- #include <stdlib.h>
- #include <delay.h>
- #include <stdio.h>
- #include <math.h>
- // I2C Bus functions
- #asm
- .equ __i2c_port=0x15 ;PORTC
- .equ __sda_bit=1
- .equ __scl_bit=0
- #endasm
- #include <i2c.h>
- // Alphanumeric LCD Module functions
- #asm
- .equ __lcd_port=0x18 ;PORTB
- #endasm
- #include <lcd.h>
- int x_value_ADC_value_from_sensore;
- int y_value_ADC_value_from_sensore;
- int z_value_ADC_value_from_sensore;
- void start_with_accelerometer(void){
- i2c_start();
- i2c_write(0xA6);
- i2c_write(0x2D);
- i2c_write(0x00);
- i2c_stop();
- i2c_start();
- i2c_write(0xA6);
- i2c_write(0x31);
- i2c_write(0x01);
- i2c_stop();
- i2c_start();
- i2c_write(0xA6);
- i2c_write(0x2D);
- i2c_write(0x08);
- i2c_stop();
- }
- void measuring_acceleration(void)
- {
- char i;
- int digital_values_from_sensor[6];
- char x_axis_0g_point=1;
- char y_axis_0g_point=1;
- char z_axis_0g_point=-8;
- //i2c reading
- i2c_start();
- i2c_write(0xA6);
- i2c_write(0x32);
- i2c_start();
- i2c_write(0xA7);
- for (i=0;i<5;i++){
- digital_values_from_sensor[i]=i2c_read(1);
- }
- digital_values_from_sensor[i]=i2c_read(0);
- i2c_stop();
- //i2c reading is done
- //comining LSB and HSB
- x_value_ADC_value_from_sensore = (digital_values_from_sensor[1]<<8) | digital_values_from_sensor[0];
- y_value_ADC_value_from_sensore = (digital_values_from_sensor[3]<<8) | digital_values_from_sensor[2];
- z_value_ADC_value_from_sensore = (digital_values_from_sensor[5]<<8) | digital_values_from_sensor[4];
- //giving initial values
- x_value_ADC_value_from_sensore -= x_axis_0g_point;
- y_value_ADC_value_from_sensore -= y_axis_0g_point;
- z_value_ADC_value_from_sensore -= z_axis_0g_point;
- }
- void process_on_accelerometer_date(void){
- int x_value_signed;
- int y_value_signed;
- int z_value_signed;
- float x_scale=7.6;
- float y_scale=7.58;
- float z_scale=7.93;
- char LSb_for_2complement;
- int the_value_for_finding_2complement=1024;
- char buffer[8];
- ////////////////////////////////////////////////////////
- LSb_for_2complement = x_value_ADC_value_from_sensore>>9;
- if(LSb_for_2complement==1){
- x_value_signed = (x_value_ADC_value_from_sensore - the_value_for_finding_2complement)*x_scale;
- }
- else
- {
- x_value_signed = x_value_ADC_value_from_sensore * x_scale;
- }
- ///////////////////////////////////////////////////////
- LSb_for_2complement = y_value_ADC_value_from_sensore>>9;
- if(LSb_for_2complement==1){
- y_value_signed = (y_value_ADC_value_from_sensore - the_value_for_finding_2complement)*y_scale;
- }
- else
- {
- y_value_signed = y_value_ADC_value_from_sensore * y_scale;
- }
- ///////////////////////////////////////////////////////
- LSb_for_2complement = z_value_ADC_value_from_sensore>>9;
- if(LSb_for_2complement==1){
- z_value_signed = (z_value_ADC_value_from_sensore - the_value_for_finding_2complement)*z_scale;
- }
- else
- {
- z_value_signed = z_value_ADC_value_from_sensore * z_scale;
- }
- /////////////////////////////////////////////////////////
- lcd_clear();
- //show X
- lcd_gotoxy(0,0);
- lcd_putsf("X");
- itoa(x_value_signed,buffer);
- lcd_puts(buffer);
- //show Y
- lcd_gotoxy(8,0);
- lcd_putsf("Y");
- itoa(y_value_signed,buffer);
- lcd_puts(buffer);
- //show Z
- lcd_gotoxy(0,1);
- lcd_putsf("Z");
- itoa(z_value_signed,buffer);
- lcd_puts(buffer);
- delay_ms(500);
- }
- void main(void)
- {
- // Declare your local variables here
- // Input/Output Ports initialization
- // Port A initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
- PORTA=0x00;
- DDRA=0x00;
- // Port B initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
- PORTB=0x00;
- DDRB=0x00;
- // Port C initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
- PORTC=0x00;
- DDRC=0x00;
- // Port D initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
- PORTD=0x00;
- DDRD=0x00;
- // Timer/Counter 0 initialization
- // Clock source: System Clock
- // Clock value: Timer 0 Stopped
- // Mode: Normal top=FFh
- // OC0 output: Disconnected
- TCCR0=0x00;
- TCNT0=0x00;
- OCR0=0x00;
- // Timer/Counter 1 initialization
- // Clock source: System Clock
- // Clock value: Timer 1 Stopped
- // Mode: Normal top=FFFFh
- // OC1A output: Discon.
- // OC1B output: Discon.
- // Noise Canceler: Off
- // Input Capture on Falling Edge
- // Timer 1 Overflow Interrupt: Off
- // Input Capture Interrupt: Off
- // Compare A Match Interrupt: Off
- // Compare B Match Interrupt: Off
- TCCR1A=0x00;
- TCCR1B=0x00;
- TCNT1H=0x00;
- TCNT1L=0x00;
- ICR1H=0x00;
- ICR1L=0x00;
- OCR1AH=0x00;
- OCR1AL=0x00;
- OCR1BH=0x00;
- OCR1BL=0x00;
- // Timer/Counter 2 initialization
- // Clock source: System Clock
- // Clock value: Timer 2 Stopped
- // Mode: Normal top=FFh
- // OC2 output: Disconnected
- ASSR=0x00;
- TCCR2=0x00;
- TCNT2=0x00;
- OCR2=0x00;
- // External Interrupt(s) initialization
- // INT0: Off
- // INT1: Off
- // INT2: Off
- MCUCR=0x00;
- MCUCSR=0x00;
- // Timer(s)/Counter(s) Interrupt(s) initialization
- TIMSK=0x00;
- // Analog Comparator initialization
- // Analog Comparator: Off
- // Analog Comparator Input Capture by Timer/Counter 1: Off
- ACSR=0x80;
- SFIOR=0x00;
- // I2C Bus initialization
- i2c_init();
- // LCD module initialization
- lcd_init(16);
- while (1)
- {
- start_with_accelerometer();
- while(1){
- measuring_acceleration();
- process_on_accelerometer_date();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement