1
0
UAHCode/CPE325/Lab10_Part2/Lab10_Part2.c

118 lines
4.6 KiB
C
Raw Permalink Normal View History

2022-08-28 21:12:16 +00:00
#include <msp430xG46x.h>
#include <math.h>
volatile long int ADCXval, ADCYval, ADCZval;
volatile float Xper, Yper, Zper, M;
#define SW1_PRESSED ((BIT0&P1IFG)==0) // SW1 Status
void TimerA_setup(void) {
TACCR0 = 3277; // 3277 / 32768 Hz = 0.1s
TACTL = TASSEL_1 + MC_1; // ACLK, up mode
TACCTL0 = CCIE; // Enabled interrupt
}
void ADC_setup(void) {
int i =0;
_EINT(); // Enable interrupts
P1IE |= BIT1; // P1.1 interrupt enabled
P1IES |= BIT1; // P1.1 hi/low edge
P1IFG &= ~BIT1; // P1.1 IFG cleared
P2DIR |= BIT2; // Set P2 output direction
P2OUT &= ~BIT2; // Turn Off LED2
P6DIR &= ~BIT3 + ~BIT5 + ~BIT7; // Configure P6.3, P6.5, and P6.7 as input pins
P6SEL |= BIT3 + BIT5 + BIT7; // Configure P6.3, P6.5, and P6.7 as analog pins
// P6DIR &= ~BIT2 + ~BIT4 + ~BIT6; // Configure P6.3, P6.5, and P6.7 as input pins
// P6SEL |= BIT2+ BIT4 + BIT6; // Configure P6.3, P6.5, and P6.7 as analog pins
ADC12CTL0 = ADC12ON + SHT0_6 + MSC; // configure ADC converter
ADC12CTL1 = SHP + CONSEQ_1; // Use sample timer, single sequence
ADC12MCTL0 = INCH_3; // ADC A3 pin - Stick Z-axis
ADC12MCTL1 = INCH_5; // ADC A5 pin - Stick Y-axis
ADC12MCTL2 = INCH_7 + EOS; // ADC A7 pin - Stick X-axis
// ADC12MCTL0 = INCH_2; // ADC A3 pin - Z-axis
// ADC12MCTL1 = INCH_4; // ADC A5 pin - Y-axis
// ADC12MCTL2 = INCH_6 + EOS; // ADC A7 pin - X-axis
// EOS - End of Sequence for Conversions
ADC12IE |= 0x03; // Enable ADC12IFG.1
for (i = 0; i < 0x3600; i++); // Delay for reference start-up
ADC12CTL0 |= ENC; // Enable conversions
}
void UART_putCharacter(char c) {
while(!(IFG2 & UCA0TXIFG)); // Wait for previous character to be sent
UCA0TXBUF = c; // Send byte to the buffer for transmitting
}
void UART_setup(void) {
P2SEL |= BIT4 + BIT5; // Set up Rx and Tx bits
UCA0CTL0 = 0; // Set up default RS-232 protocol
UCA0CTL1 |= BIT0 + UCSSEL_2; // Disable device, set clock
UCA0BR0 = 27; // 1048576 Hz / 38400
UCA0BR1 = 0;
UCA0MCTL = 0x94;
UCA0CTL1 &= ~BIT0; // Start UART device
}
void sendData(void) {
int i;
Xper = (((ADCXval*(3.0/4095)) - 1.5)/0.3); // Calculate percentage outputs
Yper = (((ADCYval*(3.0/4095)) - 1.5)/0.3);
Zper = (((ADCZval*(3.0/4095)) - 1.5)/0.3);
M = sqrt(pow(Xper,2)+pow(Yper,2)+pow(Zper,2));
if(M > 2){
P2OUT |= BIT2; //Turn LED1 on
}
// Use character pointers to send one byte at a time
char *xpointer=(char *)&Xper;
char *ypointer=(char *)&Yper;
char *zpointer=(char *)&Zper;
UART_putCharacter(0x55); // Send header
for(i = 0; i < 4; i++) { // Send x percentage - one byte at a time
UART_putCharacter(xpointer[i]);
}
for(i = 0; i < 4; i++) { // Send y percentage - one byte at a time
UART_putCharacter(ypointer[i]);
}
for(i = 0; i < 4; i++) { // Send y percentage - one byte at a time
UART_putCharacter(zpointer[i]);
}
}
void main(void) {
WDTCTL = WDTPW +WDTHOLD; // Stop WDT
TimerA_setup(); // Setup timer to send ADC data
ADC_setup(); // Setup ADC
UART_setup(); // Setup UART for RS-232
_EINT();
while (1){
ADC12CTL0 |= ADC12SC; // Start conversions
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0
}
}
#pragma vector = ADC12_VECTOR
__interrupt void ADC12ISR(void) {
ADCXval = ADC12MEM0; // Move results, IFG is cleared
ADCYval = ADC12MEM1;
ADCZval = ADC12MEM2;
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
#pragma vector = TIMERA0_VECTOR
__interrupt void timerA_isr() {
sendData(); // Send data to serial app
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
// Port 1 interrupt service routine
#pragma vector = PORT2_VECTOR
__interrupt void Port2_ISR (void) {
if(!SW1_PRESSED){
P2OUT &= ~BIT2;
P1IES |= BIT1; // P1.1 hi/low edge
P1IFG &= ~BIT1; // P1.1 IFG cleared
}
}