Pracuję nad robotem Bioloid i mam do zaprogramowania komunikację pomiędzy Zig-100 a mikrokontrolerem CM-5.
Połączenie zostało nawiązane, ale niestety program ktróry powinien odsyłać echem nadawą liczbę - odsyła inne liczby.
Np. wysyłam 1, a echem przychodzi 241.
Komunikaty wysyłam i odbieram w Docklight.
Gdzieś mam błąd... Każda pomoc się przyda.
Poniżej zamieszczam kod programu:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <util/delay.h>
//#define F_CPU 8000000UL // zegar w Hz
#define F_CPU 16000000UL // 16MHz
//#define FOSC 1843200
#define RS_BAUD 56700
#define RS_UBRR F_CPU / 16 / RS_BAUD - 1
void uart_init(uint16_t ubrr){
PORTD &= ~0x80; //PORT_LINK_PLUGIN = 0; // no pull up
PORTD &= ~0x20; //PORT_ENABLE_RXD_LINK_PC = 0;
PORTD |= 0x40; //PORT_ENABLE_RXD_LINK_ZIGBEE = 1;
UCSR1A = 0b01000010;//
UCSR1B = 0b10011000;//
UCSR1C = 0b00000110;//
UBRR1H = (unsigned char)((RS_UBRR & 0xFF00) >> 8);
UBRR1L = (unsigned char)(RS_UBRR & 0x00FF);
UDR1 = 0xFF;}
void uart_putc(uint8_t data){
while (!(UCSR1A & (1 << UDRE1)));
UDR1 = data;}
uint8_t uart_ischar(){
return (UCSR1A & (1 << RXC1));}
uint8_t uart_getc(){
while(!uart_ischar());
return UDR1;}
int main(void){
uint8_t c;
uart_init(RS_UBRR);
while(1) {
c = uart_getc();
_delay_ms(500);
uart_putc(c); }}
Połączenie zostało nawiązane, ale niestety program ktróry powinien odsyłać echem nadawą liczbę - odsyła inne liczby.
Np. wysyłam 1, a echem przychodzi 241.
Komunikaty wysyłam i odbieram w Docklight.
Gdzieś mam błąd... Każda pomoc się przyda.
Poniżej zamieszczam kod programu:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <util/delay.h>
//#define F_CPU 8000000UL // zegar w Hz
#define F_CPU 16000000UL // 16MHz
//#define FOSC 1843200
#define RS_BAUD 56700
#define RS_UBRR F_CPU / 16 / RS_BAUD - 1
void uart_init(uint16_t ubrr){
PORTD &= ~0x80; //PORT_LINK_PLUGIN = 0; // no pull up
PORTD &= ~0x20; //PORT_ENABLE_RXD_LINK_PC = 0;
PORTD |= 0x40; //PORT_ENABLE_RXD_LINK_ZIGBEE = 1;
UCSR1A = 0b01000010;//
UCSR1B = 0b10011000;//
UCSR1C = 0b00000110;//
UBRR1H = (unsigned char)((RS_UBRR & 0xFF00) >> 8);
UBRR1L = (unsigned char)(RS_UBRR & 0x00FF);
UDR1 = 0xFF;}
void uart_putc(uint8_t data){
while (!(UCSR1A & (1 << UDRE1)));
UDR1 = data;}
uint8_t uart_ischar(){
return (UCSR1A & (1 << RXC1));}
uint8_t uart_getc(){
while(!uart_ischar());
return UDR1;}
int main(void){
uint8_t c;
uart_init(RS_UBRR);
while(1) {
c = uart_getc();
_delay_ms(500);
uart_putc(c); }}