void setup()
{
DDRD = 0xFF; // all bits of PORTD are outputs
PORTD = 0xFF; // all bits of PORTD are one
DDRB = 0x0F; // 4 low bits of PORTB are outputs
PORTB = 0x0F; // 4 low bits of PORTB are one
}
void loop()
{
uint16_t val = 0; // variable to store the value read
uint16_t analogPin = A0; //perform A/D conversion on AD0
uint16_t average = 0;
Serial.begin(9600); //automatically uses D0 and D1
while(1)
{
delay(500);
val = analogRead(analogPin); // read the input pin
average = GetAverage();
Serial.print("Raw Value = ");
Serial.print(val);
Serial.print(" ");
Serial.print("Average = ");
Serial.print(average);
Serial.print("\n");
uint16_t GetAverage()
{
uint32_t new1 = 0;
uint32_t analogPin = A0; //perform A/D conversion on AD0
uint32_t total = 0;
uint32_t average = 0;
for (int x = 1; x <= 300; x++)
{
new1 = analogRead(analogPin);
total = total + new1;
}
average = (total / 300);
total = 0;
return average;
}
On the Arduino board I connect an analog input to ADC0 (pin 23) and I print the correct value between 0 and 1023.
When I put the same chip on my custom PCB I always print the value 1023.
On both boards I see the analog voltage on pin 23(ADC0).