Guest User

Untitled

a guest
May 26th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. /*
  2. File: main.c
  3. Version: 1.0.0
  4. Date: May. 11, 2006
  5.  
  6. GP2D120 Distance Measurement Demo #1 on an ATMega8 AVR.
  7.  
  8. ****************************************************************************
  9. Copyright (C) 2006 Micah Carrick <email@micahcarrick.com>
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. ****************************************************************************
  25. */
  26.  
  27. #define F_CPU 8000000UL /* 8 MHz crystal clock */
  28. #define UART_BAUD_RATE 9600 /* UART Baud Rate */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <avr/io.h>
  34. #include <avr/pgmspace.h>
  35. #include <avr/interrupt.h>
  36. #include <util/delay.h>
  37.  
  38. #include "gp2d120.h"
  39. #include "uart.h"
  40.  
  41. /* initializes the ADC */
  42. void adc_init(void)
  43. {
  44. ADMUX = 0 | _BV(ADLAR); // channel 0, left-justified result
  45. ADCSRA = _BV(ADEN) | _BV(ADPS2);
  46. }
  47.  
  48. /* starts an ADC conversion */
  49. void adc_start_conversion(void)
  50. {
  51. ADCSRA |= _BV(ADSC);
  52. }
  53.  
  54. /* returns boolean value indicating if the ADC conversion is still in prgress */
  55. int adc_conversion_in_progress(void)
  56. {
  57. return !(ADCSRA & _BV(ADIF));
  58. }
  59.  
  60. /* clears the ADC interrupt flag */
  61. void adc_clear_interrupt_flag(void)
  62. {
  63. ADCSRA |= _BV(ADIF);
  64. }
  65.  
  66. int
  67. main (void)
  68. {
  69. uint8_t adc_value; /* gp2d120 ADC value */
  70. uint8_t distance; /* gp2d120 distance in cm */
  71. char buffer[4]; /* max '255\0' */
  72. uint8_t i; /* loop counter */
  73.  
  74. /* initialize */
  75. uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
  76. sei();
  77. uart_puts_P("*** GP2D120 Test Application ***\n\r");
  78. adc_init();
  79.  
  80. while (1)
  81. {
  82.  
  83. /* get ADC value */
  84. adc_start_conversion();
  85. while (adc_conversion_in_progress());
  86. adc_value = ADCH;
  87. adc_clear_interrupt_flag();
  88.  
  89. /* calculate the distance */
  90. distance = gp2d120_adc8_to_cm(adc_value);
  91.  
  92. /* convert ADC value to ascii for UART */
  93. itoa (adc_value, buffer, 10);
  94. uart_putc('\t');
  95. uart_puts(buffer);
  96.  
  97. /* convert distance to ascii for UART */
  98. itoa (distance, buffer, 10);
  99. uart_putc('\t');
  100. uart_puts(buffer);
  101. uart_puts_P("cm\n\r");
  102.  
  103. /* long delay */
  104. for (i=0; i<20; i++) { _delay_ms(250); }
  105. }
  106. return (0);
  107. }
Add Comment
Please, Sign In to add comment