// HC11.h #ifndef __HC11_H #define __HC11_H 1 #define _IO_BASE 0x00 #define PORTA *(unsigned char volatile *)(_IO_BASE + 0x00) #define PIOC *(unsigned char volatile *)(_IO_BASE + 0x02) #define PORTC *(unsigned char volatile *)(_IO_BASE + 0x03) #define PORTB *(unsigned char volatile *)(_IO_BASE + 0x04) #define PORTCL *(unsigned char volatile *)(_IO_BASE + 0x05) #define DDRC *(unsigned char volatile *)(_IO_BASE + 0x07) #define PORTD *(unsigned char volatile *)(_IO_BASE + 0x08) #define DDRD *(unsigned char volatile *)(_IO_BASE + 0x09) #define PORTE *(unsigned char volatile *)(_IO_BASE + 0x0A) #define CFORC *(unsigned char volatile *)(_IO_BASE + 0x0B) #define OC1M *(unsigned char volatile *)(_IO_BASE + 0x0C) #define OC1D *(unsigned char volatile *)(_IO_BASE + 0x0D) #define TCNT *(unsigned short volatile *)(_IO_BASE + 0x0E) #define TIC1 *(unsigned short volatile *)(_IO_BASE + 0x10) #define TIC2 *(unsigned short volatile *)(_IO_BASE + 0x12) #define TIC3 *(unsigned short volatile *)(_IO_BASE + 0x14) #define TOC1 *(unsigned short volatile *)(_IO_BASE + 0x16) #define TOC2 *(unsigned short volatile *)(_IO_BASE + 0x18) #define TOC3 *(unsigned short volatile *)(_IO_BASE + 0x1A) #define TOC4 *(unsigned short volatile *)(_IO_BASE + 0x1C) #define TOC5 *(unsigned short volatile *)(_IO_BASE + 0x1E) #define TCTL1 *(unsigned char volatile *)(_IO_BASE + 0x20) #define TCTL2 *(unsigned char volatile *)(_IO_BASE + 0x21) #define TMSK1 *(unsigned char volatile *)(_IO_BASE + 0x22) #define TFLG1 *(unsigned char volatile *)(_IO_BASE + 0x23) #define TMSK2 *(unsigned char volatile *)(_IO_BASE + 0x24) #define TFLG2 *(unsigned char volatile *)(_IO_BASE + 0x25) #define PACTL *(unsigned char volatile *)(_IO_BASE + 0x26) #define PACNT *(unsigned char volatile *)(_IO_BASE + 0x27) #define SPCR *(unsigned char volatile *)(_IO_BASE + 0x28) #define SPSR *(unsigned char volatile *)(_IO_BASE + 0x29) #define SPDR *(unsigned char volatile *)(_IO_BASE + 0x2A) #define BAUD *(unsigned char volatile *)(_IO_BASE + 0x2B) #define SCCR1 *(unsigned char volatile *)(_IO_BASE + 0x2C) #define SCCR2 *(unsigned char volatile *)(_IO_BASE + 0x2D) #define SCSR *(unsigned char volatile *)(_IO_BASE + 0x2E) #define SCDR *(unsigned char volatile *)(_IO_BASE + 0x2F) #define ADCTL *(unsigned char volatile *)(_IO_BASE + 0x30) #define ADR1 *(unsigned char volatile *)(_IO_BASE + 0x31) #define ADR2 *(unsigned char volatile *)(_IO_BASE + 0x32) #define ADR3 *(unsigned char volatile *)(_IO_BASE + 0x33) #define ADR4 *(unsigned char volatile *)(_IO_BASE + 0x34) #define OPTION *(unsigned char volatile *)(_IO_BASE + 0x39) #define COPRST *(unsigned char volatile *)(_IO_BASE + 0x3A) #define PPROG *(unsigned char volatile *)(_IO_BASE + 0x3B) #define HPRIO *(unsigned char volatile *)(_IO_BASE + 0x3C) #define INIT *(unsigned char volatile *)(_IO_BASE + 0x3D) #define TEST1 *(unsigned char volatile *)(_IO_BASE + 0x3E) #define CONFIG *(unsigned char volatile *)(_IO_BASE + 0x3F) #define INTR_ON() asm(" cli") #define INTR_OFF() asm(" sei") #define bit(x) (1 << (x)) /* SCI bits */ /*#define RDRF bit(5)*/ /*#define TDRE bit(7)*/ #define T8 bit(6) #define R8 bit(7) /* SPI bits */ #define MSTR bit(4) #define SPE bit(6) #define SPIF bit(7) /* EEPROM */ #define EEPGM bit(0) #define EELAT bit(1) #endif /*lab1*/ #define BAUD38K 0x20 #define CR 0x0D #define LF 0x0A #define SP 0x20 #define BS 0x08 #define DEL 0x7F #define ESC 0x1B #define OC4 0x10 #define TICK8 256 #define TICK1MS 2048 #define TDRE 0x80 #define RDRF 0x20 #define DDRA3 0x08 #define DDRA7 0x80 #define _DEBOUNCE 10 #define SPI_1MHz 0x00 #define SPI_500kHz 0x01 #define SPI_125kHz 0x02 #define SPI_62kHz 0x03 unsigned int _Time; #pragma interrupt_handler OC4han() void OC4han(void){ if(TCTL1==0x08){ TOC4=TOC4+1024; TCTL1 = 0x0C; _Time = _Time +1; }else{ TOC4=TOC4+1024; TCTL1=0x08; } TFLG1 |= OC4; _Time=_Time+1; } extern void OC4han(); #pragma abs_address:0xffe2; void (* OC4_handler[])()={ OC4han }; #pragma end_abs_address void init(void){ asm(" sei"); CONFIG=0x04; BAUD=BAUD38K; SCCR1=0x00; SCCR2=0x0C; _Time=0; TMSK2=0x03; TMSK1|=OC4; TFLG1|=OC4; TCTL1=0x08; TOC4=TCNT+TICK1MS; asm(" cli"); } void wait_tick(short duration){ while(_Time <= duration){}; _Time=0; } void pause(short duration){ _Time=0; while(_Time<=duration){}; } void disable_sci(void){ SCCR2=0x00; SCSR = 0xC0; pause(10); } void enable_sci(void){ SCCR2=0x0C; pause(10); } void shiftout(unsigned char data,unsigned char rate){ unsigned char dummy; DDRD |= 0x38; PORTD = 0x00; SPCR = 0x50; SPCR |= rate; SPDR = data; while((SPSR & SPIF)==0){}; dummy=SPDR; PORTD |= 0x20; PORTD &= ~0x20; SPCR = 0x00; } void OutChar(char data){ while((SCSR & 0x80) == 0); SCDR=data; } void OutString(char *pt){ char letter; while(letter=*pt++)OutChar(letter); } void OutUDec(unsigned short number){ if(number >= 10){ OutUDec(number/10); OutUDec(number%10); } else OutChar(number+'0'); } char InChar(void){ while((SCSR & RDRF) == 0){} return (SCDR); } void InString(char *string, unsigned int max){ unsigned int length=0; unsigned char character; while ((character=InChar())!=CR){ if(character==BS){ if(length){ string--; length--; OutChar(BS); } } else if(length='0' && character <= '9'){ number=10*number+character-'0'; length++; OutChar(character); } else if (character == BS && length){ number/=10; length--; OutChar(character); } } return number; } void set_pin(unsigned char pin){ switch(pin){ case 0: PACTL |= DDRA3; PORTA |= bit(3); break; case 1: PACTL |= DDRA7; PORTA |= bit(7); break; case 2: DDRD |= bit(2); PORTD |= bit(2); break; case 3: DDRD |= bit(3); PORTD |= bit(3); break; case 4: DDRD |= bit(4); PORTD |= bit(4); break; case 5: DDRD |= bit(5); PORTD |= bit(5); break; case 6: PORTA |= bit(5); break; case 7: PORTA |= bit(6); break; default: break; } } void clear_pin(unsigned char pin){ switch(pin){ case 0: PACTL |= DDRA3; PORTA &= ~bit(3); break; case 1: PACTL |= DDRA7; PORTA &= ~bit(7); break; case 2: DDRD |= bit(2); PORTD &= ~bit(2); break; case 3: DDRD |= bit(3); PORTD &= ~bit(3); break; case 4: DDRD |= bit(4); PORTD &= ~bit(4); break; case 5: DDRD |= bit(5); PORTD &= ~bit(5); break; case 6: PORTA &= ~bit(5); break; case 7: PORTA &= ~bit(6); break; default: break; } } void toggle_pin(unsigned char pin){ switch(pin){ case 0: PACTL |= DDRA3; PORTA ^= bit(3); break; case 1: PACTL |= DDRA7; PORTA ^= bit(7); break; case 2: DDRD |= bit(2); PORTD ^= bit(2); break; case 3: DDRD |= bit(3); PORTD ^= bit(3); break; case 4: DDRD |= bit(4); PORTD ^= bit(4); break; case 5: DDRD |= bit(5); PORTD ^= bit(5); break; case 6: PORTA ^= bit(5); break; case 7: PORTA ^= bit(6); break; default: break; } } unsigned char read_pin(unsigned char pin){ unsigned char data; data = 0x01; switch(pin){ case 0: PACTL &= ~DDRA3; if((PORTA & bit(3))==0) data = 0x00; break; case 1: PACTL &= ~DDRA7; if((PORTA & bit(7))==0) data = 0x00; break; case 2: DDRD &= ~bit(2); if((PORTD & bit(2))==0) data = 0x00; break; case 3: DDRD &= ~bit(3); if((PORTD & bit(3))==0) data = 0x00; break; case 4: DDRD &= ~bit(4); if((PORTD & bit(4))==0) data = 0x00; break; case 5: DDRD &= ~bit(5); if((PORTD & bit(5))==0) data = 0x00; break; case 6: if((PORTA & bit(0))==0) data = 0x00; break; case 7: if((PORTA & bit(1))==0) data = 0x00; break; default: break; } return data; } void wait_pin(unsigned char pin){ switch(pin){ case 0: PACTL &= ~DDRA3; while((PORTA & bit(3))==0); break; case 1: PACTL &= ~DDRA7; while((PORTA & bit(7))==0); break; case 2: DDRD &= ~bit(2); while((PORTD & bit(2))==0); break; case 3: DDRD &= ~bit(3); while((PORTD & bit(3))==0); break; case 4: DDRD &= ~bit(4); while((PORTD & bit(4))==0); break; case 5: DDRD &= ~bit(5); while((PORTD & bit(5))==0); break; case 6: while((PORTA & bit(0))==0); break; case 7: while((PORTA & bit(1))==0); break; default: break; } } void button(short pin){ int time_past; int state_past; int state; state_past=1; while(state_past==1) state_past=read_pin(pin); wait_pin(pin); time_past=_Time; while(_Time-time_past < _DEBOUNCE){}; } int count(short pin, short duration){ int button_count=0; int state_past; int state; int time_past; _Time=0; state_past=read_pin(pin); while(_Time <= duration){ state=read_pin(pin); if(state_past!=state){ if(state==1) button_count++; time_past=_Time; while(_Time-time_past < _DEBOUNCE){}; } state_past=state; } return button_count; }