







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Submitted by: Steve Redfield Here's my encoder deciphering code. This does both encoders independently (both can change value in same while() execution) /////////////////////////////////////////////////////////////// // Encoder deciphering excecution is as follows: // // 1) Update Current Encoder Value. // 2) If Current is different from Last, which bit is different? // This gives you 4 cases, 2 forward, 2 backward, each with // its own distinct "Current" value. // 3) Correlate each "Current" value to either Increment or // Decrement Volume. // 4) Update Last Encoder Value. EncoderL = PINE & 0x30; EncoderR = PINE & 0x0C; if(EncoderL != EncoderLtmp) { if((EncoderL ^ EncoderLtmp) == 0x20) { if(EncoderL == 0x30 || EncoderL == 0x00) {Volume++;} else {Volume ;} } else { if(EncoderL == 0x10 || EncoderL == 0x20) {Volume++;} else {Volume ;} } } if(EncoderR != EncoderRtmp) { if((EncoderR ^ EncoderRtmp) == 0x08) { if(EncoderR == 0x0C || EncoderR == 0x00) {Volume++;} else {Volume ;} } else { if(EncoderR == 0x04 || EncoderR == 0x08) {Volume++;} else {Volume ;} } } EncoderLtmp = EncoderL; EncoderRtmp = EncoderR; ///////////////////////////////////////////////////////////////
Submitted by: Hai Yue Han Here's the code for the music that Jason and I are wanting to demo I'm submitting a portion of the code that's from my music file. /////////////////////////////////////////////////////////////// //Fight song array volatile int music[3][333] = +{{12,12,11,12, 1,7, 1,7,12,5,4,12, 1,12,12,11,12, 1,7, 1,7,12,5,4,12,1,0,0,0,0,4,4,4,7,7,7,4,2,0,12,12,11,12, {1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1, 1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,2, {2,2,2,2,2,2,2,2,2,2,2,2,8,2,2,2,2,2,2,2,2,2,2,2,2,7,3,2,2,2,4,2,2,2, 2,2,4,2,4,2,2,2,2,2,2,2,2,2,2,2,2,7,3,2,2,2,4,2,2,2,2,2,4, unsigned int songCounter = 0; //Tone array unsigned int tones[3][13] = {{61156,57723,54483,51426,48539,45815,43242,40815,38525,36362, 34321,32395,30576}, {30576,28861,27241,25711,24268,22906,21621,20407,19262,18180,17160,16197, 288}, {15288,14429,13619,12855,12133,11452,10809,10203,9630,9089,8579,8098,7643}} ;
Here is my code for displaying the frequency to the lcd. I only kept the directly relevant sections. // in main loop if (update_lcd) { spi_init(); update_lcd = 0; //write frequency itoa(frequency, st3 + 1, 10); home_line2(); if (!fm_freq) { //display frequency / // frequency is always in the form 1.xxx st3[0] = st3[1]; st3[1] = '.'; string2lcd(st3); string2lcd(" MHz"); } else { // display actual frequency of lo len = strlen(st3); st3[len] = st3[len 1]; st3[len 1] = '.'; st3[len + 1] = '\0'; string2lcd(st3 + 1); string2lcd(" MHz"); } .. } SIGNAL(SIG_OVERFLOW0) { .. count++; freq += TCNT3; TCNT3=0; .. // every second if (count % 128 == 0) { update_lcd = 1; .. if (!fm_freq) frequency = (int) 1000 * (freq / 1000000.); else frequency = (int) 10 * (freq * 64. / 1000000); freq = 0; .. } ..
I don't know if you wanted to include my fancy "off" button for the alarm clock. Like you said, it's a feature. Here's the code: ***IN ALARM FUNCTION //if the current time is within the range of the time passed into alarm() //and the alarm duration AND alarm is armed by ARM_ALARM flag AND the OFF //flag cleared THEN enable output compare 1A interrupt if((HOUR == hour) && (MINUTE >= minute) && (MINUTE <= (minute+duration)) && (AM == am) && (ARM_ALARM == 1) && (OFF == 0)){ TIMSK |= (1<<OCIE1A); } //otherwise, make sure to clear the OFF flag for the next alarm event else if((MINUTE <= minute) || (MINUTE > (minute+duration))){ OFF = 0; //clear OFF flag if alarm is not going off } ***OFF BUTTON case 5: //OFF = S state5 = (state5 << 1) | (PIND == 0xEF) | 0xE000; if (state5 == 0xF000){ OFF ^= 1; return 5; } break;
/* encoder_chk / /Takes an argument (either 0 or 1) the encoder to check. If the encoder / /is moved, the function returns: / / 1 if CW rotation detected, / / 0 if CCW rotation detected / / 1 if no movement detected. / /Note: the return value is a singed 8 bit int. / /Expected pinout: / / encoder 0, A output = PORTE.3 / / B output = PORTE.2 / / encoder 1, A output = PORTE.5 / / B output = PORTE.4 / /Port E is expected to be pulled up with the encoder causing a switch / /closure to ground. Encoder pulls to ground with a 1K resistor. / /Debounce time is 12 times each ISR run or loop time. / /Code was adapted from Ganssel's "Guide to Debouncing" */ /**************************************************************************/ int8_t encoder_chk(uint8_t encoder) { static uint16_t state[2] = {0,0}; //holds shifted in bits from encoder uint8_t a_pin, b_pin; //if no encoder 1 is not specified, assume encoder 0 if (encoder == 1) { a_pin = 5; b_pin = 4;} else { a_pin = 3; b_pin = 2;} state[encoder] = (state[encoder] << 1) | (! bit_is_clear(PINE, a_pin)) | 0xE000; if (state[encoder] == 0xF000){ //falling edge detected on A output if (bit_is_set(PINE, b_pin)) //get state of "B" to determine direction return 1; //detected CW rotation else return 0; //detected CCW rotation } else return 1; //no movement detected } //encoder_chk
/* chk_button / /Checks the state of the button number passed to it. It shifts in ones / /till the button is pushed. Function returns a 1 only once per debounced / / button push so a debounce and toggle function can be implemented at the / /same time. Adapted to check all buttons from Ganssel's "Guide to / /Debouncing". Expects active low pushbuttons on PIND port. Debounce time / /is determined by external loop delay times 12. The argument "button" is / /the pushbutton on the mega128 board S2 (button = 0) through / /S9 (button = 7) */ /***************************************************************************/ uint8_t chk_button(uint8_t button) { static uint16_t state[8]= {0,0,0,0,0,0,0,0}; //holds shifted in button bits state[button] = (state[button] << 1) | (! bit_is_clear(PIND, button)) | 0xE000; if (state[button] == 0xF000) return 1; //if held true for 12 passes through else return 0; } //chk_button
I've attached my frequency counter code (counter and display code) and button debouncer. //globals NBUTTONS is defined 8 or less unsigned char buttonVals = 0xff; //holds button stable values unsigned char buttonHits = 0; //indicates button status changes unsigned char buttonQ[NBUTTONS]; //debounce queue int main () { while(1) { //infinite loop scanButtons(); //update buttonHits, buttonVals if(buttonHits) { //respond to button activity //put button response code here buttonHits = 0; //clear flags } } } // debounce and monitor button inputs. button inputs are passed into a 8 place // history queue (an unsigned char, one per button.) An input is considered // debounced and valid only when the entire queue sees the same bit. When the // queue first stabilizes to a single value, the button hit flag is raised for // that button, and its value is recorded. It is the responsibility of the // button action recipient to clear the button hit flag. External alteration // of buttonVals will confuse this routine. This routine must run an absolute // minimum of sixteen times before a change in button status will be noted; // bouncing will increase this requirement. uses all of port D. void scanButtons(void) { int i; unsigned char scandata = PIND; //gather input from all buttons unsigned char oldvalue = buttonVals; for(i = 0; i < NBUTTONS; i++) { buttonQ[i] <<= 1; //advance the queue buttonQ[i] |= (scandata & 1); //enqueue latest bit if((buttonQ[i] == 0x00) && (oldvalue & 1)) { buttonVals &= ~(1 << i); //clear val bit buttonHits |= (1 << i); //set hit bit } if((buttonQ[i] == 0xff) && !(oldvalue & 1)) { buttonVals |= (1 << i); //set val bit buttonHits |= (1 << i); //set hit bit } scandata >>=1; //advance to next bit oldvalue >>=1; } }
//globals volatile unsigned char flags = 0; //status/control flags volatile unsigned long int run_cnt = 0; //running counter tally volatile unsigned long int freq_cnt = 0; //updated each second volatile unsigned char time_cnt = 0; //counts to one second @ 128 Hz char freq_ascii[7] = "reset"; int main () { init(); //one time initialization tasks while(1) { //infinite loop ..... if(flags & UPDATELCD) { cntToASCII(); updateLCD(); //output to LCD flags &= ~UPDATELCD; //clear flag } ..... } void init(void) {...deleted to save room..} void cntToASCII(void) { int offset = 21000; //measured error offset WRONG int i; unsigned long int n,a; n = freq_cnt; n += offset; //add offset (prescaled) n *= 64; //LO is prescaled by / a = 100000000; for(i=0; i < 6; i++) { //convert int to ascii freq_ascii[i] = (n / a)+0x30; //get digit value, add ASCII 0 n %= a; //remove that digit from int a /= 10; //next power of ten } freq_ascii[6] = freq_ascii[5]; //insert decimal point freq_ascii[5] = freq_ascii[4]; //via embarrassingly lazy method freq_ascii[4] = freq_ascii[3]; freq_ascii[3] = '.'; }