LIÊN THÔNG CƠ ĐIỆN TỬ K5- ĐH CÔNG NGHIỆP HÀ NỘI
CHÀO MỪNG BẠN
Đến Với Forum Của Lớp Cơ Điện Tử.
Mời Bạn ĐăngNhập Để Đóng Góp Ý Kiến Cho Forum
Chúc Bạn Luôn Vui Và Thành Công

Thành công đến từ những việc nhỏ nhất khi bạn nghiêm túc làm...


You are not connected. Please login or register

Lập trình cho vi điều khiển AVR với mạch thời gian

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

#include <built_in.h>
#define clk1 PORTB0_bit
#define lat1 PORTB2_bit
#define sdo1 PORTB1_bit
#define clk2 PORTB3_bit
#define lat2 PORTB5_bit
#define sdo2 PORTB4_bit
#define ADC_VREF_TYPE 0x40
//CAU HINH NUT NHAN
#define BUTTON1 PINA0_bit
#define BUTTON2 PINA1_bit
#define BUTTON3 PINA2_bit

// CAU HINH GIAO TIEP I2C
sbit Soft_I2C_Scl_Output at PORTC0_bit;
sbit Soft_I2C_Sda_Output at PORTC1_bit;
sbit Soft_I2C_Scl_Input at PINC0_bit;
sbit Soft_I2C_Sda_Input at PINC1_bit;
sbit Soft_I2C_Scl_Direction at DDC0_bit;
sbit Soft_I2C_Sda_Direction at DDC1_bit;


//KHAI BAO BIEN TOAN CUC
char seconds, minutes, hours,hours1, ampm, day, date, month, year, pm, pr, flag, oldpm, count, mode, j,match,m;
int temp,adc_rd;
signed char i;
char seven_seg_code[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0XBF,0X86,0XDB,0XCF,0XE6,0XED,0XFD,0X87,0XFF,0XEF,0x37,0X39, 0XB7, 0XB9};
char timer_set[28]={0,50,40,50,40,50,40,30,30,20,10,20,10,0,7,7,8,8,9,9,10,11,13,14,15,15,16,17};
//char timer_set[28]={7,0,7,50,8,40,8,50,9,40,9,50,10,40,11,30,13,30,14,20,15,10,15,20,16,10,17,0};
char dat[14]={1,2,3,4,5,6,7,8,9,8,7,6,3,1}; //CHUA DU LIEU CUA TUNG LED
//HAM DOC DS1307
char DS1307_Read_Byte(char addr){ // ADDR LA DIA CHI THANH GHI CAN DOC
char temp;
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(addr);
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
temp=Soft_I2C_Read(0);
Soft_I2C_Stop();
return temp; //HAM TRA VE NOI DUNG CUA THANH GHI
}
//HAM GHI DS1307
void DS1307_Write_Byte(char addr, char dat){ // ADDR LA DIA CHI THANH GHI CAN DOC, DATA LA DU LIEU CAN GHI
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(addr);
Soft_I2C_Write(dat);
Soft_I2C_Stop();
}
// HAM DOC THONG TIN DS1307
void RTC_Read(){
seconds = DS1307_Read_Byte(0x00);
minutes = DS1307_Read_Byte(0x01);
hours = DS1307_Read_Byte(0x02);
ampm = hours&0x40;
day = DS1307_Read_Byte(0x03);
day = day&0x07;
date = DS1307_Read_Byte(0x04);
month = DS1307_Read_Byte(0x05);
year = DS1307_Read_Byte(0x06);
//XU LY HIEN THI
if(day==1)
{dat[1]=20;
dat[0]=21;
}
else{
dat[0]=0;
dat[1]=day;
}
dat[3]=date&0x0F;
dat[2]=(date & 0x30) >> 4;
dat[5]=month & 0x0F;
dat[4]=(month & 0x10) >> 4;
dat[7]=(year & 0x0F);
dat[6]= (year & 0xF0) >> 4;
dat[9]= minutes & 0x0F;
dat[8]= (minutes & 0x70) >> 4;
if(ampm){
dat[11]= hours & 0x0F;
dat[10]= (hours & 0x10) >> 4;
pm = hours&0x20;
}
if(ampm==0){
dat[11]= hours & 0x0F;
dat[10]= (hours & 0x30) >> 4;

}

//CHUYEN DOI BCD SANG DEC CO CHON LOC
seconds = ((seconds & 0x70) >> 4)*10 + (seconds & 0x0F); //LAY 3 BIT CAO 4BIT THAP
minutes = ((minutes & 0x70) >> 4)*10 + (minutes & 0x0F); //LAY 3 BIT CAO 4BIT THAP
if(ampm){
hours =((hours & 0x10) >> 4)*10+ (hours & 0x0F); //LAY 1 BIT CAO 4BIT THAP
}
if(ampm==0) {
hours = ((hours & 0x30) >> 4)*10 + (hours & 0x0F); //LAY 2 BIT CAO 4BIT THAP
if(hours>=12) pm =0x20;
}
year = ((year & 0xF0) >> 4)*10 + (year & 0x0F); //LAY 3 BIT CAO 4BIT THAP
date = ((date & 0x30) >> 4)*10 + (date & 0x0F); //LAY 3 BIT CAO 4BIT THAP
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); //LAY 3 BIT CAO 4BIT THAP
}


//HAM DOC PHIM - TRA VE VI TRI PHIM DUOC AN - MUC TAC DONG PHIM LA THAP
char Button_Read(){
if(BUTTON1==0)return 1;
if(BUTTON2==0)return 2;
if(BUTTON3==0)return 3;
return 0;
}

//HAM DOC PHIM VA CHONG DOI
char Button_Read_Debounce(){
char temp;
temp=Button_Read();
if(temp==0){ //NEU TEMP =0 THI KO CO PHIM DC NHAN
if(pr==1){ //NEU PR =1 THI TRUOC DO CO PHIM NHAN => CO CANH XUONG
pr=0; //DAT LAI TRANG THAI - LAN XET NAY KHONG CO PHIM NHAN => DANG O TRANG THAI CHO
return;
}
else {
flag=1; //BAO HIEU DANG O TRANG THAI CHO => PR TRUOC DO =0
return; //TRO VE
}
}
else{ //CO PHIM DUOC NHAN
delay_ms(30); // CHO 1 KHOANG 50MS DE CHONG DOI
temp=Button_Read(); //DOC LAI GIA TRI LAN NUA
if(temp==0) return; //NEU TEMP = 0 THI XUNG DOC DUOC KHI NAY LA DO RUNG PHIM


else{ //NEU TEMP !=0 THI BAT DAU XU LY

pr=1; // BAO HIEU LA PHIM DANG DUOC NHAN, YEU CAU XU LY XONG PH
return temp; //TRA VE DU LIEU DOC DUOC
}

}
}
//TAO THONG TIN CHO BIET DANG O CHE DO DIEU CHINH NGAY GIO VA DOI TUONG DANG DUOC CHON
void Mode_Maker(){
if(mode==1){
if(day==1){
dat[0]+=2;
dat[1]+=2;
}
else{
dat[0]+=10;
dat[1]+=10;
}
}
if(mode==2){
dat[2]+=10;
dat[3]+=10;
}
if(mode==3){
dat[4]+=10;
dat[5]+=10;
}
if(mode==4){
dat[6]+=10;
dat[7]+=10;
}
if(mode==5){
dat[10]+=10;
dat[11]+=10;
}
if(mode==6) {
dat[8]+=10;
dat[9]+=10;
}
return ;
}

//KHOI TAO CAC THIET BI
void Init(void){
//KHOI TAO UART

Soft_I2C_Init();
// CAU HINH TIMER 0
//XUNG NHIP THACH ANH CHIA 1024
CS00_bit=0;
CS01_bit=0;
CS02_bit=1;
TOIE0_bit=1;//cho phep ngat tran_ thanh ghi TIMSK
SREG_I_bit=1;//cho phep ngat toan cuc_thanh ghi SREG
TCNT0=-50;
//KIEM TRA DS1307 - CHO PHEP DAO DONG
if(Ds1307_Read_Byte(0x00)>>7==1) DS1307_Write_Byte(0x00,0x00);
DS1307_Write_Byte(0x07,0x10);
mode=8;
}

//HAM XU LY PHIM NHAN
void Button_Process(){
char key; //KHAI BAO BIEN CUC BO LUU TRU THONG TIN PHIM
key=Button_Read_Debounce();//DOC PHIM - PHIM NHAN UU TIEN CAC PHIM CO TRI SO THAP
if((pr!=0)&&(flag==1)){
flag=0;
if(key==1){ //CHON CHE DO HIEU CHINH
if(mode==Cool mode=0; //TANG MODE XOAY VONG KHI CO PHIM DUOC NHAN
mode++;
}
if(key==2){ //TANG GIA TRI DOI TUONG LEN 1 DON VI, NEU AN DE THI TU DONG TANG VI FLAG =1
RTC_Read(); //DOC GIAI TRI DOI TUONG TRUOC KHI XU LY
flag=0; //CHO PHEP DE PHIM
delay_ms(100); //DELAY DE KHI DE PHIM, GIA TRI TANG KHONG QUA NHANH
switch(mode){ //CHON DOI TUONG XU LY
case 1: //XU LY NGAY TRONG TUAN
if(day==7) day=0; // NHU TREN
++day;
day=dec2bcd(day);
DS1307_Write_Byte(0x03,day);
break;
case 2:
//XU LY NGAY
if(date==31) date=0; //NGAY 1<=DATE<=31
++date; //TANG BIEN DATE VUA DOC DUOC
date=dec2bcd(date); //DOI SANG BCD
DS1307_Write_Byte(0x04,date); //GHI VAO DS1307
break;
case 3: //XU LY THANG
if(month==12) month=0; //THANG 1<=MONTH<=12
++month;
month=dec2bcd(month); //NHU TREN
DS1307_Write_Byte(0x05,month);
break;
case 4: //XU LY NAM
++year; //NHU TREN
if(year==100) year=0;
year=dec2bcd(year);
DS1307_Write_Byte(0x06,year);
break;
case 5: // XU LY GIO
++hours; // TANG BIEN GIO VUA DOC DUOC
if(ampm){ // THEO CHE DO 12H
if(hours>=13){ //1<=HOURS<=12
hours=1;
}
if(hours>=12){ // QUA 1 CHU KY THI DOI BUOI
if(pm!=0) pm=0;
else pm=0x20;
}
hours=dec2bcd(hours); //CHUYEN SANG BCD
oldpm=pm; //LUU BUOI HIEN TAI TRUOC KHI CHUYEN DOI HE DON VI 12=>24 VI O NHO PM SE BI GHE DE
DS1307_Write_Byte(0x02,pm|hours|ampm); //GHI VAO DS1307 VOI GIA TRI GIO, BUOI VA CHE DO GIO
}
else { if(hours==24) hours=0; //THEO CHE DO 24H
hours=dec2bcd(hours); // 0<=HOURS<=24
DS1307_Write_Byte(0x02,hours|ampm); //GHI VAO DS1307 VOI GIA TRI GIO VA CHE DO GIO
}
break;
case 6: //XU LY PHUT
if(++minutes==60) minutes=0; // PHUT 0<=MINUTES<=59
minutes=dec2bcd(minutes);
DS1307_Write_Byte(0x01,minutes);
//XOA GIAY
seconds=0; // GIAY
DS1307_Write_Byte(0x00,0);
break;
}
}

if(key==3){ //CHON HE THONG GIO 12<=>24
RTC_Read();

if(ampm){ // DANG O CHE DO 12H
oldpm=pm;
if((pm!=0)&&(hours!=12)) hours=hours+12; // NEU LA BUOI CHIEU VA KHAC 12H THI +12 VAO GIO HIEN TAI
if((pm==0)&&(hours==12)) hours=hours-12; // NEU LA BUOI SANG VA DANG LA 12 H THI GIU NGUYEN
hours=dec2bcd(hours); // DOI GIO SANG BCD
hours=hours&(~0x40); //CHO BIT 6 =0, CHON CHE DO
DS1307_Write_Byte(0x02,hours);
}
else{ //DANG O CHE DO 24H
if(hours==0) hours=12; //DANG O 0H THI CHUYEN THANH 12H
if(hours>12) hours=hours-12; //DANG O BUOI CHIEU THI TRU DI 12H
//pm=oldpm; //TRA LAI BUOI TRUOC KHI CHUYEN DOI
hours=dec2bcd(hours); // DOI GIO SANG BCD
hours=hours|0x40|pm; //CHO BIT 6=0, CHON CHE DO 12H VA KHOI PHUC BIT5(AM/PM)
DS1307_Write_Byte(0x02,hours);
}
Init();
}
return;
}
}

//***********************
//HAM GHI DU LIEU VAO 74595
void Ic74595_Write(int dat, char ic){
signed char func_count; //BIEN DEM
if(ic==1){ //CHON GHI VAO IC THU 4 DO RONG 4 BYTE
for(func_count=7;func_count>=0;func_count--){
sdo1=(dat>>func_count)&0x01; // L?Y T?NG BIT
clk1=1; //TAO XUNG DICH DU LIEU
clk1=0;
}
lat1=1; //TAO XUNG CHOT DU LIEU
lat1=0;
}
if(ic==2){ // CHON GHI VAO IC THU 2 DO RONG 2 BYTE
for(func_count=15;func_count>=0;func_count--){
sdo2=(dat>>func_count)&0x01; // LAY TUNG BIT
clk2=1; //TAO XUNG DICH DU LIEU
clk2=0;
}
lat2=1; //TAO XUNG CHOT DU LIEU
lat2=0;
}
}
// HAM QUET LED 7 DOAN
void Seven_Seg_Scan(){
if(++i>=14)i=0; // MOI LAN GHI DU LIEU 1 CON LED
// GHI DU LIEU RA CAC LED
Ic74595_Write(0xFFFF,2);//XOA DU LIEU CHON LED
Ic74595_Write(seven_seg_code[dat[i]],1);
// GHI DU LIEU CHON LED
Ic74595_Write(~(0x0001<<i),2);
}
// KHOI TAO ADC
void ADC_Init(){
REFS1_bit=1; //Vref=2.56V
REFS0_bit=1;
ADLAR_bit=0;
ADMUX|= 3;// 3 LA KENH THU ADC3
ADEN_bit=1;
ADIE_bit=1;
SFIOR=0x00; //FREE RUNNING MODE
ADCSRA|=7; //PRESCALER =128
delay_ms(10);
//ADCSRA=0x84;
}
//HAM DOC NHIET DO
void LM35_Read(){
adc_rd=ADC_Read(3);
temp=adc_rd/4-3; http://2.048
dat[13]=temp/10;
dat[12]=temp%10;
}
// KIEM TRA BAO GIO
char Alarm(){
char t1,t2;
int temp;
RTC_Read();
seconds = DS1307_Read_Byte(0x00);
seconds = ((seconds & 0x70) >> 4)*10 + (seconds & 0x0F); //LAY 3 BIT CAO 4BIT THAP
if(seconds>=10) return 0;// NEU GIAY >=10 THI TAT CHUONG
// KIEM TRA DANG O CHE DO 12 HAY 24 NEU O CHE DO 12 THI CHUYEN SANG 24 DE SO SANH
hours1=hours;
if(ampm){ // DANG O CHE DO 12H
oldpm=pm;
if((pm!=0)&&(hours1!=12)) hours1=hours1+12; // NEU LA BUOI CHIEU VA KHAC 12H THI +12 VAO GIO HIEN TAI
if((pm==0)&&(hours1==12)) hours1=hours1-12; // NEU LA BUOI SANG VA DANG LA 12 H THI GIU NGUYEN
}
for(t1=0;t1<=13;t1++){ //t1 luon la so chan

if(minutes==timer_set[t1]){
if(hours1==timer_set[t1+14]){
return 1;
}
}

}
return 0;
}


void main() {
DDRB=0xFF;
PORTB=0XFF;
DDRA=0x00;
DDRD=0xFF;
Sound_Init(&PORTD,0);
Init();

while(1){
ADC_Init();
// DOC ADC VA HIEN THI
if(j++==30)
{
j=0;
LM35_Read();
}
// DOC RTC VA HIEN THI
RTC_Read();
//KIEM TRA BAO GIO
PORTD7_bit=Alarm();
//DANH DAU MODE O CHE DO NAO
Mode_Maker();
// DOC VA XY LY PHIM
Button_Process();
}
}

void Timer0_ISR() org 0x12{
Seven_Seg_Scan();
TCNT0=-60;
}

Xem lý lịch thành viên http://sinvietelevator.com

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết