BASIC4MCU | 질문게시판 | ADC채널 3개를 이용한 센서 제어 문제 ㅠㅠ
페이지 정보
작성자 hiㅡO3O 작성일2020-11-27 03:45 조회7,368회 댓글4건본문
학교에서 이번학기동안 하는 프로젝트지만 너무 안되서 ㅠㅠ 죄송하지만 부탁드립니다.
#include <avr/io.h>
#include <avr/interrupt.h>
#include "ATmega128_v20m.H"
#define WATER_VALUE 800
#define CDS_VALUE 300
#define F_CPU 16000000UL
void show_cds(unsigned int value);
void show_water(unsigned int value);
unsigned int sum, water_result,cds_result;
int ADConvertor(char ch){
ADMUX = (ch & 0x1F) | 0x40;
Delay_ms(1);
ADCSRA = 0xC7;
Delay_ms(1);
while((ADCSRA & 0x10)!=0x10);
unsigned int resL = ADCL;
Delay_ms(1);
unsigned int resH = ADCH;
Delay_ms(1);
sum=resL+resH*256;
Delay_ms(1);
return(ADC);
}
int main(void)
{
MCU_initialize();
sei();
TCCR3A=0x83;
TCCR3B=0x0B;
TCCR3C=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ETIMSK=0x04;
unsigned int cds_result1 = 1;
unsigned int cds_result2 = 1;
while (1)
{
water_result = ADConvertor(0);
water_result = sum;
Delay_ms(100);
cds_result1 = ADConvertor(2);
cds_result1 = sum;
Delay_ms(100);
cds_result2 = ADConvertor(4);
cds_result2 = sum;
Delay_ms(100);
int SIG1 = water_result-WATER_VALUE;
if(cds_result1 >= cds_result2)
{
cds_result = cds_result2;
}
else
{
cds_result = cds_result1;
}
if(SIG1 >=0)
{
OCR3A =0x00FF;
}
else
{
OCR3A =0x00;
}
show_cds(cds_result);
show_water(water_result);
Delay_ms(200);
}
}
void show_water(unsigned int value)
{
//value = 800;
if(value >= WATER_VALUE)
PORTD |= 0b10000000;
else
PORTD &= 0b01111111;
}
void show_cds(unsigned int value1)
{
if (value1 <= CDS_VALUE)
PORTD |= 0b01000000;
else
PORTD &= 0b10111111;
}
///////////////////////////////////////////void MCU_initialize(void) 이건 헤더파일에 있는겁니다.
void MCU_initialize(void) /* initialize ATmege128 MCU */
{
MCUCR = 0x80; // Enable external memory and I/O, Disable SLEEP mode
XMCRA = 0x44; // 0x1100 - 0x7FFF (1 wait), 0x8000 - 0xFFFF (0 wait)
XMCRB = 0x80; // Enable bus keeper. Use PC0-PC7 as high byte of address bus
DDRB = 0xF0; // PORTB7..4 = output, PORTB3..0 = Input
PORTB = 0x00;
// DDRD = 0x00; // PORTD = Input
DDRD = 0xFC; // PORTD7..2 =Output, PORTD1..0 = Input
PORTD = 0x00;
// DDRE = 0x02; // PORTE1 = output, All other PORTs are Input
DDRE = 0x0A; // PORTE1, 3 = output, All other PORTs are Input
// PORTE = 0x00;
PORTE = 0xE0; // Pull up the PORTE7..5
DDRF = 0x00; // PORTF = Input
PORTF = 0x00;
DDRG = 0x1F;
LCD_CONTROL = 0x00;
LCD_DATABUS = 0x00;
}
CDS가 제어되면 워터가 제어가 안 되고 워터가 제어되면 CDS가 제어가 안 됩니다.
이때 제어가 안 되는 센서의 ADC 값을 보면 ADC 값이 0이나 1023 값을 가지고 있었습니다.
보드에 usb 전원으로만 실행 중인데 이것이 혹시 문제 될 수 있을까요.
바쁜 일을 하심에도 한번 코드를 읽고 해결법이나 조언 부탁드립니다.
감사합니다.
댓글 4
조회수 7,368master님의 댓글
master 작성일
cds 및 워터센서가 아두이노용 모듈이라면
디지털 출력이 아닌 아나로그 출력을 사용하세요
master님의 댓글
master 작성일
ADConvertor(4);
PF4는 JTAG 핀에 해당되므로
퓨즈비트에서 JTAG를 선택했다면 PF4를 다른 용도로 사용하면 안됩니다.
PF0,PF2,PF3 핀을 사용하세요
master님의 댓글
master 작성일
#include <avr/io.h>
#include <avr/interrupt.h>
#include "ATmega128_v20m.H"
//
#define F_CPU 16000000UL
#define WATER_VALUE 800
#define CDS_VALUE 300
//
int ADConvertor(char ch){
int i;
ADMUX=ch|0x40; Delay_ms(1);
i=ADC; // avrstudio4 인 경우에는 res=ADCW;
return(i);
}
//
void MCU_initialize(void){ /* initialize ATmege128 MCU */
MCUCR=0x80; // Enable external memory and I/O,Disable SLEEP mode
XMCRA=0x44; // 0x1100 - 0x7FFF(1 wait),0x8000 - 0xFFFF(0 wait)
XMCRB=0x80; // Enable bus keeper. Use PC0-PC7 as high byte of address bus
DDRB=0xF0; // PORTB7..4=output,PORTB3..0=Input
DDRD=0xFC; // PORTD7..2=Output,PORTD1..0=Input
DDRE=0x0A; // PORTE1,3=output,All other PORTs are Input
PORTE=0xE0; // Pull up the PORTE7..5
DDRG=0x1F;
LCD_CONTROL=0; LCD_DATABUS=0;
}
//
int main(void){
int water_result,cds_result,cds_result1=1,cds_result2=1,SIG1;
MCU_initialize();
ADCSRA=0xE7;
TCCR3A=0x83; TCCR3B=0x0B; ETIMSK=0x04;
sei();
while(1){
water_result=ADConvertor(0);
SIG1=water_result-WATER_VALUE;
if(SIG1>=0){ OCR3A=0xFF; } else{ OCR3A=0; }
if(water_result>=WATER_VALUE)PORTD|=0x80; else PORTD&=~0x80; // show_water
//
cds_result1 =ADConvertor(1);
cds_result2 =ADConvertor(2);
if(cds_result1>=cds_result2){ cds_result=cds_result2; }
else { cds_result=cds_result1; }
if(cds_result<=CDS_VALUE)PORTD|=0x40; else PORTD&=~0x40; // show_cds
//
Delay_ms(500);
}
}
hiㅡO3O님의 댓글
hiㅡO3O 작성일
정말 감사합니다 ADC 문제만 해도 맨땅에서 7일을 열심히 공부해서 여가까지 왔는데 안되었을 때 너무 슬펐습니다.
하지만 정말 좋은 조언과 해결책을 제공해주셔서 소스의 흐름을 잘 이해해서 공부하는 데에 도움이 되었습니다.
감사합니다. 좋은 날이 있기를 빕니다.
^ㅡ^