Society of Robots - Robot Forum
Software => Software => Topic started by: jzaun 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:
#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:
#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.
-
I'd give you a hand if I could remember off-hand, but rather than doing stuff from scratch, I use WebbotLib + Project Designer (http://webbot.org.uk/iPoint/30.page) 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 would love to but I'm on a mac and there are issued with webbotlib and the designer.
-
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 (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.
-
Thanks Admin.
I sent you a PM but got an error so not sure if it went through.
-
I would love to but I'm on a mac and there are issued with webbotlib and the designer.
Like what?