mahm150
Full Member level 1
- Joined
- Dec 14, 2010
- Messages
- 98
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,288
- Location
- Cairo, Egypt, Egypt
- Activity points
- 2,001
#include <avr/io.h>
#include <avr/delay.h>
#define ADC_VREF_TYPE 0x40
unsigned int read_adc(unsigned char adc_input)
{
ADMUX= ADC_VREF_TYPE | adc_input;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
_delay_us(100); // make this delay but not solve this problem
ADCSRA |=0x10;
//Tadc=(Tadc<< 8)| ADCL;
return ADCW;
}
main(){
unsigned int ADR;
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x82;
DDRD = 0xFF;
DDRC = 0;
do{
ADR = read_adc(1) >> 2;
PORTD = ADR;
}while(1);
}
Tadc=(Tadc<< 8)| ADCL;
#include <mega8.h>
#include <delay.h>
#include <stdio.h>
unsigned int Tadc;
unsigned char dummyH;
unsigned char dummyL;
#define relay1 PORTB.0
#define relay2 PORTD.7
#define ADC_VREF_TYPE 0x40
unsigned int read_adc(unsigned char adc_input){
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA.ADSC = 1;
// Wait for the AD conversion to complete
while (ADCSRA.ADIF==0);
ADCSRA.ADIF = 1;
dummyL = ADCL;
dummyH = ADCH;
//Tadc=(Tadc<< 8)| ADCL;
return ADCW;
}
void tst_value(){
if (Tadc<410){
relay1 =0;
relay2 =1;
TCCR1B=0x19;
OCR1AH=0x01;// 1
OCR1AL=0xff;// ff
PORTD |= (dummyL & 0x7c);
}
// if (615>Tadc && Tadc>410)
// { TCCR1B=0x00; }
if (Tadc>=460){
relay1 =1;
relay2 =0;
TCCR1B=0x19;
OCR1AH=0x00;// 1
OCR1AL=0xff;// ff
PORTD|=dummyL & 0x7f;
}
}
void main(void){
DDRC.0 = 0;
DDRD = 0xFF;
DDRB.0 = 1;
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x82;
while (1){
Tadc=read_adc(0);
tst_value();
}
}
Try it. There's a difference. Try it and then I'll show the difference.
ADCSRA.ADSC not define in codevision
---------- Post added at 19:06 ---------- Previous post was at 19:05 ----------
and ADCSRA.ADIF which code vision version you use
When ADCL is read, the ADC Data Register is not updated until ADCH is read. Consequently, if
the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read
ADCH. Otherwise, ADCL must be read first, then ADCH.
I was writing another program and thought that I'd explain after he'd tried out the program. Anyways, I showed the difference now. So, all's good.Why is it a secret?
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 // Read the AD conversion result unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <mega8.h> #include <delay.h> #define ADC_VREF_TYPE 0x00 // Read the AD conversion result unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } // Declare your global variables here void main(void) { // ADC initialization // ADC Clock frequency: 125,000 kHz // ADC Voltage Reference: AREF pin ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x86; while (1) { // Place your code here read_adc(0); delay_ms(1); }; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 void tst_value() { if (Tadc<128) { relay1 =0; relay2 =1; TCCR1B=0x19; OCR1AH=0x01;// 1 OCR1AL=0xff;// ff //PORTD |= (ADCL & 0x7c); } if (Tadc>=128) { relay1 =1; relay2 =0; TCCR1B=0x19; OCR1AH=0x00;// 1 OCR1AL=0xff;// ff //PORTD|=ADCL & 0x7f; } }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?