Social Icons

Thứ Hai, 11 tháng 11, 2013

AVR sử dụng ADC và LCD

                                             SOURCE  

/*****************************************************
Project : ADC_LCD
Version :
Date    : 11-Nov-2013
Author  : QuiNguyen
Company : Vinasemiconductor
Comments:
Chip type               : ATmega16
Program type            : Application
AVR Core Clock frequency: 12.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega16.h>

#include <delay.h>

// Alphanumeric LCD Module functions
#include <alcd.h>

#define ADC_VREF_TYPE 0x20

// Read the 8 most significant bits
// of the AD conversion result
unsigned char 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 ADCH;
}

// Declare your global variables here
void lcd_putnum(unsigned int so)
{
     unsigned int Nghin, Tram, Chuc, Donvi;
     Nghin = so/1000;
     Tram = (so%1000)/100;
     Chuc = (so%100)/10;
     Donvi = so%10;
     lcd_putchar(Nghin + 48);
     lcd_putchar(Tram + 48);
     lcd_putchar(Chuc + 48);
     lcd_putchar(Donvi + 48);
}
void main(void)
{

unsigned char i;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 750.000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: ADC Stopped
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 4
// RD - PORTD Bit 3
// EN - PORTD Bit 2
// D4 - PORTB Bit 4
// D5 - PORTB Bit 5
// D6 - PORTB Bit 6
// D7 - PORTB Bit 7
// Characters/line: 16
lcd_init(16);
lcd_gotoxy(0,0);
lcd_puts("Vinasemi...");
delay_ms(500);
lcd_clear();
while (1)
      {
      // Place your code here
    i=read_adc(0);
    lcd_gotoxy(0,0);
     lcd_puts("ADC Value=");
     lcd_gotoxy(0,16);
     lcd_putnum(i);
      }
}

                                         MÔ PHỎNG


0 nhận xét:

Đăng nhận xét

 

Sample text

Sample Text

Sample Text