Advertisement
Guest User

Untitled

a guest
Apr 11th, 2011
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.05 KB | None | 0 0
  1. ////////////////////////////////////////////////
  2. /*****************************************************
  3. This program was produced by the
  4. CodeWizardAVR V1.25.9 Professional
  5. Automatic Program Generator
  6. © Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
  7. http://www.hpinfotech.com
  8.  
  9. Project :
  10. Version :
  11. Date    : 4/10/2011
  12. Author  : F4CG                            
  13. Company : F4CG                            
  14. Comments:
  15.  
  16.  
  17. Chip type           : ATmega16L
  18. Program type        : Application
  19. Clock frequency     : 1.000000 MHz
  20. Memory model        : Small
  21. External SRAM size  : 0
  22. Data Stack size     : 256
  23. *****************************************************/
  24.  
  25. #include <mega16.h>
  26. #include <stdlib.h>
  27. #include <delay.h>
  28. #include <stdio.h>
  29. #include <math.h>
  30.  
  31.  
  32. // I2C Bus functions
  33. #asm
  34.    .equ __i2c_port=0x15 ;PORTC
  35.    .equ __sda_bit=1
  36.    .equ __scl_bit=0
  37. #endasm
  38. #include <i2c.h>
  39.  
  40. // Alphanumeric LCD Module functions
  41. #asm
  42.    .equ __lcd_port=0x18 ;PORTB
  43. #endasm
  44. #include <lcd.h>
  45.  
  46. int x_value_ADC_value_from_sensore;
  47. int y_value_ADC_value_from_sensore;
  48. int z_value_ADC_value_from_sensore;
  49.  
  50.  
  51.  
  52.  
  53. void start_with_accelerometer(void){
  54.       i2c_start();
  55.       i2c_write(0xA6);
  56.       i2c_write(0x2D);
  57.       i2c_write(0x00);
  58.       i2c_stop();
  59.       i2c_start();
  60.       i2c_write(0xA6);
  61.       i2c_write(0x31);
  62.       i2c_write(0x01);
  63.       i2c_stop();
  64.       i2c_start();
  65.       i2c_write(0xA6);
  66.       i2c_write(0x2D);
  67.       i2c_write(0x08);
  68.       i2c_stop();
  69. }
  70.  
  71.  
  72.  
  73.  
  74. void measuring_acceleration(void)
  75. {    
  76.       char i;
  77.       int digital_values_from_sensor[6];
  78.       char x_axis_0g_point=1;
  79.       char y_axis_0g_point=1;
  80.       char z_axis_0g_point=-8;
  81.  
  82.      
  83.       //i2c reading
  84.       i2c_start();
  85.       i2c_write(0xA6);
  86.       i2c_write(0x32);
  87.       i2c_start();
  88.       i2c_write(0xA7);
  89.       for (i=0;i<5;i++){
  90.       digital_values_from_sensor[i]=i2c_read(1);
  91.       }
  92.       digital_values_from_sensor[i]=i2c_read(0);
  93.       i2c_stop();
  94.       //i2c reading is done
  95.  
  96.         //comining LSB and HSB
  97.       x_value_ADC_value_from_sensore = (digital_values_from_sensor[1]<<8) |  digital_values_from_sensor[0];
  98.       y_value_ADC_value_from_sensore = (digital_values_from_sensor[3]<<8) |  digital_values_from_sensor[2];
  99.       z_value_ADC_value_from_sensore = (digital_values_from_sensor[5]<<8) |  digital_values_from_sensor[4];
  100.       //giving initial values
  101.       x_value_ADC_value_from_sensore -= x_axis_0g_point;
  102.       y_value_ADC_value_from_sensore -= y_axis_0g_point;
  103.       z_value_ADC_value_from_sensore -= z_axis_0g_point;
  104.      
  105. }
  106.  
  107.  
  108.  
  109.  
  110. void process_on_accelerometer_date(void){
  111.         int x_value_signed;
  112.         int y_value_signed;
  113.         int z_value_signed;
  114.         float x_scale=7.6;
  115.         float y_scale=7.58;
  116.         float z_scale=7.93;
  117.         char LSb_for_2complement;
  118.         int the_value_for_finding_2complement=1024;
  119.         char buffer[8];
  120.          
  121.         ////////////////////////////////////////////////////////
  122.         LSb_for_2complement = x_value_ADC_value_from_sensore>>9;
  123.         if(LSb_for_2complement==1){
  124.         x_value_signed = (x_value_ADC_value_from_sensore - the_value_for_finding_2complement)*x_scale;
  125.         }
  126.         else
  127.         {
  128.         x_value_signed = x_value_ADC_value_from_sensore * x_scale;
  129.         }
  130.         ///////////////////////////////////////////////////////
  131.         LSb_for_2complement = y_value_ADC_value_from_sensore>>9;
  132.         if(LSb_for_2complement==1){
  133.         y_value_signed = (y_value_ADC_value_from_sensore - the_value_for_finding_2complement)*y_scale;
  134.         }
  135.         else
  136.         {
  137.         y_value_signed = y_value_ADC_value_from_sensore * y_scale;
  138.         }
  139.         ///////////////////////////////////////////////////////
  140.         LSb_for_2complement = z_value_ADC_value_from_sensore>>9;
  141.         if(LSb_for_2complement==1){
  142.         z_value_signed = (z_value_ADC_value_from_sensore - the_value_for_finding_2complement)*z_scale;
  143.         }
  144.         else
  145.         {
  146.         z_value_signed = z_value_ADC_value_from_sensore * z_scale;
  147.         }
  148.         /////////////////////////////////////////////////////////
  149.        
  150.         lcd_clear();
  151.         //show X  
  152.         lcd_gotoxy(0,0);
  153.         lcd_putsf("X");                  
  154.         itoa(x_value_signed,buffer);
  155.         lcd_puts(buffer);
  156.      
  157.      
  158.         //show Y
  159.          lcd_gotoxy(8,0);
  160.         lcd_putsf("Y");
  161.          itoa(y_value_signed,buffer);
  162.          lcd_puts(buffer);
  163.  
  164.      
  165.      
  166.         //show Z
  167.         lcd_gotoxy(0,1);
  168.         lcd_putsf("Z");
  169.         itoa(z_value_signed,buffer);
  170.         lcd_puts(buffer);
  171.      
  172.          delay_ms(500);
  173.  
  174.  
  175.  
  176. }
  177.  
  178.  
  179.  
  180.  
  181. void main(void)
  182. {
  183. // Declare your local variables here
  184.  
  185. // Input/Output Ports initialization
  186. // Port A initialization
  187. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  188. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  189. PORTA=0x00;
  190. DDRA=0x00;
  191.  
  192. // Port B initialization
  193. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  194. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  195. PORTB=0x00;
  196. DDRB=0x00;
  197.  
  198. // Port C initialization
  199. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  200. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  201. PORTC=0x00;
  202. DDRC=0x00;
  203.  
  204. // Port D initialization
  205. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  206. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  207. PORTD=0x00;
  208. DDRD=0x00;
  209.  
  210. // Timer/Counter 0 initialization
  211. // Clock source: System Clock
  212. // Clock value: Timer 0 Stopped
  213. // Mode: Normal top=FFh
  214. // OC0 output: Disconnected
  215. TCCR0=0x00;
  216. TCNT0=0x00;
  217. OCR0=0x00;
  218.  
  219. // Timer/Counter 1 initialization
  220. // Clock source: System Clock
  221. // Clock value: Timer 1 Stopped
  222. // Mode: Normal top=FFFFh
  223. // OC1A output: Discon.
  224. // OC1B output: Discon.
  225. // Noise Canceler: Off
  226. // Input Capture on Falling Edge
  227. // Timer 1 Overflow Interrupt: Off
  228. // Input Capture Interrupt: Off
  229. // Compare A Match Interrupt: Off
  230. // Compare B Match Interrupt: Off
  231. TCCR1A=0x00;
  232. TCCR1B=0x00;
  233. TCNT1H=0x00;
  234. TCNT1L=0x00;
  235. ICR1H=0x00;
  236. ICR1L=0x00;
  237. OCR1AH=0x00;
  238. OCR1AL=0x00;
  239. OCR1BH=0x00;
  240. OCR1BL=0x00;
  241.  
  242. // Timer/Counter 2 initialization
  243. // Clock source: System Clock
  244. // Clock value: Timer 2 Stopped
  245. // Mode: Normal top=FFh
  246. // OC2 output: Disconnected
  247. ASSR=0x00;
  248. TCCR2=0x00;
  249. TCNT2=0x00;
  250. OCR2=0x00;
  251.  
  252. // External Interrupt(s) initialization
  253. // INT0: Off
  254. // INT1: Off
  255. // INT2: Off
  256. MCUCR=0x00;
  257. MCUCSR=0x00;
  258.  
  259. // Timer(s)/Counter(s) Interrupt(s) initialization
  260. TIMSK=0x00;
  261.  
  262. // Analog Comparator initialization
  263. // Analog Comparator: Off
  264. // Analog Comparator Input Capture by Timer/Counter 1: Off
  265. ACSR=0x80;
  266. SFIOR=0x00;
  267.  
  268. // I2C Bus initialization
  269. i2c_init();
  270.  
  271. // LCD module initialization
  272. lcd_init(16);
  273.  
  274. while (1)
  275.       {
  276.       start_with_accelerometer();
  277.      
  278.       while(1){
  279.       measuring_acceleration();
  280.       process_on_accelerometer_date();
  281.      
  282.                          
  283.  
  284.       }
  285.       };
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement