Hi,
The code below started out as an Arduino sketch I wrote as an exercise, teaching myself about and bits and bytes but turned into a "Knight Rider" effect with LED's.
I have become fascinated with how the 328p works and decided to try and replace the arduino stuff like "digital Write" and "analog Read" by using direct manipulation of the registers, and so with time, google and the data sheet I have begun to understand more and more and I am thoroughly enjoying it! I then decided to use the ADC and a pot to vary the speed, I thought it would be great to output the result of the ADC to the serial monitor. It is here that I have hit a sort of block, I think I am going to need to change the data type maybe with a command like "char" or something like that.
In the code I have put what I thought would work, simply putting the result from ADC0H into UDR0 with "UDR0 = ADCH;" but this is wrong, the line below it puts a character into the UDR0 register and that's ok so my setup is working of sorts. I am very new to coding and am plodding along at my own pace, but I can't seem to get my head around this for some reason, this whole thing is just for fun, and upto now have managed to think it through, but would appreciate some guidance here, as I don't get it!...cheers.
#define F_CPU 1000000L #include <avr/io.h> #include <util/delay.h> int Led_number; int Counter; int Delay = 15; bool Direction; void delay_ms( int ms ); int main(void) { DDRB = 0xff; //***************** ADC0 is used here and is set by default ******************* ADMUX |= 1 << REFS0; // This sets our reference to the 5v input rail ADMUX |= 1 << ADLAR; // This moves the result into the high byte of ADCH for 8 bit ADCSRA |= 1 << ADEN; // This Enables the ADC //********* This is setting up USART to send the potentiometer value serialy *** UBRR0 = 12; // this sets baud rate at 9600 UCSR0A = (1 << U2X0); // this doubles data rate in asynchronous mode UCSR0B = (1 << TXEN0); // This enables transmit UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // This sets character size to 8 bits while (1) { for ( Counter = 1; Counter <= 7; Counter ++) { if (Direction == 0) { PORTB = (1 << Led_number); // This shifts a 1 along the register ADCSRA |= 1 << ADSC; // This Starts Conversion delay_ms (Delay + ADCH); // This adds Result of Conversion (ADCH) and calls delay function, as "_delay_ms()" requires a constant Led_number = (Led_number + 1); } else { PORTB = (1 << Led_number); // This shifts a 1 along the register in the opposite direction ADCSRA |= 1 << ADSC; // This Starts Conversion delay_ms (Delay + ADCH); Led_number = (Led_number - 1); } UDR0 = ADCH; // This puts the pot value into UDR0 for transmission via serial // UDR0 = '#'; // This puts character '#' into register for transmission } Direction = ! Direction; } } void delay_ms( int ms ) // This is a delay function { for (int i = 0; i < ms; i++) { _delay_ms(1); } }