Genau das versuche ich gerade heraus zu finden.
Leider ist das ganze UART Thema für mich auch Neuland.
- - - Aktualisiert - - -
Ich hab schön langsam eine Ahnung, ich glaub ich hab mehr RAM verbraucht als der ATMEL hat und
sobald ich dann irgendwo im RAM schreibe wo ich nicht sein sollte macht es boom
Wenn ich nämlich wie jetzt hier den buffer auf 512 setze geht es noch bei 1024 gibt es undefiniertes verhalten.
Und das Datenblatt bestätigt das wohl gerade
– 1KByte Internal SRAM
daran dachte ich wohl nicht.
Klingt das für euch Logisch?
Code:
#define MCU atmega8
#define F_CPU 2000000UL
//-U lfuse:w:0xe2:m -U hfuse:w:0xd9:m
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#define BT_EN_REGISTER DDRC
#define BT_EN_PORT PORTC
#define BT_EN_PIN PC0
#define LED_REGISTER DDRC
#define LED_PORT PORTC
#define LED_RED_PIN PC5
#define LED_GREEN_PIN PC4
#define LED_BLUE_PIN PC3
// BAUDRATE
#define BAUD_PRESCALE (((F_CPU / (9600 * 16UL))) - 1) /* Define prescale value */
//Data from App
long pl_valid_time_sec = 0;
void USART_SendString(char *str){
int i=0;
while (str[i]!=0){
UDR = str[i]; /* Write data to be transmitting in UDR */
while (!(UCSRA & (1<<UDRE))); /* Wait until data transmit and buffer get empty */
i++;
}
}
void get_BTData(){
// INIT USART
UCSRB |= (1 << RXEN) | (1 << TXEN); /* Enable USART transmitter and receiver */
UCSRC |= (1 << URSEL)| (1 << UCSZ0) | (1 << UCSZ1); /* Write USCRC for 8 bit data and 1 stop bit */
UBRRL = BAUD_PRESCALE; /* Load UBRRL with lower 8 bit of prescale value */
UBRRH = (BAUD_PRESCALE >> 8); /* Load UBRRH with upper 8 bit of prescale value */
LED_PORT |= (1<<LED_BLUE_PIN);
int i=0;
uint8_t c = '\0';
uint8_t uart_buffer[1024];
while( i < 512){
while (!(UCSRA & (1<<RXC))){}
c = UDR;
if ( c == '\n' ){ break; }
uart_buffer[i] = c;
i++;
}
uart_buffer[i]='\0';
USART_SendString("TEST");
USART_SendString(uart_buffer);
char spl_buffer[12];
spl_buffer[0] = '1'; spl_buffer[1] = '2'; spl_buffer[1] = '\0';
pl_valid_time_sec = atol(spl_buffer);
_delay_ms(5000);
LED_PORT &= ~(1<<LED_BLUE_PIN);
}
int main(void){
// Direction Registers
DDRB = 0x00; // B as input
DDRC = 0x00; // D as Input
DDRD = 0x00; // D as Input
// Set Output Registers
BT_EN_REGISTER |= (1 << BT_EN_PIN); // Bluetooth PWR Controll
LED_REGISTER |= (1 << LED_RED_PIN); // LED as output
LED_REGISTER |= (1 << LED_GREEN_PIN); // LED as output
LED_REGISTER |= (1 << LED_BLUE_PIN); // LED as output
// Ports to LOW (GND no Pullup)
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
for (int i=0; i < 4; i++){
LED_PORT ^= (1 << LED_GREEN_PIN);
_delay_ms(500);
}
get_BTData(); // INIT MODE
while(1){
LED_PORT ^= (1 << LED_RED_PIN);
_delay_ms(500);
}
}
Lesezeichen