📄 codehc11.h
字号:
/**********************************************************************/
/* This is the code that's HC11 specific */
/**********************************************************************/
/**********************************************************************/
/* This is the place where we deposit code that MUST be executed right*/
/* at startup. */
/**********************************************************************/
void _HC11Setup(void)
{
INTSOFF; /*disable all interrupts */
TMSK2=0x03; /*program TMSK2 */
}
/**********************************************************************/
/* This is the unexpected interrupt handler. */
/**********************************************************************/
#pragma interrupt_handler unexpih
void unexpih(void)
{
while (TRUE) /*scream for help */
{
while (!TX_EMPTY) ;
PUT_TX('U');
while (!TX_EMPTY) ;
PUT_TX('I');
}
}
/**********************************************************************/
/* This is the serial interface interrupt handler. It must handle */
/* both receiver and transmitter interrupts. */
/**********************************************************************/
#pragma interrupt_handler sci_ih
void sci_ih(void)
{
byte s0sts;
/*Check the receiver for data first *****************************/
s0sts=SCSR; /*get status */
if(s0sts & RDRF) /*check RX for data */
{
if(s0sts & (FE+OR)) /*if an error occurred */
{
}
rcveih(); /*process character */
}
/*Then check if the transmitter is ready ************************/
s0sts=SCSR; /*get status again */
if(s0sts & TDRE) /*check if TX ready */
{
xmitih(); /*process interrupt */
}
}
/**********************************************************************/
/* This is the TOC2 interrupt handler, which we expect to service */
/* every 0.5 seconds. */
/**********************************************************************/
#pragma interrupt_handler toc2_ih
void toc2_ih(void)
{
/*Calculate and set the next Timer Output Compare value. This */
/*is done by adding a new offset, and ignoring the overflow */
TOC2+=HALFSEC; /*add offset, ign. ovflw */
COPRST=0x55; /*reset the */
COPRST=0xAA; /* COP timer */
/*Track the current time */
trktim(); /*keep track of real time */
/*Clean up and go home */
TFLG1=OC2; /*clear the interrupt */
}
/**********************************************************************/
/* This is the usb interrupt handler. */
/**********************************************************************/
#pragma interrupt_handler usb_ih
void usb_ih(void)
{
usb_isr(); /*process interrupt */
}
/**********************************************************************/
/* This is the initialization code. */
/**********************************************************************/
void init_micro(void)
{
int tmp;
/*Re-establish requisite reset state ****************************/
/*This defines our TTY initialization code: *********************/
BAUD=BAUD9600; /*9600 baud, 8 MHz proc */
SCCR1=0; /*8 data bits */
GET_RX(tmp); /*clear receive buffer */
GET_RX(tmp); /* twice */
tmp=SCSR; /*clear error bits */
/*Configure Port C for totem-pole output buffers, no handshake, */
/*normal strobe polarity, interrupts OFF */
PIOC=0; /*totem pole, no HNDS */
/*Enable the SPI port, putting it in Mode 0 (CPOL=0, CPHA=0), */
/*Master mode, interrupts OFF, 1 MHz clock (with 8 MHz osc.), */
/*Port D totem pole, bit 2 is input, all others outputs */
/*Bit 5 is used as the CS* for the USBN9602 */
PORTD=0xFF; /*set to known value */
DDRD=0x3B; /*set PORTD[0..5] dir. */
SPCR=SPE+MSTR; /*enable, in master mode */
tmp=SPSR; /*flush the */
tmp=SPDR; /* SPI port */
/*Turn on the A/D unit, make IRQ levl sensitive, turn on STOP */
/*exit delay, turn off the clock monitor, and set the COP */
/*(deadman) timer to its longest period: */
OPTION=ADPU+DLY+CR1+CR0; /*prog the OPTION reg */
/*Turn on the serial port, enabling only receive interrupts for */
/*now. Transmit interrupts will be enabled only when necessary.*/
SCCR2=RIE+TE+RE; /*prog the SCCR2 reg */
/*Initialize the timer. TOC2 is used to provide a 0.5 second */
/*interrupt (toggling PA6 in the process) */
PACTL=0; /*disable pulse acc. */
CFORC=0; /*no forced compares */
OC1M=0; /*no action on OC1 */
TCTL2=0; /*input capture disabled */
TCTL1=OL2; /*toggle PA6 on OC2 */
TMSK1=OC2; /*enable OC2 interrupts */
/*Initialize all interrupt vectors to the default handler *******/
for(tmp=SCI_VECT; tmp<=CLK_MONT; tmp+=3) SETVECT(tmp,unexpih);
/*Setup specific vectors ****************************************/
SETVECT(SCI_VECT,sci_ih); /*set serial intf. vector */
SETVECT(IRQ_VECT,usb_ih); /*set external int vector */
SETVECT(TO_CMP_2,toc2_ih); /*set timer comp. vector */
/*initiate continuous conversions on the first 4 A/D channels */
ADCTL=SCAN+MULT+0; /*cont. scan channels 1-4 */
}
/**********************************************************************/
/* This subroutine delays 10 ms (approximately). */
/**********************************************************************/
void ee_wait(void)
{
int dlyindx;
for(dlyindx=0; dlyindx<700; dlyindx++); /*assumes 8Mhz proc */
}
/**********************************************************************/
/* This subroutine erases does a byte or bulk erase of the EEPROM. */
/* Note that adr is an offset, not an absolute address. */
/**********************************************************************/
void ee_erase(byte adr, byte bulkmode)
{
byte modebit;
if(bulkmode) modebit=0; else modebit=P_BYTE;
PPROG=modebit+P_ERASE+EELAT; /*set to proper erase mode*/
*(byte *)(_EE_BASE+adr)=0; /*write any data to EEPROM*/
PPROG=modebit+P_ERASE+EELAT+EEPGM; /*set to bulk erase mode */
ee_wait(); /*wait 10 ms */
PPROG=0; /*voltage off, read mode */
}
/**********************************************************************/
/* This subroutine erases the specified EE 'register' which is 16 bits*/
/* and composed of two adjacent eeprom locations. */
/**********************************************************************/
void eerase(byte adr)
{
#define BYTEMODE FALSE
adr=adr << 1; /*adjust base address */
ee_erase(adr ,BYTEMODE); /*erase low order byte */
ee_erase(adr+1,BYTEMODE); /*erase hi order byte */
}
/**********************************************************************/
/* This subroutine bulk erases the EEPROM: */
/**********************************************************************/
void eebulk(void)
{
#define BULKMODE TRUE
ee_erase(0,BULKMODE); /*bulk erase EEPROM */
}
/**********************************************************************/
/* This subroutine reads the specified EE 'register' */
/**********************************************************************/
void eergrd(byte adr)
{
adr=adr << 1; /*adjust base address */
eebufl= *(byte *)(_EE_BASE+adr); /*get low order byte */
eebufh= *(byte *)(_EE_BASE+adr+1); /*get hi order byte */
}
/**********************************************************************/
/* This subroutine writes the EE byte whose address is in B on entry: */
/**********************************************************************/
void eebytewr(byte adr, byte dta)
{
PPROG=EELAT; /*set to proper prgrm mode*/
*(byte *)(_EE_BASE+adr)=dta; /*write data to EEPROM */
PPROG=EELAT+EEPGM; /*set to program bit */
ee_wait(); /*wait 10 ms */
PPROG=0; /*voltage off, read mode */
}
/**********************************************************************/
/* This subroutine writes the EE 'register' whose address is in B on */
/* entry: */
/**********************************************************************/
void eergwr(byte adr)
{
adr=adr << 1; /*adjust base address */
eebytewr(adr, eebufl); /*program low order byte */
eebytewr(adr+1,eebufh); /*program hi order byte */
}
/**********************************************************************/
/* This subroutine does an A-to-D conversion on the requested channel */
/* and then returns the 8-bit result. */
/**********************************************************************/
byte A2D_conv(byte chnl)
{
if(chnl) return(ADR2); else return (ADR3); /*send back data*/
}
/**********************************************************************/
/* This subroutine reads the USB register whose address is given. */
/**********************************************************************/
byte read_usb(byte adr)
{
USBCSON; /*turn on CS */
SPIADRCMD(adr,USBREAD); /*send cmd and addr */
SPIOUT(0); /*send dummy data */
USBCSOFF; /*turn off CS */
return(SPDR); /*return the result */
}
/**********************************************************************/
/* This subroutine writes the USB register whose address is given. */
/**********************************************************************/
void write_usb(byte adr, byte dta)
{
USBCSON; /*turn on CS */
SPIADRCMD(adr,USBWRITE); /*send cmd and addr */
SPIOUT(dta); /*send the data */
USBCSOFF; /*turn off CS */
}
/**********************************************************************/
/* This subroutine checks the joystick switches. */
/**********************************************************************/
void chk_sw(void)
{
byte rpt_sw; /*temp switch value */
rpt_sw =PORTE; /*get the switch data */
rpt_sw ^=0xF0; /*flip to proper polarity */
rpt_sw &=BIT5+BIT4; /*isolate the bits */
TSTSWITCH; /*store and send if dif. */
}
/**********************************************************************/
/* This subroutine puts the board to sleep, to reduce the overall */
/* current consumption in the suspend mode. */
/**********************************************************************/
void deep_sleep(void)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -