on my way for making this project I am trying to simulate and test my code (the part on hardware) in proteu8 professional.
in the the last post I was told that using Serial.write()
in the ISR part would be a good idea but I didn't know what should I put inside the Serial.write()
command. it came to my mind that I can put ADCH there but I kept getting garbage value in the visual terminal in that case so I put the code this way:
void setup()
{
Serial.begin(115200);
ADCSRA = 0; // clear ADCSRA register
ADCSRB = 0; // clear ADCSRB register
ADMUX |= (0 & 0x07); // set A0 analog input pin
ADMUX |= (1 << REFS0) | (1 << REFS1); // set reference voltage
ADMUX |= (1 << ADLAR); // left align ADC value to 8 bits from ADCH register
// sampling rate is [ADC clock] / [prescaler] / [conversion clock cycles]
// for Arduino Uno ADC clock is 16 MHz and a conversion takes 13 clock cycles
//ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // 32 prescaler for 38.5 KHz
ADCSRA |= (1 << ADPS2); // 16 prescaler for 76.9 KHz
//ADCSRA |= (1 << ADPS1) | (1 << ADPS0); // 8 prescaler for 153.8 KHz
ADCSRA |= (1 << ADATE); // enable auto trigger
ADCSRA |= (1 << ADIE); // enable interrupts when measurement complete
ADCSRA |= (1 << ADEN); // enable ADC
ADCSRA |= (1 << ADSC); // start ADC measurements
}
ISR(ADC_vect)
{
byte x = ADCH; // read 8 bit value from ADC
}
void loop()
{
Serial.println(ADCH);
}
and in the proteus8 I got all zeros in serial monitor.
however in 1Mb/s baud I got some strange values but they were nonsense too I guess.
so my questions are:
1-how can I use Serial.write()
in the ISR?
2-how should I change both my code or my simulation to get sure that at least I'm okay on the hardware part?
P.S: I have just done a hardware test. with my arduino, a 5v adapter and a voltage divider (divide by two). and here are my results so far:
Have I written the factor right? (a=5/255) or should it be (5/1023)?
and does anyone have any idea why the voltage I'm getting is a bit low?