//*********************************************************************************** uint8_t main() { uint16_t sum=0; uint8_t digit=0; //set port bits 4-7 B as outputs DDRB = 0xF0; PORTB = 0x00; while(1){ //insert loop delay for debounce _delay_ms(3); //make PORTA an input port with pullups DDRA = 0x00; PORTA = 0xFF; //enable tristate buffer for pushbutton switches PORTB |= 0x70; //now check each button and increment the count as needed for(uint8_t i=0; i<8; ++i){ sum += chk_buttons(i) << i; } //disable tristate buffer for pushbutton switches PORTB &= 0x5F; //bound the count to 0 - 1023 if(sum>1023){ //sum = (sum+1)%1024; sum = 0; segment_data[0] = 0x01; segment_data[1] = 0x00; segment_data[2] = 0x00; segment_data[3] = 0x00; segment_data[4] = 0x00; } // sum %= 1024; //break up the disp_value to 4, BCD digits in the array: call (segsum) segsum(sum); //make PORTA an output DDRA = 0xFF; //prevent ghosting PORTA = 0x00; //send 7 segment code to LED segments //TODO: why did I put dec_to_7seg here and it works? PORTA = dec_to_7seg[segment_data[digit]]; //send PORTB the digit to display PORTB = ((0x8<<4) & PORTB)|(digit<<4); //update digit to display digit = (++digit)%5; }//while return 0; }//main
//*********************************************************************************** // The Main function initializes PORT A B E and calculates for the result of sum, it changes // the value by pushing the button switch S1 and S6. Pushing S1 means to increment or // decrement 2, linghting D1 and D2 on bar graph; Pushing S6 means to increment or // decrement 4, linghting D1-4 //*********************************************************************************** int main() { int16_t sum = 0; //the value of sum int8_t rawkeypressed = 0; uint8_t button; uint8_t past_state = 0; uint8_t mode; // 1 -- in mode 1; 2 -- in mode 2; 4 -- in mode 4 //set PB[7:4] as output for 74HC138 decoder; //set PB[3:0] to turn on MISO as input, and turn on SS, MOSI, SCLK as output DDRB = 0xF7; DDRE |= 0xC0; // Turn on CLK_INH, SH/LD as output 11000000 DDRA = 0x00; //set port A all input for 7-segment tcnt0_init(); //initalize counter timer zero spi_init(); //initalize SPI port sei(); //enable interrupts before entering loop while(1){ _delay_ms(3); //delay 3 ms PORTA = 0xFF; //set port A to be with pullups PORTB = 0x70; //set the decoder uint8_t i; //use i to check out which button is pushed for (i = 6; i < 8; i++){ //push the button if (chk_buttons(i)) { //if push button S6 or S7 button = i; if (button == 6) { //push S6 if (past_state == 6) mode = 1; //push S6 twice else if (past_state == 7) { if (mode == 0) mode = 2; else mode = 0; //push S6 and then push S7 } else { // only push S6 one time mode = 2; past_state = button; } }//if else if (button == 7) { //push S7 if (past_state == 7) mode = 1; //push S7 twice else if (past_state == 6) mode = 0; //push S7 and then push S6 else { // only push S7 one time mode = 4; past_state = button; } }//if else { // come back to mode 1 mode = 1; } }//if }//for switch (mode) { case 0: coefficient = 0; break; case 1: coefficient = 1; past_state = 0; display_count &= 0x00; break; // low bytes stand for counting 1 case 2: coefficient = 2; display_count &= 0x00; display_count |= 0x03; break; // low bytes stand for counting 4 case 4: coefficient = 4; display_count &= 0x00; display_count |= 0x0F; break; } //only showing 0-1023, sum is effected by count, sign and x2/x4 if ((sum < 1024) && (sum >= 0)) {sum += count * count_sign * coefficient;} //if the sum is larger than the max value 1024, subtract 1023 and show the remaining else if (sum >= 1024){sum -= 1023;} else if (sum < 0) {sum += 1024;} if (sum < 10) { a[0] = sum%10; //get the number of units a[1] = 10; a[3] = 10; a[4] = 10; } else if (sum < 100) { a[0] = sum%10; //get the number of units a[1] = sum/10%10; //get the number of tens a[3] = 10; a[4] = 10; } else if (sum <1000) { a[0] = sum%10; //get the number of units a[1] = sum/10%10; //get the number of tens a[3] = sum/100%10; //get the number of hundreds a[4] = 10; } else { a[0] = sum%10; //get the number of units a[1] = sum/10%10; //get the number of tens a[3] = sum/100%10; //get the number of hundreds a[4] = sum/1000; //get the number of thousands } //send sum to display 7-segement segment_value(a); //reset counter count = 0; //use buffer to forbid from assigning value of button PORTB = 0x5F; //set port A all output for 7-segment DDRA = 0xFF; //reset PORTA PORTA = 0x00; //reset button state for releasing rawkeypressed = 0; //send value of 4 digit segments to display segment_sum(SEG); }//while }//main