Morse code in c for keil 8051 microcontroler

Thread Starter

kasuci

Joined Jan 26, 2012
4
I apologize in advence if there is another open thread about this.

I'm trying to write a program in "c" for keil 8051 microcontroler which would
as input "take scan if the key on a PC keyboard has been pressed and if it was than interpret which key it was" and send it out to a external speeker on
port 1.0 as a morse code sound.

Please help. I haven't had much chance to work in c so help is greatly appreciated.
 

t06afre

Joined May 11, 2009
5,934
I do not know keil C. But this can quite simple be implemented with two look up tables. One lookup table contains dash and dots stored binary as bits (1 for dash and zero for dot). The other table contains the total of dash/dots. The letter A is dot dash so the lenght is two. But the letter J is dot das dash dash so the a total of four tones should be played
 

John P

Joined Oct 14, 2008
2,025
You could combine the 2 tables if you said that the top 3 bits of a byte contained the number of beeps, and the bottom 5 contained the actual data. There might be some reason to split the byte 2 and 6, or 4 and 4 instead. There's certainly extra space available.
 

Thread Starter

kasuci

Joined Jan 26, 2012
4
I tried but I have no luck (not to mention knowledge).
If someone could write the program I'd be really greatfull.
 

Thread Starter

kasuci

Joined Jan 26, 2012
4
OK. Here is what I've got now

Rich (BB code):
#include <c:\Keil\Reg535.h>
#include <c:\Keil\M535.h>
#include <c:\Keil\stdio.h>

sbit out=P4^7;


data unsigned int count;
code unsigned char tablica[2][4][6][8]= {{1,1,0,2}, {1,1,2,2}};
code unsigned char TablicaA[2]={0,1};  //tablica trajanja za 'a'
code unsigned char TablicaB[4]={1,0,0,0}; //tablica trajanja za 'b'
code unsigned char TablicaC[6]={1,0,1,0,};
code unsigned char TablicaD[8]={1,0,0};
code unisgned ;char TablicaE[2]={0};
code unsigned char TablicaF[4]={0,0,1,0};
code unsigned char TablicaG[6]={1,1,0};
code unsigned char TablicaH[8]={0,0,0,0};
code unsigned ;char TablicaI[2]={0,0};
code unsigned char TablicaJ[4]={0,1,1,1};
code unsigned char TablicaK[6]={1,0,1};
code unsigned char TablicaL[8]={0,1,0,0};
code unsigned ;char TablicaM[2]={1,1};
code unsigned char TablicaN[4]={1,0};
code unsigned char TablicaO[6]={1,1,1};
code unsigned char TablicaP[8]={0,1,1,0};
code unsigned ;char TablicaQ[2]={1,1,0,1};
code unsigned char TablicaR[4]={0,1,0};
code unsigned char TablicaS[6]={0,0,0};
code unsigned char TablicaT[8]={1};
code unsigned ;char TablicaU[2]={0,0,1};
code unsigned char TablicaV[4]={0,0,0,1};
code unsigned char TablicaW[6]={0,1,1};
code unsigned char TablicaX[8]={1,0,0,1};
code unsigned ;char TablicaY[2]={1,0,1,1};
code unsigned char TablicaZ[4]={1,1,0,0};
//-------------------------------------------------------------------
main()
{
    while(1)
    {
        data unsigned char z;
        z=GetKey();

        if(z == 'a')
        {
            for(count = 0; count < 2; count++)
            {
                out = 1;
                if(TablicaA[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z == 'b') 
        {
            for(count = 0; count < 4; count++)
            {
                out = 1;
                if(TablicaB[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if (z== 'c')
        {
            for(count = 0; count < 6; count ++)
            {
                out =1;
                if(TablicaC[count]==0)
                     Delay(100);
                else
                     Delay(300);
                out = 0;
                     Delay(100);
               }
        }
        else if(z=='d')
        {
               for(count = 0; count < 8; count++)
               {
                out = 1;
                if(TablicaD[count]==0)
                      Delay(100);
                else
                      Delay(300);
                out = 0;
                      Delay(100);
              }
        }
        if(z=='e')
        {
               for(count = 0; count < 2; count++);
               {
                out = 1;
                if(TablicaE[count]==0)
                       Delay(100);
                else
                       Delay(300);
                out = 0;
                       Delay(100);
               }
        }
        else if(z=='f')
        {
            for(count = 0; count < 4; count++)
            {
                out = 1;
                if(TablicaF[count]==0)
                       Delay(100);
                else
                      Delay(300);
                out = 0;
                       Delay(100);
            }
        }
        if(z=='g')
        {
            for(count = 0; count < 6; count++)
            {
                out = 1;
                if(TablicaG[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='h')
        {
            for(count = 0; count < 8; count++)
            {
                out = 1;
                if(TablicaH[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='i')
        {
            for(count = 0; count < 2; count++)
            {
                out = 1;
                if(TablicaI[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='j')
        {
            for(count = 0; count < 4; count++)
            {
                out = 1;
                if(TablicaJ[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='k')
        {
            for(count = 0; count < 6; count++)
            {
                out = 1;
                if(TablicaK[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='l')
        {
            for(count = 0; count < 8; count++)
            {
                out = 1;
                if(TablicaL[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='m')
        {
            for(count = 0; count < 2; count++)
            {
                out = 1;
                if(TablicaM[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='n')
        {
            for(count = 0; count < 4; count++)
            {
                out = 1;
                if(TablicaN[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='o')
        {
            for(count = 0; count < 6; count++)
            {
                out = 1;
                if(TablicaO[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='p')
        {
            for(count = 0; count < 8; count++)
            {
                out = 1;
                if(TablicaP[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='q')
        {
            for(count = 0; count < 2; count++)
            {
                out = 1;
                if(TablicaQ[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='r')
        {
            for(count = 0; count < 4; count++)
            {
                out = 1;
                if(TablicaR[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='s')
        {
            for(count = 0; count < 6; count++)
            {
                out = 1;
                if(TablicaS[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='t')
        {
            for(count = 0; count < 8; count++)
            {
                out = 1;
                if(TablicaT[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='u')
        {
            for(count = 0; count < 2; count++)
            {
                out = 1;
                if(TablicaU[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='v')
        {
            for(count = 0; count < 4; count++)
            {
                out = 1;
                if(TablicaV[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        if(z=='w')
        {
            for(count = 0; count < 6; count++)
            {
                out = 1;
                if(TablicaW[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        else if(z=='x')
        {
            for(count = 0; count < 8; count++)
            {
                out = 1;
                if(TablicaX[count]==0)
                    Delay(100);
                else
                    Delay(300);
                out = 0;
                    Delay(100);
            }
        }
        .......
                



        out = 0;

    }       
}
The problem now is that it says line 37,41 and 45 "array[]" over initilized or
something like that.
I really don't know where the problem is.

Line 37 is the one with TablicaQ
 
Last edited by a moderator:

THE_RB

Joined Feb 11, 2008
5,438
That's a messy way to do it! :eek:

Have a look at this project, it has source code in C that might give you some ideas;
http://www.romanblack.com/shift1/sh1_projects.htm (bottom project on page)
it generates official timing for the morse characters (Paris standard) with official WPM (words per minute).

Source code is for the PIC 12F675 but may work on other micros if you change it as needed to suit.
 

MMcLaren

Joined Feb 14, 2010
861
hi kasuci,

One relatively efficient technique for representing Morse character dit and dah elements in an array involves using an extra '1' bit as a terminator immediately following the dit (0) and/or dah (1) bits. This allows for storing Morse character patterns up to seven elements in length and it doesn't force you to use a counter variable when parsing the dit/dah pattern. Use a null pattern that contains just the terminator bit (0b10000000) when you encounter the space character, or any non-Morse character, as input.

Rich (BB code):
;
;    rom char morse[] = { 0b11001110,   // ',' (44) dah-dah-di-di-dah-dah
;                         0b10000110,   // '-' (45) dah-di-di-di-di-dah
;                         0b01010110,   // '.' (46) di-dah-di-dah-di-dah
;                         0b10010100,   // '/' (47) dah-di-di-dah-dit
;                         0b11111100,   // '0' (48) dah-dah-dah-dah-dah
;                         0b01111100,   // '1' (49) di-dah-dah-dah-dah
;                         0b00111100,   // '2' (50) di-di-dah-dah-dah
;                         0b00011100,   // '3' (51) di-di-di-dah-dah
;                         0b00001100,   // '4' (52) di-di-di-di-dah
;                         0b00000100,   // '5' (53) di-di-di-di-dit
;                         0b10000100,   // '6' (54) dah-di-di-di-dit
;                         0b11000100,   // '7' (55) dah-dah-di-di-dit
;                         0b11100100,   // '8' (56) dah-dah-dah-di-dit
;                         0b11110100,   // '9' (57) dah-dah-dah-dah-dit
;                         0b10000000,   // ':' (58) null
;                         0b10000000,   // ';' (59) null
;                         0b10000000,   // '<' (60) null
;                         0b10000000,   // '=' (61) null
;                         0b10000000,   // '>' (62) null
;                         0b00110010,   // '?' (63) di-di-dah-dah-di-dit
;                         0b10000000,   // '@' (64) null
;                         0b01100000,   // 'A' (65) di-dah
;                         0b10001000,   // 'B' (66) dah-di-di-dit
;                         0b10101000,   // 'C' (67) dah-di-dah-dit
;                         0b10010000,   // 'D' (68) dah-di-dit
;                         0b01000000,   // 'E' (69) dit
;                         0b00101000,   // 'F' (70) di-di-dah-dit
;                         ~~~~
;                         0b10011000,   // 'X' (88) dah-di-di-dah
;                         0b10111000,   // 'Y' (89) day-di-dah-dah
;                         0b11001000 }; // 'Z' (90) dah-dah-di-dit
;
Processing this type of Morse character pattern is relatively straight forward, as can be seen in this pseudo C code example;

Rich (BB code):
;    ~~~~
;    static unsigned char mchar = 128;  //
;
;    ~~~~
;    if(rxchar >= 97) rxchar -= 32;     // lower to upper case alpha
;    if((rxchar>=44) && (rxchar<=90))   // if in array range
;      mchar = morse[rxchar-44];        // get morse element pattern
;   /*                                                                 *
;    *  send Morse dit/dah elements (mchar = 0b10000000 on exit)       *
;    *                                                                 */
;    while(mchar != 128)                // while elements remain
;    { sendmorse(mchar.7);              // send dit (0) or dah (1)
;      ditspace(1);                     // inter-element 1 dit space
;      mchar <<= 1;                     // shift next element into b7
;    }                                  //
;    ditspace(2);                       // inter-char space (3)
;    if(rxchar == '.') ditspace(4);     // inter-word space (7)
;    if(rxchar == ',') ditspace(4);     // inter-word space (7)
;    if(rxchar == '?') ditspace(4);     // inter-word space (7)
;    if(rxchar == ' ') ditspace(5);     // inter-word space (7)
;
Hope this helps. Good luck with your project.

Cheerful regards, Mike
 
Last edited:

panic mode

Joined Oct 10, 2011
2,715
i just glanced the code and it seems that Q was declared as array[2] (elements 0, 1, 2) but assigned value had one extra element.
 

Thread Starter

kasuci

Joined Jan 26, 2012
4
Thank you all for your input.
I mannaged to solve problems and everything is working
as it was asked in the assigment.

Next one will be more complicated and more specific in details I guess.
 
Top