Freescale MC9S08QG8 microcontroller project help

Thread Starter

johhny101

Joined May 5, 2014
2
This semester I had my first microcontrollers class and cant seem to get enough of it. For the classes final project my professor gave each student the choice between 4 different projects. I decided that I wanted to go through and do them all for practice. The program that I am working on takes a string of ASCII characters and sends them over the SCI and converts it to morse code. The following code is what I have done so far but I am having trouble with the last ISR and dont really have a clue how to do what the instructions says. The part I am confused about is under the line of asterisks.
Any help would be appreciated. Thanks!

Rich (BB code):
#include <hidef.h> /* for EnableInterrupts macro */ 
#include "mc9s08qg8.h" /* include peripheral declarations */ 
 
#define RBUFSIZ 80 
#define BAUD 9600 
#define SCIBDVAL (1000000+BAUD)/(2*BAUD) 
#define INITIAL_UNIT 25 
#define Morse_Unit 25 
    extern unsigned const char morsecode[]; 
    static unsigned char UnitDelay=INITIAL_UNIT, UnitCountDown=INITIAL_UNIT; 
    static unsigned char MorseState=0; 
    static unsigned char rcvbuff[RBUFSIZ] = "hello there"; 
    static unsigned char *rcviptr, *rcvoptr; // my code 
    static unsigned char chara; 
    static unsigned char codechar; 
    
void main(void) { 
    unsigned int i;      
    SOPT1 = SOPT1_BKGDPE_MASK+SOPT1_RSTPE_MASK; 
    PTAPE = 0xff; 
    PTBDD = 0xff; 
 
    ICSTRM = NV_ICSTRM; 
 
    for (i=0;i<400;i++); /* delay 2 ms (at 4 MHz) */  
   
    ICSC2 = 0;  /* up speed to 8 MHz */ 
   
    PTADD_PTADD0 = 1; /* set pin 16 as an output */ 
    PTADS_PTADS0 = 1; /* make capable of driving LEDs */ 
     
    SCIBD = SCIBDVAL; 
    SCIC2 = SCIC2_RE_MASK+SCIC2_RIE_MASK; 
     
     
/* Correctly complete these 2 lines to produce 250 TOF interrupts per sec */ 
    MTIMSC = 0b01010000; 
    MTIMMOD = 249; 
 
/* The following configures TPM Channel 0 to toggle pin 16 */ 
    TPMC0SC = 0x14; 
/* The defalult for all other registers is okay except */ 
/* the modulo register sets the frequency of the tone produced */ 
/* on pin 16 according to the formula: freq = 8000000/(2*(TPMMOD+1)) */ 
   TPMMOD = 300;  /* pick a value for a freq between 500-2000 */     
     
 
    rcvoptr = rcvbuff; 
    rcviptr = rcvbuff+11;  /* "hello there" is in buff */   
 
    EnableInterrupts; /* enable interrupts */ 
 
    for(;;);   /* loop forever leaving all work to the ISRs */ 
} 
 
 
interrupt VectorNumber_Vmtim void mtimisr() { 
    static unsigned char charcode; 
    MTIMSC_TOF = 0; 
    if (--UnitCountDown==0)    
       { 
       UnitCountDown = Morse_Unit; // default delay will be 1 
       switch (MorseState) 
          { 
          case 0: 
            if (rcvoptr != rcviptr) 
               { 
               chara = *rcvoptr++; 
               if (rcvoptr > rcvbuff+RBUFSIZ) 
                  rcvoptr = rcvbuff; 
               if (chara ==' '){ 
                {UnitCountDown = 3*Morse_Unit; break;}  
             
               codechar = (unsigned char)morsecode[chara]; 
               startsound(); 
               MorseState=1; 
               } 
               if (codechar & 0x80==0x80) 
                /*most significant bit of codechar is 1*/ 
                  UnitCountDown = 3*Morse_Unit;    //UnitCoundDown changed from    MorseCountDown 
                
            break; 
          case 1: 
            /*insert code to stop sound and advance to state 2*/ 
             
            stopsound(); 
            MorseState = 2; 
                
               
           
            break; 
          case 2: 
            /* the code here first shifts codechar one bit left */ 
             
            codechar = (codechar << 1); 
            /* then it checks if it is 0x80 indicating char complete */ 
            if(codechar = 0x80)  
               
            MorseState = 0; 
             
            /* follow the state arrow indicated from the above result */ 
                /* if char not complete code is a copy of the four lines */ 
            /* starting with startsound() from state 0 above */ 
              else{ 
              startsound(); 
              MorseState = 1; 
              } 
               if ((codechar & 0x80)==0x80){ 
                 
                  UnitCountDown = 3*Morse_Unit;  
                } 
          break; 
           } 
       } 
    } 
} 
void startsound() { 
/*  add code to start sound by starting TPMCNT counting 
    by setting the the CLKSA bit in TPMSC.  This selects 
    the bus clock as the clock for the counter. */ 
            TPMSC_CLKSA = 1; 
    } 
     
void stopsound() { 
/*  add code to stop sound (clear CLKSA which selects nothing 
    as the clock for the counter thus disabling it */ 
           TPMSC_CLKSA = 0; 
    } 
    
interrupt 15 void scirisr() { 
    static unsigned char charb; 
    charb = SCIS1; 
    charb = SCID; 
  //*********************************************************
 //This part below is what I am having Trouble with.  
 /* test to see if the character is one of any special characters you picked */ 
    /* to vary the speed or tone frequency of the Morse code being produced */ 
    /* if it is do the specified change.  The speed is modified by changing the */ 
    /* value of MorseUnit.  The freq of the tone is modified by changing TMPMOD. */ 
    /* if not command char, place SCID value in the circular receive buffer */ 
 } 
   
/* complete the constant table below */  
  unsigned const char morsecode[] =  
    {0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0, 
     0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0, 
     0x80,0,0,0,0x13,0,0,0,                          /* sp - - - $ - - - */ 
     0,0,0,0,0xce,0,0x56,0,                          /*  - - - - , - . - */ 
     0xfc,0x7c,0x3c,0x1c,0x0c,0x04,0x84,0xc4,        /*  0 1 2 3 4 5 6 7 */ 
     0xe4,0xf4,0xe2,0xaa,0x00,0x00,0x00,0x32,        /*  8 9 : ; - - - ? */ 
     0x00,0x60,0x88,0xa8,0x90,0x40,0x28,0xd0,        /*  - A B C D E F G */ 
       0x08, 0x20, ox78, 0xb0, 0x98, 0xe0, 0xa0,       /*  H,J,K,L,M,N,O*/ 
     0xf0,0x98,0xb8,0xc8,0,0,0,0,0,                  /*  X Y Z - - - - - */ 
     0x00,0x60,0x88,0xa8,0x90,0x40,0x28,0xd0,        /*  - a b c d e f g */ 
       0x68, 0xd8, 0x50, 0x10, 0xc0, 0xc0, 0x30,       /*p, q, r,s,t,u,v*/ 
     0x18, 0x60,0x98,0xb8,0xc8,0,0,0,0,0};           /*  x y z - - - - - */ 
 
/* end of source file */
 

ErnieM

Joined Apr 24, 2011
8,377
" //This part below is what I am having Trouble with. "

Please define "trouble." If you mean it does not compile check line 152: "ox78" should be "0x78".
 

Thread Starter

johhny101

Joined May 5, 2014
2
Sorry about some of the sloppy code. I wrote it in a text editor since I am doing this from my house and dont have a system that is running Windows and Codewarrior does not support unix like systems. What I cant figure out is how to change the length of the tone and produce a "dit" or a "dah" when I get a certain Character using the Constant table
 
Top