-
Content Count
43 -
Joined
-
Last visited
Reputation Activity
-
Theshadowwalker91 reacted to RobG in shift register
For 24 bits, you need to use long, so how about this (untested):
//*************************************************************************************** // MSP430 Driver for 74HC595 Shift Register // // Description; Drives 8 LED's with 3 digital pins of the MSP430, via a shift register // // MSP430x2xx // //*************************************************************************************** #include //Define our pins #define DATA BIT0 // DS -> 1.0 #define CLOCK BIT4 // SH_CP -> 1.4 #define LATCH BIT5 // ST_CP -> 1.5 #define ENABLE BIT6 // OE -> 1.6 // The OE pin can be tied directly to ground, but controlling // it from the MCU lets you turn off the entire array without // zeroing the register // Declare functions void delay ( unsigned int ); void pulseClock ( void ); void shiftOut ( unsigned char ); void enable ( void ); void disable ( void ); void init ( void ); void pinWrite ( unsigned int, unsigned char ); int main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; P1DIR |= (DATA + CLOCK + LATCH + ENABLE); // Setup pins as outputs enable(); // Enable output (pull OE low) int i; //Do a "ping-pong" effect back and forth for(;{ for ( i = 0 ; i < 24 ; i++ ){ shiftOut(1 << i); delay(50); } for ( i = 23 ; i >= 0 ; i-- ){ shiftOut(1 << i); delay(50); } } } // Delays by the specified Milliseconds // thanks to: // http://www.threadabort.com/archive/2010/09/05/msp430-delay-function-like-the-arduino.aspx void delay(unsigned int ms) { while (ms--) { __delay_cycles(1000); // set for 16Mhz change it to 1000 for 1 Mhz } } // Writes a value to the specified bitmask/pin. Use built in defines // when calling this, as the shiftOut() function does. // All nonzero values are treated as "high" and zero is "low" void pinWrite( unsigned int bit, unsigned long val ) { if (val){ P1OUT |= bit; } else { P1OUT &= ~bit; } } // Pulse the clock pin void pulseClock( void ) { P1OUT |= CLOCK; P1OUT ^= CLOCK; } // Take the given 8-bit value and shift it out, LSB to MSB void shiftOut(unsigned long val) { //Set latch to low (should be already) P1OUT &= ~LATCH; char i; // Iterate over each bit, set data pin, and pulse the clock to send it // to the shift register for (i = 0; i < 24; i++) { pinWrite(DATA, (val & (1 << i))); pulseClock(); } // Pulse the latch pin to write the values into the storage register P1OUT |= LATCH; P1OUT &= ~LATCH; } // These functions are just a shortcut to turn on and off the array of // LED's when you have the enable pin tied to the MCU. Entirely optional. void enable( void ) { P1OUT &= ~ENABLE; } void disable( void ) { P1OUT |= ENABLE; }
-
Theshadowwalker91 reacted to oPossum in Software async serial tx/rx without timer
unsigned state, c, n, addr, fgr, fgg, fgb, bgr, bgg, bgb, mkr, mkg, mkb; for(state = 0; { c = getc(); if(c == '<') { state = 1; } else if(c == '>') { if(state == 11) { // End of packet - do something } state = 0; } else if(c >= '0' && c <= '9') { n = c - '0'; if(state > 1 && n > 1) state = 0; switch(state) { case 1: ++state; addr = n; break; case 2: ++state; fgr = n; break; case 3: ++state; fgg = n; break; case 4: ++state; fgb = n; break; case 5: ++state; bgr = n; break; case 6: ++state; bgg = n; break; case 7: ++state; bgb = n; break; case 8: ++state; mkr = n; break; case 9: ++state; mkg = n; break; case 10: ++state; mkb = n; break; default: break; } } else { state = 0; } }
-
Theshadowwalker91 reacted to EngIP in LED Flash Rate Change
Tidied it up, now it just flashes P1.0, the rate of flash doubling with a short press of S1, halving with a long press - tested on a fresh out of the box LP and G2231 - HTH
#include long test, a=0, delay=2000, delay1=2000, half; void main(void) { P1DIR |= BIT0; //Set P1.0 as an output P1OUT &= ~BIT0; //Set P1.0 low WDTCTL = WDTPW + WDTHOLD; // Hold the WDT (WatchDog Timer) while(1){ //loop forever delay1--; //decrement counter half = (delay/2); //set value of half to be delay/2 if (delay1<=half){ //if counter value is less than P1OUT &= ~BIT0; } else { P1OUT |= BIT0; } if (delay1<=0){ delay1=delay; } if (P1IN & BIT3){ //Rudimentary debounce software also measures test=a; //duration button pressed in program cycles a=0; //and alters delay value accordingly. } if (!(P1IN & BIT3)){ a++; } if (test>=500){ delay=(delay*2); delay1=delay; test = 0; } if ((test>=100)&&(test<500)){ delay=(delay/2); delay1=delay; test = 0; } } }
-
Theshadowwalker91 reacted to SirZusa in Brake Signal Light
try something like:
// when P1.1 LOW AND P1.2 HIGH then ... if(!(P2IN & BIT1) && (P2IN & BIT2)) { ... } // OR if((P2IN & ~BIT1) && (P2IN & BIT2)) { ... }
should both do the same
-
Theshadowwalker91 reacted to HylianSavior in Change led with button
Whoops!
What the code needs is an extra while loop to wait when the button is let go again.
Try this:
#define LED0 BIT0 #define LED1 BIT1 #define LED4 BIT4 #define LED5 BIT5 #define LED6 BIT6 #define LED7 BIT7 #define BUTTON BIT2 void main (void) { WDTCTL = WDTPW | WDTHOLD; P1DIR = LED0 | LED1 | LED4| LED5 | LED6 | LED7; //Sets all the LEDs to output P1OUT = LED0 | LED1; //Turns LED0 and LED1 on. for(; //Infinite loop { while((P1IN & BUTTON) == 0) {} //Waste time while the button ISN'T pressed P1OUT ^= LED1|LED0|LED4|LED5; //"Flip" the outputs of LED0, LED1, LED4, and LED5 while((P1IN & BUTTON) != 0) {} while((P1IN & BUTTON) == 0) {} P1OUT ^= LED4|LED5|LED6|LED7; while((P1IN & BUTTON) != 0) {} while((P1IN & BUTTON) == 0) {} P1OUT ^= LED6|LED7|LED0|LED1; while((P1IN & BUTTON) != 0) {} } }