Advertisement
papat96

jam rtc ds1307 atmega16

Oct 19th, 2019
2,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. #include <mega16.h>
  2. #include <delay.h>
  3. // I2C Bus functions
  4. #include <i2c.h>
  5. // DS1307 Real Time Clock functions
  6. #include <ds1307.h>
  7. // Declare your global variables here
  8.  int jamp,jams,menp,mens;
  9.  int interval;
  10.  char jam,menit,detik;
  11. // Voltage Reference: AREF pin
  12. #define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (1<<ADLAR))
  13. // Read the 8 most significant bits
  14. // of the AD conversion result
  15. unsigned char read_adc(unsigned char adc_input)
  16. {
  17. ADMUX=adc_input | ADC_VREF_TYPE;
  18. // Delay needed for the stabilization of the ADC input voltage
  19. delay_us(10);
  20. // Start the AD conversion
  21. ADCSRA|=(1<<ADSC);
  22. // Wait for the AD conversion to complete
  23. while ((ADCSRA & (1<<ADIF))==0);
  24. ADCSRA|=(1<<ADIF);
  25. return ADCH;
  26. }
  27. //FUNGSI SEVEN SEGMENT
  28. void segment(int angka){
  29. if (angka==0){
  30.  PORTD=0x3F;}
  31.   if (angka==1){
  32.    PORTD=0x6;}
  33.   if (angka==2){
  34.       PORTD=0x5B;}
  35.  if (angka==3){
  36.     PORTD=0x4F;}  
  37.  if (angka==4){
  38.    PORTD=0x66;}  
  39.  if (angka==5){
  40.    PORTD=0x6D; }  
  41.  if (angka==6){
  42.    PORTD=0x7D; }  
  43.  if (angka==7){
  44.     PORTD=0x7; }
  45. if (angka==8){
  46.    PORTD=0x7F; }
  47. if (angka==9){
  48.   PORTD=0x6F; }
  49.  
  50. }
  51.  
  52. //com
  53. void digit(int dig){
  54.         if(dig==1){
  55.         PORTB=0B0000111;}
  56.          if(dig==2){
  57.         PORTB=0B0001011;}    
  58.           if(dig==3){
  59.         PORTB=0B0001101;}
  60.          if(dig==4){
  61.         PORTB=0B0001110;}
  62.      }
  63. //atur jam menit
  64. void aturjam(){
  65.  if (PINB.4==0) {if (jam<23) {  jam++;
  66.     }
  67. else  { jam=0;  
  68.     };    
  69.     delay_ms(20);
  70.     rtc_set_time(jam,menit,detik);  //mengatur waktu
  71.  
  72.     }  
  73.  }  
  74.      
  75. void aturmenit(){
  76.  if (PINB.5==0) { if (menit<59) {  menit++;
  77.     }
  78. else  { menit=0;  
  79.     };    
  80.     delay_ms(20);
  81.      rtc_set_time(jam,menit,detik); //mengatur waktu
  82.  
  83.     }  
  84.      
  85. }
  86.    
  87. void main(void)
  88. {
  89. // Declare your local variables here
  90.    
  91. // Input/Output Ports initialization
  92. // Port A initialization
  93. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  94. DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
  95. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  96. PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
  97.  
  98. // Port B initialization
  99. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=Out Bit2=Out Bit1=Out Bit0=Out
  100. DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0);
  101. // State: Bit7=P Bit6=P Bit5=P Bit4=P Bit3=1 Bit2=1 Bit1=1 Bit0=1
  102. PORTB=(1<<PORTB7) | (1<<PORTB6) | (1<<PORTB5) | (1<<PORTB4) | (1<<PORTB3) | (1<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
  103.  
  104. // Port C initialization
  105. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  106. DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
  107. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  108. PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
  109.  
  110. // Port D initialization
  111. // Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out
  112. DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
  113. // State: Bit7=1 Bit6=1 Bit5=1 Bit4=1 Bit3=1 Bit2=1 Bit1=1 Bit0=1
  114. PORTD=(1<<PORTD7) | (1<<PORTD6) | (1<<PORTD5) | (1<<PORTD4) | (1<<PORTD3) | (1<<PORTD2) | (1<<PORTD1) | (1<<PORTD0);
  115.  
  116. // Timer/Counter 0 initialization
  117. // Clock source: System Clock
  118. // Clock value: Timer 0 Stopped
  119. // Mode: Normal top=0xFF
  120. // OC0 output: Disconnected
  121. TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (0<<CS02) | (0<<CS01) | (0<<CS00);
  122. TCNT0=0x00;
  123. OCR0=0x00;
  124.  
  125. // Timer/Counter 1 initialization
  126.  
  127. TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
  128. TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (0<<CS10);
  129. TCNT1H=0x00;
  130. TCNT1L=0x00;
  131. ICR1H=0x00;
  132. ICR1L=0x00;
  133. OCR1AH=0x00;
  134. OCR1AL=0x00;
  135. OCR1BH=0x00;
  136. OCR1BL=0x00;
  137.  
  138. // Timer/Counter 2 initialization
  139.  
  140. ASSR=0<<AS2;
  141. TCCR2=(0<<PWM2) | (0<<COM21) | (0<<COM20) | (0<<CTC2) | (0<<CS22) | (0<<CS21) | (0<<CS20);
  142. TCNT2=0x00;
  143. OCR2=0x00;
  144.  
  145. // Timer(s)/Counter(s) Interrupt(s) initialization
  146. TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
  147.  
  148. // External Interrupt(s) initialization
  149. // INT0: Off
  150. // INT1: Off
  151. // INT2: Off
  152. MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);
  153. MCUCSR=(0<<ISC2);
  154.  
  155. // USART initialization
  156. // USART disabled
  157. UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (0<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
  158.  
  159. // Analog Comparator initialization
  160. // Analog Comparator: Off
  161. // The Analog Comparator's positive input is
  162. // connected to the AIN0 pin
  163. // The Analog Comparator's negative input is
  164. // connected to the AIN1 pin
  165. ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
  166.  
  167. // ADC initialization
  168. // ADC Clock frequency: 1000.000 kHz
  169. // ADC Voltage Reference: AREF pin
  170. // ADC Auto Trigger Source: ADC Stopped
  171. // Only the 8 most significant bits of
  172. // the AD conversion result are used
  173. ADMUX=ADC_VREF_TYPE;
  174. ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0);
  175. SFIOR=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);
  176.  
  177. // SPI initialization
  178. // SPI disabled
  179. SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);
  180.  
  181. // TWI initialization
  182. // TWI disabled
  183. TWCR=(0<<TWEA) | (0<<TWSTA) | (0<<TWSTO) | (0<<TWEN) | (0<<TWIE);
  184.  
  185. // Bit-Banged I2C Bus initialization
  186. // I2C Port: PORTC
  187. // I2C SDA bit: 1
  188. // I2C SCL bit: 0
  189. // Bit Rate: 100 kHz
  190. // Note: I2C settings are specified in the
  191. // Project|Configure|C Compiler|Libraries|I2C menu.
  192. i2c_init();
  193.  
  194. // DS1307 Real Time Clock initialization
  195. // Square wave output on pin SQW/OUT: Off
  196. // SQW/OUT pin state: 0
  197. rtc_init(0,0,0);
  198.  
  199. while (1)
  200.       {  
  201.        rtc_get_time(&jam,&menit,&detik);  //membaca waktu
  202.         jamp=jam/10;
  203.         jams=jam%10;
  204.         menp=menit/10;
  205.         mens=menit%10;  
  206.          interval=0;
  207.         for (interval;interval<20;interval++){  
  208.          aturjam();  //memanggil fungsi atur jam
  209.         aturmenit();   //memanggil fungsi atur menit
  210.         digit(1);
  211.         segment(mens);
  212.         delay_ms(1);
  213.         digit(2);
  214.         segment(menp);
  215.         delay_ms(1);
  216.         digit(3);
  217.         segment(jams);  
  218.         //dot (menyala jika detik genap)
  219.         if(detik%2==0){
  220.         PORTD.7=1;}
  221.         else{PORTD.7=0;}
  222.         delay_ms(1);
  223.        
  224.         digit(4);
  225.         segment(jamp);
  226.         delay_ms(1);
  227.        }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement