2

I am using ATmega16 in my project. I want to get the value=1023 when I give 5V as input, and 0 when input = 0. The readADC function reads the particular channel of micro-controller ADC. The frequency of my clock is 4MHz. But on reading the input, I'm getting 255 as the maximum value instead of 1023. Someone who knows about avr programming can help !!!

My code:

#include <avr/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>

unsigned char ReadADC(unsigned char ch)
{
   ch = ch&0b00000111;
   ADMUX&=0xF8;
   ADMUX |= ch;
   ADCSRA |= (1<<ADSC);
   while(ADCSRA & (1<<ADSC);   //wait for conversion to complete
   return(ADC);
}

int main(void)
{
   SegDataDDR = 0xFF;
   SegCntrlDDR = 0xF3;
   SegCntrlPort = 0xF3;
   SegDataPort = 0x00;

ADMUX = (1<<REFS0) | (0<<REFS1);
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0);   // ADEN set to turn ADC on
// clock frequency divided by 32 for operable 125KHz frequency

while(1) 
{ 
   ADCSRA |= (1<<ADSC);    // start conversion
   unsigned char value = ReadADC(0);    // Reads the value of the ADC
}
4

2 回答 2

3

在您的代码中,该行

unsigned char value = ReadADC(0);

将您限制为仅 8 位。您需要将 的类型更改为value更大的类型,例如unsigned short,它可以保存 16 位的数据。如果您进行此更改,您还必须将定义更改ReadADC

unsigned short ReadADC(unsigned char ch)
于 2015-06-03T11:21:07.757 回答
-1

用 unit16_t 替换 unsigned char

于 2015-06-03T11:41:53.443 回答