Chat
Danh mục
Mạch led ma trận dùng PIC 16F877A

Mạch led ma trận dùng PIC 16F877A

Số lượng:
Thêm vào giỏ
Mạch led ma trận dùng PIC 16F877A đã được thêm vào giỏ hàng




Nhiều bạn sẽ cần file lập trình code C chip PIC 16F877A hiển thị chạy chữ trên led ma trận. Code do CTV Dao Nguyen chia sẻ.

Các bạn sinh viên có thể download về nghiên cứu:

File gồm:
+ Mô Phỏng Protues 8.6
+ Code C
+ Nguyên lý, PCB vẽ orcad/ Altium các bạn cần liên hệ bên dưới để được tư vấn

Code C:
#include <main.h>
#define IN   PIN_D1
#define CS   PIN_D2
#define CLK  PIN_D3
unsigned char Buffer_display[8][4];
#define max_char 50
unsigned char text[max_char]="Vui long nhap data   ";

const unsigned char maquet[8]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
const unsigned char font[][7]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00, //  32
0x10,0x38,0x38,0x10,0x10,0x00,0x10, //! 33
0x6C,0x6C,0x48,0x00,0x00,0x00,0x00, //" 34
0x00,0x28,0x7C,0x28,0x28,0x7C,0x28, //# 35
0x20,0x38,0x40,0x30,0x08,0x70,0x10, //$ 36
0x64,0x64,0x08,0x10,0x20,0x4C,0x4C, //% 37
0x20,0x50,0x50,0x20,0x54,0x48,0x34, //& 38
0x30,0x30,0x20,0x00,0x00,0x00,0x00, //' 39
0x10,0x20,0x20,0x20,0x20,0x20,0x10, //( 40
0x20,0x10,0x10,0x10,0x10,0x10,0x20, //) 41
0x00,0x28,0x38,0x7C,0x38,0x28,0x00, //* 42
0x00,0x10,0x10,0x7C,0x10,0x10,0x00, //+ 43
0x00,0x00,0x00,0x00,0x00,0x30,0x30, //, 44
0x00,0x00,0x00,0x7C,0x00,0x00,0x00, //- 45
0x00,0x00,0x00,0x00,0x00,0x30,0x30, //. 46
0x00,0x04,0x08,0x10,0x20,0x40,0x00, /// 47
0x38,0x44,0x4C,0x54,0x64,0x44,0x38, //0 48
0x00,0x04,0x08,0x10,0x20,0x40,0x00, //1 49
0x38,0x44,0x04,0x18,0x20,0x40,0x7C, //2 50
0x38,0x44,0x04,0x38,0x04,0x44,0x38, //3 51
0x08,0x18,0x28,0x48,0x7C,0x08,0x08, //4 52
0x7C,0x40,0x40,0x78,0x04,0x44,0x38, //5 53
0x18,0x20,0x40,0x78,0x44,0x44,0x38, //6 54
0x7C,0x04,0x08,0x10,0x20,0x20,0x20, //7 55
0x38,0x44,0x44,0x38,0x44,0x44,0x38, //8 56
0x38,0x44,0x44,0x3C,0x04,0x08,0x30, //9 57
0x00,0x00,0x30,0x30,0x00,0x30,0x30, //: 58
0x00,0x00,0x30,0x30,0x00,0x30,0x30, //; 59
0x08,0x10,0x20,0x40,0x20,0x10,0x08, //< 60
0x00,0x00,0x7C,0x00,0x00,0x7C,0x00, //= 61
0x20,0x10,0x08,0x04,0x08,0x10,0x20, //> 62
0x38,0x44,0x04,0x18,0x10,0x00,0x10, //? 63
0x38,0x44,0x5C,0x54,0x5C,0x40,0x38, //@ 64
0x38,0x44,0x44,0x44,0x7C,0x44,0x44, //A 65
0x78,0x44,0x44,0x78,0x44,0x44,0x78, //B 66
0x38,0x44,0x40,0x40,0x40,0x44,0x38, //C 67
0x78,0x44,0x44,0x44,0x44,0x44,0x78, //D 68
0x7C,0x40,0x40,0x78,0x40,0x40,0x7C, //E 69
0x7C,0x40,0x40,0x78,0x40,0x40,0x40, //F 70
0x38,0x44,0x40,0x5C,0x44,0x44,0x3C, //G 71
0x44,0x44,0x44,0x7C,0x44,0x44,0x44, //H 72
0x38,0x10,0x10,0x10,0x10,0x10,0x38, //I 73
0x04,0x04,0x04,0x04,0x44,0x44,0x38, //J 74
0x44,0x48,0x50,0x60,0x50,0x48,0x44, //K 75
0x40,0x40,0x40,0x40,0x40,0x40,0x7C, //L 76
0x44,0x6C,0x54,0x44,0x44,0x44,0x44, //M 77
0x44,0x64,0x54,0x4C,0x44,0x44,0x44, //N 78
0x38,0x44,0x44,0x44,0x44,0x44,0x38, //O 79
0x78,0x44,0x44,0x78,0x40,0x40,0x40, //P 80
0x38,0x44,0x44,0x44,0x54,0x48,0x34, //Q 81
0x78,0x44,0x44,0x78,0x48,0x44,0x44, //R 82
0x38,0x44,0x40,0x38,0x04,0x44,0x38, //S 83
0x7C,0x10,0x10,0x10,0x10,0x10,0x10, //T 84
0x44,0x44,0x44,0x44,0x44,0x44,0x38, //U 85
0x44,0x44,0x44,0x44,0x44,0x28,0x10, //V 86
0x44,0x44,0x54,0x54,0x54,0x54,0x28, //W 87
0x44,0x44,0x28,0x10,0x28,0x44,0x44, //X 88
0x44,0x44,0x44,0x28,0x10,0x10,0x10, //Y 89
0x78,0x08,0x10,0x20,0x40,0x40,0x78, //Z 90
0x38,0x20,0x20,0x20,0x20,0x20,0x38, //[ 91
0x00,0x40,0x20,0x10,0x08,0x04,0x00, //\ 92
0x38,0x08,0x08,0x08,0x08,0x08,0x38, //] 93
0x10,0x28,0x44,0x00,0x00,0x00,0x00, //^ 94
0x00,0x00,0x00,0x00,0x00,0x00,0x7C, //_ 95
0x30,0x30,0x10,0x00,0x00,0x00,0x00, //` 96
0x00,0x00,0x38,0x04,0x3C,0x44,0x3C, //a 97
0x40,0x40,0x78,0x44,0x44,0x44,0x78, //b 98
0x00,0x00,0x38,0x44,0x40,0x44,0x38, //c 99
0x04,0x04,0x3C,0x44,0x44,0x44,0x3C, //d 100
0x00,0x00,0x38,0x44,0x78,0x40,0x38, //e 101
0x18,0x20,0x20,0x78,0x20,0x20,0x20, //f 102
0x00,0x3C,0x44,0x44,0x3C,0x04,0x38, //g 103
0x40,0x40,0x70,0x48,0x48,0x48,0x48, //h 104
0x10,0x00,0x10,0x10,0x10,0x10,0x18, //i 105
0x08,0x00,0x18,0x08,0x08,0x48,0x30, //j 106
0x40,0x40,0x48,0x50,0x60,0x50,0x48, //k 107
0x20,0x20,0x20,0x20,0x20,0x20,0x38, //l 108
0x00,0x00,0x68,0x54,0x54,0x44,0x44, //m 109
0x00,0x00,0x70,0x48,0x48,0x48,0x48, //n 110
0x00,0x00,0x38,0x44,0x44,0x44,0x38, //o 111
0x00,0x78,0x44,0x44,0x44,0x78,0x40, //p 112
0x00,0x3C,0x44,0x44,0x44,0x3C,0x04, //q 113
0x00,0x00,0x58,0x24,0x20,0x20,0x70, //r 114
0x00,0x00,0x38,0x40,0x38,0x04,0x38, //s 115
0x00,0x20,0x78,0x20,0x20,0x28,0x10, //t 116
0x00,0x00,0x48,0x48,0x48,0x58,0x28, //u 117
0x00,0x00,0x44,0x44,0x44,0x28,0x10, //v 118
0x00,0x00,0x44,0x44,0x54,0x7C,0x28, //w 119
0x00,0x00,0x48,0x48,0x30,0x48,0x48, //x 120
0x00,0x48,0x48,0x48,0x38,0x10,0x60, //y 121
0x00,0x00,0x78,0x08,0x30,0x40,0x78, //z 122
0x18,0x20,0x20,0x60,0x20,0x20,0x18, //{ 123
0x10,0x10,0x10,0x10,0x10,0x10,0x10, //| 124
0x30,0x08,0x08,0x0C,0x08,0x08,0x30, //} 125
0x00,0x00,0x28,0x50,0x00,0x00,0x00, //~ 126
};

void ghi_byte(unsigned char data)    // ham tach va dich bit vao ic MAX
{
 unsigned char i;
 output_low(CS);
 for (i=0;i<8;i++)                    // lap 8 lan tuong duong 8 bit
  {
     output_low(CLK);                 // cho sck xuong thap
     if((data&0x80) != 0) output_high(IN);            // ghi data max ben tren
     else output_low(IN); 
     data<<=1;                  //dich trai 1 lan: VD : 00011100 <<=1 = 00111000
     output_high(CLK);                // dich bit vao trong ic MAX
   }
}
void MAX_init()
{
  int i;
  output_low(CS);for(i=0;i<8;i++){ghi_byte(0x09);ghi_byte(0x00);}output_high(CS);  // che do giai ma
  output_low(CS);for(i=0;i<8;i++){ghi_byte(0x0a);ghi_byte(0x03);}output_high(CS); // cai dat cuong do sang cua LED
  output_low(CS);for(i=0;i<8;i++){ghi_byte(0x0b);ghi_byte(0x07);}output_high(CS);  // cai dat so luong led dieu khien la 7
  output_low(CS);for(i=0;i<8;i++){ghi_byte(0x0c);ghi_byte(0x01);}output_high(CS);  // cho phep ic hoat dong
  output_low(CS);for(i=0;i<8;i++){ghi_byte(0x0f);ghi_byte(0x00);}output_high(CS);  // //
 
}
void MAX_7219_Writedata(unsigned char cot,unsigned char data)
{
  ghi_byte(cot);
  ghi_byte(data);
}

//------------------------------Low layer----------------------------------------------------//
/*
 - Tang thap nhat: thay doi theo phan cung su dung
*/
#int_TIMER1
void  Ngat_TIMER1(void)
{
  static unsigned char z ; // Bien dem
  char a;
  output_low(CS); 
  for(a=0;a<8;a++)
     MAX_7219_Writedata(z+1,Buffer_display[z][a]);
  output_high(CS);  //chot du lieu
  z++;if(z==8)z=0; //tang bien dem cho hang tiep theo
  set_timer1(-2500); // 2500tick = 500us
}
//--------------------------------------END----------------------------------------------------//

void clear_display() //xoa man hinh
{
    int i;
    for(i=0;i<8;i++)
    {
       Buffer_display[i][0]=0;
       Buffer_display[i][1]=0;
       Buffer_display[i][2]=0;
       Buffer_display[i][3]=0;
    }
}
int16 dichtrai,txt_count;
void chaychu_phaitrai()
{
 signed char y,i;   
 for(y=0;y<3;y++)  //dich trai toan bo man hinh 1 pixel
  {       
      for(i=1;i<8;i++)
      {                 
          Buffer_display[i][y]<<= 1;              //dich trai tung byte
          Buffer_display[i][y]|= (Buffer_display[i][y+1]>>7); //lay bit cao cua byte lien ke ben phai cho vao bit thap cua byte truoc 
      } 
  } 
      for(i=1;i<8;i++)       // ghi data vao con led cuoi cung ben tay phai
      {                 
          Buffer_display[i][3]<<= 1;   
          Buffer_display[i][3]|= ((font[text[txt_count]-32][i-1] & (0x80>>dichtrai)) >> (7-dichtrai));
      }                                     
      dichtrai++;if(dichtrai==6){ //chay het 1 ki tu (font 6x7)
         dichtrai=0;
         txt_count++;
         if(text[txt_count]==0) //chay het 1 dong chu
            txt_count=0; //reset ve chu dau tien
      }
}

//ngat uart nhan du lieu tu PC
char status_machine,save_flag;
int16 rx_txt_count;
#int_RDA
void  RDA_isr(void)//Ngat du lieu khi truyen nhan
{
     unsigned char c,i;
     c=getc();//Gan ki tu vua nhan duoc vao bien tam
     if(status_machine)
      {
        if(c==255) //check stop
        {
          text[rx_txt_count]=0;
          dichtrai=txt_count=status_machine=0; //reset
          save_flag=1;
          for(i=0;i<8;i++){Buffer_display[i][0]=0;Buffer_display[i][1]=0;Buffer_display[i][2]=0;Buffer_display[i][3]=0;}
          return;
        }
        text[rx_txt_count++]=c;
      }
     if(c==254) //check start
      {status_machine=1;rx_txt_count=0;}
}

void save_text()
{
  int16 index;
  if(save_flag) //neu co thay doi data
  {
   save_flag=0;
   for(index=0;index<max_char;index++)
     write_eeprom(index,text[index]);
     write_eeprom(255,1); //tao chu ki (SIGNATURE)
  }
}
void read_text()
{
  int16 index;
  if(read_eeprom(255) == 1) //check chu ki (SIGNATURE), neu co thi doc data trong eeprom, khong thi bo qua
  {
   for(index=0;index<max_char;index++)
     text[index]=read_eeprom(index);
  }
}
void main()
{
   clear_display();
 
   MAX_init();
 
   ENABLE_INTERRUPTS(INT_RDA); //cho phep ngat uart
   ENABLE_INTERRUPTS(GLOBAL); //cho phep ngat toan cuc
   enable_interrupts(INT_TIMER1); //kich hoat ngat timer 1
   setup_timer_1(T1_INTERNAL); //0.2us 1 tick in 20Mhz
   set_timer1(-2500); // 2500tick = 500us
 
   while(TRUE)
   {
      chaychu_phaitrai();
      delay_ms(100);
   }

}


( Nguyên lý + Code chuẩn) Link Mediafire
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
------------------------------------------------------------------------------------
DV1 - Nhận vẽ mạch thuê ONLINE Altium Designer/ Orcad 9.2/ Protues nhanh trong ngày



-----------------------------------------------------------------------------------

DV2 - Mua linh kiện về làm mạch nhanh - Miễn phí Ship





DV3 - Kênh youtube chia sẻ kiến thức điện tử thực hành ngay


DV4 - Kênh cộng đồng Facebook Điện Tử ChipKool




eChipKool - Chia sẻ kiến thức Kết Nối đam mê điện tử