



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




// Project: Morse Code (-- --- - *** * -- --- -** *)
// Author: Amna Ashfaq Registration #: FA06_BET_
// Module description:
// Writing a c-program and simulating it in VMLAB to transmit Morse Code.
// Text to be transmitted will be contained in an ascii-Z string.
// Notes:
// 1. The program should work for any string of characters both in upper and lower case.
// 2. Any illegal characters should simply be dropped.
// ***********************************************************
#include <avr\io.h> // Most basic include files
#include <avr\interrupt.h> // Add the necessary ones
#include <avr\signal.h> // here
#include <string.h> //For using the function 'strlen' in order to find string length
//////////////////////////////////Constant and Global Variable////////////////////////////////////////////
#define DELAYTIME 10
char morse_code[53][7]={"0-----","1----","2---","3--","4****-","5*****","6-****","7--","8--- ","9----","A-","B-","C--","D-","E","F-","G--","H****","I","J---","K--","L-","M-- ","N-","O---","P--","Q---","R-","S","T-","U-","V-","W--","X--","Y---","Z--","'---- ","(----",")----",",----","--****-",".---","/--","?--","_---",":---",";---","Á--- ","Ä--","É-","Ñ----","Ö---","Ü**--"};
//////////////////////////////Function Prototypes/////////////////////////////////////
void wait(int);
int get_index(int);
int index_noncon(int);
else if((index!=-1)&&(index!=100)) // Gives 1, when the current character is valid and is not a spacebar.
{
check=0; // sets the value back to zero, for next spacebar to be transmitted.
code_len=strlen(morse_code[index]); // 'code_len' stores the string length of the index'th row of the 2d array 'morse_code'
// e.g code-len = strlen(morse_code[10]);
// => code-len = 3 i.e 10th row of 'morse_code' has 3 characters.
for(j=1;j<code_len;j++) // loop to get the morse code of index'th character from 'morse_code'
{
PORTB=1;
if(morse_code[index][j]=='*')
{
wait(1); // When reads '*' i.e. a di, it waits for 1 unit
}
else
{
wait(3); // When reads '-' i.e. a dah, it waits for 3 units
}
PORTB=0;
wait(1); // Waits for 1 unit, i.e. space between two characters
}
wait(3); // Waits for 3 units, i.e. space between two letters
}
}
wait(7); // to distinguish two words or sentences, when they'll be repeated in the infinite loop
// The speed is roughly 32 words per minute for 8MHz clock
}
/////////////////////Function definitions/////////////////////
// Wait
//************
// void wait (int pause) keeps the processor busy roughly for pause times 50 microseconds
// i.e. for pause = 1 it waits for 50 microseconds
// for pause = 3 it waits for 130 microseconds
// for pause = 7 it waits for 500 microseconds
void wait(int pause)
{
int time;
for(time=0;time<(pause*(DELAYTIME));time++);
}
// GET Index_
//**************
// int get-index( int char-asci) takes the ascii value of a character (in decimal) and returns the respective index of that character as defined in 'morse_code'
// The characters present in the International Morse Code Table can be divide into four classes:
// 1. Alphabets (both upper and lower case)
// 2. Numeric
// 3. Punctuation
// 4. Special characters
// The Alphabets and Numeric characters have contiguous ascii values, so they can be sorted out by using simple arithmetic comparisons and operations.
return(indx);
}
// Index_Noncon
//****************
// int index_noncon (int asci) takes the ascii value of non contiguous elements and returns its respective index as defined in the 2d array 'morse_code'
int index_noncon (int asci)
{
unsigned int i,ascii_nonc[17]={39,40,41,44,45,46,47,63,95,58,59,193,196,201,209,214,220};
// 'ascii_nonc' holds the ascii values of non contiguous elements
for (i=0;i<17;i++)
{
if(ascii_nonc[i]==asci)
{
return(i+36); break;
}
} return-1; // If element is not found, i.e. current character is not valid.
}
// ****************************************