Infrared toy gun source code

[sourcecode language=”css”]
/*
Author: Lars Kristian Roland, APUS. http://lars.roland.bz/
License: CC Attribution-ShareAlike (CC BY-SA) http://creativecommons.org/licenses/
*/

/*
This is ‘draft code’. I’m not sure I’ll finish it, as it’s for a toy gun, and
I suddenly got all sorts of moral doubts about making toy guns. There were
various planned improvements before it would be finished. The code is for the
TI MSP430 Launchpad value line MCUs, such as msp430g2211. No guarantees, and use
at your own risk, totally…
*/

#include "msp430x22x4.h" // Not sure why this one works.
//#include "msp430g2211.h"

int IRRX_PIN = BIT3;
int BUZZER_PIN = BIT1;
int IROUT_PIN = BIT0;
int TRIGGER_PIN = BIT2;

static int pulse_port = 0;
static unsigned int before_pulse = 0;
static unsigned int pulse_start = 0;
static int pulse_end = 0;

static int signal[8];
static int signal_length[8];
static int decoded_ir = 0;
static int rx_ok = 0;
static int rx_count = 0;

static int dead = 0;

static int tx = 0;

void delay();
void handle_rx();
void handle_tx();
void tx_bit(int bit);
void pulse();
void buzz();
void buzz_long();
void buzz_shot();

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
DCOCTL = CALDCO_16MHZ;
BCSCTL1 = CALBC1_16MHZ;

TACTL = TASSEL_2 + MC_2 + ID0 + ID1;

P1DIR = IROUT_PIN | BUZZER_PIN | BIT6;
P1REN = TRIGGER_PIN;
P1OUT = TRIGGER_PIN;

_BIS_SR(GIE);

P1IES = IRRX_PIN;
P1IE = IRRX_PIN;

while (1) {
if (!(P1IN & TRIGGER_PIN) && dead == 0) {
buzz_shot();
handle_tx();
}
if (rx_ok) {
handle_rx();
}
}

}

void buzz()
{
for (int t = 0; t < 100; t++) {
P1OUT |= BUZZER_PIN;
for (int i = 0; i < 40; i++) { // 20
delay();
}
P1OUT &= ~BUZZER_PIN;
for (int i = 0; i < 40; i++) {
delay();
}
}
}

void buzz_long()
{
for (int t = 0; t < 10; t++) {
buzz();
for (int i = 0; i < 10000; i++) {
delay();
}
}

}

void buzz_shot()
{
for (int t = 0; t < 10; t++) {
P1OUT |= BUZZER_PIN;
for (int i = 0; i < 400; i++) { // 20
delay();
}
P1OUT &= ~BUZZER_PIN;
for (int i = 0; i < 400; i++) {
delay();
}
}
}

void delay()
{
for (int i = 0; i < 36; i++) {
;
}
}

void handle_rx()
{
if (decoded_ir == 172) {
//P1OUT |= BIT6;
dead = 1;
buzz_long();
//P1OUT &= ~BIT6;
dead = 0;
}
else {
//P1OUT &= ~BIT6;
}
rx_ok = 0;
decoded_ir = 0;
rx_count = 0;
for (int i = 0; i < 8; i++) {
signal[i] = -1;
signal_length[i] = -1;
}
}

void handle_tx()
{
// This should obviously be more clever. For example reading a dip s
// switch and then sending a different code.
tx_bit(99);
tx_bit(0);
tx_bit(0);
tx_bit(1);
tx_bit(1);
tx_bit(0);
tx_bit(1);
tx_bit(0);
tx_bit(1);
tx_bit(0);
tx_bit(0);
tx_bit(0);
tx_bit(0);
for (int i = 0; i < 1000; i++) {
delay();
}
}

void tx_bit(int bit)
{
if (bit == 99) {
for (int i = 0; i < 60; i++) { // 21
pulse();
}
for (int i = 0; i < 40; i++) {
delay();
}
}
else if (bit == 0){
for (int i = 0; i < 20; i++) { // 5
pulse();
}
for (int i = 0; i < 40; i++) {
delay();
}
}
else if (bit == 1){
for (int i = 0; i < 40; i++) { // 11
pulse();
}
for (int i = 0; i < 40; i++) {
delay();
}
}
}

void pulse()
{
P1OUT |= IROUT_PIN;
delay();
P1OUT &= ~IROUT_PIN;
delay();
}

#pragma vector=PORT1_VECTOR
__interrupt void port_1 (void)
{
if (P1IFG & IRRX_PIN && rx_ok == 0) {
if (pulse_start == 0) {
pulse_start = TAR;
P1OUT |= BIT6;
P1IES = 0;
}
else if (pulse_end == 0) {
pulse_end = TAR;
P1OUT &= ~BIT6;
// Check for pulse length
unsigned int pulse_length = pulse_end – pulse_start;
if (pulse_length > 4000) {
rx_count= 0;
}
else if (rx_count < 8 && pulse_length < 2500 && pulse_length > 1000) {
signal[rx_count] = 0;
signal_length[rx_count++] = pulse_length;
}
else if (rx_count < 8 && pulse_length < 3800 && pulse_length > 2500) {
decoded_ir |= (1 << rx_count);
signal[rx_count] = 1;
signal_length[rx_count++] = pulse_length;
}
else if (rx_count < 8 ) {
signal[rx_count] = -1;
signal_length[rx_count++] = pulse_length;
}
else {
rx_ok = 1;
}
pulse_start = 0;
pulse_end = 0;
P1IES = IRRX_PIN;
}

}

P1IFG = 0;
}

[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *