K
K
kagoyashi2022-03-28 19:52:16
Yii
kagoyashi, 2022-03-28 19:52:16

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;
  }
}


and simple arduino code for esp
#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);
}


why it works, mb they need to be synchronized somehow. there is also an assumption that they use different encodings, but then there should have been problems when debugging

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2016-07-20
@bitver

Yii::$app->request->referrer

K
kagoyashi, 2022-03-29
@kagoyashi

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");
      }
    }
       }
}

and arduino
#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);                                                               
}

looks terrible, but works until I have enough of this))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question