Hello,
I am trying to get value of ADC on to the termial of mac or cool term i belive i have used the right code to set up everying thing. But i am not sure why the code wont work?
here is the code . I am using atmega32u4 AStar LV board and pololu isp programmer 1300. i want to receive the the value of ADC to my terminal or coolterm
Any help would be welcome
// avr-gcc polulu.c usb_serial.c -mmcu=atmega32u4 -Os -DF_cpu=16000000UL -std=gnu99 -o polulu.o
//avr-objcopy -j .text -j .data -O ihex polulu.o polulu.hex
//avrdude -c stk500v2 -P /dev/tty.usbmodem00148991 -p atmega32u4 -U flash:w:polulu.hex:i
// tty.usbmodem00148991
// tty.usbmodem00148993
// screen /dev/tty.usbmodem00148991 9600 to see the data from the mcu on terminal
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "usb_serial.h"
#define F_CPU 16000000UL
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
void usart_transmit(unsigned char data);
void USART_putstring(char* StringPtr);
unsigned char uart_recieve (void);
void usart_init(void);
void send_debug_string(char* string);
void init_hardware(void);
void setup_adc(void);
uint8_t read_channel();
int main(void){
init_hardware();
setup_adc();
usart_init();
uint8_t i=0;
uint8_t value =0 ;
unsigned char a;
char buffer[10];
while(1){
value = read_channel();// reading channel 0
USART_putstring("Reading channel ");
usart_transmit('0' + i); //This is a nifty trick when we only want to send a number between 0 and 9
USART_putstring(" : "); //Just to keep things pretty
//Read one ADC channel
itoa(value, buffer, 10); //Convert the read value to an ascii string
USART_putstring(buffer); //Send the converted value to the terminal
USART_putstring(" "); //Some more formatting
return (0);
}
void setup_adc(void){
ADMUX = (0b01 << REFS0) + (1 << ADLAR ) + (0b0000 << MUX0);
ADCSRA = (1 << ADEN ) + (0 << ADATE) + (0 << ADIF) + (0 << ADIE) + (1 << ADPS2) + (1 << ADPS1) + (0 << ADPS0);
}
uint8_t read_channel(){
ADMUX |= (0b0000 << MUX0);
ADCSRA |= (1 << ADSC);
while( ((ADCSRA >> ADSC) & 1) != 0 ){;}
uint8_t result = ADCH;
return result;
}
void init_hardware(void){
DDRC |= (1 << 7);/* make the LED pin an output */
DDRB &= 0b00000000;
DDRD &= 0b00000000;
DDRF &= 0b00000000;
}
void usart_init(void){
/* Set baud rate */
UBRR1H = (unsigned char)(BAUD_PRESCALLER>>8);
UBRR1L = (unsigned char)BAUD_PRESCALLER;
/* Enable receiver and tranmitter */
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
/* Set frame format: 8data, 1stop bit */
UCSR1C |= (1<<USBS1)|(3<<UCSZ10);
}
void usart_transmit(unsigned char data){
/* Wait for empty transmit buffer */
while ( !( UCSR1A & (1<<UDRE1)) )
;
/* Put data into buffer, sends the data */
UDR1 = data;
}
unsigned char uart_recieve (void)
{
while(!(UCSR1A) & (1<<RXC1)); // wait while data is being received
return UDR1; // return 8-bit data
}
void USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){
usart_transmit(*StringPtr);
StringPtr++;}
}