Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #define F_CPU 14745600UL
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. unsigned int ubrr = F_CPU/16/9600-1;
  8. unsigned int x=0;
  9. volatile unsigned long long taktis = 0;
  10.  
  11. void USART_Init( unsigned int ubrr)
  12. {
  13. /* Set baud rate */
  14. UBRR0H = (unsigned char)(ubrr>>8);
  15. UBRR0L = (unsigned char)ubrr;
  16. /* Enable receiver and transmitter */
  17. UCSR0B = (1<<RXEN)|(1<<TXEN);
  18. /* Set frame format: 8data, 2stop bit */
  19. UCSR0C = (1<<USBS)|(3<<UCSZ0);
  20. }
  21.  
  22. void USART_Transmit( char data2 )
  23. {
  24. /* Wait for empty transmit buffer */
  25. while ( !( UCSR0A & (1<<UDRE)) )
  26. ;
  27. /* Put data into buffer, sends the data */
  28. UDR0= data2;
  29. }
  30.  
  31. void USART_String(char data1[]){
  32. x=0;
  33.             while(x < strlen(data1)){
  34.  
  35.             USART_Transmit(data1[x]);
  36.                 x++;
  37.                 }
  38.                 }
  39.  
  40. unsigned char USART_Receive( void )
  41. {
  42. /* Wait for data to be received */
  43. while ( !(UCSR0A & (1<<RXC)) )
  44. ;
  45. /* Get and return received data from buffer */
  46. return UDR0;
  47. }
  48.  
  49.  
  50. void EEPROM_write(unsigned int Address, unsigned char Data)
  51. {
  52. /*
  53. Wait for completion of previous write */
  54. while(EECR & (1<<EEWE));
  55. /*
  56. Set up address and data registers */
  57. EEAR = Address;
  58. EEDR = Data;
  59. /*
  60. Write logical one to EEMWE */
  61. EECR |= (1<<EEMWE);
  62. /*
  63. Start eeprom
  64. write by setting EEWE */
  65. EECR |= (1<<EEWE);
  66. }
  67.  
  68.  
  69. unsigned char EEPROM_read(unsigned int Address)
  70. {
  71. /*
  72. Wait for completion of previous write */
  73. while(EECR & (1<<EEWE));
  74. /*
  75. Set up address register */
  76. EEAR = Address;
  77. /*
  78. Start
  79. eeprom
  80. read by writing EERE */
  81. EECR |= (1<<EERE);
  82. /*
  83. Return data from data register */
  84. return EEDR;}
  85.  
  86.  
  87.     ISR(TIMER0_OVF_vect)
  88.     {
  89.     taktis = taktis + 2048; //TO DO
  90.     }
  91.  
  92.     char time[] ="00:00:00";
  93.  
  94.  
  95. int main(void)
  96. {
  97.     unsigned char *sP, s = 51;
  98.     unsigned char *mP, m = 10;
  99.     unsigned char *hP, h = 10;
  100.  
  101.     sP=&s;
  102.     mP=&m;
  103.     hP=&h;
  104.      
  105.     //sprintf(time,"%02d:%02d:%02d",h,m,s);
  106.    
  107.  
  108.     USART_Init(ubrr);
  109.  
  110.    
  111.    
  112.  
  113.     //uzstada pinus uz izvadi
  114.     DDRD =  (1 << DDD7) | (1 << DDD6) | (1 << DDD5) |  (1 << DDD4) | (1 << DDD3) | (1 << DDD2) |   (1<<DDD1) | (1<<DDD0) ;
  115.     // 0xFF
  116.     PORTD = 0;
  117.     //uzstadam takts avotu / 8
  118.     //uzstadam taimeri
  119.     TCCR0 = (0 << CS02) | (0 << CS01) | (1 << CS00);
  120.     TIMSK = (1 <<TOIE0); //0x01
  121.     //atlaujam taimera parpildes partraukumu
  122.  
  123.  
  124.  
  125.     unsigned long long parbaude = F_CPU*8/3600;
  126.     sei();
  127.             //EEPROM_write(50, 00);
  128.            
  129.             //EEPROM_write(51, 00);
  130.            
  131.             //EEPROM_write(52, 00);
  132.  
  133. s=EEPROM_read(50);
  134. m=EEPROM_read(51);
  135. h=EEPROM_read(52);
  136.  
  137. while(1){
  138.  
  139.  
  140.        
  141.     if(taktis>=parbaude){
  142.     taktis=0;
  143.             s++;
  144.             if(s>=60){
  145.             s=0;
  146.             m++;
  147.             }
  148.             if(m>=60){
  149.             h++;
  150.             m=0;
  151.             }
  152.                
  153.         EEPROM_write(50, *sP);
  154.            
  155.         EEPROM_write(51, *mP);
  156.            
  157.         EEPROM_write(52, *hP);
  158.            
  159.         sprintf(time,"%02d:%02d:%02d",*hP,*mP,*sP);
  160.         USART_String(time);
  161.  
  162. }
  163. }
  164.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement