uwezi icon

20200125_tiny85_neopixel

uwezi | PRO | 01/25/20 03:27:49 PM UTC | 0 ⭐ | 585 👁️ | Never ⏰ | []
C |

1.16 KB

|

None

|

0 👍

/

0 👎

/*
 * 20200125_tiny85_neopixel.c
 *
 * Created: 2020-01-25 17:13:13
 * Author : uwezi
 */ 
 
#define F_CPU 8000000UL
 
#include <avr/io.h>
#include <util/delay.h>
#include <util/atomic.h>
 
#define T0H 0.1
#define T0L 0.9
#define T1H 0.7
#define T1L 0.4
 
#define WSPORT PORTB
#define WSDDR  DDRB
#define WSPIN  PB3
 
void send_byte(uint8_t outbyte)
{
  uint8_t old_port, port_h, port_l;
  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  {
    old_port = WSPORT;
    port_h   = old_port |  (1 << WSPIN);
    port_l   = old_port & ~(1 << WSPIN);
    uint8_t i;
    for (i=0; i<8; i++)
    {
      if (outbyte & 0b10000000)
      {
        WSPORT = port_h; // here we set the pin to 1
        _delay_us(T1H);
        WSPORT = port_l;
        _delay_us(T1L);
      }
      else
      {
        WSPORT = port_h;
        _delay_us(T0H);
        WSPORT = port_l;
        _delay_us(T0L);
      }
      outbyte = outbyte << 1;
    }
    WSPORT = old_port;
  }
}
 
 
int main(void)
{
  uint8_t i, j;
  WSDDR |= (1 << WSPIN);
  i = 0;
 
  while (1) 
  {
    for (j=0; j<3*3; j++)
    {
      send_byte(i);
    }
    i++;
    _delay_ms(10);    
  }
}

Comments