ADC0, ADC1을 이용하여 LED0 LED1을 ON,OFF
(tifoda wrote the code below▼)
ADC0,ADC1_Two LED ON,OFF Each.txt
I hard for mecant check your functions. If you not sure - do simple test program to test them. Just while(1) and move your servo, print on lcd etc. Errors( warnings) i see is 1 - setting whole port when you need 1 pin only. Like PORTC = 0x20. You can forget something and set/reset pins you dont need to. Use PORTC |= 0x20 and PORTC &= ~(1<<0x20). 2 - this will not work 100% :) for(i=0; i<10; i++) { PORTE = 0b00000001; //Forward Rotation _delay_ms(2000); //2 sec + 10 times = 20sec } 어렵다. to chekc 네 function을 If 네가 확실치않으면 - do 간단히 테스트해라. 3 - COUNTA = ADC/20; //since the resolution (2.56/2^10 = 0.0025) is 2.5mV there will be an increment of 4 for every 10mV input, that means for every degree raise there will be increment of 4 in digital value. So to get the temperature we have to divide ADC output by four. But you have AVCC undefined as reference. So you have your input voltage as reference, but not internal 2.56 4 - PORTA = 0b00001111; //Fan OFF, Heater ON } else { PORTA = 0b11110000; //Fan ON, Heater OFF } Do you power FAN with 4 pins? Bad idea. Use 1 pin and external transistor or relay I advise you to test all functions separetly( if you not sure they are working good) and then combine.
ADC0 ADC1 (각각 온도, 가스)
Low Low : Heater ON
Low High : Heater ON + Servo 90º Fan ON
High Low : Fan ON
High High : Fan ON + Servo 90º
기존 회로상에서...
Low Low : Servo 0 PB00000000 + PA00001111(Heater ON)
Low High : Servo 90 PB11111111 + PA00001111(Heater ON)
High Low : Servo 0 PB00000000 + PA11110000 (Fan ON)
High High : Servo 90 PB11111111 + PA11110000 (Fan ON)
===========================================================
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
//========================
// Turn OFF M103C fuse !!!
//========================
// Output pins setup, can be any pins of any port.
#define OUTPUT_PORT PORTC
#define OUTPUT_DDR DDRC
#define OUTPUT_PIN0 0
#define OUTPUT_PIN1 1
#define OUTPUT_PIN5 5
#define THRESHOLD0 127 // output is HIGH when adc value is greater than threshold. Max value is 255.
#define THRESHOLD1 127 // actually atmega has 10bit ADC, but least significant bits are always thrash. So we use only most significant 8 bits.
// ADC voltage reference ( uncomment ONE only)
//#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (1<<ADLAR)) // AREF
#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (1<<ADLAR)) // AVCC
//#define ADC_VREF_TYPE ((1<<REFS1) | (1<<REFS0) | (1<<ADLAR)) // internal 2.56V
uint8_t read_adc(uint8_t);
void some_delay(uint16_t);
int main(void)
{
OUTPUT_DDR |= (1<<OUTPUT_PIN0) | (1<<OUTPUT_PIN1); // setup outputs
DDRC = 0xFF; //For Servo
DDRA = 0xFF;
DDRB = 0xFF;
ACSR=0x80; // turn off analog comparator
// ADC initialization
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADFR) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0);
SFIOR=(0<<ACME);
while (1)
{
if(read_adc(1) > THRESHOLD0) // read adc pin0 and
{
//OUTPUT_PORT |= (1<<OUTPUT_PIN0); // either turn ON led 0
for(int i=0; i<100; i++)
{
PORTC = 0x20; //0010 0000
// PORTC = 0x40;
_delay_us(600); //Degree 0
PORTC = 0x00;
_delay_ms(23);
}
PORTB = 0b11111111; //Fan is ON
//http://www.engineersgarage.com/sites/default/files/imagecache/Original/wysiwyg_imageupload/28714/Servo%20Motor%20Timing%20Diagram.jpg
//Servo Angle Information
}
else
{
//OUTPUT_PORT &= ~(1<<OUTPUT_PIN0); // or turn it OFF
for(int i=0; i<100; i++)
{
PORTC = 0x20;
//PORTC = 0x40;
//_delay_us(2400); //Degree 180
_delay_us(1500);
PORTC = 0x00;
_delay_ms(23);
}
PORTB = 0b00000000; //Fan is OFF
}
//some_delay(65000);
if(read_adc(0) > THRESHOLD1) // same with pin1
{
//OUTPUT_PORT |= (1<<OUTPUT_PIN1);
PORTA = 0b11110000; //Fan is ON
}
else
{
//OUTPUT_PORT &= ~(1<<OUTPUT_PIN1);
PORTA = 0b00001111; //Heater is ON
}
//some_delay(65000);
}
}
// Read the 8 most significant bits
// of the AD conversion result
uint8_t read_adc(uint8_t adc_input)
{
// Set multiplexer
ADMUX = adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
some_delay(100);
// Start the AD conversion
ADCSRA |= (1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF)) == 0);
// Clear flag
ADCSRA |= (1<<ADIF);
return ADCH;
}
// Simple delay function
void some_delay(uint16_t delay)
{
uint16_t i;
for(i=0; i<delay; i++);
}
'졸업작품 > Multi ADC (AVR)' 카테고리의 다른 글
회로도 () (3) | 2016.11.15 |
---|