Advertisement
uwezi

20200125_tiny85_16MHz_neopixel

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