Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <msp430.h>
- #include <signal.h>
- #define IR_LED BIT6
- #define TRIGGER BIT3
- #define STATUS_LED BIT0
- static volatile int cycle_count; // IR carrier cycle count
- unsigned long time = 0; // current time, 1 click = 32.8ms
- unsigned long last_shot = 0; // time of last manual shot
- unsigned long interval = 100; // currently set interval
- unsigned long trigger_down_time = 0; // time button was depressed
- int trigger_down = 0;
- int interval_on = 0;
- int manual_shot = 0;
- #pragma vector = WDT_VECTOR
- __interrupt void wdt_isr(void)
- {
- time++;
- __bic_SR_register_on_exit(LPM1_bits);
- }
- #pragma vector = TIMERA0_VECTOR // Timer A Capture/Compare 0 interrupt
- __interrupt void timera0_isr(void) // This ISR must be very simple because there may
- // be as few as 16 cycles available for it to complete
- { //
- --cycle_count; // Decrement cycle count
- }
- #pragma vector = PORT1_VECTOR
- __interrupt void port1_isr(void)
- {
- if (P1IFG & TRIGGER)
- {
- P1IES ^= TRIGGER; // switch interrupt direction
- trigger_down = !trigger_down; // toggle state
- trigger_down_time = time;
- manual_shot = trigger_down;
- __bic_SR_register_on_exit(LPM1_bits);
- }
- P1IFG = 0; // clear p1 interrupts
- }
- static const int pentax[] = {
- 26, // Carrier period in us ( 1 MHz / 38 kHz ~= 26 )
- 8, // Number of pairs
- 500, 115, // 13 ms on, 3 ms off
- 38, 39, // 1 ms on, 1 ms off
- 38, 39, // 1 ms on, 1 ms off
- 38, 39, // 1 ms on, 1 ms off
- 38, 39, // 1 ms on, 1 ms off
- 38, 39, // 1 ms on, 1 ms off
- 38, 39, // 1 ms on, 1 ms off
- 38, 39 // 1 ms on, 1 ms off
- };
- static const int nikon[] = {
- 26,
- 4,
- 76, 1064,
- 15, 60,
- 15, 136,
- 15, 38
- };
- static const int canon[] = {
- 30,
- 2,
- 16, 231,
- 16, 33
- };
- void send_code(const int *code)
- {
- TACCR0 = *code++ - 1 ; // Set timer period
- TACCR1 = TACCR0 >> 1; // Set timer on duration - 50% duty cycle
- int pair = *code++; // Get pair count
- cycle_count = 5; // Setup some initial IR off lead in
- TACCTL0 = CCIE; // Enable Timer A CC0 interrupt
- do { // Do all pairs
- int cc = *code++; // Get on cycles
- while(cycle_count > 0); // Wait for off cycles to complete
- TACCTL1 = OUTMOD_7; // Turn on IR
- cycle_count += cc; // Set on cycle count
- cc = *code++; // Get off cycles
- while(cycle_count > 0); // Wait for on cycles to complete
- TACCTL1 = OUTMOD_0; // Turn off IR
- cycle_count += cc; // Set off cycle count
- } while(--pair); // Decrement pair count, do next pair if not zero
- while(cycle_count > 0); // Wait for off cycles to complete
- TACCTL0 = 0; // Turn off Timer A CC0 interrupt
- }
- void send_all_codes()
- {
- send_code(pentax);
- send_code(nikon);
- send_code(canon);
- }
- int main(void)
- {
- WDTCTL = WDTPW | WDTHOLD; // Disable watchdog reset
- DCOCTL = 0; // Run at 1 MHz
- BCSCTL1 = CALBC1_1MHZ; //
- DCOCTL = CALDCO_1MHZ; //
- P1DIR |= TRIGGER; // Set P1.3 to input
- P1IE |= TRIGGER; // P1.3 enabled interrupts
- P1IES |= TRIGGER; // P1.3 Hi/lo edge
- P1IFG &= ~TRIGGER; // P1.3 clear IFG
- TACTL = TASSEL_2 | MC_1; // Timer A config: SMCLK, count up
- TACCTL1 = OUTMOD_0; // Make sure IR is off
- P1OUT = IR_LED;
- P1DIR = STATUS_LED | IR_LED; // Enable PWM output on P1.6
- P1SEL = IR_LED;
- WDTCTL = WDTPW | WDTTMSEL | WDTCNTCL;
- IE1 |= WDTIE;
- eint(); // Enable global interrupts
- while(1) {
- if (interval_on && (time - last_shot >= interval)) // intervalometer
- {
- send_all_codes(); // Send Pentax IR code
- last_shot = time;
- }
- if (manual_shot) // manual triggering
- {
- manual_shot = 0;
- send_all_codes();
- interval = time - last_shot; // save interval
- interval = (interval > 15? interval: 15);
- last_shot = time;
- }
- if (trigger_down && (time - trigger_down_time) > 30)
- // hold button for 1 sec to switch modes
- {
- trigger_down_time = time;
- interval_on = !interval_on;
- if (interval_on)
- P1OUT ^= STATUS_LED;
- else
- P1OUT &= ~STATUS_LED;
- }
- __bis_SR_register(LPM1_bits + GIE);
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement