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

📄 siodemo.c

📁 TQ公司的STK16x开发系统的源码
💻 C
字号:
/* SIODEMO.C */
              
/***************************************************************************
*

* ---------------------------------------------
*Main module for STK16X (ASC0, 57600 Baud, 8, 1, n) demo 
*
*
* TQ-Systems GmbH
* ----------------                                                         
* Customer: TQ-Components
* Project : TQMX16xU
* Tools   : uVision 2.33
*
* Copyright (C) 1995 TQ-Systems
* -----------------------------
* Rev: Date:     Name:            Modification:
* ----+---------+----------------+------------------------------------------
* 100  16.05.03  Volker Jung      changes for XC16x Controller
* 251  29.07.03  Volker Jung	  Anpassung an TQ_Select 
  
/*****************************************************************************/

/************************************************************************/

/****************************************************************************
*
*
*                            availability summary
*
*                           
*****************************************************************************/

/*--------------------------------------------------------------------------*
* Standard-Include-Files: 
*---------------------------------------------------------------------------*/

#include <stdio.h>
#include <ctype.h>   /* Typecast functions        */
#include <string.h>  /* String functions          */
#include <setjmp.h>  /* Global jump functions     */

/*--------------------------------------------------------------------------*
* Hardware- and Compiler-spezifische Include-Files: 
*---------------------------------------------------------------------------*/

#include <absacc.h>  /* absolute accesss          */
#include <intrins.h> /* Intrinsic functions       */
#include <xc161.h>  /* Special function register */

/*--------------------------------------------------------------------------*
*product specific include-files: 
*---------------------------------------------------------------------------*/

#include "header.h"
#include "timer0.h"


/* control character: */
#define BK    0x03
#define BS    0x08
#define CR    0x0D
#define LF    0x0A
#define XON   0x11
#define XOFF  0x13

/* baudrate definition */
#define BR9600   0
#define BR19200  1
#define BR38400  2
#define BR57600  3
#define BR115200 4

/* appropriated Baudrate */
#define USED_BAUDRATE BR57600

/* control-bits serial interface: */
sbit P3_8   = P3^8;
sbit P3_10  = P3^10;
sbit DP3_8  = DP3^8;
sbit DP3_9  = DP3^9;
sbit DP3_10 = DP3^10;
sbit DP3_11 = DP3^11;

/* to initialize Asynchrone serial Interface ASC0 : */
void initasc()
{ 
//  Reload-Values @ 40MHz
//  for           9600, 19200, 38400, 57600 und 115200 Baud: 
  const int br[5] = { 0x81, 0x40, 0x20, 0x15, 0x0A }; 
  
  int  n;   /* Number of Baudrate  */
  n = USED_BAUDRATE;
  /* to initialize data reception over interface: */
  P3  |= 0x0400;               /* SET PORT 3.10 OUTPUT LATCH (TXD)              */
  DP3 |= 0x0400;               /* SET PORT 3.10 DIRECTION CONTROL (TXD OUTPUT)  */
  DP3 &= 0xF7FF;               /* RESET PORT 3.11 DIRECTION CONTROL (RXD INPUT) */

  ASC0_TIC = 0x80;             /* SET TRANSMIT INTERRUPT FLAG                   */
  ASC0_RIC = 0x00;             /* DELETE RECEIVE INTERRUPT FLAG                 */
  ASC0_BG   = br[n];           /* initialize Baudrate                           */
  ASC0_CON = 0x8011;            /* SET SERIAL MODE                               */         
                               /* start up Baudrate-Generator ,                 */
                               /* activate receiving Data  ,                    */
                               /* 8-Datenbits, no Parit鋞sbit                   */
  ALTSEL0P3 |= 0x0C00;  /* Configure port pins for serial interface 0           */
  PSW_IEN    = 1;              /* to enable global Interrupt                    */
}

/* Test to receive characters : */
/* return: Integer = >0, if receive a character  */

int keypressed()
{
 int   flag; 
 char  ch;
 /* read Data : */
 flag=ASC0_RIC_IR;   /* ASC0-receive-Flag  */
 ch  =ASC0_RBUF;     /* receive character  */
 /* to ignore LF, Xon and Xoff :           */
 if (flag && (ch==LF || ch==XON || ch==XOFF)) flag=0;
 return flag;
}

/* read character : overwrite Standartfunction */
/* return: char = from ASC0 received character   */

char _getkey()
{
  char  ch;
  while(!keypressed());       /* wait of character         */
  ch=ASC0_RBUF;               /* read received character   */
  ASC0_RIC_IR=0;              /* to relocate recieve-Flag  */
  return ch;
}

/* to write out character : overwrite Standardfunction, for example printf verw. */
/* committal: char = character to send                                  */
/* return: char = transmited character                                     */

char putchar (char ch)
{
  if (ch=='\n') putchar(13);        /* converting LF -> CR+LF              */
  while (!ASC0_TIC_IR);             /* wait until last character is transmited */
  ASC0_TIC_IR=0;                    /* to relocate transmit-Flag              */
  return (ASC0_TBUF=ch);            /* write character to transmit buffer    */
}

/* receive a character:                                             */
/* return: char = received character                                   */
/*         released character: "0".."9" , "A".."Z" , " " , "," , "="   */
/*         converting  "a".."z" into "A".."Z"                          */

char sio_getchar()
{
  /* Local variables: */
  char  c;

  while (1)
  {
    c=_getkey();

    /* Testing the received character: */
    switch (c)
    {
      case BS:
      case BK:
        return c;

      case CR:
        putchar(c);
        return c;

      /* Filtering valid characters: */
      default:
        if (isalnum(c)||(c==' ')||(c==',')||(c=='='))
        {
          c=toupper(c);
          putchar(c);
          return c;
        }
    }
  }
  return 0;
}

/* arithmetic technique prime number : */
void primzahlen()
{
  unsigned long  z, t, r;
  printf("\n\nPrimzahlen: Abbruch auf Tastendruck.");
  z=1; r=1;
  while(!keypressed()) /* truncation top press any Key */
  {
    while(r*r<z) r++;
    for(t=3; t<=r; t+=2) if(z%t==0) break;
    if(t>r) printf("\n%5li", z);
    z+=2;
  }
  _getkey(); /* read character */
}

/*  rolling direction: */
void lauflicht()
{
  printf("\n\nLauflicht auf Port 2: Abbruch auf Tastendruck.");
   DP2 = 0xFFFF; 
   P2  = 0x0100;           
 
  while(!keypressed()) /* truncation top press any Key */
  {
      timer_delay_10ms(30);
      P2=P2*2;
  	  
	  if((P2==0x8000))  
	  {
	    timer_delay_10ms(30);
	    P2=0x0100; 
	  }
  }
 /* set back Port P1H to input: */
 DP2 = 0;   /* set back P2 to input: */
}

/* mainfunktion: */
void main (void)
{
   char  ch;

  timer_init(); 
  initasc();         /* initialize input- und output  */
  while(1)
  {
    printf(
      "\n\nDemonstrationsprogramm fuer TQMX167U / TQMX161U : SIODEMO.C"\
	  "\nASC0, 57600 Baud, 8, 1, n"\
      "\n\n<P> :Primzahlenberechnung"\
      "\n<L> :Lauflicht an Port 2"\
      "\n<R> :Reset (TRAP 0)"\
      "\n");
    do
    {
	  printf("\n>");
      ch=toupper(_getkey());
	  putchar(ch);
      switch(ch)
      {
	    case 'P' :primzahlen(); break;
        case 'L' :lauflicht(); break;
        case 'R' :_trap_(0);
      }
    }
    while(ch>32);
  }
}

⌨️ 快捷键说明

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