Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

adc.c

Example of A/D converter use.
/*
 * Copyright (C) 2003 BLADOX, s.r.o.  All rights reserved.
 *
 * This file is part of an example program for Turbo. This example
 * program may be used, distributed and modified without limitation.
 *
 */

#include <config.h>
#include <turbo/turbo.h>

#include <avr/sfr_defs.h>
#include <avr/io.h>

u8 PROGMEM t_Foo_en[] = "A/D Converter";
u8 PROGMEM t_mV[] = " mV";
u8 PROGMEM t_Error[] = "A/D conv. ERROR";
u8 PROGMEM t_Ch0[] = "Ch0=";
u8 PROGMEM t_Ch1[] = "Ch1=";

//------------------------------------------------------------------------
//  ADC input pins ADC0 = PF0, ADC1 = PF1
//------------------------------------------------------------------------
#define ADC0_DDR DDRF
#define ADC0_DDR_PIN DD0
#define ADC0_PORT PORTF
#define ADC0_PORT_PIN PORT0

#define ADC1_DDR DDRF
#define ADC1_DDR_PIN DD1
#define ADC1_PORT PORTF
#define ADC1_PORT_PIN PORT1

void init_adc ()
{
  // deactivate pull-ups on ADC0 and ADC1 pins

  cbi (ADC0_DDR, ADC0_DDR_PIN);
  cbi (ADC1_DDR, ADC1_DDR_PIN);
  asm __volatile__ ("nop");     //wait

  cbi (ADC0_PORT, ADC0_PORT_PIN);
  cbi (ADC1_PORT, ADC1_PORT_PIN);
}

// Measure Voltage on pin PF2 (ADC2)
u8 adc_measure (u16 * voltage, u8 channel)
{
  if (channel >= 8)
    return 1;

  //Set Voltage reference to internal 2,56V
  //Left Ajust Result disable
  //Single Ended ADC0-ADC7 input
  outb (ADMUX, 0xC0 | channel);

//    //Enable high speed ADC mode
//    sbi(SFIOR,ADHSM);
  //Disable high speed ADC mode
  cbi (SFIOR, ADHSM);

  // Enable ADC
  // Start conversion
  // Disable Free running mode
  // ADCclk=Fosc/8
  outb (ADCSR, 0xC3);

  // Wait here when conversion is not completed with data registers updating
  while ((inb (ADCSR) & (1 << ADIF)) == 0);

  //clear ADCIF flag
  sbi (ADCSR, ADIF);

  //store measured voltage    
  *voltage = inw (ADCW);

  // Disable ADC
  outb (ADCSR, 0x00);

  //Disable high speed ADC mode
  cbi (SFIOR, ADHSM);

  //Turn Off Internal Voltage reference 2,56V
  outb (ADMUX, 0x00);

  return 0;
}

void action_menu (void *data)
{
  u8 *buf = buf_B ();
  u8 *r = buf;
  u16 voltage0;
  u16 mvoltage0;
  u16 mvoltage1;
  u16 voltage1;
  u8 s;

  s = adc_measure (&voltage0, 0);
  s = adc_measure (&voltage1, 1);

#define ADC_REF_VOLTAGE_MV 2560
#define ADC_RANGE 1024

  //convert Volts to miliVolts
  mvoltage0 = voltage0 * 10;
  mvoltage0 /= 4;
  mvoltage1 = voltage1 * 10;
  mvoltage1 /= 4;

  if (s == 0)
  {
    dbsp ("Voltage: ");
    dbih (voltage0);
    dbih (voltage1);
    dbc ('\n');

    r = sprints (r, t_Ch0);
    r = sprinti (r, mvoltage0);
    r = sprints (r, t_mV);

    r = sprintc (r, '\n');

    r = sprints (r, t_Ch1);
    r = sprinti (r, mvoltage1);
    r = sprints (r, t_mV);
  }
  else
    r = sprints (r, t_Error);

  r = sprintc (r, '\0');
  display_text_raw (buf, Q_DISPLAY_TEXT_USER_CLEAR);
}

void turbo_handler (u8 action, void *data)
{
  switch (action)
  {
    case ACTION_INSERT_MENU:
      insert_menu (t_Foo_en);
      init_adc ();
      break;
    case ACTION_MENU_SELECTION:
      stk_thread (action_menu, data);
      break;
    default:
      break;
  }
}


Copyright © 2004-2006 BLADOX
Turbo version 1.2