Thread: Trying to get the 1 second interrupt into my code. Help!

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    9

    Trying to get the 1 second interrupt into my code. Help!

    Can someone please help! This is probably really simple but here goes.
    Ok this code programs a freescale HCs12 board with a serial input along with a lcd tower for starters. The board takes input from a external keyboard connected to the serial port of the HCS12. It then displays up to 16 bits, as x's, on the lcd's first line. When it has reached 16 bits the buffer is full and the program halts to display buffer full. The program uses a FIFO method. I need the program to Use RTI to interrupt in 1 second intervals to retrieve the stored data and display it onto the LCD. I have most of the interrupt written, I believe. It just doesn't work. Am I implementing it right. I have included a photo of the display. I am also using a terminal program to take input from keyboard. Let me know if I need to add more info. Thanks in advance.

    http://www.ee.nmt.edu/~erives/308L_11/AN2882.pdf
    Trying to get the 1 second interrupt into my code. Help!-img_20140405_233450-jpg

    Trying to get the 1 second interrupt into my code. Help!-img_20140405_233347-jpg




    Code:
    #include <hidef.h>            /* common defines and macros */
    #include "derivative.h"       /* derivative-specific definitions */
    #include <stdio.h>
    #include <termio.h>
    
    
    void initLCD(void);
    void initPorts(void);
    void cmdwrt(unsigned char);
    void datawrt(char);
    void delay1u(void);
    void delay100u(void);
    void delay2m(void);
    void delays(int);
    void LCDputs(char *);
    void position(int, int); 
    void initSCI(void);
    void writeSCI(char);
    void InitFifo(void);
    int PutFifo (char);
    int GetFifo (char *);
    char REC_SCI(void);
    void initSCI(void);
    void isrRti(void);
    
    
    
    
    unsigned char flags;
    
    
    #define One_Sec_Past 0x01
    #define FifoSize 16
    
    
    
    
    
    
    char *PutPt;                      /* put next ptr */
    char *GetPt;                /* get next ptr */
    char Fifo[FifoSize];          /* statically allocated fifo data */
    
    
    
    
     
    
    
    //globals - 4-bit initialization sequence for LCD  -  data: PC7,6,5,4  E: PC2 , RS: PC0
    const unsigned char cInit_commands[20] = {0x30,0x30,0x30,0x20,0x20,0x90,0x10,0x50,0x70,0x80,0x50,0xE0,
                                              0x60,0xA0,0x00,0xE0,0x00,0x10,0x00,0x60};
    
    
    const unsigned char cE  = 0x04;         // PC2
    const unsigned char cRS = 0x01;         // PC0
    
    
    #define BAUD    9600L         // Baud rate to be set at 9600 bits per second
    #define SYSCLK  6250000L      // Default system clock is 6.25 MHz
    
    
    void main(void) 
    {
    
    
      char read_temp, butterGet, foo[10];
      unsigned char i; 
    
    
      initPorts( );                        // port initializations
      initLCD( );                          // LCD inititialization
      initSCI();
    
    
      cmdwrt(0x00);
      cmdwrt(0xC0);                        // disable the cursor
    
    
      InitFifo();
      
      //TSCR1 = 0x80;         // enable timer with fast flag clear automatically
        //TSCR2 = 0x86;       // no overflow interrupt, prescaler = 128
      
      //TFLG2 = 0x00;      // reset ch0 Int flag
      //TIE = 0x01;         // OC ch0 interrupt
      
      TC0 = TCNT + 31250; // record counts for 1 second
                          // 4MHz / 128 = 31250 Hz , Simply use TC0 = TCNT + 31250 to obtain 1 second
      EnableInterrupts;
      
        
      while(1) 
        {
          read_temp = REC_SCI();
    
    
          if (flags & One_Sec_Past) 
          {
              i = GetFifo(&butterGet);
            //position(1, (GetPt - Fifo - 1));     // Move cursor to first row and retrieve the data, remove x
              datawrt(' ');                        // Because the pointer is post increment, so minus 1
    
    
            cmdwrt(0x90);
              cmdwrt(0x00);
              datawrt(butterGet);
          
              if (i == 0)
                LCDputs("  Queue Empty");
              else
              LCDputs("             ");    
                
          } else{
                  if (-1 != PutFifo(read_temp))
                {
                  cmdwrt(0x90);
                    cmdwrt(0x00);
                  LCDputs("Error,Queue Full");
                    break;
                }
                else
                {
                    i = PutPt - Fifo;
                  position(1, i-1);                    // Find the first row location and add an "x"
                    sprintf(foo,"%d",i-1);               // Because the pointer is post increment, so minus 1
                    puts(foo);                         // Send its ASCII code back
                    datawrt('x');
                    
                    cmdwrt(0x90);                       // clear second row of LCD
                  cmdwrt(0x00);
                  LCDputs("                ");
              }
            }
            
              flags &= ~One_Sec_Past;  
            
        }
       
       
      for (;;) {
      }
    
    
    }
    
    
    
    
    void InitFifo(void) 
    { 
    
    
      // Your work ??
      
      PutPt = GetPt =& Fifo[0];
    
    
    }
    
    
    
    
    
    
    int PutFifo (char data) 
    { 
      // Your work ??
      
      char *Ppt;
      
      Ppt = PutPt;
      *Ppt++ = data;
      
      printf("%d\n\r",*(Ppt-1));
      
      if(Ppt == &Fifo[FifoSize])
        Ppt = &Fifo[0];
      
      if(Ppt == GetPt) {
        puts("Buffer Full!\n");
        return(0);
      } 
      else{
        PutPt = Ppt;
        puts("Rcv'd\n\r");
        return(-1);
      }
    
    
    }
    
    
    
    
    int GetFifo (char *datapt) 
    { 
      // Your work ??
      
      if(PutPt ==GetPt)
        return(0);
      else {
        *datapt = *GetPt++;
        if(GetPt == &Fifo[FifoSize]) {
          GetPt = &Fifo[0];
        }return(-1);
      }
    }
    
    
    
    
    void initSCI()
    {    
    
    
        SCI0BD = (unsigned int)(SYSCLK/BAUD/16);    // Set Baud Rate for UART
        SCI0CR1 = 0;                                        // UART configuration: 8 data bits, No parity bit, 1 stop bit
        SCI0CR2 = SCI0CR2_TE_MASK | SCI0CR2_RE_MASK;// SCI Tx and Rx enabled
        DDR0AD_DDR0AD7 = 0;                                // PAD15 is connected to RS232 tranceiver enable    
        PPS0AD_PPS0AD7 = 0;                         // pull-up port1AD15
        PER0AD_PER0AD7 = 1;                                // Make PAD15 input and enable pullup to activate RS232
    }
    
    
    
    
    
    
    char REC_SCI()
    {
     // Your work ??
     char c;
     
     while(!SCI0SR1_RDRF);
     ;;
     c = SCI0DRL;
     return c;
             
    }
    
    
    
    
    
    
    void position(int iRow_value, int iCol_value) 
    {
         int iPos_h, iPos_l, iValue;
         
         if(iRow_value == 1) 
            iValue = (0x80+0x00);
         
         if(iRow_value == 2) 
            iValue = (0x80+0x10);
         
         if(iRow_value == 3) 
            iValue = (0x80+0x20);
         
         iPos_h = ((iValue + iCol_value) & 0xF0);
         iPos_l = ((iValue + iCol_value) & 0x0F) << 4;
         
         cmdwrt(iPos_h);
         cmdwrt(iPos_l);
    }
            
    
    
    
    
    //Sends a string of characters to the LCD;...  
    void LCDputs(char *sptr)
    {  
         while(*sptr)
         {                                //...the string must end in a 0x00 (null character)
            datawrt(*sptr);               // sptr is a pointer to the characters in the string
            ++sptr;
         }
    }  
                                              
    
    
    
    
    // sends initialization commands one-by-one
    void initLCD( )
    {
      unsigned char i;
      
      for (i=0;i<20;i++)
      {  
        cmdwrt(cInit_commands[i]);
      }  
    }
    
    
     
    void initPorts( )
    { 
      unsigned char cValue;
      
      DDRB   = 0x80;                        //LCD CSB active low
      DDRC   = 0xFF;                        // PC7-PC4 - 4-bit LCD data bus, PC2 - E, PC1 - R/W~, PC0 - RS: all outputs
      cValue = PORTB;
      PORTB  = (cValue & ~0x80);            // LCD CSB (PORT B7) enabled with a logic low
      PORTB  = (cValue & ~0x80);            // LCD CSB (PORT B7) enabled with a logic low
      DDRP   = 0x01;
      PTP   |= 0x01;
    }  
      
    
    
    // sends a control word to LCD bus
    void cmdwrt(unsigned char cCtrlword)
    {  
      PORTC = cCtrlword;   // output command onto LCD data pins
      PORTC = cCtrlword + cE;   // generate enable pulse to latch it (xxxx x100)
      
      delay1u( );    // hold it for 1us
      
      PORTC = cCtrlword;    // end enable pulse  (xxxx x000)
      
      delay2m();    // allow 2ms to latch command inside LCD
      
      PORTC = 0x00;
      
      delay2m();    // allow 2ms to latch command inside LCD
    }
    
    
    
    
    
    
    
    
    // sends the character passed in by caller to LCD 
    void datawrt(char cAscii)
    {                   
      char cAscii_high, cAscii_low;
      
      cAscii_high = (cAscii & 0xF0);
      cAscii_low  = (cAscii & 0x0F) << 4; // Shift left by 4 bits 
      PORTC = cAscii_high;                // output ASCII character upper nibble onto LCD data pins
      PORTC = cAscii_high + cRS + cE;     // generate enable pulse to latch it  (0xxx x101)
      
      delay1u( );                         // hold it for 1us
      
      PORTC = cAscii_high + cRS;          // end enable pulse   (0xxx x001)
      
      delay1u( );                         // hold it for 1us
      
      PORTC = cAscii_low;                 // output ASCII character lower nibble onto LCD data pins
      PORTC = cAscii_low + cRS + cE;      // generate enable pulse to latch it  (0xxx x101)
      
      delay1u( );                         // hold it for 1us
      
      PORTC = cAscii_low + cRS;           // end enable pulse   (0xxx x001)
      
      delay100u( );                       // allow 100us to latch data inside LCD
    }
    
    
    
    
    
    
    
    
    void delay1u( )
    {
      unsigned int i;
      
      for(i=0;i<=0x0f;i++)
      { /* adjust condition field for delay time */
        asm("nop");
      }
    }
    
    
    void delay100u( )
    {
      unsigned int i,j;
      
      for(i=0;i<=0x02;i++)
      {  /* adjust condition field for delay time */
        for(j=0;j<=0xff;j++)
        {
           asm("nop");
        }
      }
    }
    
    
    void delay2m( )
    {
       unsigned int i,j; 
       
       for (i=0;i<=0x20;i++)
       { /* adjust condition field for delay time */
         for (j=0;j<=0xff;j++)
         {
          asm("nop");
        }     
       }
    }   
    
    
     
    void delays(int k )
    {
       unsigned int i,j; 
       
       for (i=0;i<=k;i++)
       { /* adjust condition field for delay time */
         for (j=0;j<=0xff;j++)
         {
          asm("nop");
        }     
       }
    }     
      
    
    
    
    
    
    
    /******* Interrupt Service Routine ********/
    #pragma CODE_SEG NON_BANKED 
    interrupt ( ( (0x10000 - 0xFFEE) / 2 ) - 1 )  void isrOC_ch0(void)
    {
       TC0 = TCNT + 31250; // reset for next second
       flags |= One_Sec_Past;
    }
    
    
    #pragma CODE_SEG DEFAULT
    Last edited by runhard33; 04-06-2014 at 02:23 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I would suggest you create another program just to test your timer ISR code.

    Say for example, toggle the state of a LED once a second.
    You'll have a much shorter program to look at, and it'll allow you to really focus on one issue at a time.


    Also, please post a URL for your devboard.
    I found this, but it's probably the wrong one.
    LFEBS12UB: 16-bit HCS12 DG128 Stand Alone Student Learning Kit
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code for hcs12 towerw/ lcd and interrupt question???
    By runhard33 in forum C Programming
    Replies: 5
    Last Post: 11-10-2013, 12:27 PM
  2. Interrupt Question
    By formulajoe in forum C Programming
    Replies: 6
    Last Post: 08-28-2006, 12:31 AM
  3. ASM interrupt and others
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-08-2002, 07:18 PM
  4. interrupt
    By juandy in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2002, 08:24 AM
  5. Interrupt???
    By GiSH in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2001, 12:27 PM