go away spammer

Author Topic: Axon2 - first steps at MCU programming. Uart question  (Read 2367 times)

0 Members and 1 Guest are viewing this topic.

Offline jzaunTopic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Axon2 - first steps at MCU programming. Uart question
« on: March 29, 2012, 02:40:46 PM »
Hi all.

I got my toolchain all setup and can compile and load a program onto my AxonII. I've managed to get a led to blink so I'm happy so far. the next thing I'm trying to do is get some output back to my computer over the usb port. I'm trying to code up the uart to do this but am running into problems. I'm not getting anything sent, not even garbage.

If someone would take a moment to look over my code and tell me what I'm doing wrong I would greatly appreciate it. I'm sure I'm doing a lot wrong :)


uart.c:
Code: [Select]
#include <inttypes.h>
#include <avr/io.h>

void uartInit(uint8_t uart, uint32_t baud)
{
    switch (uart) {
        case 0:
            // Set baud rate
            UBRR0H = (uint8_t)(((F_CPU/(baud<<4))-1)>>8);
            UBRR0L = (uint8_t)((F_CPU/(baud<<4))-1);
            // Set frame format to 8 data bits, no parity, 1 stop bit
            UCSR0C = (3<<UCSZ00);
            // Enable receiver and transmitter
            UCSR0B = (1<<RXEN0)|(1<<TXEN0);
            break;
           
        case 1:
            // Set baud rate
            UBRR1H = (uint8_t)(((F_CPU/(baud<<4))-1)>>8);
            UBRR1L = (uint8_t)((F_CPU/(baud<<4))-1);
            // Set frame format to 8 data bits, no parity, 1 stop bit
            UCSR1C = (3<<UCSZ00);
            // Enable receiver and transmitter
            UCSR1B = (1<<RXEN1)|(1<<TXEN1);
            break;
           
        case 2:
            // Set baud rate
            UBRR2H = (uint8_t)(((F_CPU/(baud<<4))-1)>>8);
            UBRR2L = (uint8_t)((F_CPU/(baud<<4))-1);
            // Set frame format to 8 data bits, no parity, 1 stop bit
            UCSR2C = (3<<UCSZ00);
            // Enable receiver and transmitter
            UCSR2B = (1<<RXEN2)|(1<<TXEN2);
            break;
           
        case 3:
            // Set baud rate
            UBRR3H = (uint8_t)(((F_CPU/(baud<<4))-1)>>8);
            UBRR3L = (uint8_t)((F_CPU/(baud<<4))-1);
            // Set frame format to 8 data bits, no parity, 1 stop bit
            UCSR3C = (3<<UCSZ00);
            // Enable receiver and transmitter
            UCSR3B = (1<<RXEN3)|(1<<TXEN3);
            break;

        default:
            break;
    }
}

void uartSendByte(uint8_t uart, uint8_t u8Data)
{
    switch (uart) {
        case 0:
            // Wait if a byte is being transmitted
            while((UCSR0A&(1<<UDRE0)) == 0);
            // Transmit data
            UDR0 = u8Data;
            break;
           
        case 1:
            // Wait if a byte is being transmitted
            while((UCSR1A&(1<<UDRE1)) == 0);
            // Transmit data
            UDR1 = u8Data;
            break;
           
        case 2:
            // Wait if a byte is being transmitted
            while((UCSR2A&(1<<UDRE2)) == 0);
            // Transmit data
            UDR2 = u8Data;
            break;
           
        case 3:
            // Wait if a byte is being transmitted
            while((UCSR3A&(1<<UDRE3)) == 0);
            // Transmit data
            UDR3 = u8Data;
            break;
           
        default:
            break;
    }
}

uint8_t uartRecvByte(uint8_t uart)
{
    switch (uart) {
        case 0:
            // Wait until a byte has been received
            while((UCSR0A&(1<<RXC0)) == 0);
            // Return received data
            return UDR0;
            break;
           
        case 1:
            // Wait until a byte has been received
            while((UCSR1A&(1<<RXC1)) == 0);
            // Return received data
            return UDR1;
            break;
           
        case 2:
            // Wait until a byte has been received
            while((UCSR2A&(1<<RXC2)) == 0);
            // Return received data
            return UDR2;
            break;
           
        case 3:
            // Wait until a byte has been received
            while((UCSR3A&(1<<RXC3)) == 0);
            // Return received data
            return UDR3;
            break;
           
        default:
            break;
    }
    return 0;
}


main.c:
Code: [Select]
#include <avr/io.h>
#include <avr/interrupt.h>
#include "board.h"
#include "uart.h"

int main(void) {
initBoard();

    // Setup the clock
    cli();                          // Disable global interrupts
    TCCR1B |= 1<<CS11 | 1<<CS10;    // Divide by 64
    OCR1A = 15624;                  // Count 15624 cycles for 1 second interrupt
    TCCR1B |= 1<<WGM12;             // Put Timer/Counter1 in CTC mode
    TIMSK1 |= 1<<OCIE1A;            // Enable timer compare interrupt
    sei();                          // Enable global interrupts
   
   
    uint8_t uart = 1;
    uartInit(uart, 9600);
    uartSendByte(uart, '.');
    char data = 0;
    while(1)
    {
        data = uartRecvByte(uart);
        uartSendByte(uart, '[');
        uartSendByte(uart, data);
        uartSendByte(uart, ']');
    }
}


// Interrupt Service Routine
ISR(TIMER1_COMPA_vect)
{
    PORTC ^= (1<<3);
}

I'm really just looking to get a echo back of what I send. I know I'm not using interrupts for the uart and its not buffered or anything fancy, but I have to start someplace :)

I had an dumb error in my code. This seems to be working now. Still any comments or suggestions for a beginner.
« Last Edit: March 29, 2012, 05:23:40 PM by jzaun »

Offline Gertlex

  • Supreme Robot
  • *****
  • Posts: 763
  • Helpful? 24
  • Nuclear Engineer ยท Roboticist
    • Index of Oddities
Re: Axon2 - first steps at MCU programming. Uart question
« Reply #1 on: March 29, 2012, 05:47:22 PM »
I'd give you a hand if I could remember off-hand, but rather than doing stuff from scratch, I use WebbotLib + Project Designer to generate my projects (and it includes examples for whatever hardware I'm including), and then I do the coding from there.

This is just a recommendation; Some folks on here will certainly be able to help if you want to go the scratch route :)
I

Offline jzaunTopic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: Axon2 - first steps at MCU programming. Uart question
« Reply #2 on: March 29, 2012, 09:50:30 PM »
I would love to but I'm on a mac and there are issued with webbotlib and the designer.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Axon2 - first steps at MCU programming. Uart question
« Reply #3 on: March 29, 2012, 11:36:18 PM »
Can you tell us more about the mac problem you are having? It should work . . .


My old Axon code didn't use WebbotLib:
http://www.societyofrobots.com/axon/downloads/Axon_Source_102009.zip

It'll work just fine with the Axon II, but some of the digital I/O changed so you'll need to update the utils file.

You can also copy/paste the UART code from it.

Offline jzaunTopic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: Axon2 - first steps at MCU programming. Uart question
« Reply #4 on: March 30, 2012, 12:33:16 AM »
Thanks Admin.

I sent you a PM but got an error so not sure if it went through.


Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Axon2 - first steps at MCU programming. Uart question
« Reply #5 on: March 31, 2012, 09:26:05 AM »
I would love to but I'm on a mac and there are issued with webbotlib and the designer.

Like what?
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list