(Atmega16 / 32) Hiển thị nhiệt độ lên LED 7seg Và LCD 16x2 - CodeVisionAVR

VD1: Hiển thị nhiệt độ lên LED 7 đoạn

Sơ đồ kết nối 

Code


 /*******************************************************
Chip type               :        ATmega16
Program type            :        Application
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*******************************************************/
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
unsigned char flash Maled[12] = {0xc0,0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x9C, 0xC6};
unsigned char value;
// Voltage Reference: Internal 2.56V AREF pin
#define ADC_VREF_TYPE ((1<<REFS1) | (1<<REFS0) | (1<<ADLAR))
// Read the 8 most significant bits
// of the AD conversion result
void hienthi(unsigned int num)
{
     unsigned int ch, dv;
     //tach num luu vao bien ch va dv \
     dv = num%10;  //ex: 15%10=5
     ch = num/10;
      //ex: 15/10=1  
     //hien thi led so 1
     PORTD = 0x00;
     PORTD.0 = 1;
     PORTC = Maled[ch];
     delay_ms(1); 
     //hien thi led so 2 
     PORTD = 0x00;
     PORTD.1 = 1;
     PORTC = Maled[dv];
     delay_ms(1);
     //hien thi led so 3
     PORTD = 0x00;
     PORTD.2 = 1;
     PORTC = Maled[10];
     delay_ms(1);
     //hien thi led so 4
     PORTD = 0x00;
     PORTD.3 = 1;
     PORTC = Maled[11];
     delay_ms(1); 
}

void ADC_Init()
{
   /* Make ADC port as input */  
   DDRA=0x00;            
    /* 
    ADC initialization
    ADC Clock frequency: 1000,000 kHz <=> Pescaler/8
    ADC Voltage Reference: AREF pin
    ADC Auto Trigger Source: Free Running
    Only the 8 most significant bits of
    the AD conversion result are used 
    */
   ADCSRA = (1<<ADEN) | (1<<ADATE) | (1<<ADPS1) | (1<<ADPS0); 
    /*Voltage Reference: Internal 2.56V AREF pin*/ 
   ADMUX = (1<<REFS1) | (1<<REFS0) | (1<<ADLAR); 
}

unsigned char  read_adc(unsigned char   adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCH;
}

void main(void)
{
ADC_Init();
DDRC = 0xff;
DDRD = 0xff;
while(1)
      {
      value=read_adc(0);
      hienthi(value);
      }
}
 

Demo

VD2: Hiển thị nhiệt độ lên LCD

Sơ đồ kết nối 

Code


#include <mega16.h>
#include <alcd.h>
#include <delay.h>
#include <stdio.h>

char String[5];
char buff[20];
int value;
// Voltage Reference: Internal 2.56V AREF pin

#define ADC_VREF_TYPE ((1<<REFS1) | (1<<REFS0) | (1<<ADLAR))
// Read the 8 most significant bits
// of the AD conversion result
void ADC_Init()
{
   /* Make ADC port as input */  
   DDRA=0x00;            
    /* 
    ADC initialization
    ADC Clock frequency: 1000,000 kHz <=>
        Pescaler/8
    ADC Voltage Reference: AREF pin
    ADC Auto Trigger Source: Free Running
    Only the 8 most significant bits of
    the AD conversion result are used 
    */

   ADCSRA = (1<<ADEN)|(1<<ADATE)|(1<<ADPS1)|(1<<ADPS0); 
    /*Voltage Reference: Internal 2.56V AREF pin*/ 
   ADMUX = (1<<REFS1)|(1<<REFS0)|(1<<ADLAR); 
}

unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA&(1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCH;
}
void main(void)
{
DDRC=0x00;  /* Make port as input */ 
PORTC=0x00;
DDRD=0x00;  /* Make ADC port as input */ 
PORTD=0x00;
ADC_Init();

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 5
// RD - PORTC Bit 6
// EN - PORTC Bit 7
// D4 - PORTD Bit 4
// D5 - PORTD Bit 5
// D6 - PORTD Bit 6
// D7 - PORTD Bit 7
// Characters/line: 16

lcd_init(16);
while(1)
      {    
      value=read_adc(1);
      lcd_gotoxy(0,0); 
      sprintf(buff,"nhiet do: %d",value);
      lcd_puts(buff); 
      lcd_gotoxy(3,1);  
      lcd_puts("tcl47.com");
//      lcd_gotoxy(4,1);
//      lcd_putchar(0x30+value/10); 
//      lcd_putchar(0x30+value%10);
      }
}

Demo

Nhận xét

Mới hơn Cũ hơn