Kasutaja tarvikud

Lehe tööriistad


kursused:roboti_algkursused:2017.1:adc

LED-i vilgutamine

/*
 * LED_Blink main.c
 */
 
#include <avr/io.h>
#include <util/delay.h>
 
int main(void)
{
    //set LED pin 5 to be output
    DDRB |= (1 << DDB5);
    while (1)
    {
        // bitwise XOR ^ toggles the LED value ON and OFF
        PORTB ^= (1 << PORTB5);
        // 100 millisecond delay
        _delay_ms(100);
    }
}

ADC

main.c

/*
 * ADC main.c
 *
 */
 
#define LED_PIN 1<<5
 
#include <avr/io.h>
#include <util/delay.h>
#include "ADC.h"
 
int main(void)
{
   adc_init();
 
   DDRB |= (1<<5);
 
   uint16_t adc_value = 0;
 
   while(1) //infinite loop
   {
      adc_value = adc_read(0);
 
      PORTB |=(1<<5);
      for(int i=0; i < adc_value; i++)
         _delay_ms(1);
 
      PORTB &= ~(1<<5);
      for(int i=0; i < adc_value; i++)
         _delay_ms(1);
   }
}

ADC.h

/*
 * ADC.h
 *
 */
 
 
#ifndef ADC_H_
#define ADC_H_
 
void adc_init();
uint16_t adc_read(uint8_t channel);
 
 
#endif /* ADC_H_ */

ADC.c

/*
 * ADC.c
 *
 */
 
#include <avr/io.h>
 
#include "ADC.h"
 
void adc_init(void)
{
   ADCSRA = (0x7 <<ADPS0); //set prescaler to 128
   ADCSRB = 0;
   DIDR0 = (1 << ADC0D);
   ADMUX = (1 << REFS0);
   ADCSRA |= (1 << ADEN);
}
 
uint16_t adc_read(uint8_t channel)
{
   if ((channel < 9) || (channel == 14) || (channel == 15))
   {
      ADMUX = (1<< REFS0) | (channel << MUX0);
   }
   else
   {
      return 0xFFFF;
   }
   ADCSRA |= (1 << ADSC); //käivitamine ühe lugemise võtmise
 
   while (ADCSRA & (1 << ADSC))  //ootame et lugemine lõppeks
   {
   }
 
   return ADC;
}
kursused/roboti_algkursused/2017.1/adc.txt · Viimati muutnud: 2017/03/11 21:10 persoon maarjo.mass