/* * GccApplication1.c * * Created: 2016-04-13 08:35:42 * Author: digpi04 */ #include #include #include unsigned char c; unsigned int pulse; unsigned int count = 0; unsigned int interruptCount = 0; unsigned int top = 0; unsigned int val; void timerSetup(void) { TCCR1A = 0; TCCR1B = 0; TCNT1 = 0x3CAF; TIMSK = 0x04; //Overflow interrupt enable TIFR = 0x04; //Overflow flag } void startTimer(void) { TCCR1B = 0b00000010; //Sets prescaler to 8 } void resetTimer(void) //Stops and resets timer { TCCR1B = 0; TCNT1 = 0x3CAF; } void counterSetup(void) { TCCR0 = 0; TCNT0 = 0xFF; TCNT0 = 0; } void startCounter(void) { TCCR0 = 0x07; } void resetCounter(void) //Stops and clears counter { TCCR0 = 0; TCNT0 = 0; } void ELow() { PORTA &= ~(1 << PA0); } void EHigh() { PORTA |= (1 << PA0); } void RSLow() { PORTA &= ~(1 << PA2); } void RSHigh() { PORTA |= (1 << PA2); } void RWLow() { PORTA &= ~(1 << PA1); } void RWHigh() { PORTA |= (1 << PA1); } void greenOn(void) { PORTC |= (1 << PC1); } void redOn(void) { PORTC |= (1 << PC0); } void yellowOn(void) { PORTA |= (1 << PA4); } void greenOff(void) { PORTC &= ~(1 << PC1); } void redOff(void) { PORTC &= ~(1 << PC0); } void yellowOff(void) { PORTA &= ~(1 << PA4); } int button() { int val = PINB; int val1 = val & 0x04; if(val1 != 0) { return 1; } else { return 0; } } void writeLetter(char c) { EHigh(); RSHigh(); PORTD = c; ELow(); } void pulseNbr(int nbr) { switch(nbr) { case 0: writeLetter(0x30); break; case 1: writeLetter(0x31); break; case 2: writeLetter(0x32); break; case 3: writeLetter(0x33); break; case 4: writeLetter(0x34); break; case 5: writeLetter(0x35); break; case 6: writeLetter(0x36); break; case 7: writeLetter(0x37); break; case 8: writeLetter(0x38); break; case 9: writeLetter(0x39); break; } } void pulseText(void) { writeLetter(0x20); //" " _delay_us(10); writeLetter(0x73); //s _delay_us(10); writeLetter(0x6C); //l _delay_us(10); writeLetter(0x61); //a _delay_us(10); writeLetter(0x67); //g _delay_us(10); writeLetter(0x2F); //"/" _delay_us(10); writeLetter(0x6D); //m _delay_us(10); writeLetter(0x69); //i _delay_us(10); writeLetter(0x6E); //n _delay_us(10); } void display(int pulse) //Displays the pulse and text on the display { int temp; int nbr = pulse/100; //nbr is the first digit of the pulse if it's more than 100 if (nbr > 0 ) { pulseNbr(nbr); //Writes the first digit of the pulse temp = pulse - nbr*100; //Converts the rest of the pulse to two digits } else { temp = pulse; } nbr = temp/10; //nbr is the second digit of the pulse if it's more than 100, otherwise the first pulseNbr(nbr); //Writes the second digit of the pulse nbr = temp - nbr*10; //Converts the rest of the pulse to one digit _delay_us(10); pulseNbr(nbr); //Writes the last digit of the pulse _delay_us(10); pulseText(); //Writes the text slag/min } void setConverter(void) { ADMUX = 0b11000111; ADCSRA = 0b11000110; } ISR(TIMER1_OVF_vect) { //Reads the ADconverter and counts the interrrupts setConverter(); if((ADCSRA & 0b00010000) != 0) { val = ADC; if(val >= 0x136 && top == 0) { top = 1; } else if(val < 0x136 && top == 1) { count ++; yellowOn(); top = 0; } else { yellowOff(); } } interruptCount++; resetTimer(); } void clearDisplay(void) { EHigh(); RSLow(); RWLow(); PORTD = 0x01; // Sets DB0 to 1 ELow(); } void setDisplay(void) { EHigh(); RSLow(); RWLow(); PORTD = 0x30; ELow(); } void displayOn(void) { EHigh(); RSLow(); RWLow(); PORTD = 0b00001111; ELow(); } void homeCursor(void) { EHigh(); RSLow(); RWLow(); PORTD = 0x02; ELow(); } void start(void) { DDRA |= (1 << PA0); //Sets PA0 to outgoing DDRA |= (1 << PA1); //Sets PA1 to outgoing DDRA |= (1 << PA2); //Sets PA2 to outgoing DDRA &= ~(1 << PA7); //Sets PA7 to ingoing DDRA |= (1 << PA4); //Sets PA4 to outgoing DDRB &= ~(1 << PB2); // Sets PB2 to ingoing DDRC |= (1 << PC0); //Sets PC0 to outgoing DDRC |= (1 << PC1); //Sets PC1 to outgoing DDRD = 0xFF; //Sets all D-ports to outgoing _delay_us(50); displayOn(); _delay_us(50); setDisplay(); _delay_us(50); clearDisplay(); _delay_ms(2); homeCursor(); _delay_ms(2); sei(); timerSetup(); counterSetup(); } int main(void) { start(); while(1) { int i = button(); if(i == 1) { clearDisplay(); //_delay_ms(2); redOff(); greenOff(); } while (i == 1) { i = 1; startTimer(); startCounter(); if(interruptCount == 300) { yellowOff(); pulse = count*4; display(pulse); if(pulse > 85 || pulse < 60) { redOn(); } else { greenOn(); } interruptCount = 0; count = 0; i = 0; } } } }