⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sm1.c

📁 用freescale 8bitMCU做的触摸屏软件
💻 C
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : SM1.C
**     Project   : touchpanal
**     Processor : MC9S08QG8CDT
**     Beantype  : SynchroMaster
**     Version   : Bean 02.264, Driver 01.17, CPU db: 2.87.115
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 2007-10-10, 13:20
**     Abstract  :
**         This bean "SynchroMaster" implements MASTER part of synchronous
**         serial master-slave communication.
**     Settings  :
**         Synchro type                : MASTER
**
**         Serial channel              : SPI
**
**         Protocol
**             Init baud rate          : 22_936us
**             Clock edge              : falling
**             Width                   : 8 bits (always)
**             Empty character         : 0
**             Empty char. on input    : RECEIVED
**
**         Registers
**             Input buffer            : SPID      [$002D]
**             Output buffer           : SPID      [$002D]
**             Control register        : SPIC1     [$0028]
**             Mode register           : SPIC2     [$0029]
**             Baud setting reg.       : SPIBR     [$002A]
**
**             Priority                : undef
**
**             Priority                : undef
**
**         Used pins                   :
**         ----------------------------------------------------------
**              Function    | On package |    Name
**         ----------------------------------------------------------
**               Input      |     8      |  PTB4_MISO
**               Output     |     9      |  PTB3_KBIP7_MOSI_ADP7
**               Clock      |     10     |  PTB2_KBIP6_SPSCK_ADP6
**         ----------------------------------------------------------
**
**     Contents  :
**         RecvChar - byte SM1_RecvChar(SM1_TComData *Chr);
**         SendChar - byte SM1_SendChar(SM1_TComData Chr);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2006
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/


/* MODULE SM1. */

#include "SM1.h"
#include "AS1.h"
#include "TI1.h"
#include "KBI1.h"
#include "Events.h"


/* Internal method prototypes */

#define OVERRUN_ERR      0x01          /* Overrun error flag bit   */
#define CHAR_IN_RX       0x08          /* Char is in RX buffer     */
#define FULL_TX          0x10          /* Full transmit buffer     */
#define RUNINT_FROM_TX   0x20          /* Interrupt is in progress */
#define FULL_RX          0x40          /* Full receive buffer      */

static byte SerFlag;                   /* Flags for serial communication */
                                       /* Bits: 0 - OverRun error */
                                       /*       1 - Unused */
                                       /*       2 - Unused */
                                       /*       3 - Char in RX buffer */
                                       /*       4 - Full TX buffer */
                                       /*       5 - Running int from TX */
                                       /*       6 - Full RX buffer */
                                       /*       7 - Unused */
static SM1_TComData BufferRead;        /* Input char SPI commmunication */

/*
** ===================================================================
**     Method      :  SM1_RecvChar (bean SynchroMaster)
**
**     Description :
**         If any data is received, this method returns one
**         character, otherwise it returns an error code (it does
**         not wait for data).
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - A pointer to the received character
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_RXEMPTY - No data in receiver
**                           ERR_OVERRUN - Overrun error was detected
**                           from the last char or block received. In
**                           polling mode, this error code is
**                           returned only when the hardware supports
**                           detection of the overrun error. 
**                           ERR_FAULT - Fault error was detected
**                           from the last char or block received.
**                           This error may not be supported on some
**                           CPUs (see generated code).
** ===================================================================
*/
byte SM1_RecvChar(SM1_TComData *Chr)
{
  byte FlagTmp;

  if (!(SerFlag & CHAR_IN_RX)) {       /* Is any char in RX buffer? */
    return ERR_RXEMPTY;                /* If no then error */
  }
  EnterCritical();                     /* Save the PS register */
  *Chr = BufferRead;                   /* Read the char */
  FlagTmp = SerFlag;                   /* Safe the flags */
  SerFlag &= ~(OVERRUN_ERR | CHAR_IN_RX | FULL_RX); /* Clear flag "char in RX buffer" */
  ExitCritical();                      /* Restore the PS register */
  if ((FlagTmp & OVERRUN_ERR) != 0) {  /* Has the overrun occured? */
    return ERR_OVERRUN;                /* If yes then return error */
  } else {
    return ERR_OK;
  }
}

/*
** ===================================================================
**     Method      :  SM1_SendChar (bean SynchroMaster)
**
**     Description :
**         Sends one character to the channel.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Chr             - Character to send
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - Device is disabled (only
**                           if output DMA is supported and enabled)
**                           ERR_TXFULL - Transmitter is full
** ===================================================================
*/
byte SM1_SendChar(SM1_TComData Chr)
{
  if (SerFlag & FULL_TX) {             /* Is any char in the TX buffer? */
    return ERR_TXFULL;                 /* If yes then error */
  }
  EnterCritical();                     /* Save the PS register */
  SPID = Chr;                          /* Store char to transmitter register */
  SerFlag |= FULL_TX;                  /* Set the flag "full TX buffer" */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_Interrupt (bean SynchroMaster)
**
**     Description :
**         The method services the error interrupt of the selected 
**         peripheral(s) and eventually invokes the bean's event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_ERROR    0x01
#define ON_FULL_RX  0x02
#define ON_RX_CHAR  0x04
#define ON_FREE_TX  0x08
#define ON_TX_CHAR  0x10
ISR(SM1_Interrupt)
{
  SM1_TComData Data;                   /* Temporary variable for data */
  byte Flags = 0;                      /* Temporary variable for flags */
  byte Status;                         /* Temporary variable for flags */

  Status = SPIS;                       /* Read the device error register */
  Data = SPID;                         /* Read data from receiver */
  if (SerFlag & CHAR_IN_RX) {          /* Is the overrun error flag set? */
    SerFlag |= OVERRUN_ERR;            /* If yes then set the OnError flag */
    Flags |= ON_ERROR;                 /* If yes then set the OnError flag */
  }
  SerFlag |= CHAR_IN_RX;               /* Set flag "char in RX buffer" */
  BufferRead = Data;                   /* Read data from receiver */
  SerFlag &= ~FULL_TX;                 /* Reset flag "full TX buffer" */
  if(Flags & ON_ERROR) {               /* Is the error flag set? */
    SM1_OnError();                     /* If yes then invoke user event */
  }
  else {
    SM1_OnRxChar();                    /* If yes then invoke user event */
  }
  SM1_OnTxChar();                      /* If yes then invoke user event */
}

/*
** ===================================================================
**     Method      :  SM1_Init (bean SynchroMaster)
**
**     Description :
**         Initializes the associated peripheral(s) and the bean internal 
**         variables. The method is called automatically as a part of the 
**         application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void SM1_Init(void)
{
  SerFlag = 0;                         /* Reset all flags */
  #pragma MESSAGE DISABLE C4002        /* Disable warning C4002 "Result not used" */
  (void)SPIS;                          /* Read the status register */
  (void)SPID;                          /* Read the device register */
  /* SPIBR: ??=0,SPPR2=0,SPPR1=1,SPPR0=0,??=0,SPR2=1,SPR1=0,SPR0=0 */
  setReg8(SPIBR, 0x24);                /* Set the baud rate register */ 
  /* SPIC2: ??=0,??=0,??=0,MODFEN=0,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */
  setReg8(SPIC2, 0x00);                /* Configure the SPI port - control register 2 */ 
  /* SPIC1: SPIE=1,SPE=0,SPTIE=0,MSTR=1,CPOL=0,CPHA=0,SSOE=0,LSBFE=1 */
  setReg8(SPIC1, 0x91);                /* Configure the SPI port - control register 1 */ 
  SPIC1_SPE = 1;                       /* Enable device */
}


/* END SM1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 3.00 [03.89]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -