How to use text as inputs?

Discussion in 'Programmer's Corner' started by 500ml, Mar 22, 2011.

  1. 500ml

    Thread Starter New Member

    Mar 22, 2011

    I am working on a project which is to read some text from external memory and turn on LED acording to the morse code. E.g. for ABC first the LED 1 would come on and the for B LED's 2 3 and 4 would come on and for C LED's 2 and 4 would come on. I am using PIC16F84a and IAR workbench or maby Flowcode.

    At the moment i'm just trying to get a simple form of this to work. All i want to do is read a word from within the code and turn on LED's acordingly.

    I am thinking of making a table with all the possible output patterns (e.g. for a = 10000, b =01110,c = 01010) in the alphabetic sequence. Then i could ask the program to refer to the table to see which LED's to turn on.

    Where i need help is where do i save the text being read and how do i use it as an input? so in other words how do i say if first letter of text is 'A' go to table and output pattern? And how could i impliment the table?

    BTW I'm not very good at C:confused:.

    Any Help will be apreciated! Thanks
  2. mbabayan


    Jul 12, 2010
    First of all, morse code for A is "dot dash" - not sure what do you mean by "10000" -

    as for the table, assuming you have found a way to represent the Morse representation of a character as an "int",
    'A' is ASCII 65, 'B' is 66, etc. If you have an "int[] table" array, and "char ch" is your character, then table[(int)(ch - 'A')] would give you your int representation of a char. need to uppercase letters first.
  3. 500ml

    Thread Starter New Member

    Mar 22, 2011
    Yes i understand morse code 'A' is "dot dash" I was going to represent dot with an on(1) LED and dash with an off(0) one with five LED's total. But i do realise that has a problem of all the off LED's meaning dash when they dont so now i'm thinking of having a line of Green LED's to represent the dash. so everything will be done twice once for dot and the other for dash. I hope that explains it.

    so, if i say something like
    int [ 10000, 01110, 01010 .....etc] //all the patterns for "dot" being on

    char ch = "ALPHA"

    out = [(int)(ch-A)]

    could i send the value of "out" to port B?
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    If each morse code letter is made of up of 5 dash/dots. You have 3 bits in a byte you can use to decide the lenght In the look-up table. Then you can have a read/green led combination. Or even cooler a dual colored LED. You will have to use 2 ports(dor port and dash port). Some bit bit twiddling will be needed. But it should not take to long to code. Which C compiler do you use.
  5. mjhilger


    Feb 28, 2011
    t06afre has given you great direction. However, there are a few morse signals (error, question mark, etc.) that are more than 5 dit/dah's long. It depends on what text you are trying to turn into the lights (BTW I like the 2 color led suggestion). If you don't mind, then all is good. Just thought I would mention it because you would have to redesign everything if you need them.
    I used morse as an error output a few years ago and had a PIC routine to send it out a single pin. It used a table of size byte for length and of size byte for output (nothing is longer than 8). There is a way to code tables into the code space saving the RAM, so it should fit in your PIC either using 2 bytes per char or 1 as has been suggested.

    . = .-.-.-
    , = --..--
    : = ---...
    ; = -.-.-.
    ? = ..--..
    ! = -.-.--
    / = -..-.
  6. 500ml

    Thread Starter New Member

    Mar 22, 2011
    Thank You all for all the help. I've managed to make it work. I havent added all the morse code to it yet just been testing with three characters "abc" it works fine as i wanted it to.

    mjhilger - I dont think i'll be using the symbols(,.?!) but i've written the code with eight outputs so if i change my mind i just need to add the pattern to the table.

    The table didn't work when i put the patterns in binary but it did when i changed it to HEX. Dont know why? but it works now so i'm happy!

    I might add a potentiometer to change the delay and a few buttons for "previous" and "next" word/sentence. And I might add dual colour LED in the future but at the moment i've got plenty of normal LED's laying about so i'll just use them.

    Here's the code so far if it helps anyone else!

    Code ( (Unknown Language)):
    1. #include <io16f877a.h>
    2. void del ();
    4. const char patr []={ 0x40 , 0x70, 0x50 };
    5.           //01000000,
    6.           //01110000,
    7.           //01010000};
    9. //char txt [10];     //the text to be coded
    10. char msg []={"acb"};
    11. void main ()  
    12. {
    14.     unsigned int lp;
    16.     TRISB = 0;      //configure port B as output
    17.     PORTB = 0;      //set port be to 0
    19.   /*for (lp=0;lp<8;lp++)
    20.   {
    21.       txt[lp] = msg[lp];
    22.   }*/
    23.     while(1){
    25.     for (lp = 0; lp < 3; lp ++)
    26.     {
    27.         PORTB = patr[msg[lp]-0x61];   //send pattern patr of ch-60 to portb
    29.         del();
    30.      }
    31.     }
    32. }
    34. void del(void)                  //delay function
    35. {
    36.   unsigned int i;          //clear tmr0
    37.   for(i=0;i<32000;i++);    //wait for a while
    38. }                                                                    
    BTW ignore some of the comments they dont make any sense. I changed the lines without changing the comments. OUPS!
  7. mjhilger


    Feb 28, 2011
    I'm glad you have it working. But I don't know how you will read the code as the length is very important to know which char and without dual colors how do you know how many dots and dashes make up the char. (BTW your table is out of sequence, you have acb not abc while your bit pattern is abc - sort of should be a = .-; 0x80 ).
    Because [a = .- while w = .-- conversly n = -. while d = -..]
    So if you only light for dots or only for dashes you will not know if it is an 'a' or 'w' lit for dots; or lit for dashes you can't tell 'n' from 'd'.
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    yes that is correct you need 3 levels. dash/dot and nothing at all.
  9. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    I did a morse code generator that uses standard timings and frequency, and the C code can easily be adapted to your needs.

    See the project at the bottom of this page;

    The WPM can be adjusted from 5 to 35 WPM.
  10. 500ml

    Thread Starter New Member

    Mar 22, 2011
    The pattern was only a test sample so the sequence didnt matter. i've got it sorted now with all the patterns.

    i dont think you understand what i mean by doing it with different colours. for a the out put will be like:
    RED LED: 10000000
    Green LED: 01000000 while Red = dit and Green = dah

    so you could see LED's RG which can be read as dit dah

    W would be:
    RED LED: 10000000
    Green LED: 01100000

    so you could see LED's RGG which can be read as dit dah dah
    The off LED's can be ignored. I hope that explains it.

    I do have another question. I am trying to add a way to go to the start of the next or previous word. but for this i first need to divide the sentence in the message in to words in an array. then i can use the method i use to move to next character to move to next word.

    Code ( (Unknown Language)):
    1. char msg []={"today is a good day"};
    or is there a better way of doing it. All i need to do is to go to the previous or next space ' '?

    @ THE_RB thanks. that project helped with some stuff. but unfortunately nothing i could use for this.

    Thank you all for the help.