📄 cpu_c515a.c
字号:
/********************************************************************//* Function : Siemens CPU C515A-L24M dependinge Functions FILE *//*------------------------------------------------------------------*//* Description : All CPU dependig functions like UART fe. are *//* are initialized here *//*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*//* Input : none *//*------------------------------------------------------------------*//* Returnvalue : none *//*------------------------------------------------------------------*//* History : 00/03 V1.0 Initial Version *//* *//********************************************************************/#ifndef __FILE_CPU_C515A#define __FILE_CPU_C515A// All that has to be included and / or defined is done here#include "../inc/hardware_description.h"// END INCLUDES & DEFINES ===========================================/********************************************************************//* Function : CpuIdle() SUB *//*------------------------------------------------------------------*//* Description : If no CPU Power is need, we put into idle mode *//*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*//* Input : none *//*------------------------------------------------------------------*//* Returnvalue : none *//*------------------------------------------------------------------*//* History : 00/03 V1.0 Initial Version *//* *//********************************************************************/void CpuIdle(){ // The Siemens Manual says, that we have to wait until the AD-Conversion // is completed before we put the cpu into idle mode // wait until AD-Conversion is finished while( BSY ) { // } CPUIDLE = 1;#ifdef USE_SYSTEM_TIMER PCON |= 0x01; // IDL-Bit in register PCON starts IDLE Mode PCON |= 0x20; //#else // If there is no regular INT source, we better do not use // the IDLE Mode NOP;#endif CPUIDLE = 0;}/********************************************************************//* Function : WatchDog() SUB *//*------------------------------------------------------------------*//* Description : Internal (and external) Watchdogs trigger *//*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*//* Input : none *//*------------------------------------------------------------------*//* Returnvalue : none *//*------------------------------------------------------------------*//* History : 00/03 V1.0 Initial Version *//* *//********************************************************************/void WatchDog( void ){ //NOP; // retrigger Externer Watchdog // Interner Watchdog not yet implemented}// ===========================================================================// Serial IO with the internal UART in Interrupt Mode#ifdef SERIAL_VIA_INTERRUPT#define SERIAL_IS_DEFINED// Transmit Buffersize#ifndef SERIAL_VIA_INTERRUPT_XBUFLEN#error "SERIAL_VIA_INTERRUPT_XBUFLEN not defined using default size 8"#define SERIAL_VIA_INTERRUPT_XBUFLEN 8#endif// Receive Buffersize#ifndef SERIAL_VIA_INTERRUPT_RBUFLEN#error "SERIAL_VIA_INTERRUPT_RBUFLEN not defined using default size 8"#define SERIAL_VIA_INTERRUPT_RBUFLEN 8#endif// in interrupt mode the ing buffer is placed in XDATA memoryvolatile xdata static unsigned char SERIAL_VIA_INTERRUPT_RBUF[SERIAL_VIA_INTERRUPT_RBUFLEN];volatile xdata static unsigned char SERIAL_VIA_INTERRUPT_XBUF[SERIAL_VIA_INTERRUPT_XBUFLEN];volatile xdata static unsigned char SERIAL_VIA_INTERRUPT_RCNT;volatile xdata static unsigned char SERIAL_VIA_INTERRUPT_XCNT;volatile xdata static unsigned char SERIAL_VIA_INTERRUPT_RPOS;volatile xdata static unsigned char SERIAL_VIA_INTERRUPT_XPOS;volatile xdata static unsigned char SERIAL_VIA_INTERRUPT_BUSY;#endif // SERIAL_VIA_INTERRUPT// ===========================================================================// Serial IO with them internal UART in Polling Mode#ifdef SERIAL_VIA_POLLING#define SERIAL_IS_DEFINED#endif // SERIAL_VIA_POLLING#ifdef SERIAL_IS_DEFINED// calculate the reloadvalues acc. to the definitions#ifndef CPUCLKHZ#error "CPUCLKHZ not defined !"#endif#ifndef BAUDRATE#error "BAUDRATE not defined !"#endif// The Siemens CPU C515A has a build in Baudrategenerator, therefore we use it instead// of timer 1 this gives a better resolution#define BAUDRATEGENRELOADVALUE (1024-(2*CPUCLKHZ/64/BAUDRATE))#define TIMER1MODE2RELOADVALUE (256-(2*CPUCLKHZ/32/12/BAUDRATE))#endif // SERIAL_IS_DEFINED// ===========================================================================// now to the serial functions#ifdef SERIAL_VIA_INTERRUPT/********************************************************************//* Function : SERIAL_VIA_INTERRUPT() ISR *//*------------------------------------------------------------------*//* Description : If a serial INT occours, the sub function is called*//* if a char is not yet transmitted, it is stored in *//* the uart and transmitted, if a char has been *//* received, it will be placed in the rx-buffer/*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*//* Input : none *//*------------------------------------------------------------------*//* Returnvalue : none *//*------------------------------------------------------------------*//* History : 00/03 V1.0 Initial Version *//* *//********************************************************************/void SERIALVIAINTERRUPT (void) interrupt SI0_VECTOR{ if (RI) { RI = 0; // char that are already inside the buffer should not be overwritten if (SERIAL_VIA_INTERRUPT_RCNT < SERIAL_VIA_INTERRUPT_RBUFLEN) { SERIAL_VIA_INTERRUPT_RBUF [(SERIAL_VIA_INTERRUPT_RPOS+SERIAL_VIA_INTERRUPT_RCNT++) % SERIAL_VIA_INTERRUPT_RBUFLEN] = SBUF; } } if (TI) { TI = 0; if (SERIAL_VIA_INTERRUPT_BUSY = SERIAL_VIA_INTERRUPT_XCNT) { /* Assignment, _not_ comparison! */ SERIAL_VIA_INTERRUPT_XCNT--; SBUF = SERIAL_VIA_INTERRUPT_XBUF [SERIAL_VIA_INTERRUPT_XPOS++]; if (SERIAL_VIA_INTERRUPT_XPOS >= SERIAL_VIA_INTERRUPT_XBUFLEN) { SERIAL_VIA_INTERRUPT_XPOS = 0; } } }}/********************************************************************//* Function : putchar() SUB *//*------------------------------------------------------------------*//* Description : everybody knows what this function is doing *//*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*//* Input : char Byte *//*------------------------------------------------------------------*//* Returnvalue : none *//*------------------------------------------------------------------*//* History : 00/03 V1.0 Initial Version *//* *//********************************************************************/void putchar( char Byte ){ while (SERIAL_VIA_INTERRUPT_XCNT >= SERIAL_VIA_INTERRUPT_XBUFLEN) { // buffer is full, wait until room WatchDog(); CpuIdle(); } ES = 0; if (SERIAL_VIA_INTERRUPT_BUSY) { SERIAL_VIA_INTERRUPT_XBUF[(SERIAL_VIA_INTERRUPT_XPOS+SERIAL_VIA_INTERRUPT_XCNT++) % SERIAL_VIA_INTERRUPT_XBUFLEN] = Byte; } else { SBUF = Byte; SERIAL_VIA_INTERRUPT_BUSY = 1; } ES = 1;}/********************************************************************//* Function : getchar() SUB *//*------------------------------------------------------------------*//* Description : everybody knows what this function is doing *//*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*//* Input : none *//*------------------------------------------------------------------*//* Returnvalue : char Byte *//*------------------------------------------------------------------*//* History : 00/03 V1.0 Initial Version *//* *//********************************************************************/char getchar( void ){ unsigned char c; while (!SERIAL_VIA_INTERRUPT_RCNT) { // no char avail, so wait WatchDog(); CpuIdle(); } ES = 0; SERIAL_VIA_INTERRUPT_RCNT--; c = SERIAL_VIA_INTERRUPT_RBUF [SERIAL_VIA_INTERRUPT_RPOS++]; if (SERIAL_VIA_INTERRUPT_RPOS >= SERIAL_VIA_INTERRUPT_RBUFLEN) { SERIAL_VIA_INTERRUPT_RPOS = 0; } ES = 1; return (c);}/********************************************************************//* Function : keypressed() SUB *//*------------------------------------------------------------------*//* Description : checks, if a char is available *//*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*//* Input : none *//*------------------------------------------------------------------*//* Returnvalue : TRUE yes, FALSE no *//*------------------------------------------------------------------*//* History : 00/03 V1.0 Initial Version *//* *//********************************************************************/char keypressed( void ){ unsigned char c; if(SERIAL_VIA_INTERRUPT_RCNT) { c = TRUE; } else { c = FALSE; } return (c);}#endif// ===========================================================================// Now the Internal UART Handling if Polling Method is used#ifdef SERIAL_VIA_POLLING/********************************************************************//* Function : putchar() SUB *//*------------------------------------------------------------------*//* Description : everybody knows what this function is doing *//*------------------------------------------------------------------*//* Author : Michael Schmitt *//*------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -