📄 asuro 020.c
字号:
//if tone-play duration reached max (specific interrupt limit (tone-play duration) reached
if(gucTm2IntCnt==NUMOF_TIMER2_SYNTHESIZER1_INTERRUPTS){
//clear tone-duration counter
gucTm2IntCnt=0;
//toggle all leds at random
vToggleAllLedsAtRandom();
//store random value into timer/counter0 reload value
gucTm0CurRldVal=(unsigned char) random();
//store random value into timer/counter1 reload value
gucTm1CurRldVal=(unsigned char) random();
}//if
//end case
break;
//playing mode selected
case PLAY_MODE_SYNTHESIZER2:
//if tone-play duration reached max (specific interrupt limit (tone-play duration) reached
if(gucTm2IntCnt==NUMOF_TIMER2_SYNTHESIZER2_INTERRUPTS){
//clear tone-duration counter
gucTm2IntCnt=0;
gucTm0CurRldVal++;
if(gucTm0CurRldVal>=0xF0){
gucTm0CurRldVal=0x00;
}
gucTm1CurRldVal--;
if(gucTm1CurRldVal>=0xF0){
gucTm1CurRldVal=0xF0;
}
}//if
//end case
break;
//if keyboard playing mode is selected
case PLAY_MODE_KEYBOARD_PLAYING:
//if tone-play duration reached max (specific interrupt limit (tone-play duration) reached
if(gucTm2IntCnt==NUMOF_TIMER2_KEYBOARD_INTERRUPTS){
//clear tone-duration counter
//gucTm2IntCnt=0;
//stop tone0
vStopTone0();
//stop tone1
vStopTone1();
}
//end case
break;
}//switch
}
//################################################################################
// interrupt handler, external interrupt int1 pin5
// enable bit: register GICR bit INT1
// modes set: register MCUCR bits ISC11/ISC10
// #define SIG_INTERRUPT1 _VECTOR(2)
// pdf table 18, vector number decimal 03
//################################################################################
SIGNAL (SIG_INTERRUPT1){
//disable any further external INT1 interrupts on pin5
SFRX(GICR,INT1_L);
//disable all interrupts except switches interrupt
vDisableAllInterruptsExceptSwitchesInterrupt();
//switch left engine off
SFRX(PORTD,REV_LFT_H|FWD_LFT_H);
//switch right engine off
SFRX(PORTB,REV_RGT_H|FWD_RGT_H);
//define switch interrupt pin as output
SFRX(DDRGD,SWI_IAC_O);
//define switch interrupt pin as high (to be pulled low by pressed switch)
SFRX(PORTD,SWI_IAC_H);
//wait 6 milliseconds (found by experiment) to stabilize junction signal (due to capacitor C7 = 4,7uF)
vDelayXMilliSeconds(6);
//select AVCC with external cap., select switches ad-converter ADC4, right justify |
SFRX(ADMUX,REFS1_L|REFS0_H|ADLAR_L|RESERVED|MUX3_L|MUX2_H|MUX1_L|MUX0_L);
//clear any eventually pending AD-converter interrupt
SFRX(ADCSRA,ADIF_H);
//enable ad-converter 'conversion-finished' interrupt
SFRX(ADCSRA,ADIE_H);
//start ad-conversion (when ad-conversion is ready, an adc interrupt is generated)
SFRX(ADCSRA,ADSC_H);
}
//################################################################################
// interrupt handler, ad-conversion finished
// enable bit: register ADCSRA bit ADIE
// #define SIG_ADC _VECTOR(14)
// pdf table 18, vector number decimal 15
//################################################################################
SIGNAL (SIG_ADC){
//var
unsigned char ucPrsKeyVal;
unsigned int uiMsrAdcVal;
//disable any further ad-converter-ready interrupt (just for sure, may be superfluous)
SFRX(ADCSRA,ADIE_L);
//add low-value-byte and high-value-byte from AD-converter into single 16-bit integer
uiMsrAdcVal=ADCL+(ADCH<<8);
//convert measured value to byte value
ucPrsKeyVal=(unsigned char)(((1024.0/(float)uiMsrAdcVal-1.0))*63.0+0.5);
//set interrupt on raising edge, to avoid eventually new falling edge interrupt
//when key is still pressed at this point then setting pin5 to input the high level
//at the interrupt pin will go low and generate a new falling edge interrupt
SFRX(MCUCR,ISC11_L|ISC10_L);
//---prepair-switches-interrupt-input-for-new-key-presses---//
//define switch interrupt pin as input
SFRX(DDRGD,SWI_IAC_I);
//disconnect pullup from interrupt pin (is needed because output was set high)
SFRX(PORTD,SWI_IAC_N);
//wait 2 millisecond to stabilize junction signal eventually going low by pressed key and generating an unwanted interrupt
vDelayXMilliSeconds(2);
//set interrupt back to falling edge
SFRX(MCUCR,ISC11_H|ISC10_L);
//enable further external INT1 interrupts on pin5
SFRX(GICR,INT1_H);
//if same key is pressed again
if(ucPrsKeyVal==gucPrvKeyPrs){
//if current play mode is a normal play mode
if(gucCurPlyMod!=PLAY_MODE_ALL_OFF){
//just stop playing
gucNewPlyMod=PLAY_MODE_ALL_OFF_INIT;
//exit fuction
return;
}//if
//if current play mode is the 'play stopped' mode
else{
//if key 1,4 or 5 pressed
if(ucPrsKeyVal&(KEY1|KEY4|KEY5)){
//toggle sound pitch eight octaves
vToggleSoundPitchEightOctaves();
}
}
}
//if another key is pressed
else{
//remember last (will be previous later) pressed key
gucPrvKeyPrs=ucPrsKeyVal;
//if current play mode is a normal play mode
if(gucCurPlyMod!=PLAY_MODE_ALL_OFF){
//just stop playing
gucNewPlyMod=PLAY_MODE_ALL_OFF_INIT;
//exit fuction
return;
}//else
//if current play mode is the 'play stopped' mode
else{
//set timer0 to range 3 (low range)
SFRX(TCCR0 ,CS00_H);
//set timer1 to range 3 (low range)
SFRX(TCCR1B,CS10_H);
}
}
//program passes here
// - if same key is pressed in stop mode
// - if another key is pressed in stop omde
//remember last (will be previous later) pressed key
gucPrvKeyPrs=ucPrsKeyVal;
//analyse pressed key
switch(ucPrsKeyVal){
//----------key-1----------//
//switch on 1st position from left/front pressed
case KEY1:
//define selected play mode as new play mode
gucNewPlyMod=PLAY_MODE_SONG_PLAYING_INIT;
//exit case
break;
//----------key-2----------//
//switch on 2nd position from left/front pressed
case KEY2:
//switch track led on
SFRX(PORTD,TRK_LED_H);
//define selected play mode as new play mode
gucNewPlyMod=PLAY_MODE_TRACK_SENSORS_INIT;
//exit case
break;
//----------key-3----------//
//switch on 3rd position from left/front pressed
case KEY3:
//switch green system led on
SFRX(PORTB,SLD_GRN_H);
//switch red system led on
SFRX(PORTD,SLD_RED_H);
//define selected play mode as new play mode
gucNewPlyMod=PLAY_MODE_ODO_SENSORS_INIT;
//exit case
break;
//----------key-4----------//
//switch on 4th position from left/front pressed
case KEY4:
//define selected play mode as new play mode
gucNewPlyMod=PLAY_MODE_SYNTHESIZER1_INIT;
//exit case
break;
//----------key-5----------//
//switch on 5th position from left/front pressed
case KEY5:
//switch red system led on
SFRX(PORTD,SLD_RED_H);
//define selected play mode as new play mode
gucNewPlyMod=PLAY_MODE_SYNTHESIZER2_INIT;
//exit case
break;
//----------key-6----------//
//switch on 6th position from left/front pressed
case KEY6:
//switch green system led on
SFRX(PORTB,SLD_GRN_H);
//define selected play mode as new play mode
gucNewPlyMod=PLAY_MODE_KEYBOARD_PLAYING_INIT;
//exit case
break;
//more than one keypress at-one-tie detected
//default:
//toggle red led
//SFRT(PORTD,SLD_RED);
}//switch
}
//################################################################################
// interrupt handler, uart receive complete
// enable bit: register UCSRB bit RXCIE
// #define SIG_UART_RECV _VECTOR(11)
// pdf table 18, vector number decimal 12
//################################################################################
SIGNAL(SIG_UART_RECV){
//var
unsigned char ucRcvDat;
unsigned char uci;
//toggle green system led
SFRT(PORTB,SLD_GRN);
//toggle red system led
SFRT(PORTD,SLD_RED);
//----------handle-receive-errors----------//
//if there is a frame error in the receive character
if(UCSRA&FE_H){
//just exit function
return;
}
//----------get-received-character----------//
//read received byte from i/o register (this will clear the interrupt flag)
ucRcvDat=UDR;
//----------handle-hyperterminal-'q'-and'1'-keys----------//
//analyse received character
switch(ucRcvDat){
//hyperterminal key 'q' pressed
case 'q':
//if timer0 range 3 is set (low range)
if(SFRS(TCCR0,CS00_H)){
//toggle dual-tone mode
gbDuaTonMod=!gbDuaTonMod;
}
//exit function
return;
//exit case
break;
//if '1' character received
case '1':
//toggle sound pitch eight octaves
vToggleSoundPitchEightOctaves();
//exit function
return;
//exit case
break;
}//switch
//----------find-keypress-corresponding-tone-code-and-start-playing-tone----------//
//clear keyboard code search index
uci=0;
//for all keyboard codes do...
for(;;){
//if all keyboard codes in list are searched
if(gucKeyBrdTonKey[uci]==0){
//key not found, store no-play tone-code for use by interrupt2
gucNewKbdPlyTon=0;
//exit case
break;
}
//not all keyboard codes searched in list
else{
//if received keybord code found in keyboard code list
if(gucKeyBrdTonKey[uci]==ucRcvDat){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -