BASIC4MCU | 질문게시판 | QA6P 압력센서를 이용한 모터제어 질문
페이지 정보
작성자 IMAZ 작성일2024-03-17 14:45 조회1,208회 댓글2건본문
ADC기능을 이용하여 두 센서 모두 압력을 받으면 Flag가 세워지고
이 Flag를 받아 모터를 작동시키는 스위치가 작동하도록 코드를 만들어 보았습니다
압력센서가 없을 때는 나머지 기능들이 잘 작동하는데 압력센서를 이용하려고 ADC값을 바꾸고 해봤는데 잘 안되었습니다
QA6P 센서에 10K 저항을 달았습니다.
제 코드가 이상한지 한번 봐 주시면 감사하겠습니다.
#include <mega128.h>
#include <delay.h>
#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 1
unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x00
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
// Read the AD conversion result
adc_data[input_index]=ADCW;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
input_index=0;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
}
unsigned char flag = 0;
void check(void){
if( (adc_data[0]<500) && (adc_data[1]<500) ) {
flag = 1;
}
else {
flag = 0;
}
};
void main(void)
{
unsigned char FND[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x6F};
unsigned char sw;
unsigned char gear = 1;
PORTA=0xFF;
DDRA=0x00;
PORTB=0x00;
DDRB=0xFF;
PORTC=0x00;
DDRC=0xFF;
PORTD=0x00;
DDRD=0xFF;
PORTE=0x00;
DDRE=0x08;
PORTF=0x00;
DDRF=0x00;
TCCR3A=0x81;
TCCR3B=0x05;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCC;
// Global enable interrupts
#asm("sei")
while (1)
{
check();
sw = ~PINA;
if(gear>=5){
OCR3AL = 0xFF;
}
else{
OCR3AL = 0x3F;
}
if(flag == 1){
if(sw == 0x01){
PORTB.0 = 0x00;
PORTD.0 = 0x00;
PORTD.1 = 0x01;
gear++;
}
else{
PORTB.0 = 0x01;
PORTD.0 = 0x00;
PORTD.1 = 0x00;
gear--;
}
}
else if(flag == 0){
gear--;
PORTB.0 = 0x01;
PORTD.0 = 0x00;
PORTD.1 = 0x00;
}
if(sw == 0x02){
PORTB.1 = 0x01;
delay_ms(50);
PORTB.1 = 0x00;
delay_ms(50);
}
else{
PORTB.1 = 0x00;
}
if(sw == 0x04){
PORTB.2 = 0x01;
delay_ms(50);
PORTB.2 = 0x00;
delay_ms(50);
}
else{
PORTB.2 = 0x00;
}
if(sw == 0x08){
PORTD.2 = 0x01;
delay_ms(50);
PORTD.2 = 0x00;
delay_ms(50);
}
else{
PORTD.2 = 0x00;
}
if(gear>=9){
gear = 9;
}
else if(gear<=1){
gear = 1;
}
PORTC = FND[gear];
delay_ms(500);
}
}
댓글 2
조회수 1,208master님의 댓글
master 작성일
#include <mega128.h>
#include <delay.h>
//
void main(void){
char gear=1,FND[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int a0,a1;
//
PORTA=0xFF;
DDRB=0xFF; DDRC=0xFF; DDRD=0xFF; DDRE=0x08;
TCCR3A=0x81; TCCR3B=0x05;
ADCSRA=0xE7;
while(1){
ADMUX=0; delay_ms(1); a0=ADCW;
ADMUX=1; delay_ms(1); a1=ADCW;
//
if((a0<500) && (a1<500)){
if(PINA.1==0){ PORTB.0=0; PORTD.0=0; PORTD.1=1; gear++; }
else { PORTB.0=1; PORTD.0=0; PORTD.1=0; gear--; }
}
else{ PORTB.0=1; PORTD.0=0; PORTD.1=0; gear--; } // if(flag==0)
//
if(gear>=5)OCR3AL=0xFF; else OCR3AL=0x3F;
//
if(gear>=9){ gear=9; } else if(gear<=1){ gear=1; }
PORTC=FND[gear];
//-------------------------------------------------------------
if(PINA.1==0){ PORTB.1=1; delay_ms(50); PORTB.1=0; delay_ms(50); } else{ PORTB.1=0; }
if(PINA.2==0){ PORTB.2=1; delay_ms(50); PORTB.2=0; delay_ms(50); } else{ PORTB.2=0; }
if(PINA.3==0){ PORTD.2=1; delay_ms(50); PORTD.2=0; delay_ms(50); } else{ PORTD.2=0; }
//-------------------------------------------------------------
delay_ms(500);
}
}
IMAZ님의 댓글
IMAZ 작성일감사합니다