당근보드 시계&온도계&날짜 표시기 만들기(FND 이용)

글 내용

당근보드 이용.


#include <avr/io.h>
#include <stdio.h>
#include <carroty/delay.h>
#include <carroty/fnd.h>
#include <carroty/tcn75.h>

unsigned char number_array[16]= { 0,0,0,0,0,0,0,0,0,0,0xFF,0,0,0,0,0, };
unsigned char fnd_sel_array[4]= { 0,0,0,0,};
unsigned char hour, min, sec, mon, day;
int n1000, n100, n10, n, time;

void rtc_write(unsigned char address, unsigned char byte) {
 delay_us(6);    // 없거나 위치가 다르면 동작 안함 이유는?? 뭘까??
 TWCR = 0xA4;      // START condition
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x08);
 TWDR = 0xD0;
 TWCR = 0x84;      // MT_SLA_ACK
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x18);
 TWDR = address;
 TWCR = 0x84;      // MT_DATA_ACK
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x28);
 TWDR = byte;
 TWCR = 0x84;      // MT_DATA_ACK
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x28);
 TWCR = 0x94;      // STOP condition
}

unsigned char rtc_read(unsigned char address) {
 delay_us(6);    // 없거나 위치가 다르면 동작 안함 이유는?? 뭘까??
 unsigned char byte;
 TWCR = 0xA4;      // START condition
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x08);
 TWDR = 0xD0;
 TWCR = 0x84;      // MT_SLA_ACK
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x18);
 TWDR = address;
 TWCR = 0x84;      // MT_DATA_ACK
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x28);
 TWCR = 0xA4;      // RESTART condition
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x10);
 TWDR = 0xD1;
 TWCR = 0x84;      // MR_SLA_ACK
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x40);
 TWCR = 0x84;      // MR_DATA_NOACK
 while(((TWCR & 0x80) == 0x00) || (TWSR & 0xF8) != 0x58);
 byte = TWDR;
 TWCR = 0x94;      // STOP condition
 return byte;
}

void display_fnd(unsigned char number, unsigned char fnd_select) {
 PORTB= number;
 PORTC= fnd_select;
}

void time_print(unsigned int value) {
 
 n1000=value/1000;
 n100=(value-(value/1000*1000))/100;
 n10=(value-(value/100*100))/10;
 n=value-(value/10*10);
 int g,p=0;
 if(n1000==0) p=10;
 for(g=0;g<10;g++){
  display_fnd(number_array[n1000+p], fnd_sel_array[0]); delay_ms(1);
  display_fnd(number_array[n100], fnd_sel_array[1]);  delay_ms(1);
  display_fnd(number_array[n10], fnd_sel_array[2]); delay_ms(1);
  display_fnd(number_array[n], fnd_sel_array[3]);  delay_ms(1);
 }
}
void temp_print(unsigned int value) {
 
 n1000=value/1000;
 n100=(value-(value/1000*1000))/100;
 n10=(value-(value/100*100))/10;
 n=value-(value/10*10);
 int g;
 for(g=0;g<150;g++){
  display_fnd(number_array[n1000], fnd_sel_array[0]); delay_ms(1);
  display_fnd(number_array[n100], fnd_sel_array[1]);  delay_ms(1);
  display_fnd(number_array[n10], fnd_sel_array[2]); delay_ms(1);
  display_fnd(0b11000110, fnd_sel_array[3]);  delay_ms(1);
 }
}

void led_init(void) {
 DDRB= 0xFF;  //세그먼트 출력
 DDRC= 0xF0;  // 세그먼트 자리 출력
 DDRA= 0xF0;  // LED출력
}

int main(void) {
 unsigned char temp;
 TWBR= 72;  
 TWSR= 0x00;
 TWCR= 0x04;      
 number_array[0]=  NUMBER_0;
 number_array[1]=  NUMBER_1;
 number_array[2]=  NUMBER_2;
 number_array[3]=  NUMBER_3;
 number_array[4]=  NUMBER_4;
 number_array[5]=  NUMBER_5;
 number_array[6]=  NUMBER_6;
 number_array[7]=  NUMBER_7;
 number_array[8]=  NUMBER_8;
 number_array[9]=  NUMBER_9;
 
 fnd_sel_array[0]= FND_SEL_1;
 fnd_sel_array[1]= FND_SEL_2;
 fnd_sel_array[2]= FND_SEL_3;
 fnd_sel_array[3]= FND_SEL_4;
 led_init();           // FND 초기화
 PORTA=0xF0;

  /*rtc_write(0x06,0x09);
  rtc_write(0x05,0x11);
  rtc_write(0x04,0x27);
  rtc_write(0x03,0x05); 
  rtc_write(0x02,0x13);
  rtc_write(0x01,0x43);  
  rtc_write(0x00,0x00);  */
 
 
 DDRD=0x80;
 
 PORTA=0xC0;
 unsigned char compa=0,secbeep=0;
 int i=0;
 while(1) {
 
  for(i=0;i<170;i++){
  
   hour=   rtc_read(0x02);
   hour=   ((hour >>4) & 0x07)*10 + (hour & 0x0f);
  
   min=    rtc_read(0x01);
   min=    ((min >>4) & 0x07)*10 + (min & 0x0f);
  
   sec=    rtc_read(0x00);
   sec=    ((sec >>4) & 0x07)*10 + (sec & 0x0f);
  
   if(hour>=12){
   if(hour>12){hour=hour-12;}
    PORTA=PORTA&0b11011111;
    PORTA=PORTA|0b00010000;
   }
   else if(hour==0){
    hour=12;
    PORTA=PORTA&0b11101111;
    PORTA=PORTA|0b00100000;
   }
   else {
    PORTA=PORTA&0b11101111;
    PORTA=PORTA|0b00100000;
   }
  
   time=hour*100+min;
   time_print(time);
  
  
  
   if(secbeep!=sec){
    secbeep=sec;
    PORTA^=0xC0;
   }
  
  
   if(compa!=hour){
    compa=hour;
    OCR2=200;
    delay_ms(200);
    OCR2=0;
   }
  }
  for(i=0;i<4;i++){
  
   temp=tcn75_read(0x00);
   if      ((temp & 0x0001)==0x0001){ time=temp/2*100; time+=50;}
   else if ((temp & 0x0001)==0x0000) time=temp/2*100;
   PORTA=0b01110000;
   temp_print(time);
  
  }
 
  for(i=0;i<80;i++){
  
  
   PORTA=PORTA|0xF0;
   mon=    rtc_read(0x05);
   mon=    ((mon >>4) & 0x07)*10 + (mon & 0x0f);
  
   day=    rtc_read(0x04);
   day=    ((day >>4) & 0x07)*10 + (day & 0x0f);
  
   time=mon*100+day;
   time_print(time);
  
  }
 }
}


티스토리 툴바