Advertisement
Guest User

Untitled

a guest
Jul 12th, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Created on: 12-07-2014
  5.  *      Author: Admin
  6.  */
  7.  
  8.  
  9. /*
  10.  * main.c
  11.  *
  12.  *  Created on: 12-07-2014
  13.  *      Author: Admin
  14.  */
  15. #define ACK 1
  16. #define NACK 0
  17.  
  18. #include <avr/io.h>
  19. #include <avr/delay.h>
  20. #include <avr/interrupt.h>
  21. volatile uint8_t godzina, minuta, sekunda;
  22. enum {ss, mm, hh};
  23. /*
  24. PC0 - godziny
  25. PC1 - minuty
  26. PC2 - sekundy
  27. */
  28. void zapalGodziny(){
  29.     PORTC &= ~(1<<PC0);
  30.     PORTC |= (1<<PC1);
  31.     PORTC |= (1<<PC2);
  32. }
  33. void zapalMinuty(){
  34.     PORTC &= ~(1<<PC1);
  35.     PORTC |= (1<<PC0);
  36.     PORTC |= (1<<PC2);
  37. }
  38. void zapalSekundy(){
  39.     PORTC &= ~(1<<PC2);
  40.     PORTC |= (1<<PC1);
  41.     PORTC |= (1<<PC0);
  42. }
  43.  
  44. void TWI_start(void){
  45.     TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTA);
  46.     while (!(TWCR&(1<<TWINT)));
  47. }
  48.  
  49. void TWI_stop(void){
  50.     TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
  51.     while (!(TWCR&(1<<TWSTO)));
  52. }
  53.  
  54. void TWI_write (uint8_t bajt){
  55.     TWDR = bajt;
  56.     TWCR = (1<<TWINT)|(1<<TWEN);
  57.     while (!(TWCR&(1<<TWINT)));
  58. }
  59.  
  60. uint8_t TWI_read(uint8_t ack){
  61.     TWCR = (1<<TWINT)|(ack<<TWEA)|(1<<TWEN);
  62.     while (!(TWCR&(1<<TWINT)));
  63.     return TWDR;
  64. }
  65.  
  66. void TWI_write_buf ( uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf){
  67.     TWI_start();
  68.     TWI_write(SLA);
  69.     TWI_write(adr);
  70.     while (len--) TWI_write(*buf++);
  71.     TWI_stop();
  72. }
  73.  
  74. uint8_t dec2bcd(uint8_t dec){
  75.     return ((dec/10)<<4)|(dec%10);
  76. }
  77.  
  78. void zapiszGodzineDoPCF(uint8_t godzina, uint8_t minuta, uint8_t sekunda){
  79.     uint8_t bufor[4];
  80.     bufor[0]=0;
  81.     bufor[1]=dec2bcd(sekunda);
  82.     bufor[2]=dec2bcd(minuta);
  83.     bufor[3]=dec2bcd(godzina);
  84.     TWI_write_buf(0xA2, 0x01, 4, bufor);
  85. }
  86.  
  87. void TWI_read_buf(uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf){
  88.     TWI_start();
  89.     TWI_write(SLA);
  90.     TWI_write(adr);
  91.     TWI_start();
  92.     TWI_write(SLA+1);
  93.     while (len--) *buf++ = TWI_read( len? ACK : NACK);
  94.     TWI_stop();
  95. }
  96.  
  97. uint8_t bcd2dec(uint8_t bcd){
  98. return ((((bcd>>4) & 0x0f)*10) + (bcd)&0x0f);
  99. }
  100.  
  101. void odczytajCzasOdPCF(){
  102.     uint8_t bufor[3];
  103.     TWI_read_buf(0xA2, 0x02, 3, bufor);
  104.  
  105.     sekunda = bcd2dec(bufor[ss]);
  106.     minuta = bcd2dec(bufor[mm]);
  107.     godzina = bcd2dec(bufor[hh]);
  108.  
  109. }
  110.  
  111. int main(void){
  112.     sei();
  113.     zapiszGodzineDoPCF(22,32,00);
  114.     TCCR1A |= (1 << COM1A1) | (1 << COM1A0);
  115.     TCCR1B |= (1 << WGM12); //  CTC
  116.     TCCR1B |= (1 << CS12); // clk/ 256
  117.     OCR1A = 20000; // przerwanie co mniej niz 1s
  118.     TIMSK |= (1 << OCIE1A); // Zezwolenie na przerw. cmp match A
  119.  
  120.     //ustawienia timera do multipleksowania
  121.     TCCR0 |= (1<<CS00) | (1<<CS01);
  122.     TIMSK |= 1<<TOIE0;
  123.     TCNT0 = 240;
  124.  
  125.     DDRD  = 0xff;
  126.     DDRB |= (1<<PB2);
  127.     DDRC |= (1<<PC0)|(1<<PC1)|(1<<PC2);
  128.     while(1){
  129.  
  130.     }
  131. }
  132.  
  133. ISR(TIMER1_COMPA_vect)
  134. {
  135.     odczytajCzasOdPCF();
  136.         //ponizej jesli nie uzywamy RTC
  137.         /*sekunda++;
  138.  
  139.         if (sekunda > 59){
  140.             sekunda = 0;
  141.             minuta++;
  142.         }
  143.         else
  144.         if (minuta > 59){
  145.             minuta = 0;
  146.             godzina++;
  147.         }
  148.         else
  149.         if (godzina >23)
  150.             godzina = 0;
  151. */
  152. }
  153. //multiplekser
  154. volatile char stan = 1;
  155. ISR(TIMER0_OVF_vect){
  156.     TCNT0 = 240;
  157.     stan++;
  158.     if (stan >1 && stan < 3){
  159.         PORTD = 0xff;
  160.         PORTD = ~godzina;
  161.         zapalGodziny();
  162.     }
  163.     else
  164.     if (stan > 4 && stan < 6){
  165.         PORTD = 0xff;
  166.         PORTD = ~minuta;
  167.         zapalMinuty();
  168.     }
  169.     else
  170.     if (stan > 7 && stan < 9){
  171.         PORTD = 0xff;
  172.         PORTD = ~sekunda;
  173.         zapalSekundy();
  174.     }
  175.     else
  176.     if (stan > 10) {
  177.         PORTD = 0xff;
  178.         stan = 0;
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement