Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created August 4, 2019 09:20
Show Gist options
  • Save surinoel/736cc7ea456e6e7aeea235860ce17c7e to your computer and use it in GitHub Desktop.
Save surinoel/736cc7ea456e6e7aeea235860ce17c7e to your computer and use it in GitHub Desktop.
/*
* adc.c
*
* Created: 2019-05-18 오후 10:14:50
* Author: yeong
*/
#include "adc.h"
extern FILE OUTPUT;
extern FILE INPUT;
void adc_init(unsigned char ch) {
ADMUX |= (1<<REFS0);
ADMUX = ((ADMUX & 0xE0) | ch);
ADCSRA |= 0x07;
ADCSRA |= (1<<ADEN) | (1<<ADFR) | (1<<ADSC);
}
void adc_change_channel(unsigned char ch) {
ADMUX = ((ADMUX & 0xE0) | ch);
_delay_us(50);
}
int adc_read(void) {
_delay_ms(50);
while(!(ADCSRA & (1<<ADIF)));
return ADC;
}
/*
* SHARP_2Y0A21.c
*
* Created: 2019-08-04 오후 12:28:29
* Author : yeong
*/
#include <avr/io.h>
#include "uart.h"
#include "adc.h"
float calc_distance(float volt)
{
float val = 0.04 * volt;
return (1/val);
}
int main(void)
{
float volt, dist;
uart0_init();
adc_init(0x01);
/* Replace with your application code */
while (1)
{
volt = 5.0*adc_read()/1024;
// printf("voltage:%f\r\n", 5.0*adc_read()/1024);
dist = calc_distance(volt);
if(dist>=10 && dist<=80) {
printf("dist:%f\r\n", dist);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment