Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Atmega32u4 Astar LV Serial communication


#1

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++;}
    }

#2

Hello.

I briefly glanced at your code and noticed that you have a few errors (e.g. missing closing curly brace in main()). Can you elaborate what you mean by “not sure why the code wont work”? If you receive any error message when attempting to compile or run the code, can you post screenshots of the error message(s) here? Also, can you post pictures of your setup clearly showing the connections between the AVR programmer and the A-Star?

- Amanda


#3

I can complie it on my mac and upload it too. But the cool term window never recevies any data. It remains blank :frowning:

Thats how i have connected the Astar with the isp. Do i need to make any other connections?
Uploading…


#4

If you are trying to use the USB AVR programmer as a USB-to-serial adapter, you need to connect the programmer’s RX, TX, and GND to the A-Star’s TX, RX, and GND, respectively. Also, you should make sure that you are using the programmer’s TTL Serial Port in your serial terminal. To determine the TTL Serial Port, see the “Determining serial port names in Mac OS X” section in the Pololu USB AVR Programmer User’s Guide.

- Amanda