Hi all
I am trying to program the ATtiny 85 in the Arduino ide, and practice manually doing things through the registers.
The purpose of the code is to read the adc value, convert it to voltage and then if it is below a threshold, turn on an LED. The code works fine if I just read the lower 8 bits of left shifted adc data. When I change it to read both registers and try to combine the data, the LED just stays lit.
I can't see anything that would be wrong with the code so i'm asking to see if there is something i'm missing.
//Red LED on PB0
//Blue LED on PB1
//Pushbutton PB2
//Battery voltage divider PB3
//Mosfet control PB4
#include <avr/io.h>
#include <stdint.h>
#include <util/delay.h>
int mode = 0;
void setup() {
delay(10000);
DDRB = 0b0010011; //set PB0,PB1,PB4 as output, PB2,PB3 as input
PORTB = 0b00000011; //set PB0,PB1 HIGH so LEDs are off, other pins LOW
//ADMUX |= 0b00100011; //configure the ADC to be on PB3, and left shift for easy read at 8 bit resolution (this config works fine)
ADMUX |= 0b00000011; //configure the ADC to be on PB3
ADCSRA |= 0b00000110; //set clock division to 64
}
void loop() {
/*Lipo battery voltage measurement
batt voltage = (voltage divider voltage * (R1 + R2)) / R2
for 0v to 5v range, 10 bit resolution, gives 4.89mV/bit
R1 = 3.270k R2 = 1.975k*/
ADCSRA |= 0b11000000; //Enable the ADC and start measurement
delay(20); //wait for completion
uint8_t analogVL = ADCL; //Read lower portion of ADC result (ADCL must be read first)
uint8_t analogVH = ADCH; //Read upper portion of ADC result
uint16_t analogV = analogVL | (analogVH << 8); //combine values together. Shift upper portion left by 8, then add to lower portion
float battVoltage = (((analogVL * 0.00489) * 5245) / 1975); //calc lipo battery voltage, scaled for 10 bit value
//float battVoltage = (((analogVH * 0.0196) * 5245) / 1975); //calc lipo battery voltage, scaled for 8 bit value (this config works fine)
if (battVoltage < 10.2){
digitalWrite(0,LOW);
}
else{
digitalWrite(0,HIGH);
}
delay(1000);