Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- //PORT DEFINES
- #define LED_Direction DDRC
- #define LED_PORT PORTC
- #define DIGIT_Direction DDRB
- #define DIGIT_PORT PORTB
- #define BUTTON_Direction DDRA
- #define BUTTON_Vcc PORTA
- #define BUTTON_PORT PINA
- #define STATE_NONE_PRESSED 0x00
- #define STATE_SOME_PRESSED 0xff
- int main(void){ // setting ports as inputs and output
- LED_Direction |= 0xff;
- LED_PORT = 0xff;
- DIGIT_Direction |= 0xff;
- DIGIT_PORT = 0xff;
- BUTTON_Direction |= 0x00;
- BUTTON_Vcc = 0xff;
- BUTTON_PORT = 0xff;
- char symbols[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- char pin[]={0,0,0,0};
- char buttons = 0x00;
- char button_state = STATE_NONE_PRESSED;
- char key[8];
- int tminSeg = 0;
- int minSeg = 1;
- int tsecSeg = 2;
- int secSeg = 3;
- int select = 0;
- for(int i=0; i<8; i++){
- key[i] = ~(1<<i);
- }
- void incrementSec(int secSeg){
- pin[secSeg]++;
- if(pin[secSeg] > 9){
- pin[secSeg-1]++;
- pin[secSeg] = 0;
- }
- }
- void incrementTSec(int tsecSeg){
- pin[tsecSeg]++;
- if(pin[tsecSeg] > 6){
- pin[tsecSeg-1]++;
- pin[tsecSeg] = 0;
- }
- }
- void incrementMin(int minSeg){
- pin[minSeg]++;
- if(pin[minSeg] > 9){
- pin[minSeg-1]++;
- pin[minSeg] = 0;
- }
- }
- void incrementTMin(int tminSeg){
- pin[tminSeg]++;
- if(pin[tminSeg] > 6){
- pin[tminSeg] = 0;
- }
- }
- void decrementSec(int secSeg){
- pin[secSeg]--;
- if(pin[secSeg] < 0){
- pin[secSeg] = 0;
- }
- }
- void decrementTSec(int tsecSeg){
- pin[tsecSeg]--;
- if(pin[tsecSeg] < 0){
- pin[tsecSeg] = 0;
- }
- }
- void decrementMin(int minSeg){
- pin[minSeg]--;
- if(pin[minSeg] < 0){
- pin[minSeg] = 0;
- }
- }
- void decrementTMin(int tminSeg){
- pin[tminSeg]--;
- if(pin[tminSeg] < 0){
- pin[tminSeg] = symbols[pin[0]];
- }
- }
- int settingTimer = 0; // =================== czy jest wlaczone nastawianie minutnika (zbindujemy pod jakis guzik albo wywalisz ifa)
- while(1) { //output clear
- for(int i=0; i<4; i++){
- LED_PORT = symbols[pin[i]];
- DIGIT_PORT = ~(0x01 << i); //shift 1bit to output on another digit
- if(settingTimer){ // =================== mysle ze powinno mrygac jedno, nie wiem czy to ktore chcemy XD
- if(select){
- if(i == 0 || i == 1){
- _delay_ms(40); //odświeżanie
- } else {
- _delay_ms(4); //odświeżanie
- }
- }else{
- if(i == 2 || i == 3){
- _delay_ms(40); //odświeżanie
- }else {
- _delay_ms(4); //odświeżanie
- }
- }
- } else {
- _delay_ms(4); //odświeżanie
- }
- }
- buttons = BUTTON_PORT;
- if(button_state == STATE_NONE_PRESSED){
- if(buttons != 0xff){
- button_state = STATE_SOME_PRESSED;
- if(settingTimer){
- if(buttons == key[2]){ //SELECT
- select++;
- if(select > 2){
- select = 0;
- }
- }
- if(buttons == key[3]){ //SELECT
- // nie wiem, start timer czy cos
- }
- switch(select){
- case 0:
- if(buttons == key[1]){ //MORE MIN AND SEC
- incrementTSec(tminSeg);
- }
- if(buttons == key[0]){
- decrementTSec(tminSeg);
- }
- break;
- case 1:
- if(buttons == key[1]){ //MORE MIN AND SEC
- incrementTMin(tminSeg);
- }
- if(buttons == key[0]){
- decrementTMin(tminSeg);
- }
- break;
- }
- } else {
- if(buttons == key[0]){ // =================== jesli nacisniemy guzik nr 0 to zaczniemy ustawiac
- settingTimer = 1;
- }
- }
- }
- }
- if(button_state == STATE_SOME_PRESSED){
- // Wait for buttons to stop being pressed
- if(buttons == 0xff){
- button_state = STATE_NONE_PRESSED;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement