Answer the question
In order to leave comments, you need to log in
How to connect esp01 to atmega8?
I'm trying to connect esp01 to atmega via uart.
individually during debugging everything works, but together it doesn't. Sometimes it works crookedly and only when connecting a ttl converter and tracking one of the parties in the console.
atmega is programmed in c++
#define F_CPU 8000000
#define PORT_LED PORTB
#define DDR_LED DDRB
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
static int send(char c, FILE *stream)
{
if (c == '\n')
send('\r', stream);
while(!(UCSRA&(1<<UDRE))){};
UDR = c;
return 0;
}
void init()
{
DDR_LED = 0xFF;
PORT_LED = 0x00;
TCCR1A |= (1 << COM1A1 | 1 << COM1B1 | 1 << WGM10);
TCCR1B |= (1 << WGM12 | 1 << CS11);
TCCR2 |= (1 << WGM20 | 1 << WGM21 | 1 << COM21 | 1 << CS21);
TCNT1 = 0;
TCNT2 = 0;
OCR1A = 0;
OCR1B = 0;
OCR2 = 0;
DDRC = 0x00;
PORTC = 0xFF;
UCSRA = 0x00;
UCSRB = 0b00011000;
UCSRC = 0x86;
UBRRH = 0x00;
UBRRL = 51;
}
FILE * uart_str;
int main(void)
{
init();
unsigned char value_r = 255;
unsigned char value_g = 255;
unsigned char value_b = 255;
uart_str = fdevopen(send, NULL);
stdout = uart_str;
unsigned char* c[2];
while (1)
{
for (int i=0; i<2;i++)
*c[i] = 0;
int ii = 0;
if ((UCSRA&(1<<RXC)))
{
while (ii<2)
{
unsigned char x;
while (!(UCSRA&(1<<RXC)));
x=UDR;
*c[ii] = x;
ii++;
}
//расшифровка данных где первый символ это канал цвета, второй это шим
if (*c[0] == 'r')
value_r = *c[1];
else if (*c[0] == 'g')
value_g = *c[1];
else if (*c[0] == 'b')
value_b = *c[1];
}
//отладочная информация
if (ii!=0){
ii = 0;
printf("r%i\n", value_r);
// printf("g%i\n", value_g);
// printf("b%i\n", value_b);
}
OCR1A = value_r;
OCR1B = value_g;
OCR2 = value_b;
}
}
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(9600);
}
void loop() {
// delay(10000);
Serial.print('r');
Serial.println('\255');
Serial.print('g');
Serial.println('\255');
Serial.print('b');
Serial.println('\255');
Serial.print('r');
Serial.println('\0');
delay(1000);
Serial.print('r');
Serial.println('\255');
delay(1000);
Serial.print('g');
Serial.println('\0');
delay(1000);
Serial.print('g');
Serial.println('\255');
delay(1000);
Serial.print('b');
Serial.println('\0');
delay(1000);
Serial.print('b');
Serial.println('\255');
delay(1000);
}
Answer the question
In order to leave comments, you need to log in
it was long and painful, but I set everything up. a joke in the out of sync data transfer, which sometimes needs to be disposed of. bring new code
#define F_CPU 8000000
#define PORT_LED PORTB
#define DDR_LED DDRB
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
static int send(char c, FILE *stream)
{
if (c == '\n')
send('\r', stream);
while(!(UCSRA&(1<<UDRE))){};
UDR = c;
return 0;
}
void init()
{
DDR_LED = 0xFF;
PORT_LED = 0x00;
TCCR1A |= (1 << COM1A1 | 1 << COM1B1 | 1 << WGM10);
TCCR1B |= (1 << WGM12 | 1 << CS11);
TCCR2 |= (1 << WGM20 | 1 << WGM21 | 1 << COM21 | 1 << CS21);
TCNT1 = 0;
TCNT2 = 0;
OCR1A = 0;
OCR1B = 0;
OCR2 = 0;
DDRC = 0x00;
PORTC = 0xFF;
UCSRA = 0x00;
UCSRB = 0b00011000;
UCSRC = 0x86;
UBRRH = 0x00;
UBRRL = 51;
}
FILE * uart_str;
int main(void)
{
init();
unsigned char value_r = 255;
unsigned char value_g = 255;
unsigned char value_b = 255;
uart_str = fdevopen(send, NULL); //send , receive functions
stdout = uart_str;
while (1)
{
unsigned char c[5];
int ii = 0;
if ((UCSRA&(1<<RXC)))
{
while (ii<5)
{
unsigned char x;
while (!(UCSRA&(1<<RXC)));
x=UDR;
c[ii] = x;
ii++;
}
if (c[4]== '\n')
{
printf("0");
char t[3];
t[0] = c[1];
t[1] = c[2];
t[2] = c[3];
int b;
sscanf(t, "%d", &b);
if (c[0] == 'r')
value_r = b;
else if (c[0] == 'g')
value_g = b;
else if (c[0] == 'b')
value_b = b;
OCR1A = value_r;
OCR1B = value_g;
OCR2 = value_b;
}
else
{
printf("1");
char x;
for (int i=0; i< 30; i++)
{
if ((UCSRA&(1<<RXC)))
x=UDR;
//без след строки ничего не работает, полагаю, без вывода скорость становится слишкомм большой чтобы ждать новых данных, позже попробую делей
printf("c%c%c%c%c", c[0], c[1], c[2], c[3]);
}
printf("2");
}
}
}
}
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(9600);
}
void stop()
{
if (Serial.available())
{
int s = Serial.read();
if (s == 1){
Serial.write("\r", 1);
while (1)
{
if (Serial.available())
{
int s = Serial.read();
Serial.flush();
if (s == 2)
break;
}
}
}
}
}
void loop() {
Serial.write("r255\n", 5);
stop();
delay(100);
Serial.write("g255\n", 5);
stop();
delay(100);
Serial.write("b255\n", 5);
stop();
delay(100);
Serial.write("r000\n", 5);
stop();
delay(1000);
Serial.write("r255\n", 5);
stop();
delay(1000);
Serial.write("g000\n", 5);
stop();
delay(1000);
Serial.write("g255\n", 5);
stop();
delay(1000);
Serial.write("b000\n", 5);
stop();
delay(1000);
Serial.write("b255\n", 5);
stop();
delay(1000);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question