uwezi

20200125_tiny85_neopixel

Jan 25th, 2020
121
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * 20200125_tiny85_neopixel.c
  3.  *
  4.  * Created: 2020-01-25 17:13:13
  5.  * Author : uwezi
  6.  */
  7.  
  8. #define F_CPU 8000000UL
  9.  
  10. #include <avr/io.h>
  11. #include <util/delay.h>
  12. #include <util/atomic.h>
  13.  
  14. #define T0H 0.1
  15. #define T0L 0.9
  16. #define T1H 0.7
  17. #define T1L 0.4
  18.  
  19. #define WSPORT PORTB
  20. #define WSDDR  DDRB
  21. #define WSPIN  PB3
  22.  
  23. void send_byte(uint8_t outbyte)
  24. {
  25.   uint8_t old_port, port_h, port_l;
  26.   ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  27.   {
  28.     old_port = WSPORT;
  29.     port_h   = old_port |  (1 << WSPIN);
  30.     port_l   = old_port & ~(1 << WSPIN);
  31.     uint8_t i;
  32.     for (i=0; i<8; i++)
  33.     {
  34.       if (outbyte & 0b10000000)
  35.       {
  36.         WSPORT = port_h; // here we set the pin to 1
  37.         _delay_us(T1H);
  38.         WSPORT = port_l;
  39.         _delay_us(T1L);
  40.       }
  41.       else
  42.       {
  43.         WSPORT = port_h;
  44.         _delay_us(T0H);
  45.         WSPORT = port_l;
  46.         _delay_us(T0L);
  47.       }
  48.       outbyte = outbyte << 1;
  49.     }
  50.     WSPORT = old_port;
  51.   }
  52. }
  53.  
  54.  
  55. int main(void)
  56. {
  57.   uint8_t i, j;
  58.   WSDDR |= (1 << WSPIN);
  59.   i = 0;
  60.  
  61.   while (1)
  62.   {
  63.     for (j=0; j<3*3; j++)
  64.     {
  65.       send_byte(i);
  66.     }
  67.     i++;
  68.     _delay_ms(10);    
  69.   }
  70. }
RAW Paste Data