Advertisement
britneybeatey

shift register

Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3.  
  4. #define DS_PORT PORTC
  5. #define DS_PIN 8
  6. #define ST_CP_PORT PORTC
  7. #define ST_CP_PIN 9
  8. #define SH_CP_PORT PORTC
  9. #define SH_CP_PIN 10
  10.  
  11. #define DS_low() DS_PORT&=~_BV(DS_PIN)
  12. #define DS_high() DS_PORT|=_BV(DS_PIN)
  13. #define ST_CP_low() ST_CP_PORT&=~_BV(ST_CP_PIN)
  14. #define ST_CP_high() ST_CP_PORT|=_BV(ST_CP_PIN)
  15. #define SH_CP_low() SH_CP_PORT&=~_BV(SH_CP_PIN)
  16. #define SH_CP_high() SH_CP_PORT|=_BV(SH_CP_PIN)
  17.  
  18. //Define functions
  19. //===============================================
  20. void ioinit(void);
  21. void output_led_state(unsigned int __led_state);
  22. //===============================================
  23.  
  24. int main (void)
  25. {
  26. ioinit(); //Setup IO pins and defaults
  27.  
  28. while(1)
  29. {
  30. // Output a knight rider pattern
  31.  
  32. for (int i=15;i>=0;i--)
  33. {
  34. output_led_state(_BV(i));
  35. _delay_ms(50);
  36. }
  37. for (int i=1;i<15;i++)
  38. {
  39. output_led_state(_BV(i));
  40. _delay_ms(50);
  41. }
  42. }
  43. }
  44.  
  45.  
  46. void ioinit (void)
  47. {
  48. DDRC = 0b00000111; //1 = output, 0 = input
  49. PORTC = 0b00000000;
  50. }
  51.  
  52. void output_led_state(unsigned int __led_state)
  53. {
  54. SH_CP_low();
  55. ST_CP_low();
  56. for (int i=0;i<16;i++)
  57. {
  58. if ((_BV(i) & __led_state) == _BV(i)) //bit_is_set doesn’t work on unsigned int so we do this instead
  59. DS_high();
  60. else
  61. DS_low();
  62.  
  63.  
  64. SH_CP_high();
  65. SH_CP_low();
  66. }
  67. ST_CP_high();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement