C programming on a msp430 microcontroller help with strings and LED blinking light delay functions

Thread Starter

KurtisBichard

Joined Apr 19, 2020
2
Hi everyone this is my first post. I'm reaching out because I'm running into a problem. I'm programming a MSP430 microcontroller using C and the code composer compiler. The point of the project it to make a string with a message and then have the LED lights blink the message in morse code. I made a matrix assigning each letter and number to its corresponding dot-dash and it works fine on programs such as repl.it, but when programming the actual microcontroller itself it is giving me errors when trying to add any delay functions. Any help or tips would be greatly appreciated.
 

Thread Starter

KurtisBichard

Joined Apr 19, 2020
2
Hey everyone thanks for the speedy replies. This is the code I've been playing with. It works fine when I debug through code compiler but the moment I type in WDTCTL = WDTPW | WDTHOLD; to stop the watchdog timer and add delay functions it will no longer debug. I admit i'm just new at programming so if I'm doing something completely wrong I would appreciate any feedback.
Code:
#include<msp430g2553.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char str[25]= "goknights";
char str1[100];
int j=0;
int i=0;
for(i=0;i<=strlen(str);i++)
{
switch(toupper(str[i]))
{
case 'A':
str1[j++]='.';
str1[j]='-';
break;
case 'B':
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case 'C':
str1[j++]='-';
str1[j++]='.';
str1[j++]='-';
str1[j]='.';
break;
case 'D':
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case 'E':
str1[j]='.';
break;
case 'F':
str1[j++]='.';
str1[j++]='.';
str1[j++]='-';
str1[j]='.';
break;
case 'G':
str1[j++]='-';
str1[j++]='-';
str1[j]='.';
break;
case 'H':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case 'I':
str1[j++]='.';
str1[j]='.';
break;
case 'J':
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case 'K':
str1[j++]='-';
str1[j++]='.';
str1[j]='-';
break;
case 'L':
str1[j++]='.';
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case 'M':
str1[j++]='-';
str1[j]='-';
break;
case 'N':
str1[j++]='-';
str1[j]='.';
break;
case 'O':
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case 'P':
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j]='.';
break;
case 'Q':
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j]='-';
break;
case 'R':
str1[j++]='.';
str1[j++]='-';
str1[j]='.';
break;
case 'S':
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case 'T':
str1[j]='-';
break;
case 'U':
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case 'V':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case 'W':
str1[j++]='.';
str1[j++]='-';
str1[j]='-';
break;
case 'X':
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case 'y':
str1[j++]='-';
str1[j++]='.';
str1[j++]='-';
str1[j]='-';
break;
case 'Z':
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case '0':
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case '1':
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case '2':
str1[j++]='.';
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case '3':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j++]='-';
str1[j]='-';
break;
case '4':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case '5':
str1[j++]='.'
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case '6':
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case '7':
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case '8':
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case '9':
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j]='.';
break;
}
j++;
}
str1[j-1]='\0';
return 0;
 

dl324

Joined Mar 30, 2015
16,839
I admit i'm just new at programming so if I'm doing something completely wrong I would appreciate any feedback.
You never do anything with str1. You have a typo for 'Y'.

If the strings can be longer and have duplicate characters, it would be more efficient if you defined an array for the morse code substitutions.
 

MrChips

Joined Oct 2, 2009
30,706
When you have to type in long tedious code, step back and ask yourself "there has to be an easier way?".

For example, instead of:

case 'C':
str1[j++]='-';
str1[j++]='.';
str1[j++]='-';
str1[j]='.';

this is so much easier:

case 'C':
strcpy( str1, "-.-.");

Also, since the data does not change, you can create a 2-dimensional array of characters.
The compiler can generate startup code to fill the array in sequential order.
Then the letter becomes an index to the array. There is no need for a SWITCH structure.

You could do something like this:
C:
morse_code[] [6] =
{
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", // for numerals 0-9
" ", " ", " ", " ", " ", " ", " ", // pad with seven unused characters, see the ASCII table
".-", "-...", "-.-.", "_..", // for letters A, B, C, D, etc...
};
 

dl324

Joined Mar 30, 2015
16,839
I'd also consider using pointers to step through the characters in the string being converted.

From K&R C Programming Language 2nd edition:
1587358782620.png
 

BobaMosfet

Joined Jul 1, 2009
2,110
Hey everyone thanks for the speedy replies. This is the code I've been playing with. It works fine when I debug through code compiler but the moment I type in WDTCTL = WDTPW | WDTHOLD; to stop the watchdog timer and add delay functions it will no longer debug. I admit i'm just new at programming so if I'm doing something completely wrong I would appreciate any feedback.
Code:
#include<msp430g2553.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char str[25]= "goknights";
char str1[100];
int j=0;
int i=0;
for(i=0;i<=strlen(str);i++)
{
switch(toupper(str[i]))
{
case 'A':
str1[j++]='.';
str1[j]='-';
break;
case 'B':
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case 'C':
str1[j++]='-';
str1[j++]='.';
str1[j++]='-';
str1[j]='.';
break;
case 'D':
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case 'E':
str1[j]='.';
break;
case 'F':
str1[j++]='.';
str1[j++]='.';
str1[j++]='-';
str1[j]='.';
break;
case 'G':
str1[j++]='-';
str1[j++]='-';
str1[j]='.';
break;
case 'H':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case 'I':
str1[j++]='.';
str1[j]='.';
break;
case 'J':
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case 'K':
str1[j++]='-';
str1[j++]='.';
str1[j]='-';
break;
case 'L':
str1[j++]='.';
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case 'M':
str1[j++]='-';
str1[j]='-';
break;
case 'N':
str1[j++]='-';
str1[j]='.';
break;
case 'O':
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case 'P':
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j]='.';
break;
case 'Q':
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j]='-';
break;
case 'R':
str1[j++]='.';
str1[j++]='-';
str1[j]='.';
break;
case 'S':
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case 'T':
str1[j]='-';
break;
case 'U':
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case 'V':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case 'W':
str1[j++]='.';
str1[j++]='-';
str1[j]='-';
break;
case 'X':
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case 'y':
str1[j++]='-';
str1[j++]='.';
str1[j++]='-';
str1[j]='-';
break;
case 'Z':
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case '0':
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case '1':
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case '2':
str1[j++]='.';
str1[j++]='.';
str1[j++]='-';
str1[j++]='-';
str1[j]='-';
break;
case '3':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j++]='-';
str1[j]='-';
break;
case '4':
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='-';
break;
case '5':
str1[j++]='.'
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case '6':
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case '7':
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j++]='.';
str1[j]='.';
break;
case '8':
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j++]='.';
str1[j]='.';
break;
case '9':
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j++]='-';
str1[j]='.';
break;
}
j++;
}
str1[j-1]='\0';
return 0;
You should terminate your string with a 0x00 (that makes it a C string).

flow-chart your code.
think about things more intelligently.... not so much brute force. The way you are doing it is so resource consumptive and tedious.

Consider putting all your morse-code into a single string.
Consider a small int array that contains an index into that string where each morse-code equivalent begins
Use the character value you wish to translate into morse code as the index, minus 'A' as the offset to take the index from 65 to 0.
Put a space between the characters to delineate the end

Code:
void morsetranslate(char *srcS,char *outS)
   {
   char morseLettersA[] = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
   int     lDexA[] = {0,3,8,13,17,19,24,28,33,36,41,45,50,53,56,60,65,70,74,78,80,84,89,93,98,103};
   char morseNumbersA[] = "----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.";
   int     nDexA[] = {0,6,12,18,24,30,36,42,48,54};
   int  j;
   int outDex = 0;
   int dex = 0;
   char *morseP = NULL;
 
   for(j=0;j<strlen(srcS);j++)
      {
      if((toupper(srcS[j]) > 47) && (toupper(srcS[j]) < 58))                 // is char a number?
         {
         dex = nDexA[toupper(srcS[j]) - 48];                                      // yes, calculate index into morse-code numbers
         morseP = &morseNumbersA[0];
         };

      if((toupper(srcS[j]) > 64) && (toupper(srcS[j]) < 91))                 // or is it a letter?
         {
         dex = lDexA[toupper(srcS[j]) - 65];                                      // yes, calculate index into morse-code letters
         morseP = &morseLettersA[0];
         };

     while(morseP[dex] != ' ')
         outS[outDex++] = morseP[dex++];
      };

   outS[outDex] = 0x00;
   }
It's rough, straight out of my head, but should give you a different way to think about things. It works by pass the address of the source string to convert to morsecode, and the address of string to put the output morse-code into. Make sure the output string/buffer is long enough to hold whatever the morsetranslate() function outputs.
 
Last edited:
Top