Created
April 19, 2012 06:29
-
-
Save Wollw/2419114 to your computer and use it in GitHub Desktop.
ATMega328P 74HC595 Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <avr/io.h> | |
| #include <util/delay.h> | |
| #include <stdlib.h> | |
| #include <stdint.h> | |
| typedef uint8_t bits_type; | |
| #define CFG_SHIFT_DDR DDRB | |
| #define CFG_SHIFT_PORT PORTB | |
| #define CFG_SHIFT_SRCLK PB1 | |
| #define CFG_SHIFT_RCLK PB2 | |
| #define CFG_SHIFT_SER PB3 | |
| #define CFG_SHIFT_REGISTER_PIN_COUNT 8 | |
| void shift_bits_init(void) { | |
| CFG_SHIFT_DDR |= _BV(CFG_SHIFT_RCLK) | |
| | _BV(CFG_SHIFT_SRCLK) | |
| | _BV(CFG_SHIFT_SER); | |
| } | |
| void shift_bits_out(bits_type b, size_t len) { | |
| CFG_SHIFT_PORT &= ~_BV(CFG_SHIFT_RCLK); | |
| for (size_t i = 0; i < CFG_SHIFT_REGISTER_PIN_COUNT; i++) { | |
| CFG_SHIFT_PORT &= ~_BV(CFG_SHIFT_SRCLK); | |
| if (i < len && (b & ((bits_type)1 << i))) | |
| CFG_SHIFT_PORT |= _BV(CFG_SHIFT_SER); | |
| CFG_SHIFT_PORT |= _BV(CFG_SHIFT_SRCLK); | |
| CFG_SHIFT_PORT &= ~_BV(CFG_SHIFT_SER); | |
| } | |
| CFG_SHIFT_PORT |= _BV(CFG_SHIFT_RCLK); | |
| } | |
| int main(void){ | |
| shift_bits_init(); | |
| while(1){ | |
| shift_bits_out(0x00,8); | |
| _delay_ms(500); | |
| shift_bits_out(0xff,8); | |
| _delay_ms(500); | |
| } | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment