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

📄 demo.c

📁 基于16位单片机的符合osek标准的操作系统
💻 C
字号:
/*********************************************************************
 *
 * copyright (c) 2002 by 3SOFT GmbH
 * - all rights reserved -
 *
 * 3SOFT GmbH
 * Phone: +49 (0)9131 7701 0
 * Frauenweiherstr. 14, 91058 Erlangen
 * GERMANY
 *
 * $Id: demo.c,v 1.2 2004/06/17 16:45:36 herrmanu Stab $
 *
 *********************************************************************
 *
 * modification history
 * --------------------
 * $Log: demo.c,v $
 * Revision 1.2  2004/06/17 16:45:36  herrmanu
 * adapted demo to TRESOS
 *
 * Revision 1.1  2003/09/02 15:14:35  thse2247
 * initial
 *
 * Revision 1.1  2003/07/30 10:34:15  thse2247
 * initial
 *
 * Revision 1.1  2002/11/21 08:24:40  aa
 * Added support for TMS470 (formerly ARM7) derivative
 * Added support for OSEKTIME with generiv ostime.[ch] part
 * Added support for m4 preprocessing of OIL files
 * Changed allmost everithing....
 *
 *
 *********************************************************************/
#include <hidef.h>
#include <string.h>

#define INCLUDE_LocalInclude
#include "os.h"
#undef 	 INCLUDE_LocalInclude

/* PORTB definitions */
#undef PORTB 
#define PORTB    (*((volatile unsigned char*)(0x0001)))
#undef DDRB      
#define DDRB     (*((volatile unsigned char*)(0x0003))) 
/* PORTB definitions */
#undef PORTA 
#define PORTA    (*((volatile unsigned char*)(0x0000)))
#undef DDRA      
#define DDRA     (*((volatile unsigned char*)(0x0002))) 

/* RTI definitions */
#undef CRGINT 
#define CRGINT   (*((volatile unsigned char*)(0x0038)))
#undef CRGFLG  
#define CRGFLG   (*((volatile unsigned char*)(0x0037))) 
#undef RTICTL
#define RTICTL   (*((volatile unsigned char*)(0x003B))) 

/* SCI0 definitions */
#undef SCI0BDL
#define SCI0BDL   (*((volatile unsigned char*)(0x00C9))) 
#undef SCI0CR1
#define SCI0CR1   (*((volatile unsigned char*)(0x00CA)))
#undef SCI0CR2 
#define SCI0CR2   (*((volatile unsigned char*)(0x00CB))) 
#undef SCI0SR1
#define SCI0SR1   (*((volatile unsigned char*)(0x00CC))) 
#undef SCI0DRL
#define SCI0DRL   (*((volatile unsigned char*)(0x00CF))) 

/* global variables definitions */
static int waittime = 5;
static unsigned char redButtonDown = FALSE, blueButtonDown = FALSE;
static long absoluteTime = 0;

/****************************************************************************
 * Declarations
 ****************************************************************************/

DeclareAlarm(AlarmActKLineTimer);
DeclareAlarm(AlarmActCyclic);
DeclareResource(Res_CounterVar);

DeclareTask(InitTask);
DeclareTask(Loop);
DeclareTask(Cyclic);
DeclareTask(KlineCommunicationTask);
DeclareTask(CreatRespMesKWP2000Task);
DeclareTask(KLineTimerTask);


/****************************************************************************
 * Global data
 ****************************************************************************/

unsigned char counter;

static void setNextSegment(void){
  static unsigned char seg = 0;
  static const char segs[]={~0x01,~0x02,~0x04,~0x08,~0x10,~0x20};
  seg++;
  if (seg == 6) {
    seg = 0;
  }
  PORTB = segs[seg];  
  //循环显示一根条子
}

static void displayNumber(Byte nbr){
  static const char segs[]={~0x3f,~0x06,~0x5b,~0x4f,~0x66,~0x6d,~0x7d,~0x07,~0x7f,~0x6f};
  PORTB = segs[nbr];
  //显示数字
} 

static void increaseSpeed(void){
	if(waittime > 1) waittime--;
}

static void reduceSpeed(void){
	if(waittime < 9) waittime++;
}

static void WriteToSCI0(const char *text){
  while (*text != '\0'){
    while (!(SCI0SR1 & 0x80));  /* wait for output buffer empty */
    SCI0DRL = *text++;
  }
}

static void executeCommand(const char *cmd){
  if (!strcmp(cmd,"stop")) {
    WriteToSCI0("\nApplication stopped by BGND instruction.\n\n");
    __asm BGND;
  }
  if ((*cmd > '0') && (*cmd <= '9') && (*(cmd+1) == '\0')){
    waittime = 10 - (*cmd - '0');
    WriteToSCI0("\nNew speed set to ");
    WriteToSCI0(cmd);
    WriteToSCI0(".\n");
  } else {
    WriteToSCI0("\nIllegal command.\n\n");
  }
}

#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void SCI0_ISR(void){
  static char command[5]; /* enough to hold "stop" or a single digit number */
  static unsigned char i=0; /* index into command */
  unsigned char rc;
  rc = SCI0SR1; /* dummy read to clear flags */
  rc = SCI0DRL; /* data read */
  if (i >= sizeof(command)-1 || rc == 0x0D) {
    command[i] = '\0';
    i = 0;
    executeCommand(command);
  } else {
    command[i] = rc;
    i++;
  }
  SCI0DRL = rc;
}
interrupt void SCI1_ISR(void)
{  			
	
	KLineReceive();
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void RTI_ISR(void) {
  absoluteTime++;

  /* clear RTIF bit */
  CRGFLG = 0x80;  
}

#pragma CODE_SEG DEFAULT
static void RTIInit(void) {
  /* setup of the RTI interrupt frequency */
  /* adjusted to get 1 millisecond (1.024 ms) with 4 MHz oscillator */
  RTICTL = 0x13; /* set RTI prescaler */ 
  CRGINT = 0x80; /* enable RTI interrupts */ 
}

static void startTimeBase(void){
  absoluteTime = 0;
  RTIInit();
  EnableInterrupts;
}

static void wait(long ms){
  long timeout;
  timeout = absoluteTime + ms;
  while (timeout != absoluteTime) {
    __asm NOP;
    /* __asm WAI; */ /* will be waken up by the RTI exception. Not well supported in BDM mode */
  }
}

static void SCI0Init(void) {
  SCI0BDL = (unsigned char)((16000000UL /* OSC freq */ / 2) / 9600 /* baud rate */ / 16 /*factor*/); 
  SCI0CR2 = 0x2C;
}

/****************************************************************************
 * Hooks (Remember to activate these in the ProOSEK configurator if desired!)
 ****************************************************************************/

void StartupHook()
{
    /* can be used to do some initialization before the first task runs */
}


void ErrorHook(StatusType myError)
{
    LEDS_SET(myError);
    ShutdownOS(myError);
}


/****************************************************************************
 * Tasks
 ****************************************************************************/

/****************************************************************************
 * InitTask
 * Initialize the system and terminate after activating Loop
 * Prio=1; Autostart=yes
 ****************************************************************************/

TASK(InitTask)
{
AlarmBaseType albase1;

AlarmBaseType albase2;


 /*
 	PWMCNT7 = 0;
	PWMCNT1 = 0;
	PWMCNT0 = 0;
    PWMPOL = 0xff;    // output is high at the beginning of the period, then goes high when the duty count is reached.
    PWMPER0 = 0x3d;                       // and to the period register 
    PWMDTY0 = 40;                        // Store initial value to the duty-compare register 
    PWMPER1 = 100;                       // and to the period register 
    PWMDTY1 = PWMPER1>>1;                        // Store initial value to the duty-compare register 
    PWMPRCLK = 0x36;                        // Set prescaler register fa=fbus/8
    PWMSCLA = 0x00;                       // Set scale register fsa=fa/2/25
    PWMSCLB = 0x32;                       // Set scale register fsb=fb/2/25
    PWMCLK = 0xff;                    // Select clock source sa sb
    PWMCTL=0x00;
	PWMCLK=0xff;//select the clock SA for pwm0
	PWME= 0xff;		//Run the counter to send out pwm singal
 */
    LEDS_INIT();
    LEDS_INIT1();
    
 
 /*   REFDV=0x0b;
    SYNR=0x3b;//fbus=4*0x3c/0x0c=20Mhz
    PLLCTL=0xe1;										
	while((CRGFLG & 0x08)==0);// CRGFLG_LOCK!= 1);// wait for lock 
	  CLKSEL |= 0x80; //switch to PLLCLK */ 
 
 
   
 
 
 
 
  startTimeBase();
  SCI0Init();
	KlineCommInit(1,9600);    //K线通信初始化
	KlineParaInit();          //K线参数初始化
  
  WriteToSCI0("\n\n*** Disco Demo ***\n");
  WriteToSCI0("\nEnter number (1-9) to change speed or\n'stop' to halt simulation.\n");


    (void)GetAlarmBase(AlarmActCyclic, &albase1);  
    (void)GetAlarmBase(AlarmActKLineTimer, &albase2);
    /* we assume that the ticksperbase is the number of ticks needed to reach 
     * one (1) second. Also assumes that this value is lower or equal to the 
     * amxallowed value. */
    (void)SetRelAlarm(AlarmActCyclic,100,100);
    (void)SetRelAlarm(AlarmActKLineTimer,1,1);
    (void)ChainTask(Loop);
    

}

/****************************************************************************
 * Loop Task
 * Executes an infinite loop after activating Sensor
 * Prio=2; Autostart=no
 ****************************************************************************/

TASK(KlineCommunicationTask)
{
KlineCommunication();
        
    (void)TerminateTask();
}


/****************************************************************************
 * Cyclic Task
 * Runs cyclicly activated by AlarmActCyclic
 * Prio=4; Autostart=no
 ****************************************************************************/

TASK(CreatRespMesKWP2000Task)
{
CreatRespMesKWP2000();
    (void)TerminateTask();
}

/****************************************************************************
 * Cyclic Task
 * Runs cyclicly activated by AlarmActCyclic
 * Prio=4; Autostart=no
 ****************************************************************************/

TASK(KLineTimerTask)
{
KLineTimer();
    (void)TerminateTask();
}

/****************************************************************************
 * Loop Task
 * Executes an infinite loop after activating Sensor
 * Prio=2; Autostart=no
 ****************************************************************************/

TASK(Loop)
{
    volatile unsigned int dummy;
    unsigned char toggle = 0;
 
    while(1)
    {
        for (dummy=0; dummy<30000; dummy++)
            ;

        (void)GetResource(Res_CounterVar);
        if (toggle == 0)
            toggle = 1;
        else 
            toggle = 0;
        LEDS_SET(((toggle<<7)|((counter) & 0x0f)));
				//闪烁小数点
        (void)ReleaseResource(Res_CounterVar);
        
          	if (redButtonDown){
      increaseSpeed();
  	  displayNumber(10-waittime);
  	  wait(100);
  	} else if (blueButtonDown){
  	  reduceSpeed();
  	  displayNumber(10-waittime);
  	  wait(100);
    } else {
      setNextSegment();
     	wait(waittime * 100);
    }

    }
        
    (void)TerminateTask();
}


/****************************************************************************
 * Cyclic Task
 * Runs cyclicly activated by AlarmActCyclic
 * Prio=4; Autostart=no
 ****************************************************************************/

TASK(Cyclic)
{
    (void)GetResource(Res_CounterVar);
    counter = ((counter+1) & 0x0f);
    LEDS_SET(counter);
//		 if(counter&0x01)PORTA=0xff;
//		 else PORTA=0;
    (void)ReleaseResource(Res_CounterVar);

    (void)TerminateTask();
}

⌨️ 快捷键说明

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