This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hey all,
I have been working on this code for a while now and can't get any output to the terminal.
any suggestions is welcome!
#include <msp430f5438a.h>
unsigned int delay ( unsigned int x)
{
unsigned int i,j;
for (i = 0; i<= x; i++)
{
for(j=0;j<=1000; j++) ;
}
return 0;
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watch Dog Timer
P4DIR |= BIT1; // P4.1 output
TB0CCR0 = 630;
TB0CCTL1 = OUTMOD_7; // CCR1 interrupt enabled
TB0CCR1 = 450; // Set PWM period to 650 clock ticks
TB0CTL = TBSSEL_1 + MC_1 + TBIE + TBCLR;
ADC12CTL0 = ADC12SHT0_2 + ADC12ON; // Set sampling time, turn on ADC12
ADC12CTL1 = ADC12SHP;
ADC12IE = 0x01; // Enable interrupt
ADC12MCTL0 = 0x01 ;
ADC12CTL0 |= ADC12ENC ; // Conversion enabled
P6SEL |= BIT7; // P6.7 ADC option select
P1DIR |= 0x04; // P6.2 is output for LED
// Serial Port Settings
P3SEL |= 0x30; // P3.3,2 option select = USART0 TXD/RXD
P3DIR |= 0x01; // P3.0 output direction
UCA0CTL0 |= UCMST+UCSYNC+UCMSB; // 8-bit SPI mstr, MSb 1st, CPOL=0, CPHS=0
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x02; // Set Frequency
UCA0BR1 = 0;
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE;
for (;;)
{
// This should actually happen in a timer interrupt where
// we may like to sample only once in, say 1 second
delay(500);
ADC12CTL0 |= ADC12SC; // Sampling open
_BIS_SR(CPUOFF + GIE); // LPM0, ADC12_ISR will force exit
}
}
// ADC12 interrupt service routine
#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR (void)
{
unsigned short lResultRSSI ;
unsigned char part1, part2;
if (ADC12MEM0 < 0x7FF)
P1OUT &= ~0x04; // Clear P6.2 - LED off
else
P1OUT |= 0x04; // Set P6.2 - LED on
_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR)
lResultRSSI = ADC12MEM0 ; // RXBUF0 to TXBUF0
part1 = (lResultRSSI & 0x00FF); // lsb
part2 = (lResultRSSI & 0xFF00) >> 8; // msb
while (!(UCTXIFG));
UCA0TXBUF = part2; // We send MSB first
while (!(UCTXIFG));
UCA1TXBUF = part1;
}
Things like this sholdn't be ever done inside an ISR. ISRs are plain, straight in-out functions. No loops, no complex calculation, no callign of other functions. Just react on an evert, save the results of an ADC conversion, read an incoming byte or stuff an empty output register. Then set a flag that something has happened (or end LPM mode if this is notification enough) and exit. And let main do all the time-consuming jobs.Alexander Coffin said:while (!(UCTXIFG));
In your case, make part1/part2 global volatile variables, adn after waking main inside the ISR, let main send the content of the freshly filled variables.
Depending on the compiler and the compiler and optimization options, the outcome of this function is unpredictable. Worst case the compiler will completely eliminate the function as it doe snothing except wastign time. And the compiler cannot know that this is your intention.Alexander Coffin said:unsigned int delay ( unsigned int x)
Better use the __delay_cycles() intrinsic for the delay part. or, better, use a timer for timings. That's why the timers are there and are called timers.
This statement has no effect. Doesn't the compiler give you a warning for this line?Alexander Coffin said:UCA0IE;
But back to your original question (well, I assume it was meant to be aquestion):
Which output tewrminal? YOu're programming the USCI for SPI master. So it isn't a simple serial terminal. But what is it?Alexander Coffin said:I have been working on this code for a while now and can't get any output to the terminal.
SPi usually is 3-wire, and the comment doesn't fit SPI use too.Alexander Coffin said:P3SEL |= 0x30; // P3.3,2 option select = USART0 TXD/RXD
But you set UCMST and UCSYNC, so definitely SPI mode.Alexander Coffin said:UCA0CTL0 |= UCMST+UCSYNC+UCMSB; // 8-bit SPI mstr, MSb 1st, CPOL=0, CPHS=0
And unless SMCLK is 32768Hz, SMCLK/2 would be most likely much too high for an UART connection too.Alexander Coffin said:UCA0BR0 = 0x02; // Set Frequency
So again: what kind of terminal are you talking about?
**Attention** This is a public forum