Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.c
- *
- * Created on: 12-07-2014
- * Author: Admin
- */
- /*
- * main.c
- *
- * Created on: 12-07-2014
- * Author: Admin
- */
- #define ACK 1
- #define NACK 0
- #include <avr/io.h>
- #include <avr/delay.h>
- #include <avr/interrupt.h>
- volatile uint8_t godzina, minuta, sekunda;
- enum {ss, mm, hh};
- /*
- PC0 - godziny
- PC1 - minuty
- PC2 - sekundy
- */
- void zapalGodziny(){
- PORTC &= ~(1<<PC0);
- PORTC |= (1<<PC1);
- PORTC |= (1<<PC2);
- }
- void zapalMinuty(){
- PORTC &= ~(1<<PC1);
- PORTC |= (1<<PC0);
- PORTC |= (1<<PC2);
- }
- void zapalSekundy(){
- PORTC &= ~(1<<PC2);
- PORTC |= (1<<PC1);
- PORTC |= (1<<PC0);
- }
- void TWI_start(void){
- TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTA);
- while (!(TWCR&(1<<TWINT)));
- }
- void TWI_stop(void){
- TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
- while (!(TWCR&(1<<TWSTO)));
- }
- void TWI_write (uint8_t bajt){
- TWDR = bajt;
- TWCR = (1<<TWINT)|(1<<TWEN);
- while (!(TWCR&(1<<TWINT)));
- }
- uint8_t TWI_read(uint8_t ack){
- TWCR = (1<<TWINT)|(ack<<TWEA)|(1<<TWEN);
- while (!(TWCR&(1<<TWINT)));
- return TWDR;
- }
- void TWI_write_buf ( uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf){
- TWI_start();
- TWI_write(SLA);
- TWI_write(adr);
- while (len--) TWI_write(*buf++);
- TWI_stop();
- }
- uint8_t dec2bcd(uint8_t dec){
- return ((dec/10)<<4)|(dec%10);
- }
- void zapiszGodzineDoPCF(uint8_t godzina, uint8_t minuta, uint8_t sekunda){
- uint8_t bufor[4];
- bufor[0]=0;
- bufor[1]=dec2bcd(sekunda);
- bufor[2]=dec2bcd(minuta);
- bufor[3]=dec2bcd(godzina);
- TWI_write_buf(0xA2, 0x01, 4, bufor);
- }
- void TWI_read_buf(uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf){
- TWI_start();
- TWI_write(SLA);
- TWI_write(adr);
- TWI_start();
- TWI_write(SLA+1);
- while (len--) *buf++ = TWI_read( len? ACK : NACK);
- TWI_stop();
- }
- uint8_t bcd2dec(uint8_t bcd){
- return ((((bcd>>4) & 0x0f)*10) + (bcd)&0x0f);
- }
- void odczytajCzasOdPCF(){
- uint8_t bufor[3];
- TWI_read_buf(0xA2, 0x02, 3, bufor);
- sekunda = bcd2dec(bufor[ss]);
- minuta = bcd2dec(bufor[mm]);
- godzina = bcd2dec(bufor[hh]);
- }
- int main(void){
- sei();
- zapiszGodzineDoPCF(22,32,00);
- TCCR1A |= (1 << COM1A1) | (1 << COM1A0);
- TCCR1B |= (1 << WGM12); // CTC
- TCCR1B |= (1 << CS12); // clk/ 256
- OCR1A = 20000; // przerwanie co mniej niz 1s
- TIMSK |= (1 << OCIE1A); // Zezwolenie na przerw. cmp match A
- //ustawienia timera do multipleksowania
- TCCR0 |= (1<<CS00) | (1<<CS01);
- TIMSK |= 1<<TOIE0;
- TCNT0 = 240;
- DDRD = 0xff;
- DDRB |= (1<<PB2);
- DDRC |= (1<<PC0)|(1<<PC1)|(1<<PC2);
- while(1){
- }
- }
- ISR(TIMER1_COMPA_vect)
- {
- odczytajCzasOdPCF();
- //ponizej jesli nie uzywamy RTC
- /*sekunda++;
- if (sekunda > 59){
- sekunda = 0;
- minuta++;
- }
- else
- if (minuta > 59){
- minuta = 0;
- godzina++;
- }
- else
- if (godzina >23)
- godzina = 0;
- */
- }
- //multiplekser
- volatile char stan = 1;
- ISR(TIMER0_OVF_vect){
- TCNT0 = 240;
- stan++;
- if (stan >1 && stan < 3){
- PORTD = 0xff;
- PORTD = ~godzina;
- zapalGodziny();
- }
- else
- if (stan > 4 && stan < 6){
- PORTD = 0xff;
- PORTD = ~minuta;
- zapalMinuty();
- }
- else
- if (stan > 7 && stan < 9){
- PORTD = 0xff;
- PORTD = ~sekunda;
- zapalSekundy();
- }
- else
- if (stan > 10) {
- PORTD = 0xff;
- stan = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement