|
| |||||||
|
Опубликовано 2010-03-29 11:33:52 автором Ruslan Using the CPU in the AVR. Lesson 6Often 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
Here:
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
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
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. Комментарии - (4)
Добавить комментарийДля отправки комментария вы должны авторизоваться. |
|||||||