Hello friends, as the title says, I am working with an attiny85 microcontroller, with which I want to convert an analog signal to digital, and the data obtained is transmitted by serial communication. The adc converter works correctly, what I cannot do is the transmission of the data already obtained by serial and I have tried in many ways and I can not. I am trying to receive the value of serial communication with an arduino uno. Somebody could help me? The code I have for the microcontroller is the following:
#include<avr/io.h>
#include<SoftSerial.h>
#include <TinyPinChange.h>
SoftSerial mySerial(2, 3); // RX PB3, TX PB4
void adc_setup()
{
ADCSRA|=(1<<ADEN); //Enable ADC module
ADMUX=0x01; // configuring PB2 to take input
ADCSRB=0x00; //Configuring free running mode
ADCSRA|=(1<<ADSC)|(1<<ADATE); //Start ADC conversion and enabling Auto trigger
mySerial.begin(9600);
}
int main()
{
adc_setup();
while(1)
{
int adc_l=ADCL; //value of Input Voltage in lower register
int adc_val=(ADCH<<8)|adc_l; //Reading ADCH and coimbining the data
mySerial.write(adc_val);
delay(200);
ADCSRA|=(1<<ADIF);
}
}
I will be very grateful for any of your comments, greetings!
You didn't say exactly how it is failing, but: "write()" only writes a single byte (in "raw binary" format.) You probably want to either write the bytes individually, or use print()/println()
I’m not sure how well the Arduino functions like delay() and software serial work if you use your own main().
You could try calling init() early to see if things improve.
I am sure how well delay() will work: It won't. It will delay forever because the timer it's looking at to see if the delay is done will never increase because init() was not called to set it up.
Also, besides that you'll get totally borked results from the ADC with settings like that! You've left the prescaler at 000 - prescale of 2, so it's trying to run at half the system clock. I don't know what clock speed you're running it at (if you haven't done "burn bootloader" to set clock speed, then it's running at 1 MHz no matter what speed you want it to run at - classic AVR speed is set only on "burn bootloader") - more likely you want 8MHz or 16 MHz, both of which are possible without a crystal on the t85.)
However, the ADC clock must be between 100kHz and 200kHz in order to get accurate readings. for 1 MHz system clock that means prescale of 8 (011) , 8 MHz means prescale of 64 (110), and 16 means you need prescale of 128 (111). Currently you're running the ADC at 2.5, 20, or 40 times it's maximum clock speed, and as a result your ADC readings will be garbage.
Is there a reason you feel a need to override main?
Looking at the code, I'm sort of guessing that the OP started with the traditional setup() and loop(), where he had the claimed success with the ADC code, and then tried to dispense with the "Arduino" layer.
It is only on trying to work close to the bare metal that it becomes clear how much is got "free" with the Arduino core.
Maybe the OP can solve this by a bit of reverse engineering of wiring.c and Arduino's own main() function.