Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mega16.h>
- #include <delay.h>
- // I2C Bus functions
- #include <i2c.h>
- // DS1307 Real Time Clock functions
- #include <ds1307.h>
- // Declare your global variables here
- int jamp,jams,menp,mens;
- int interval;
- char jam,menit,detik;
- // Voltage Reference: AREF pin
- #define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (1<<ADLAR))
- // Read the 8 most significant bits
- // of the AD conversion result
- unsigned char read_adc(unsigned char adc_input)
- {
- ADMUX=adc_input | ADC_VREF_TYPE;
- // Delay needed for the stabilization of the ADC input voltage
- delay_us(10);
- // Start the AD conversion
- ADCSRA|=(1<<ADSC);
- // Wait for the AD conversion to complete
- while ((ADCSRA & (1<<ADIF))==0);
- ADCSRA|=(1<<ADIF);
- return ADCH;
- }
- //FUNGSI SEVEN SEGMENT
- void segment(int angka){
- if (angka==0){
- PORTD=0x3F;}
- if (angka==1){
- PORTD=0x6;}
- if (angka==2){
- PORTD=0x5B;}
- if (angka==3){
- PORTD=0x4F;}
- if (angka==4){
- PORTD=0x66;}
- if (angka==5){
- PORTD=0x6D; }
- if (angka==6){
- PORTD=0x7D; }
- if (angka==7){
- PORTD=0x7; }
- if (angka==8){
- PORTD=0x7F; }
- if (angka==9){
- PORTD=0x6F; }
- }
- //com
- void digit(int dig){
- if(dig==1){
- PORTB=0B0000111;}
- if(dig==2){
- PORTB=0B0001011;}
- if(dig==3){
- PORTB=0B0001101;}
- if(dig==4){
- PORTB=0B0001110;}
- }
- //atur jam menit
- void aturjam(){
- if (PINB.4==0) {if (jam<23) { jam++;
- }
- else { jam=0;
- };
- delay_ms(20);
- rtc_set_time(jam,menit,detik); //mengatur waktu
- }
- }
- void aturmenit(){
- if (PINB.5==0) { if (menit<59) { menit++;
- }
- else { menit=0;
- };
- delay_ms(20);
- rtc_set_time(jam,menit,detik); //mengatur waktu
- }
- }
- void main(void)
- {
- // Declare your local variables here
- // Input/Output Ports initialization
- // Port A initialization
- // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
- DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
- // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
- PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
- // Port B initialization
- // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=Out Bit2=Out Bit1=Out Bit0=Out
- DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0);
- // State: Bit7=P Bit6=P Bit5=P Bit4=P Bit3=1 Bit2=1 Bit1=1 Bit0=1
- PORTB=(1<<PORTB7) | (1<<PORTB6) | (1<<PORTB5) | (1<<PORTB4) | (1<<PORTB3) | (1<<PORTB2) | (1<<PORTB1) | (1<<PORTB0);
- // Port C initialization
- // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
- DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
- // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
- PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
- // Port D initialization
- // Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out
- DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
- // State: Bit7=1 Bit6=1 Bit5=1 Bit4=1 Bit3=1 Bit2=1 Bit1=1 Bit0=1
- PORTD=(1<<PORTD7) | (1<<PORTD6) | (1<<PORTD5) | (1<<PORTD4) | (1<<PORTD3) | (1<<PORTD2) | (1<<PORTD1) | (1<<PORTD0);
- // Timer/Counter 0 initialization
- // Clock source: System Clock
- // Clock value: Timer 0 Stopped
- // Mode: Normal top=0xFF
- // OC0 output: Disconnected
- TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (0<<CS02) | (0<<CS01) | (0<<CS00);
- TCNT0=0x00;
- OCR0=0x00;
- // Timer/Counter 1 initialization
- TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
- TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (0<<CS10);
- TCNT1H=0x00;
- TCNT1L=0x00;
- ICR1H=0x00;
- ICR1L=0x00;
- OCR1AH=0x00;
- OCR1AL=0x00;
- OCR1BH=0x00;
- OCR1BL=0x00;
- // Timer/Counter 2 initialization
- ASSR=0<<AS2;
- TCCR2=(0<<PWM2) | (0<<COM21) | (0<<COM20) | (0<<CTC2) | (0<<CS22) | (0<<CS21) | (0<<CS20);
- TCNT2=0x00;
- OCR2=0x00;
- // Timer(s)/Counter(s) Interrupt(s) initialization
- TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
- // External Interrupt(s) initialization
- // INT0: Off
- // INT1: Off
- // INT2: Off
- MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);
- MCUCSR=(0<<ISC2);
- // USART initialization
- // USART disabled
- UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (0<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
- // Analog Comparator initialization
- // Analog Comparator: Off
- // The Analog Comparator's positive input is
- // connected to the AIN0 pin
- // The Analog Comparator's negative input is
- // connected to the AIN1 pin
- ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
- // ADC initialization
- // ADC Clock frequency: 1000.000 kHz
- // ADC Voltage Reference: AREF pin
- // ADC Auto Trigger Source: ADC Stopped
- // Only the 8 most significant bits of
- // the AD conversion result are used
- ADMUX=ADC_VREF_TYPE;
- ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0);
- SFIOR=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);
- // SPI initialization
- // SPI disabled
- SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);
- // TWI initialization
- // TWI disabled
- TWCR=(0<<TWEA) | (0<<TWSTA) | (0<<TWSTO) | (0<<TWEN) | (0<<TWIE);
- // Bit-Banged I2C Bus initialization
- // I2C Port: PORTC
- // I2C SDA bit: 1
- // I2C SCL bit: 0
- // Bit Rate: 100 kHz
- // Note: I2C settings are specified in the
- // Project|Configure|C Compiler|Libraries|I2C menu.
- i2c_init();
- // DS1307 Real Time Clock initialization
- // Square wave output on pin SQW/OUT: Off
- // SQW/OUT pin state: 0
- rtc_init(0,0,0);
- while (1)
- {
- rtc_get_time(&jam,&menit,&detik); //membaca waktu
- jamp=jam/10;
- jams=jam%10;
- menp=menit/10;
- mens=menit%10;
- interval=0;
- for (interval;interval<20;interval++){
- aturjam(); //memanggil fungsi atur jam
- aturmenit(); //memanggil fungsi atur menit
- digit(1);
- segment(mens);
- delay_ms(1);
- digit(2);
- segment(menp);
- delay_ms(1);
- digit(3);
- segment(jams);
- //dot (menyala jika detik genap)
- if(detik%2==0){
- PORTD.7=1;}
- else{PORTD.7=0;}
- delay_ms(1);
- digit(4);
- segment(jamp);
- delay_ms(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement