Mạch Khóa Số Điện Tử Mạch Đếm Sản Phẩm Mạch Đèn giao thông Ngã Tư Mạch Trái Tim Final Mạch Trái Tim Final Mạch Trái Tim I Love U

May 22, 2014

Mạch đồng hồ matrix lật trang PIC




Code mã nguồn C Biên dịch bằng CCS:

//ket hop voi ma trix 16*64 8con 595 test =proteus OK
#include <18F4620.h>
#device *=16 //su dung het ram trong pic                                         //Device
#fuses hs,nowdt,protect,noput,nolvp                 //Fuses
#use delay (clock=20000000)                            
#use i2c(master,fast,sda=PIN_C4,scl=PIN_C3)//
#define out_date pin_b2
#define clock_ghi_dich pin_b3
#define clock_luu_tru pin_b1
int16 data,gioi_han=0x00;
int16 data1,chay,bien;
int8 sec,hour,min,day,date,month,year;
int8 chuc_sec,dvi_sec,chuc_hour,dvi_hour,chuc_min,dvi_min;
int8 update,nhay,dem=0,mode,toc_do_nhay;
#separate void xuat_byte_595(unsigned int cX);
void chot();
void copy_ram_matrix_0();
void copy_ram_matrix_1();
void copy_ram_matrix_2();
void copy_ram_matrix_3();
void copy_ram_matrix_4();
void copy_ram_matrix_5();
void copy_ram_matrix_6();
void copy_ram_matrix_7();
void xoa_bong_ma();
void matrix_rtc ();
void caidat_matrix_rtc ();
void DS1307_Write(int8 addr,int8 dat);
int8 DS1307_read(int8 addr);
void update_timer ();
const char TEXT[11][16]=//msb....lsb
{0x00,0x00,0x00,0x3C,0x7E,0x66,0x66,0x66,0x66,0x66,0x66,0x7E,0x3C,0x00,0x00,0x00 // 0
 0x00,0x00,0x00,0x30,0x38,0x3C,0x34,0x30,0x30,0x30,0x30,0x78,0x78,0x00,0x00,0x00 // 1
 0x00,0x00,0x00,0x3C,0x66,0x60,0x60,0x70,0x38,0x1C,0x0C,0x0E,0x7E,0x00,0x00,0x00// 2
 0x00,0x00,0x00,0x3C,0x66,0x60,0x70,0x38,0x70,0x60,0x60,0x76,0x3C,0x00,0x00,0x00// 3
 0x00,0x00,0x00,0x30,0x38,0x38,0x38,0x34,0x34,0x32,0x7E,0x30,0x30,0x00,0x00,0x00// 4
 0x00,0x00,0x00,0x7C,0x0C,0x06,0x3E,0x76,0x60,0x60,0x60,0x76,0x3C,0x00,0x00,0x00//5
 0x00,0x00,0x00,0x38,0x6C,0x06,0x06,0x3E,0x66,0x66,0x66,0x6E,0x3C,0x00,0x00,0x00 // 6
 0x00,0x00,0x00,0x7E,0x60,0x30,0x30,0x18,0x18,0x1C,0x0C,0x0C,0x0C,0x00,0x00,0x00 // 7
 0x00,0x00,0x00,0x3C,0x66,0x66,0x66,0x3C,0x66,0x66,0x66,0x6E,0x3C,0x00,0x00,0x00 // 8
 0x00,0x00,0x00,0x3C,0x76,0x66,0x66,0x66,0x7C,0x60,0x60,0x36,0x1C,0x00,0x00,0x00 // 9
 0x00,0x00,0x00,0x02,0x06,0x04,0x04,0x04,0x08,0x08,0x08,0x18,0x10,0x00,0x00,0x00};//(/)
int8 matrix_0[32];
int8 matrix_1[32];
int8 matrix_2[16];
int8 matrix_3[32];
int8 matrix_4[32];
int8 matrix_5[16];
int8 matrix_6[32];
int8 matrix_7[32];
#INT_RB
Void RB_phim() // dung ngat ngoai de doc phim
{
//####### mode ############
if(!input(PIN_B4))
{mode++;output_d(mode);
if(mode>3)mode=0;}
//###### up ############
else if(!input(PIN_B5)){
if(mode==1){//up giay
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
sec++;if(sec>59){sec=0;min++;if(min>59){min=0;hour++;if(hour>23)hour=0;}
}
DS1307_Write(0x00,sec);DS1307_Write(0x01,min);DS1307_Write(0x02,hour);
}//up giay
else if(mode==2){//up phut
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
min++;if(min>59){min=0;hour++;if(hour>23)hour=0;}
DS1307_Write(0x00,sec);DS1307_Write(0x01,min);DS1307_Write(0x02,hour);
}//up phut
else if(mode==3){//up gio
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
hour++;if(hour>23)hour=0;
DS1307_Write(0x00,sec);DS1307_Write(0x01,min);DS1307_Write(0x02,hour);
}//up gio
}
//#######end up #############
//###### down  ############
else if(!input(PIN_B6)){
if(mode==1){//down giay
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
sec--;if(sec>59){sec=59;min--;if(min>59){min=59;hour--;if(hour>23)hour=23;}
}
DS1307_Write(0x00,sec);DS1307_Write(0x01,min);DS1307_Write(0x02,hour);
}//down giay
else if(mode==2){//down phut
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
min--;if(min>59){min=59;hour--;if(hour>23)hour=23;}
DS1307_Write(0x00,sec);DS1307_Write(0x01,min);DS1307_Write(0x02,hour);
}//down phut
else if(mode==3){//down gio
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
hour--;if(hour>23)hour=23;
DS1307_Write(0x00,sec);DS1307_Write(0x01,min);DS1307_Write(0x02,hour);
}//down gio
}
//######end down###########
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
chuc_sec=sec/10;dvi_sec=sec%10;
chuc_min=min/10;dvi_min=min%10;
chuc_hour=hour/10;dvi_hour=hour%10;
 for(data1=0;data1<16;data1++){
matrix_7[data1]=TEXT[dvi_sec][data1];
matrix_6[data1]=TEXT[chuc_sec][data1];
matrix_4[data1]=TEXT[dvi_min][data1];
matrix_3[data1]=TEXT[chuc_min][data1];
matrix_1[data1]=TEXT[dvi_hour][data1];
matrix_0[data1]=TEXT[chuc_hour][data1];}
}
//************************
#int_ext
void ngat_RB0()  //doc ds 1307
{update=~update;
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
day=DS1307_read(3);
date=DS1307_read(4);
month=DS1307_read(5);
year=DS1307_read(6);
chuc_sec=sec/10;dvi_sec=sec%10;
chuc_min=min/10;dvi_min=min%10;
chuc_hour=hour/10;dvi_hour=hour%10;
if(mode==0){
for(data1=0;data1<16;data1++){
matrix_7[16+data1]=TEXT[dvi_sec][data1];
matrix_6[16+data1]=TEXT[chuc_sec][data1];
matrix_4[16+data1]=TEXT[dvi_min][data1];
matrix_3[16+data1]=TEXT[chuc_min][data1];
matrix_1[16+data1]=TEXT[dvi_hour][data1];
matrix_0[16+data1]=TEXT[chuc_hour][data1];
}}//copy gia tri vao mang
else{for(data1=0;data1<16;data1++){
matrix_7[data1]=TEXT[dvi_sec][data1];
matrix_6[data1]=TEXT[chuc_sec][data1];
matrix_4[data1]=TEXT[dvi_min][data1];
matrix_3[data1]=TEXT[chuc_min][data1];
matrix_1[data1]=TEXT[dvi_hour][data1];
matrix_0[data1]=TEXT[chuc_hour][data1];}
}
}
#int_timer1 //ngat time_0 (tao ngat 125ms)
void timer_1(){
dem++;if(dem>toc_do_nhay){dem=0;nhay=~nhay;
}
set_timer1(0x0000);}
main()                                                          
{
//=0x00;
set_tris_b(0xf1);
set_tris_a(0x00);
OUTPUT_A(0x00);
set_tris_d(0x00);
SETUP_ADC_PORTS(NO_ANALOGS);
SETUP_CCP1(CCP_OFF);
SETUP_CCP2(CCP_OFF);
update=0xaa;
nhay=0xaa;
update_timer ();
sec=DS1307_read(0);
min=DS1307_read(1);
hour=DS1307_read(2);
day=DS1307_read(3);
date=DS1307_read(4);
month=DS1307_read(5);
year=DS1307_read(6);
for(data1=0;data1<16;data1++){
matrix_7[data1]=TEXT[sec%10][data1];
matrix_6[data1]=TEXT[sec/10][data1];
matrix_5[data1]=TEXT[10][data1];//(/)
matrix_4[data1]=TEXT[min%10][data1];
matrix_3[data1]=TEXT[min/10][data1];
matrix_2[data1]=TEXT[10][data1];//(/)
matrix_1[data1]=TEXT[hour%10][data1];
matrix_0[data1]=TEXT[hour/10][data1];
}//copy gia tri vao mang

EXT_INT_EDGE(H_TO_L);
enable_interrupts(INT_EXT);//ngat_Rb0
setup_timer_1 (T1_INTERNAL | T1_DIV_BY_8);
set_timer1(0x0000);//thiet lap gia tri ban dau cho timer1 65ms
enable_interrupts(INT_TIMER1);
enable_interrupts ( INT_RB ) ; // cho pheùp ngaét RB
enable_interrupts(GLOBAL);//cho phep ngat toan cuc
toc_do_nhay=1;
mode=0;
while(1)
{if(mode==0){matrix_rtc ();}
else{
caidat_matrix_rtc ();}
 }}

#separate void xuat_byte_595(unsigned int cX)
{
int8 tam,dem;
output_bit( clock_ghi_dich , 0);
output_bit( clock_luu_tru , 0);
for(dem=0;dem<=7;dem++)
   {
 tam=(cX);
 tam=tam&0x80;
 if(tam==0x80)
      {
     output_bit( out_date , 1);
      }
 else
      {
      output_bit( out_date , 0);
      }
   output_bit( clock_ghi_dich , 1);
   output_bit( clock_ghi_dich , 0);
  rotate_left(&cX ,1);//
  }
}
void chot()
{
output_bit( clock_luu_tru , 1);//mo thanh ghi chot out du lieu
output_bit( clock_luu_tru , 0);//dong thanh ghi chot
}
void copy_ram_matrix_0(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_0[copy]=matrix_0[copy+1];}
}
//####################
void copy_ram_matrix_1(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_1[copy]=matrix_1[copy+1];}
}
//####################
void copy_ram_matrix_2(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_2[copy]=matrix_2[copy+1];}
}
//####################

void copy_ram_matrix_3(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_3[copy]=matrix_3[copy+1];}
}
//####################
void copy_ram_matrix_4(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_4[copy]=matrix_4[copy+1];}
}
//####################
void copy_ram_matrix_5(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_5[copy]=matrix_5[copy+1];}
}
//####################
void copy_ram_matrix_6(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_6[copy]=matrix_6[copy+1];}
}
//####################
void copy_ram_matrix_7(){
int8 copy;
for(copy=0;copy<31;copy++){
matrix_7[copy]=matrix_7[copy+1];}
}
//####################
void xoa_bong_ma(){
int8 xoa;
for(xoa=0;xoa<8;xoa++)
{
xuat_byte_595(0x00);
}
chot();}
//######################
//**************ds 1307*****************
int8 DS1307_read(int8 addr)
{
int16 temp,ret;
i2c_start();                 /* Start i2c bus */
i2c_write(0xD0);         /* Connect to DS1307 */
i2c_write(addr);         /* Request RAM address on DS1307 */
i2c_start();                 /* Start i2c bus */
i2c_write(0xD1);         /* Connect to DS1307 for Read */
ret = I2C_read(0);         /* Receive data no ack*/
i2c_stop();
//*********************/*BCD to DEC*/********************************
temp = ret;                                                      
ret = (((ret/16)*10)+ (temp & 0x0f));         /*for Led 7seg*/
//**********************************************************
return ret;
}

void DS1307_Write(int8 addr,int8 dat)
{
int16 temp;
//**************/*DEC to BCD*/*************************
temp = dat ;                                              
dat = (((dat/10)*16)|(temp %10));         /*for Led 7seg*/
//**********************************************
i2c_start();                 /* Start i2c bus */
i2c_write(0xD0);         /* Connect to DS1307 */
i2c_write(addr);         /* Request RAM address on DS1307 */
i2c_write(dat);         /* Connect to DS1307 for Read */
i2c_stop();
}
////cai dat ds1307
void update_timer (){
i2c_start();
i2c_write(0xD0); //Gui lenh ghi du lieu */
i2c_write(0x07); //thiet lap d/c - thanh ghi thiet lap trang thai lam viec ds1307
i2c_write(0x90);//tao f=1hz tai chan 7 (ds1307)
i2c_stop();
i2c_start();
i2c_write(0xd0);//hieu la lenh ghi
i2c_write(0x00);//d/c thanh ghi giay
i2c_start();
i2c_write(0xd1);//hieu la doc du lieu ve
sec=i2c_read(0); // doc thanh ghi ke tiep
i2c_stop();
sec=sec&0b01111111;//ch=0
i2c_start();
i2c_write(0xD0); //Gui lenh ghi du lieu */
i2c_write(0x00); //d/c thanh ghi giay
i2c_write(sec);//ch=0
i2c_stop();}
  //***************************8
void matrix_rtc (){
///////////////////////
if(update==0x55){
for(data1=0;data1<16;data1++){
if(mode>0)break;
for(data=0;data<16;data++)
{
if(mode>0)break;
  OUTPUT_A(data);
  xuat_byte_595(matrix_0[data]);
  xuat_byte_595(matrix_1[data]);
  if(nhay==0xaa){
  xuat_byte_595(matrix_2[data]);}
  else{xuat_byte_595(0x00);}
  xuat_byte_595(matrix_3[data]);
  xuat_byte_595(matrix_4[data]);
  if(nhay==0xaa){
  xuat_byte_595(matrix_2[data]);}
  else{xuat_byte_595(0x00);}
  xuat_byte_595(matrix_6[data]);
  xuat_byte_595(matrix_7[data]);
  chot();
  delay_ms(1);
  xoa_bong_ma();
   }
 //#####################
  if(dvi_sec==0)
  {copy_ram_matrix_6();}
  copy_ram_matrix_7();
  if(sec==0){copy_ram_matrix_4();}
  if(dvi_min==0&&sec==0){copy_ram_matrix_3();}
  if(min==0&&sec==0){copy_ram_matrix_1();}
  if(dvi_hour==0&&min==0&&sec==0){copy_ram_matrix_0();}
  //####################
  }
  update=0xaa;//dich xong tra lai gia tri
}
   //######################
else{
for(data=0;data<16;data++)
{
if(mode>0)break;
  OUTPUT_A(data);
  xuat_byte_595(matrix_0[data]);
  xuat_byte_595(matrix_1[data]);
  if(nhay==0xaa){
  xuat_byte_595(matrix_2[data]);}
  else{xuat_byte_595(0x00);}
  xuat_byte_595(matrix_3[data]);
  xuat_byte_595(matrix_4[data]);
  if(nhay==0xaa){
  xuat_byte_595(matrix_2[data]);}
  else{xuat_byte_595(0x00);}
  xuat_byte_595(matrix_6[data]);
  xuat_byte_595(matrix_7[data]);
  chot();
  delay_ms(1);
  xoa_bong_ma();}}
  }
 void caidat_matrix_rtc (){
/* for(data1=0;data1<16;data1++){
matrix_7[data1]=TEXT[dvi_sec][data1];
matrix_6[data1]=TEXT[chuc_sec][data1];
matrix_4[data1]=TEXT[dvi_min][data1];
matrix_3[data1]=TEXT[chuc_min][data1];
matrix_1[data1]=TEXT[dvi_hour][data1];
matrix_0[data1]=TEXT[chuc_hour][data1];}*/
 for(data=0;data<16;data++)
{
  OUTPUT_A(data);
  if(mode==1){//vao che do nhap nhay cai dat giay

  xuat_byte_595(matrix_0[data]);
  xuat_byte_595(matrix_1[data]);
  xuat_byte_595(matrix_2[data]);
  xuat_byte_595(matrix_3[data]);
  xuat_byte_595(matrix_4[data]);
  xuat_byte_595(matrix_2[data]);
  if(nhay==0xaa){
  xuat_byte_595(matrix_6[data]);
  xuat_byte_595(matrix_7[data]);
  }
  else{xuat_byte_595(0x00);xuat_byte_595(0x00);}
  }
  //##################################
  if(mode==2){//vao che do nhap nhay cai dat phut

  xuat_byte_595(matrix_0[data]);
  xuat_byte_595(matrix_1[data]);
  xuat_byte_595(matrix_2[data]);
  if(nhay==0xaa){
  xuat_byte_595(matrix_3[data]);
  xuat_byte_595(matrix_4[data]);
  }
  else{xuat_byte_595(0x00);xuat_byte_595(0x00);}
  xuat_byte_595(matrix_2[data]);
  xuat_byte_595(matrix_6[data]);
  xuat_byte_595(matrix_7[data]); }
  //##################################
  //##################################
  if(mode==3){//vao che do nhap nhay cai dat gio
  if(nhay==0xaa){
  xuat_byte_595(matrix_0[data]);
  xuat_byte_595(matrix_1[data]);
  }
  else{xuat_byte_595(0x00);xuat_byte_595(0x00);}
  xuat_byte_595(matrix_2[data]);
  xuat_byte_595(matrix_3[data]);
  xuat_byte_595(matrix_4[data]);
  xuat_byte_595(matrix_2[data]);
  xuat_byte_595(matrix_6[data]);
  xuat_byte_595(matrix_7[data]); }
  //#######################################
  chot();
  delay_ms(2);
  xoa_bong_ma();}}

( Full code + Mô phỏng Protues 8.0)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút  hiện ra ở góc phải phía trên, nhấn vào để đến trang download

Nguồn codientu.org

0 comments:

Post a Comment

Bạn đọc hãy giúp chúng tôi xây dựng cộng đồng bằng cách để lại bình luận, chúng tôi luôn đón nhận mọi ý kiến của các bạn:
» Bình luận nghiêm túc và không chứa các liên kết quảng cáo.
» Vui lòng không Spam nhận xét với mọi hình thức.
» Rất mong bạn đề tên cho nhận xét của chính mình - Bằng cách chọn vào Tên/URL và điền tên bạn vào (Phần URL có thể bỏ trống ).

- Bạn có thể chèn Link nhạc (NCT), video(Youtube),Hình ảnh vào comment bằng cú pháp:
+ [youtube] Link video Youtube [/youtube].
+ [img] Link ảnh( định dạng PNG, JPG,GIF) [/img]
+Chèn link liên kết: <a href="LINK" rel="nofollow">Name</a>
-Bạn copy mã bên cạnh biểu tượng chèn vào nhận xét để bày tỏ cảm xúc!!

:) :( :)) :(( =)) =D> :D :P