


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 (-- --- - *** * -- --- -** *) // // 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/////////////////////////////// ///////////// #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);
////////////////////////////////Main Program///////////////////////////////////////// int main(void)
{ // Initialization: //***************** unsigned int len,i,j,ascii,index,code_len,check;
unsigned char text[]="Paris";
// Processing: //*************
len=strlen(text); // Integer 'len' stores the length of 'text' DDRB|=1; PORTB=0;
while(1) { check=0; // This variable is used, to check if consecutive spacebars are present in the string 'text' // i.e if check!=0 => There are consecutive spacebars. for(i=0;i<len;i++) { ascii=text[i]; // Integer 'ascii' stores the ascii value of the ith character of 'text' index=get_index(ascii); // 'index' gets the respective index of the current character in the morse-code array if((index==100)&&(check==0)) // Gives 1 only when current character is a spacebar, and the preceeeding character was'nt a spacebar. { wait(7); check++; } 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 }
indx=char_asci-87; // For small alphabets } else if(char_asci==32) { indx=100; // A random value to show that current character is spacebar } else { indx=index_noncon(char_asci); // for special and punctuation characters } return(indx); }
// Index Noncon //**************** // int index-noncon (int asci) takes the ascii value of non contigous 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, 0}; // 'ascii-nonc' holds the ascii values of non contigous 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. }
// ****************************************