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.

CCS/MSP430G2553: I have a project creating the morse code with C language, and I need your help.

Part Number: MSP430G2553

Tool/software: Code Composer Studio

Hello everyone, I have a school project that asked me to make a morse code using the msp430g2553 controller and the CCS compiler. I am struggling with making my Dot and Dash Function timing to work correctly. My string I set to be "test" and when I run it, the controller only flashes 1 and then flashes 1 more time and stay on. Here are my morse code array: 

char morse[][numCols]= {
(".-"),("-..."),("-.-."),("-.."),("."),("..-."),("--."),("...."),(".."),(".---"),
("-.-"),(".-.."),("--"),("-."),("---"),(".--."),("--.-"),
(".-."),("..."),("-"),("..-"),("...-"),(".--"),("-..-"),
("-.--"),("--.."),
(".----"),("..---"),("...--"),("....-"),("....."),("-...."),
("--..."),("---.."),("----."),("-----")};

void main(){
WDTCTL = WDTPW + WDTHOLD;
P1DIR |=0x41;
P1OUT &= 0xBE;
char str[] = "test";
for (;;){
if (i < strlen(str)){
for (i = 0; i < strlen(str); i++){
compareChars(str[i]);
}
} else {
break;
reset();
}
}}

Then my compareChars function (short version) where it will convert my string into lower case and also have a switch case that detect every alphabet and refer to the morse array I have created: 

void compareChars(char c1){
int j,k;
if (c1 >= 'A'&& c1 <= 'Z')
{
c1 += 32;
}
switch(c1){
case 'a':
for (j = 0; j < numCols; j++){
if (morse[0][j] != '\0'){
ReadMorseAlphabet(morse[0][j]);
for (k = 5000; k > 0; k--); //to create a delay
}
}
break;
case 'b':
for (j = 0; j < numCols; j++){
if (morse[1][j] != '\0'){
ReadMorseAlphabet(morse[1][j]);
for (k = 5000; k > 0; k--); //to create a delay
}
}
break;

(ETC)

and my Dot and Dash Function and also the reset function where it will flashes green to reset: 

void ReadMorseAlphabet(char morseCode){
switch (morseCode){
case '.':
dot();
break;
case '-':
dash();
break;
default:
break;
}
}

void dot(){
P1OUT ^= 0x40;
for(i=1; i<= 20000; i++){
;}
for(i=1; i<= 20000; i++){
;}
}

void dash()
{
for(i=1; i<= 20000; i++){
;}
P1OUT ^= 0x40;
for(i=1; i<= 20000; i++){
;}
for(i=1; i<= 20000; i++){
;}
for(i=1; i<= 20000; i++){
;}
}

void reset()
{
P1OUT ^= 0x01;
for(i=1; i<= 20000; i++);
}

  • For one thing you will need to make all the delay variables volatile so that the compiler does not optimise them out.

  • dot() and dash() switch P1.6 at the beginning, but don't un-switch them at the end, so the LED will be on for who-knows-how-long. And if, e.g. you call dot() twice in succession, the first "dot" will be with the LED on and the next with the LED off. [I suspect this is your symptom]

    More generally, I recommend you turn the LED on with "P1OUT |= 0x40" and off with "P1OUT &= ~0x40" rather than "^=". Using "^",  your brain has to constantly  keep track of what state it's already in.

    [Edit: Fixed wording slightly.]

  • So apparently, the t word in my string "test" was set to a wrong element of the morse array. I did not know how to do step by step debugging so I could not found that problem and it took me days to figuring out. I am new to the compiler as well. But thank you very much for your help. 

  • Hi Quang,

    Sounds good. Please feel free to come back if any further question.

**Attention** This is a public forum