Up until now I've been using Energia for projects that required the use of UART, but I decided to get my hands dirty and just use the msp430-gcc toolchain.
The below program is simply a test program I made to send a number over UART at 9600. when I compile it without optimization, everything works just fine("msp430-gcc -mmcu=msp430g2553 -g -o adc adc.c"), but when I use an optimization flag such as -Os, -O2, or -O1 it totally breaks my code. What am I doing wrong in my code that's causing it to break when I try to do any optimization?
I'm using msp430-gcc version 4.6.3 20120301
#include <msp430.h>
#include <stdio.h> //for sprintf
void delay(int dly);
void printnumb();
void UART_Write(char dataq);
int flag=0;
char buffer[4];
int place = 2024;
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
IE1 |= WDTIE; //enable interrupt
BCSCTL1 = CALBC1_16MHZ;
DCOCTL = CALDCO_16MHZ;
P1DIR |=BIT2;
P1OUT &=BIT2;
P1SEL |=0b00000110;//page 43 on msp430g2553 datasheet
P1SEL2|=0b00000110;//page 329
UCA0CTL1 |= UCSWRST + UCSSEL_2;
UCA0BR0=104;
UCA0BR1=0;
UCA0MCTL =0b00110001;//modulation control p 431
UCA0CTL1 &=~UCSWRST;
__enable_interrupt();
while(1){
printnumb();
delay(2000);
}
}
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{
flag=1;
}
void delay(int dly){
dly= dly*2;
int cycle=0;
WDTCTL = WDTPW | WDTTMSEL| WDTIS0;
while(cycle<dly){
if(flag==1){
cycle++;
flag=0;
}
}
WDTCTL = WDTPW | WDTHOLD;
}
void printnumb(){
sprintf(buffer, "%i", place);
int cnt = 0;
while(cnt < 4){
UART_Write(buffer[cnt]);
cnt=cnt+1;
}
UART_Write('\r');
UART_Write('\n');
}
void UART_Write(char dataq){
while((IFG2&UCA0TXIFG)==0){
}
UCA0TXBUF=dataq;
}