Hello there !
I am working on an external testcase (not using arduino IDE and arduino core) using avr core and avr dude on arduino nano atmega328p, i am facing a problem in which i cannot read the value of the joystick module from ADC0 (also tried ADC1), the value of ADC0 is always 1023 when combining both ADCL and ADCH on a uint16_t.
Here is the UART result when the joystick is in neutral position:
1010
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
...
Here is my connection:
- Arduino AREF -> 5v.
- POT-GND -> Arduino GND.
- POT-VCC -> Arduino 5v.
- POT-SIGNAL -> Arduino ADC0.
Here is my code:
#include<Serial.h>
void Serial::UART::startProtocol() {
UCSR0B = (1 << TXEN0) | (1 << RXEN0); // TXEN_BIT = 1, enables the transmitter buffer register.
UCSR0C = (1 << USBS0) | (3 << UCSZ00); // enables the UCSZ0, UCSZ1 and URSEL
UBRR0 = 0x10; // 0x10 (16) for BR = 57600 // 0x33 (51) for 9600
}
void Serial::UART::stopProtocol() {
UCSR0B = 0x00;
}
uint8_t Serial::UART::read() {
while (!(UCSR0A & (1 << RXC0)));
return UDR0;
}
void Serial::UART::cprint(char& data) {
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = data;
}
void Serial::UART::cprintln(char& data) {
cprint(data);
sprint(NEW_LINE_CARRIAGE_R);
}
void Serial::UART::sprint(char* data) {
int i = 0;
while (i < strlen(data)) {
cprint(data[i++]);
}
}
void Serial::UART::sprintln(char* data) {
sprint(data);
sprint(NEW_LINE_CARRIAGE_R);
}
void Serial::UART::print(const uint64_t& data, const uint8_t& base) {
char* strBuffer;
if (base == BIN_RADIX) {
strBuffer = (char*) calloc(1, CHAR_OF_BIN);
} else if (base == DEC_RADIX) {
strBuffer = (char*) calloc(1, CHAR_OF_DEC);
} else {
return;
}
// convert input to string
itoa(data, strBuffer, base);
int i = 0;
while (i < strlen(strBuffer)) {
cprint(strBuffer[i++]);
}
free(strBuffer);
}
void Serial::UART::println(const uint64_t& data, const uint8_t& base) {
print(data, base);
sprint(NEW_LINE_CARRIAGE_R);
}
#include<Analog.h>
void Analog::Adc::startProtocol() {
// setup ADCSRA
ADCSRA = (1 << ADEN) /*enable adc protocol*/ | (1 << ADIE) /*enable interrupt service*/
| (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) /*set clock prescaler to clk/128*/;
}
void Analog::Adc::stopProtocol() {
ADCH = 0x00;
ADCL = 0x00;
ADCSRA = 0x00;
}
void Analog::Adc::startConversion(const uint8_t& PIN) {
// setup ADMUX
ADMUX = 0b00000000 | PIN;
ADCSRA |= (1 << ADSC); // the last step: start conversion
}
uint16_t Analog::Adc::analogRead() {
volatile uint8_t adcl = ADCL;
volatile uint8_t adch = ADCH;
return ((0x00 | adch) << 8) | adcl; // concatenate the 2 (8-bit registers) in a 16-bit software register
}
#define F_CPU 16000000UL
#include<avr/io.h>
#include<util/delay.h>
#include<string.h>
#include <avr/interrupt.h>
#include<Serial.h>
#include<Analog.h>
Serial::UART* uart = Serial::UART::getInstance();
Analog::Adc* adc = Analog::Adc::getInstance();
ISR(ADC_vect){
uart->println(adc->analogRead(), 10);
adc->startConversion(0b0000);
_delay_ms(500);
}
int main(void) {
uart->startProtocol();
sei();
adc->startProtocol();
adc->startConversion(0b0000);
while (1);
return 0;
}
I hope i am not doing a silly mistake as usual
.
Thank you !

