Freescale MC9S08QG8 microcontroller project help

Discussion in 'Embedded Systems and Microcontrollers' started by johhny101, May 5, 2014.

  1. johhny101

    Thread Starter New Member

    May 5, 2014
    2
    0
    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!

    Code ( (Unknown Language)):
    1.  
    2. #include <hidef.h> /* for EnableInterrupts macro */
    3. #include "mc9s08qg8.h" /* include peripheral declarations */
    4.  
    5. #define RBUFSIZ 80
    6. #define BAUD 9600
    7. #define SCIBDVAL (1000000+BAUD)/(2*BAUD)
    8. #define INITIAL_UNIT 25
    9. #define Morse_Unit 25
    10.     extern unsigned const char morsecode[];
    11.     static unsigned char UnitDelay=INITIAL_UNIT, UnitCountDown=INITIAL_UNIT;
    12.     static unsigned char MorseState=0;
    13.     static unsigned char rcvbuff[RBUFSIZ] = "hello there";
    14.     static unsigned char *rcviptr, *rcvoptr; // my code
    15.     static unsigned char chara;
    16.     static unsigned char codechar;
    17.    
    18. void main(void) {
    19.     unsigned int i;      
    20.     SOPT1 = SOPT1_BKGDPE_MASK+SOPT1_RSTPE_MASK;
    21.     PTAPE = 0xff;
    22.     PTBDD = 0xff;
    23.  
    24.     ICSTRM = NV_ICSTRM;
    25.  
    26.     for (i=0;i<400;i++); /* delay 2 ms (at 4 MHz) */  
    27.    
    28.     ICSC2 = 0;  /* up speed to 8 MHz */
    29.    
    30.     PTADD_PTADD0 = 1; /* set pin 16 as an output */
    31.     PTADS_PTADS0 = 1; /* make capable of driving LEDs */
    32.      
    33.     SCIBD = SCIBDVAL;
    34.     SCIC2 = SCIC2_RE_MASK+SCIC2_RIE_MASK;
    35.      
    36.      
    37. /* Correctly complete these 2 lines to produce 250 TOF interrupts per sec */
    38.     MTIMSC = 0b01010000;
    39.     MTIMMOD = 249;
    40.  
    41. /* The following configures TPM Channel 0 to toggle pin 16 */
    42.     TPMC0SC = 0x14;
    43. /* The defalult for all other registers is okay except */
    44. /* the modulo register sets the frequency of the tone produced */
    45. /* on pin 16 according to the formula: freq = 8000000/(2*(TPMMOD+1)) */
    46.    TPMMOD = 300;  /* pick a value for a freq between 500-2000 */    
    47.      
    48.  
    49.     rcvoptr = rcvbuff;
    50.     rcviptr = rcvbuff+11;  /* "hello there" is in buff */  
    51.  
    52.     EnableInterrupts; /* enable interrupts */
    53.  
    54.     for(;;);   /* loop forever leaving all work to the ISRs */
    55. }
    56.  
    57.  
    58. interrupt VectorNumber_Vmtim void mtimisr() {
    59.     static unsigned char charcode;
    60.     MTIMSC_TOF = 0;
    61.     if (--UnitCountDown==0)    
    62.        {
    63.        UnitCountDown = Morse_Unit; // default delay will be 1
    64.        switch (MorseState)
    65.           {
    66.           case 0:
    67.             if (rcvoptr != rcviptr)
    68.                {
    69.                chara = *rcvoptr++;
    70.                if (rcvoptr > rcvbuff+RBUFSIZ)
    71.                   rcvoptr = rcvbuff;
    72.                if (chara ==' '){
    73.                 {UnitCountDown = 3*Morse_Unit; break;}  
    74.              
    75.                codechar = (unsigned char)morsecode[chara];
    76.                startsound();
    77.                MorseState=1;
    78.                }
    79.                if (codechar & 0x80==0x80)
    80.                 /*most significant bit of codechar is 1*/
    81.                   UnitCountDown = 3*Morse_Unit;    //UnitCoundDown changed from    MorseCountDown
    82.                
    83.             break;
    84.           case 1:
    85.             /*insert code to stop sound and advance to state 2*/
    86.              
    87.             stopsound();
    88.             MorseState = 2;
    89.                
    90.                
    91.            
    92.             break;
    93.           case 2:
    94.             /* the code here first shifts codechar one bit left */
    95.              
    96.             codechar = (codechar << 1);
    97.             /* then it checks if it is 0x80 indicating char complete */
    98.             if(codechar = 0x80)  
    99.                
    100.             MorseState = 0;
    101.              
    102.             /* follow the state arrow indicated from the above result */
    103.                 /* if char not complete code is a copy of the four lines */
    104.             /* starting with startsound() from state 0 above */
    105.               else{
    106.               startsound();
    107.               MorseState = 1;
    108.               }
    109.                if ((codechar & 0x80)==0x80){
    110.                  
    111.                   UnitCountDown = 3*Morse_Unit;  
    112.                 }
    113.           break;
    114.            }
    115.        }
    116.     }
    117. }
    118. void startsound() {
    119. /*  add code to start sound by starting TPMCNT counting
    120.     by setting the the CLKSA bit in TPMSC.  This selects
    121.     the bus clock as the clock for the counter. */
    122.             TPMSC_CLKSA = 1;
    123.     }
    124.      
    125. void stopsound() {
    126. /*  add code to stop sound (clear CLKSA which selects nothing
    127.     as the clock for the counter thus disabling it */
    128.            TPMSC_CLKSA = 0;
    129.     }
    130.    
    131. interrupt 15 void scirisr() {
    132.     static unsigned char charb;
    133.     charb = SCIS1;
    134.     charb = SCID;
    135.   //*********************************************************
    136.  //This part below is what I am having Trouble with.  
    137.  /* test to see if the character is one of any special characters you picked */
    138.     /* to vary the speed or tone frequency of the Morse code being produced */
    139.     /* if it is do the specified change.  The speed is modified by changing the */
    140.     /* value of MorseUnit.  The freq of the tone is modified by changing TMPMOD. */
    141.     /* if not command char, place SCID value in the circular receive buffer */
    142.  }
    143.    
    144. /* complete the constant table below */  
    145.   unsigned const char morsecode[] =  
    146.     {0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,
    147.      0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,
    148.      0x80,0,0,0,0x13,0,0,0,                          /* sp - - - $ - - - */
    149.      0,0,0,0,0xce,0,0x56,0,                          /*  - - - - , - . - */
    150.      0xfc,0x7c,0x3c,0x1c,0x0c,0x04,0x84,0xc4,        /*  0 1 2 3 4 5 6 7 */
    151.      0xe4,0xf4,0xe2,0xaa,0x00,0x00,0x00,0x32,        /*  8 9 : ; - - - ? */
    152.      0x00,0x60,0x88,0xa8,0x90,0x40,0x28,0xd0,        /*  - A B C D E F G */
    153.        0x08, 0x20, ox78, 0xb0, 0x98, 0xe0, 0xa0,       /*  H,J,K,L,M,N,O*/
    154.      0xf0,0x98,0xb8,0xc8,0,0,0,0,0,                  /*  X Y Z - - - - - */
    155.      0x00,0x60,0x88,0xa8,0x90,0x40,0x28,0xd0,        /*  - a b c d e f g */
    156.        0x68, 0xd8, 0x50, 0x10, 0xc0, 0xc0, 0x30,       /*p, q, r,s,t,u,v*/
    157.      0x18, 0x60,0x98,0xb8,0xc8,0,0,0,0,0};           /*  x y z - - - - - */
    158.  
    159. /* end of source file */
    160.                
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    " //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".
     
  3. johhny101

    Thread Starter New Member

    May 5, 2014
    2
    0
    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
     
Loading...