ATtiny85 serial communication and PWM

Hello, this is my first time using the forum for help, sorry if something is unclear or wrong :slight_smile:

Specs:

  • 8 MHz
  • The Library used is ATTinyCore ( Spence Konde - Dr. Azzy)
  • Communicating through FTDI cable

I'm trying to program an ATtiny85, which can send data from my PC (Arduino Terminal), and depending on the data output a specific PWM (duty cycle). I can get the serial communication to work nicely without setting a duty cycle, and vice versa. The problem I face now is that whenever I try to implement both, I can get it to work somewhat. I can read a correct data the first time I send. But whenever I try to renew the data, then I get unknown data in my terminal. But by the fifth time, it reads a correct value ( it varies). It seems likely something with the interrupt or something, because whenever it reads unknown data, the duty cycle goes to zero.

#include <SoftwareSerial.h>
const int Rx = 2; // this is physical pin 7
const int Tx = 0; // this is physical pin 5
SoftwareSerial myserial(Rx, Tx); // set the serial connection
char rc = 0;
int flag = 0;

void PWM_config()
{
DDRB = (1<<PB1);                      // set PB1 as output to enable PWM generation
TCCR0A=0x00;                          //Normal mode
TCCR0A|=(1<<WGM00)|(1<<COM0B1);       // Clear OC0B on compare Match when up-counting. Set OC0B on compare Match when down-counting  ( COM0B1 is set to PB1, if i change it to COM0A1, then pb0. )
TCCR0B=0x00;
TCCR0B |= (1<<CS00);                  //prescaling with 1
TCNT0=0;
}

void setup() {                // This executes once
  PWM_config();
  Serial.begin(9600);             // Initialize serial port
  myserial.begin(9600);
  pinMode(Rx, INPUT);
  pinMode(Tx, OUTPUT);
   pinMode(1, OUTPUT);        // Prepare output pin
}

void loop() {                 // This loops continuously
 
    if (myserial.available() > 0) {           // Checks if anything written to serial
        rc =  myserial.read();            // Read said data into the variable "rc"
        Serial.println(rc);
        if (rc == 49) {                               // char 1 = int 49
           OCR0B = 100;
        }
        else if (rc == 50) {                     // // char 2 = int 50
          OCR0B = 200;
        }
        
    }

Picture of the terminal values can be seen below:
image

I hope if any of you can help and I appreciate you for using your time looking through this:)

You shouldn't be doing the pinMode() for rx and tx in setup(). That's taken care of in the softwareserial 'instantiation'.

Or maybe that's a peculiar attiny85 thing ?

I just followed the documentation on the arduino website SoftwareSerial Library - Arduino
The pinMode is used there

You are in fact using two different softwareserial implementations at the same time.
One time a separate softwareserial library and one time the built in softwareserial from Attinycore, which is called "Serial".
Keep in mind the Attiny85 has no HW serial support.

I see that there. However, I've used SoftwareSerial many times and never done that.
Oh well.

Ohh okay, I will test it on Monday and write back here if that was the problem. I'm unfortunately not home this weekend...

I will try and see if that works on mine too:)

That did the trick, thanks alot guys!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.