Опубликовано 2010-03-29 11:33:52 автором Ruslan

Using the CPU in the AVR. Lesson 6


Often the need to measure voltage. For these purposes, a microcontroller is ADC (analog-digital Converter). ADC is a device that converts an analog signal into a digital representation. The ADC input analog signal at the output we get is the equivalent of a digital signal.

the Main characteristics of the ADC

  • Frequency conversion is the number of times per second ADC can measure the voltage
  • bitness - the number of discrete values of voltage, which divided the whole operating range of input voltages. ADC in AVR ten bit. That is, the maximum voltage at the ADC input will be translated into the 210=1024
  • input voltage Range is the minimum and the maximum voltage that can be fed to the inputs of the ADC. For avr is a range from 0 to supply voltage microcontroller
To work ADC is required voltage reference). Is the standard against which it measures the voltage at the input. In the AVR as the source of the reference voltage can be the supply voltage MK, the reference voltage source connected to the leg ARef and inner ION 2.56 century ION should be as stable, depends on the accuracy of the measurements. To touch all this, let's do a simple voltmeter at 5V. Launch CVAVR, the question run CodeWizardAVR click "Yes" and go to the tab ADC

installing the ADC in CWAVR

Here:

  • ADC Enable - enables or disables work ADC
  • Use 8 bit - ADC will be 8 bit, i.e. the maximum number of the output of the ADC is 255 (default ADC 10-bit)
  • ADC Interrupt - will generate an interrupt when the ADC to finish the conversion. When you select this option, there are additional items  interrupt after measurement ADCS
    • Noise Canceler because the processor is idle mode Idle Mode). This reduces the amount of interference which could affect the accuracy of the ADC
    • Automatically scan inputs - sequential automatic scanning inputs ADC. Measurement results are placed into the array
  • Volt Ref selects the reference voltage source

     choice the voltage reference CWAVR

    • AREF pin - reference voltage source on the leg AREF
    • AVCC pin - reference voltage source on the leg of the AVCC
    • Int., cap. on AREF - to connect to the conclusion aref internal voltage reference 2,56в and capacitor
  • Clock - the choice of frequency of ADC
Us for our voltmeter need to set the reference voltage on the leg of the AVCC (leg food ADC), conversion frequency 500 kHz

 installation options the voltmeter CWAVR

We our measurements with the ADC will display on the lcd display, for its initialization go to the tab LCD and install all like in the screenshot

 initialize LCD in CWAVR

Now all settings are made, click " file->Generate. save and exit. Add the code that generated the CWAVR, and remove it initialization periphery MK, that we do not use, this generates the following code:

#include <mega8.h> 
#include <delay.h> 
#include <stdio.h> 
// Alphanumeric LCD Module functions 
#asm 
.equ __lcd_port=0x12 ;PORTD is set 
#endasm 
#include <lcd.h> 
 
#define ADC_VREF_TYPE 0x40 
 
// Read the AD conversion result 
unsigned int read_adc(unsigned char adc_input) 
{ 
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); 
// Delay needed for the stabilization of the ADC input voltage 
delay_us(10); 
// Start the AD conversion 
ADCSRA|=0x40; 
// Wait for the AD conversion to complete 
while ((ADCSRA & 0x10)==0); 
ADCSRA|=0x10; 
return ADCW; 
} 
 
void main(void) 
{ 
char lcd_buffer[16]; 
unsigned int u; 
// ADC initialization 
// The ADC Clock frequency: 500,000 kHz 
// ADC Voltage Reference: AVCC pin 
ADMUX=ADC_VREF_TYPE & 0xff; 
ADCSRA=0x81; 
 
// LCD module initialization 
lcd_init(16); 
 
while (1) 
{ 
/*because the ADC us 10-bit, the maximum number that will return function, read_adc() 
will be equal to 1024, this number is the equivalent of the input voltage abc0. 
For example, if read_adc() returned to 512, this means that at the entrance abc0 we filed a half reference voltage 
To calculate the real voltage, we should write a proportion of the 
reference voltage - 1024 
the desired voltage adc 
We voltage reference = 5 
The desired voltage = 5 * adc/1024, or the Desired voltage = 0,005*adc 
for simplicity, let's translate Volta in миливольты, by multiplying by 1000 
The desired voltage = 0,005*adc*1000 
*/ 
u=read_adc(0) * 5;//call the function to measure voltage and pass it the number of the legs, on which you want to measure a voltage 
lcd_clear(); //clean the display before output 
lcd_gotoxy(0,0); // translate the cursor at the position x=0 y=0 
sprintf(lcd_buffer,"U = %i mv",u); // produce a string for output 
lcd_puts(lcd_buffer); //display the string on the display 
delay_us(500); //make the delay 500 ml 
}; 
}

The program is ready, the case for the scheme

scheme of a simple voltmeter

The scheme is very simple, here we see the atmega8 microcontroller and lcd display pipe 16x2 character (example of lcd described here). Our simple voltmeter measures voltage to 5 century How to measure the voltage of more than 5 read this article. circuit is in Proteus all the necessary files for this tutorial are in the archive Volt.zip.

Комментарии - (3)

Добавить комментарий

Для отправки комментария вы должны авторизоваться.