| 1 |
80 |
olivier.gi |
#include "msp430x20x3.h"
|
| 2 |
|
|
#include <signal.h> // Needed for using interrupts with msp430-gcc
|
| 3 |
|
|
#include "spacewar.h"
|
| 4 |
|
|
|
| 5 |
|
|
//************************************************************
|
| 6 |
|
|
// externals
|
| 7 |
|
|
//
|
| 8 |
|
|
extern volatile unsigned char flags;
|
| 9 |
|
|
|
| 10 |
|
|
extern void reset_rkts(rkt_data *, rkt_data *);
|
| 11 |
|
|
extern void reset_game(rkt_data *);
|
| 12 |
|
|
|
| 13 |
|
|
//************************************************************
|
| 14 |
|
|
//
|
| 15 |
|
|
// init_hardware
|
| 16 |
|
|
//
|
| 17 |
|
|
// initalize all the MSP430 hardware before we start
|
| 18 |
|
|
//
|
| 19 |
|
|
/* Description:
|
| 20 |
|
|
Sets up all the hardware in the MSP430 used by the SPACEWAR game.
|
| 21 |
|
|
Stops the watchdog timer. Sets the internal cpu clock to a maximun.
|
| 22 |
|
|
Sets the timer to cause an interrupt every 10ms. Sets the SPI interface
|
| 23 |
|
|
to talk to the dual DAC. Sets the A to D to use an external reference
|
| 24 |
|
|
and unipolar operation. Finally initializes all the variable used by the game.
|
| 25 |
|
|
*/
|
| 26 |
|
|
void init_all(rkt_data *rkt1, rkt_data *rkt2)
|
| 27 |
|
|
{
|
| 28 |
|
|
|
| 29 |
|
|
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
|
| 30 |
|
|
|
| 31 |
|
|
BCSCTL1 = 0x08; // Setup DCO highest range
|
| 32 |
|
|
DCOCTL = 0xe0; // biggest DCO
|
| 33 |
|
|
|
| 34 |
|
|
P1OUT = 0x04; // TLV5618A CS high
|
| 35 |
|
|
P1DIR |= 0x01+0x04; // P1.0=LED, P1.2=TLV5618A_cs
|
| 36 |
|
|
P1SEL = 0x08; // P1.3 = VREF
|
| 37 |
|
|
|
| 38 |
84 |
olivier.gi |
//BCSCTL2 = 0x00; // SMCLK divider = 1
|
| 39 |
|
|
//BCSCTL2 = 0x02; // SMCLK divider = 2
|
| 40 |
|
|
//BCSCTL2 = 0x04; // SMCLK divider = 4
|
| 41 |
|
|
BCSCTL2 = 0x06; // SMCLK divider = 8
|
| 42 |
80 |
olivier.gi |
CCTL0 = CCIE; // CCR0 interrupt enabled
|
| 43 |
|
|
CCR0 = 23500;
|
| 44 |
|
|
TACTL = TASSEL_2 + MC_1; // SMCLK, upmode
|
| 45 |
|
|
_BIS_SR(GIE); // enable interrupts
|
| 46 |
|
|
|
| 47 |
|
|
// USICTL0 |= USIPE7+USIPE6+USIPE5+USIMST+USIOE; // Port, SPI master
|
| 48 |
|
|
// USICKCTL = USIDIV_0+USISSEL_2+USICKPL; // divide by 1 SMCLK, inactive high
|
| 49 |
|
|
// USICTL0 &= ~USISWRST; // release USI for operation
|
| 50 |
|
|
// USICTL1 = USICKPH; // take data on falling edge
|
| 51 |
|
|
|
| 52 |
|
|
// SD16CTL = SD16SSEL_1; // exter ref, SMCLK
|
| 53 |
|
|
// SD16CCTL0 = SD16UNI + SD16SNGL; // 256OSR, unipolar, inter
|
| 54 |
|
|
// SD16AE = 0; // P1.1 A4+, A4- = VSS
|
| 55 |
|
|
// // P1.4 A2+, A2- = VSS
|
| 56 |
|
|
// SD16INCTL0 = SD16INCH_4; // A4+/- start with rocket 1
|
| 57 |
|
|
// SD16CCTL0 |= SD16SC; // Start A2D conversion
|
| 58 |
|
|
|
| 59 |
|
|
reset_rkts(rkt1, rkt2); // reset rkt positons
|
| 60 |
|
|
reset_game(rkt1);
|
| 61 |
|
|
reset_game(rkt2);
|
| 62 |
|
|
rkt1->game = 0;
|
| 63 |
|
|
rkt2->game = 0;
|
| 64 |
|
|
flags |= time_tick; // force an update at startup
|
| 65 |
|
|
}
|
| 66 |
|
|
|
| 67 |
|
|
|
| 68 |
|
|
//************************************************************
|
| 69 |
|
|
//
|
| 70 |
|
|
// Timer A0 interrupt service routine
|
| 71 |
|
|
//
|
| 72 |
|
|
/* Description:
|
| 73 |
|
|
Interrupt service routine for the timer. The function sets a flag for the main
|
| 74 |
|
|
loop to update object positions.
|
| 75 |
|
|
*/
|
| 76 |
|
|
interrupt (TIMERA0_VECTOR) irq_routine(void)
|
| 77 |
|
|
{
|
| 78 |
|
|
|
| 79 |
|
|
// P1OUT ^= 0x01; // Toggle P1.0
|
| 80 |
|
|
flags |= time_tick; // flag a timer tick has occured
|
| 81 |
|
|
}
|
| 82 |
|
|
|
| 83 |
|
|
//************************************************************
|
| 84 |
|
|
//
|
| 85 |
|
|
// read_a2d
|
| 86 |
|
|
//
|
| 87 |
|
|
/* Description:
|
| 88 |
|
|
Waits for present A to D to finish. Reads 16 bit A to D value. Switches
|
| 89 |
|
|
A to D mux to a channel passed into function. Starts another A to D on new
|
| 90 |
|
|
mux input. Returns int value read from last mux input A to D.
|
| 91 |
|
|
*/
|
| 92 |
|
|
unsigned int read_a2d(unsigned int next_mux)
|
| 93 |
|
|
{
|
| 94 |
|
|
unsigned int last_a2d;
|
| 95 |
|
|
|
| 96 |
|
|
if (next_mux==SD16INCH_2) {
|
| 97 |
|
|
last_a2d = MY_CNTRL1;
|
| 98 |
|
|
} else {
|
| 99 |
|
|
last_a2d = MY_CNTRL2;
|
| 100 |
|
|
}
|
| 101 |
|
|
|
| 102 |
|
|
if (last_a2d & 0x8) { // CCW
|
| 103 |
|
|
last_a2d = 0xE000;
|
| 104 |
|
|
} else if (last_a2d & 0x4) { // CW
|
| 105 |
|
|
last_a2d = 0xB000;
|
| 106 |
|
|
} else if (last_a2d & 0x2) { // Thrust
|
| 107 |
|
|
last_a2d = 0x8000;
|
| 108 |
|
|
} else if (last_a2d & 0x1) { // Fire
|
| 109 |
|
|
last_a2d = 0x4000;
|
| 110 |
|
|
} else {
|
| 111 |
|
|
last_a2d = 0x0000;
|
| 112 |
|
|
}
|
| 113 |
|
|
|
| 114 |
|
|
//while ((SD16CCTL0 & SD16IFG) == 0); // wait for a2d to finish
|
| 115 |
|
|
//last_a2d = SD16MEM0; // save results from last a2d
|
| 116 |
|
|
//SD16INCTL0 = next_mux; // switch analog mux for next rocket
|
| 117 |
|
|
//SD16CCTL0 |= SD16SC; // Start another conversion
|
| 118 |
|
|
return last_a2d;
|
| 119 |
|
|
}
|
| 120 |
|
|
|
| 121 |
|
|
// ************************************************************
|
| 122 |
|
|
//
|
| 123 |
|
|
// send one 16 bit value to SPI DAC
|
| 124 |
|
|
//
|
| 125 |
|
|
/* Description:
|
| 126 |
|
|
First put the value into the transmit register. Chip select the DAC.
|
| 127 |
|
|
Start the automatic SPI transfer. Wait until the transfer is complete.
|
| 128 |
|
|
Finally remove the chip select.
|
| 129 |
|
|
*/
|
| 130 |
|
|
void set_one(int set_1)
|
| 131 |
|
|
{
|
| 132 |
|
|
|
| 133 |
|
|
USISR = set_1; // send value to DAC
|
| 134 |
|
|
P1OUT &= ~0x04; // chip select TLV5618A
|
| 135 |
|
|
USICNT = 0x10 + USI16B; // start spi
|
| 136 |
|
|
while ((USIIFG & USICTL1) == 0) ; // wait until y spi done
|
| 137 |
|
|
P1OUT |= 0x04; // remove chip select
|
| 138 |
|
|
}
|
| 139 |
|
|
|
| 140 |
|
|
// ************************************************************
|
| 141 |
|
|
//
|
| 142 |
|
|
// Move DAC's to dot position
|
| 143 |
|
|
//
|
| 144 |
|
|
// set_x and set_y enter as 0 to 4095
|
| 145 |
|
|
// Masked to 12 bit dac 0 to 4095
|
| 146 |
|
|
/* Description:
|
| 147 |
|
|
Move DAC to position set_x, set_y. Write the set_y value into the DAC's
|
| 148 |
|
|
BUFFER. Write the set_x value to DAC_x and at the same time move the BUFFER
|
| 149 |
|
|
value to DAC_y. This technique removes the stair steping in lines.
|
| 150 |
|
|
*/
|
| 151 |
|
|
void set_xy(int set_x, int set_y)
|
| 152 |
|
|
{
|
| 153 |
|
|
|
| 154 |
|
|
//set_one((set_y & 0x0FFF) | 0x5000); // send y value to BUFFER
|
| 155 |
|
|
//set_one((set_x & 0x0FFF) | 0xc000); // send x DAC_X, BUFFER to DAC_Y
|
| 156 |
|
|
|
| 157 |
|
|
while (MY_DAC_X_STAT);
|
| 158 |
|
|
while (MY_DAC_Y_STAT);
|
| 159 |
|
|
MY_DAC_Y = (set_y & 0x0FFF);
|
| 160 |
|
|
MY_DAC_X = (set_x & 0x0FFF);
|
| 161 |
|
|
|
| 162 |
|
|
}
|