Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/delay.h>
- #include <stdint.h>
- #include <math.h>
- #include <avr/interrupt.h>
- #define RS 0
- #define EN 1
- #define D4 4
- #define D5 5
- #define D6 6
- #define D7 7
- #define LCD PORTA
- #define KL_PORT PORTC
- #define CMD (uint8_t)0
- #define DATA (uint8_t)1
- #define _nop() asm volatile("nop")
- volatile uint16_t now = 0;
- volatile int c = 0;
- volatile int tryb = 0;
- void send(const uint8_t data, const uint8_t type) {
- if (type == 0) {
- LCD &= ~_BV(RS); // rejestr
- } else if (type == 1) {
- LCD |= _BV(RS); // dane
- }
- LCD |= _BV(EN);
- LCD = (LCD & 0x0F) | (data & 0xF0);
- LCD &= ~_BV(EN);
- _nop();
- LCD |= _BV(EN);
- LCD = (LCD & 0x0F) | (data << 4);
- LCD &= ~_BV(EN);
- _delay_ms(2);
- }
- void init() {
- DDRA = 0xFF;
- LCD = 0;
- _delay_ms(20);
- send(0b00000010, CMD);
- send(0b00101000, CMD);
- send(0b00001100, CMD);
- send(0b00000001, CMD);
- _delay_ms(20);
- }
- void lcd_clear() {
- send(0b00000001, CMD); // clear
- }
- void string(const uint8_t* data) {
- uint8_t line = 0;
- uint8_t i = 0;
- while (data[i])
- {
- if (i > 15 && line == 0) {
- send(0xC0, CMD);
- line = 1;
- }
- send(data[i], DATA);
- i++;
- }
- }
- ISR(TIMER0_OVF_vect){
- if(c == 10){
- if(tryb == 1) now--;
- if(tryb == 0) now++;
- c = 0;
- }
- TCNT0 = 155;
- c++;
- }
- void update_time() {
- if(tryb == 2){
- lcd_clear();
- string("twoj czas minął");
- }else{
- if(tryb == 1 && now == 0){
- buzz();
- tryb = 2;
- }
- uint8_t hours = (uint8_t)floor(now / (60 * 60));
- uint8_t divisor_for_minutes = (uint8_t)(now % (60 * 60));
- uint8_t minutes = floor(divisor_for_minutes / 60);
- uint8_t divisor_for_seconds = (uint8_t)(divisor_for_minutes % 60);
- uint8_t seconds = (uint8_t)ceil(divisor_for_seconds);
- char buf[32];
- sprintf(buf, "%.2d:%.2d:%.2d", hours, minutes, seconds);
- lcd_clear();
- string(buf);
- }
- }
- void buzz(){
- PORTB = 1;
- _delay_ms(10);
- PORTB = 0;
- }
- void ogarnij_wyswietlacz(){
- if(tryb!=3){
- update_time();
- }
- }
- int main() {
- DDRB = 255;
- PORTB = 0;
- tryb = 0;
- init();
- TCCR0 |= (1<<CS00) | (1<<CS02);
- TIMSK |= (1<<TOIE0);
- TCNT0 = 55;
- sei();
- //klawiatura
- DDRC = 0xF0;
- KL_PORT = 0xFF;
- char x, kl;
- while(1){
- ogarnij_wyswietlacz();
- x = (PINC & 0x0f);
- if(x == 0b00001110)tryb = 1;
- if(x == 0b00001101)tryb = 0;
- if(x == 0b00001011)tryb = 3;
- if(x == 0b00000111)kl = 4;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement