본문 바로가기
코스웨어/16년 스마트컨트롤러

ADC

by 알 수 없는 사용자 2016. 3. 23.
728x90
반응형

#include "main.h"
#include "Lcd.h"
#include "Tc0.h"
#include "Usart.h"
#include "ADC.h"


int main()

 unsigned char ucData;
 char caVal[] = "0000";
 unsigned int uiVal;
 
 
 LCD_Init();
 USART0_Init();
 USART0_Str("AVR ON");
 ADC_Init();
 ADC_Run0();
 
 while(1) //
 {
   uiVal =ADC_Run0();
  caVal[0] = '0' + (uiVal/1000);
  caVal[1] = '0' + (uiVal%1000)/100;
  caVal[2] = '0' + (uiVal%100)/10;
  caVal[3] = '0' + (uiVal%10);
   
  LCD_Str(caVal);
  USART0_Str(caVal);
  LCD_Inst(LCD_HOME);
 }
  return 0;
}

#ifndef __MAIN_H__
#define __MAIN_H__

#define PORTA (*((volatile unsigned char *)0x22))
#define DDRA  (*((volatile unsigned char *)0x21))
#define PINA  (*((volatile unsigned char *)0x20))
#define PORTC (*((volatile unsigned char *)0x28))
#define DDRC  (*((volatile unsigned char *)0x27))
#define PINC  (*((volatile unsigned char *)0x26))

#define EIMSK (*((volatile unsigned char *)0x3D))
#define SREG  (*((volatile unsigned char *)0x5F))
#define EICRA (*((volatile unsigned char *)0x69))

#define TCCR0A (*((volatile unsigned char *)0x44))
#define TCCR0B (*((volatile unsigned char *)0x45))
#define TCNT0 (*((volatile unsigned char *)0x46))
#define OCR0A (*((volatile unsigned char *)0x47))
#define OCR0B (*((volatile unsigned char *)0x48))
#define TIMSK0 (*((volatile unsigned char *)0x6E))
#define TIFR0 (*((volatile unsigned char *)0x35))

#define  UBRR0H (*((volatile unsigned char *)0xC5))
#define  UBRR0L (*((volatile unsigned char *)0xc4))
#define  UBRR1H (*((volatile unsigned char *)0xCD))
#define  UBRR1L (*((volatile unsigned char *)0xCC))
#define  UBRR2H (*((volatile unsigned char *)0xD5))
#define  UBRR2L (*((volatile unsigned char *)0xD4))
#define  UBRR3H (*((volatile unsigned char *)0x135))
#define  UBRR3L (*((volatile unsigned char *)0x134))

#define  UDR0 (*((volatile unsigned char *)0xC6))
#define  UDR1 (*((volatile unsigned char *)0xCE))
#define  UDR2 (*((volatile unsigned char *)0xD6))
#define  UDR3 (*((volatile unsigned char *)0x136))

#define  UCSR0A (*((volatile unsigned char *)0xC0))
#define  UCSR0B (*((volatile unsigned char *)0xc1))
#define  UCSR0C (*((volatile unsigned char *)0xC2))

#define  UCSR1A (*((volatile unsigned char *)0xC8))
#define  UCSR1B (*((volatile unsigned char *)0xC9))
#define  UCSR1C (*((volatile unsigned char *)0xCA))

#define  UCSR2A (*((volatile unsigned char *)0xD0))
#define  UCSR2B (*((volatile unsigned char *)0xD1))
#define  UCSR2C (*((volatile unsigned char *)0xD2))

#define  UCSR3A (*((volatile unsigned char *)0x130))
#define  UCSR3B (*((volatile unsigned char *)0x131))
#define  UCSR3C (*((volatile unsigned char *)0x132))

#define ADMUX  (*((volatile unsigned char *)0x7C))
#define ADCSRB  (*((volatile unsigned char *)0x7B))
#define ADCSRA  (*((volatile unsigned char *)0x7A))
#define ADCH  (*((volatile unsigned char *)0x79))
#define ADCL  (*((volatile unsigned char *)0x78))
#define ADC   (*((volatile unsigned int *)0x78))

//#define DIDR0  (*((volatile unsigned char *)0x7E))
// #define DIDR2 (*((volatile unsigned char *)0x7D))

#define INT7 7
#define INT6 6
#define INT5 5
#define INT4 4
#define INT3 3
#define INT2 2
#define INT1 1
#define INT0 0
#define ISC3 6
#define ISC2 4
#define ISC1 2
#define ISC0 0
#define ISC7 6
#define ISC6 4
#define ISC5 2
#define ISC4 0

#define COM0A1   7
#define COM0A0 6
#define COM0B1 5
#define COM0B0 4
#define WGM01 1
#define WGM00 0

#define FOC0A 7
#define FOC0B  6
#define WGM02  3
#define CS02  2
#define CS01  1
#define CS00  0

#define OCIE0B  2
#define OCIE0A  1
#define TOIE0  0

#define RXC   7
#define TXC   6
#define UDRE  5
#define FE   4
#define DOR   3
#define UPE  2
#define U2X  1
#define MPCM 0

#define RXCIE 7
#define TXCIE 6
#define UDRIE 5
#define RXEN  4
#define TXEN  3
#define UCSZ2 2
#define RXB8  1
#define TXB8  0

#define UMSEL1 7
#define UMSEL0 6
#define UPM1  5
#define UPM0  4
#define USBS  3
#define UCSZ1  2
#define UCSZ0  1
#define UCPOL 0

//ADMUX . ADC Multiplexer Selection Register
#define REFS1 7  //REFS1:0: Reference Selection Bits Voltage Reference Selection(1)
#define REFS0 6
#define ADLAR 5 //ADC Left Adjust Result 전압값 변화는 것을 측정
#define MUX4 4
#define MUX3 3
#define MUX2 2
#define MUX1 1
#define MUX0 0
 
//ADCSRB . ADC Control and Status Register B
#define ACME  6
#define MUX5 3
#define ADTS2 2
#define ADTS1 1
#define ADTS0 0

//ADCSRA . ADC Control and Status Register A
#define ADEN 7
#define ADSC 6
#define ADATE 5
#define ADIF 4
#define ADIE 3
#define ADPS2 2
#define ADPS1 1
#define ADPS0 0

// ADCH The ADC Data Register
#define ADC9 1
#define ADC8 0

// ADCL
#define ADC7 7
#define ADC6 6
#define ADC5 5
#define ADC4 4
#define ADC3 3
#define ADC2 2
#define ADC1 1
#define ADC0 0


//DIDR0 . Digital Input Disable Register 0(0x7E)
//#define ADC7D 7
//#define ADC6D 6
//#define ADC5D 5
//#define ADC4D 4
//#define ADC3D 3
//#define ADC2D 2
//#define ADC1D 1
//#define ADC0D 0

//DIDR2 . Digital Input Disable Register 2
//#define ADC15D 7
//#define ADC14D 6
//#define ADC13D 5
//#define ADC12D 4
//#define ADC11D 3
//#define ADC10D 2
//#define ADC9D 1
//#define ADC8D 0

#define sei()     __asm__ __volatile__ ("sei" ::)//어셈블리
#define sleep()  __asm__ __volatile__ ( "sleep" "\n\t" :: )//

 

void Init(void);
void Port_Init(void);
void INT_Init(void);

void __vector_1(void) __attribute__((signal, used, externally_visible));
void __vector_2(void) __attribute__((signal, used, externally_visible));
void __vector_3(void) __attribute__((signal, used, externally_visible));
void __vector_21(void) __attribute__((signal, used, externally_visible));

 

#endif //__MAIN_H__

 

 

#ifndef __ADC_H__
#define __ADC_H__
#include "main.h"


unsigned int ADC_Run0(void);
void ADC_Init(void);


#endif //__ADC_H__

#include "ADC.h"

void ADC_Init(void)
{
 ADMUX = (0<<REFS1)|(0<<REFS0)|(0<< ADLAR)|(0<< MUX4)|(0<< MUX3)|(0<< MUX2) |(0<<MUX1)|(0<< MUX0); 
 ADCSRA = (1<< ADEN)|(0<<ADSC)|(0<< ADATE)|(0<< ADIF)|(0<< ADIE)|(1<< ADPS2)|(1<<ADPS1)|(0<< ADPS0);
 ADCSRB = (0<<ACME)|(0<< MUX5)|(0<< ADTS2)|(0<< ADTS1)|(0<< ADTS0);
 //ADCH = (0<<ADC9)|(0<<ADC8);
 //ADCL = (0<<ADC7)|(0<<  ADC6)|(0<< ADC5)|(0<< ADC4)|(0<< ADC3)|(0<< ADC2)|(0<<  ADC1)

unsigned int ADC_Run0(void) 
{
 //ADC_Init();
 ADCSRA = ADCSRA|(1<<ADSC); //?
 while(0!=(ADCSRA&(1<<ADSC))); // ??
 return ADC; 
 
  
}

 

 

 

 

728x90