8/01/2013

3.2. Chương trình điều khiển 8 led đơn sáng lần lượt theo quy luật: LED0 sáng – LED1 sáng – ….- LED7 sáng.

Download Code Here
-----------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.all;

entity D3_C2 is
port(
rst : in STD_LOGIC;
clk : in STD_LOGIC;
seg : out STD_LOGIC_VECTOR(7 downto 0)
    );
end D3_C2;

architecture D3_C2 of D3_C2 is
begin
process(rst,clk)
variable dem:integer range 0 to 9;
begin
if (rst='1') then dem:=0;
elsif (rising_edge(clk)) then
if (dem=8) then dem:=0;
else dem:=dem+1;
end if;
end if;
case dem is
when 0=> seg <="00000000";
when 1=> seg <="00000001";
when 2=> seg <="00000010";
when 3=> seg <="00000100";
when 4=> seg <="00001000";
when 5=> seg <="00010000";
when 6=> seg <="00100000";
when 7=> seg <="01000000";
when others=> seg <="10000000";
end case;
end process;
end D3_C2;
-- rst=500Khz; clk=10Mhz;
---------------------------------------------------------------------------------------------------------
Chi tiết xin liên hệ:
Nguyễn Duy Tân
Email: nguyenduytan1909@gmail.com hoặc duytandhdt3k5@gmail.com
Yahoo: nguyenduytan1909
Skype: Tannd1909
FaceBook:Nguyễn Duy Tân

3.1. Thiết kế và mô phỏng bộ đếm 9 có chức năng lựa chọn đếm tiến hoặc đếm lùi và hiển thị kết quả đếm trên LED 7 thanh.

Download Code Here
-----------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.all;

entity D3_C1 is
port(
rst : in STD_LOGIC;
sel : in STD_LOGIC;
clk : in STD_LOGIC;
seg : out STD_LOGIC_VECTOR(7 downto 0)
    );
end D3_C1;

architecture D3_C1 of D3_C1 is
begin
process(rst,clk,sel)
variable dem:integer range 0 to 9;
begin
if (rst='1') then dem:=0;
elsif (rising_edge(clk)) then
if (sel='1') then
if (dem=9) then dem:=0;
else dem:=dem+1;
end if;
elsif (sel='0') then
if(dem=0) then dem:=9;
else dem:=dem-1;
end if;
end if;
end if;
case dem is
                when 0 => seg<= x"C0";
                when 1 => seg<= x"F9";
                when 2 => seg<= x"A4";
                when 3 => seg<= x"B0";
                when 4 => seg<= x"99";
                when 5 => seg<= x"92";
                when 6 => seg<= x"82";
                when 7 => seg<= x"F8";
                when 8 => seg<= x"80";
                when others => seg<= x"90";
end case;
end process;
end D3_C1;
-- rst=500Khz; sel=1Mhz; clk=20Mhz;
---------------------------------------------------------------------------------------------------------
Chi tiết xin liên hệ:
Nguyễn Duy Tân
Email: nguyenduytan1909@gmail.com hoặc duytandhdt3k5@gmail.com
Yahoo: nguyenduytan1909
Skype: Tannd1909
FaceBook:Nguyễn Duy Tân

2.2. Chương trình đếm tiến theo mã nhị phân Kđ = 128, đầu ra hiển thị trên 8 LED đơn

Download Code Here
-----------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.STD_LOGIC_ARITH.all;

entity D2_C2 is
port(
rst : in STD_LOGIC;
clk : in STD_LOGIC;
seg : out STD_LOGIC_VECTOR(7 downto 0)
    );
end D2_C2;

--}} End of automatically maintained section

architecture D2_C2 of D2_C2 is
begin
process(rst,clk)
variable dem:integer range 0 to 127;
begin
if (rst='1') then dem:=0;
else
if (rising_edge(clk)) then
if(dem=127) then dem:=0;
else dem:=dem+1;
end if;
end if;
end if;
seg<=conv_std_logic_vector(dem,8);
end process;

end D2_C2;
-- rst=0 or 78Khz; clk=25Mhz;


---------------------------------------------------------------------------------------------------------
Chi tiết xin liên hệ:
Nguyễn Duy Tân
Email: nguyenduytan1909@gmail.com hoặc duytandhdt3k5@gmail.com
Yahoo: nguyenduytan1909
Skype: Tannd1909
FaceBook:Nguyễn Duy Tân

2.1. Thiết kế và mô phỏng bộ đếm BCD và hiển thị kết quả đếm trên LED 7 thanh theo phương pháp máy trạng thái.

Download Code Here
-----------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.all;

entity D2_C1 is
port(
rst : in STD_LOGIC;
clk : in STD_LOGIC;
Q : out STD_LOGIC_VECTOR(7 downto 0)
    );
end D2_C1;

architecture D2_C1 of D2_C1 is
type state is (s0,s1,s2,s3,s4,s5,s6,s7,s8,s9);
signal s:state;
begin

next_state:process(rst,clk)
begin
if (rst='1') then s<=s0;
else
if (rising_edge(clk)) then
case s is
when s0 => s <=s1;
when s1 => s <=s2;
when s2 => s <=s3;
when s3 => s <=s4;
when s4 => s <=s5;
when s5 => s <=s6;
when s6 => s <=s7;
when s7 => s <=s8;
when s8 => s <=s9;
when s9 => s <=s0;
end case;
end if;
end if;
end process;
output_state:process(s)
begin
case s is
when s0=> Q<="00000000";
when s1=> Q<="00000001";
when s2=> Q<="00000010";
when s3=> Q<="00000011";
when s4=> Q<="00000100";
when s5=> Q<="00000101";
when s6=> Q<="00000110";
when s7=> Q<="00000111";
when s8=> Q<="00001001";
when s9=> Q<="00001010";
end case;
end process;
end D2_C1;
-- rst = 0.5Mhz, clk=10Mhz
---------------------------------------------------------------------------------------------------------
Chi tiết xin liên hệ:
Nguyễn Duy Tân
Email: nguyenduytan1909@gmail.com hoặc duytandhdt3k5@gmail.com
Yahoo: nguyenduytan1909
Skype: Tannd1909
FaceBook:Nguyễn Duy Tân

1.2. Chương trình điều khiển 2 led : LED0 và LED7 sáng nhấp nháy theo chu kỳ 1s.

Download Code Here
-----------------------------------------------------------------------------------------------------------


library IEEE;
use IEEE.STD_LOGIC_1164.all;

entity D1_C2 is
port(
clk:in std_logic;
s0,s7 : out STD_LOGIC
    );
end D1_C2;

--}} End of automatically maintained section

architecture D1_C2 of D1_C2 is
begin

process(clk)
begin
 if(clk='1') then s0<='1';s7<='1';
 else s0<='0';s7<='0';
 end if;
end process;

end D1_C2;
-- clk=0.5hz


---------------------------------------------------------------------------------------------------------
Chi tiết xin liên hệ:
Nguyễn Duy Tân
Email: nguyenduytan1909@gmail.com hoặc duytandhdt3k5@gmail.com
Yahoo: nguyenduytan1909
Skype: Tannd1909
FaceBook:Nguyễn Duy Tân

1.1. Thiết kế và mô phỏng bộ đếm BCD có chức năng lựa chọn đếm tiến hoặc đếm lùi và hiển thị kết quả đếm trên LED 7 thanh.

Download Code Here
-----------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.all;

entity D1_C1 is
port(
clk : in STD_LOGIC;
sel : in STD_LOGIC;
rst : in STD_LOGIC;
seg : out STD_LOGIC_VECTOR(7 downto 0)
    );
end D1_C1;

architecture D1_C1 of D1_C1 is
signal dem:integer range 0 to 9;
begin
process(rst,clk,sel)
variable dem:integer range 0 to 9;
begin
if (rst='1') then dem:=0;
elsif (rising_edge(clk)) then
if (sel='1') then
if (dem=9) then dem:=0;
else dem:=dem+1;
end if;
elsif (sel='0') then
if(dem=0) then dem:=9;
else dem:=dem-1;
end if;
end if;
end if;
case dem is
when 0=>seg<="00000000";
when 1=>seg<="00000001";
when 2=>seg<="00000010";
when 3=>seg<="00000011";
when 4=>seg<="00000100";
when 5=>seg<="00000101";
when 6=>seg<="00000110";
when 7=>seg<="00000111";
when 8=>seg<="00001000";
when others=>seg<="00001001";
end case;
end process;
end D1_C1;
-- rst:500Khz, sel:1Mhz, clk:20Mhz
---------------------------------------------------------------------------------------------------------
Chi tiết xin liên hệ:
Nguyễn Duy Tân
Email: nguyenduytan1909@gmail.com hoặc duytandhdt3k5@gmail.com
Yahoo: nguyenduytan1909
Skype: Tannd1909
FaceBook:Nguyễn Duy Tân

7/30/2013

Hiện thị text lên ma trận led 8x8 3 màu (có sẵn font chữ)

#include <REG51.H>
#include <string.h>
void delay(unsigned int t)
{
unsigned int i;
for(i=0;i<t;i++);
}
unsigned char code mahang[]={
// 1  2 3 4 5  6   7 8
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,//SPACE-32
0xFF,0xFF,0xA0,0xFF,0xFF,0xFF,//!    33
0xFF,0xFF,0xF8,0xF4,0xFF,0xFF,//"    34
0xEB,0x80,0xEB,0x80,0xEB,0xFF,//#    35
0xDB,0xD5,0x80,0xD5,0xED,0xFF,//$    36
0xD8,0xEA,0x94,0xAB,0x8D,0xFF,//%    37
0xC9,0xB6,0xA9,0xDF,0xAF,0xFF,//&    38
0xFF,0xFF,0xF8,0xF4,0xFF,0xFF,//'    39
0xFF,0xE3,0xDD,0xBE,0xFF,0xFF,//(    40
0xFF,0xBE,0xDD,0xE3,0xFF,0xFF,//)    41
0xD5,0xE3,0x80,0xE3,0xD5,0xFF,//*    42
0xF7,0xF7,0xC1,0xF7,0xF7,0xFF,//+    43
0xFF,0xA7,0xC7,0xFF,0xFF,0xFF,//,    44
0xF7,0xF7,0xF7,0xF7,0xF7,0xFF,//-    45
0xFF,0x9F,0x9F,0xFF,0xFF,0xFF,//x    46
0xFF,0xC9,0xC9,0xFF,0xFF,0xFF,// /   47
0xC1,0xAE,0xB6,0xBA,0xC1,0xFF,//0    48
0xFF,0xBD,0x80,0xBF,0xFF,0xFF,//1    49
0x8D,0xB6,0xB6,0xB6,0xB9,0xFF,//2    50
0xDD,0xBE,0xB6,0xB6,0xC9,0xFF,//3    51
0xE7,0xEB,0xED,0x80,0xEF,0xFF,//4    52
0xD8,0xBA,0xBA,0xBA,0xC6,0xFF,//5    53
0xC3,0xB5,0xB6,0xB6,0xCF,0xFF,//6    54
0xFE,0x8E,0xF6,0xFA,0xFC,0xFF,//7    55
0xC9,0xB6,0xB6,0xB6,0xC9,0xFF,//8    56
0xF9,0xB6,0xB6,0xD6,0xE1,0xFF,//9    57
0xFF,0xC9,0xC9,0xFF,0xFF,0xFF,//:    58
0xFF,0xA4,0xC4,0xFF,0xFF,0xFF,//;    59
0xF7,0xEB,0xDD,0xBE,0xFF,0xFF,//<    60
0xEB,0xEB,0xEB,0xEB,0xEB,0xFF,//=    61
0xFF,0xBE,0xDD,0xEB,0xF7,0xFF,//>    62
0xFD,0xFE,0xAE,0xF6,0xF9,0xFF,//?    63
0xCD,0xB6,0x8E,0xBE,0xC1,0xFF,//@    64
0x83,0xF5,0xF6,0xF5,0x83,0xFF,//A    65
0xBE,0x80,0xB6,0xB6,0xC9,0xFF,//B    66
0xC1,0xBE,0xBE,0xBE,0xDD,0xFF,//C    67
0xBE,0x80,0xBE,0xBE,0xC1,0xFF,//D    68
0x80,0xB6,0xB6,0xB6,0xBE,0xFF,//E    69
0x80,0xF6,0xF6,0xFE,0xFE,0xFF,//F    70
0xC1,0xBE,0xB6,0xB6,0xC5,0xFF,//G    71
0x80,0xF7,0xF7,0xF7,0x80,0xFF,//H 72
0xFF,0xBE,0x80,0xBE,0xFF,0xFF,//I 73
0xDF,0xBF,0xBE,0xC0,0xFE,0xFF,//J 74
0x80,0xF7,0xEB,0xDD,0xBE,0xFF,//K    75
0x80,0xBF,0xBF,0xBF,0xFF,0xFF,//L    76
0x80,0xFD,0xF3,0xFD,0x80,0xFF,//M    77
0x80,0xFD,0xFB,0xF7,0x80,0xFF,//N    78
0xC1,0xBE,0xBE,0xBE,0xC1,0xFF,//O    79
0x80,0xF6,0xF6,0xF6,0xF9,0xFF,//P    80
0xC1,0xBE,0xAE,0xDE,0xA1,0xFF,//Q    81
0x80,0xF6,0xE6,0xD6,0xB9,0xFF,//R    82
0xD9,0xB6,0xB6,0xB6,0xCD,0xFF,//S    83 
0xFE,0xFE,0x80,0xFE,0xFE,0xFF,//T    84
0xC0,0xBF,0xBF,0xBF,0xC0,0xFF,//U    85
0xE0,0xDF,0xBF,0xDF,0xE0,0xFF,//V    86
0xC0,0xBF,0xCF,0xBF,0xC0,0xFF,//W    87
0x9C,0xEB,0xF7,0xEB,0x9C,0xFF,//X    88
0xFC,0xFB,0x87,0xFB,0xFC,0xFF,//Y    89
0x9E,0xAE,0xB6,0xBA,0xBC,0xFF,//Z    90
0xFF,0x80,0xBE,0xBE,0xFF,0xFF,//[    91
0xFD,0xFB,0xF7,0xEF,0xDF,0xFF,//\    92
0xFF,0xBE,0xBE,0x80,0xFF,0xFF,//]    93
0xFB,0xFD,0xFE,0xFD,0xFB,0xFF,//^    94
0x7F,0x7F,0x7F,0x7F,0x7F,0xFF,//_    95
0xFF,0xFF,0xF8,0xF4,0xFF,0xFF,//'    96
0xDF,0xAB,0xAB,0xAB,0xC7,0xFF,//a    97
0x80,0xC7,0xBB,0xBB,0xC7,0xFF,//b 98
0xFF,0xC7,0xBB,0xBB,0xBB,0xFF,//c 99
0xC7,0xBB,0xBB,0xC7,0x80,0xFF,//d 100
0xC7,0xAB,0xAB,0xAB,0xF7,0xFF,//e    101
0xF7,0x81,0xF6,0xF6,0xFD,0xFF,//f 102
0xF7,0xAB,0xAB,0xAB,0xC3,0xFF,//g    103
0x80,0xF7,0xFB,0xFB,0x87,0xFF,//h    104
0xFF,0xBB,0x82,0xBF,0xFF,0xFF,//i    105
0xDF,0xBF,0xBB,0xC2,0xFF,0xFF,//j    106
0xFF,0x80,0xEF,0xD7,0xBB,0xFF,//k    107
0xFF,0xBE,0x80,0xBF,0xFF,0xFF,//l    108
0x83,0xFB,0x87,0xFB,0x87,0xFF,//m    109
0x83,0xF7,0xFB,0xFB,0x87,0xFF,//n    110
0xC7,0xBB,0xBB,0xBB,0xC7,0xFF,//o    111
0x83,0xEB,0xEB,0xEB,0xF7,0xFF,//p    112
0xF7,0xEB,0xEB,0xEB,0x83,0xFF,//q    113
0x83,0xF7,0xFB,0xFB,0xF7,0xFF,//r    114
0xB7,0xAB,0xAB,0xAB,0xDB,0xFF,//s    115
0xFF,0xFB,0xC0,0xBB,0xBB,0xFF,//t    116
0xC3,0xBF,0xBF,0xDF,0x83,0xFF,//u    117
0xE3,0xDF,0xBF,0xDF,0xE3,0xFF,//v    118
0xC3,0xBF,0xCF,0xBF,0xC3,0xFF,//w    119
0xBB,0xD7,0xEF,0xD7,0xBB,0xFF,//x    120
0xF3,0xAF,0xAF,0xAF,0xC3,0xFF,//y    121
0xBB,0x9B,0xAB,0xB3,0xBB,0xFF,//z    122
0xFB,0xE1,0xE0,0xE1,0xFB,0xFF//^    123
};
unsigned char code macotd[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned  int l,m,n,t,k,j,i;
unsigned char *xau=" Blog's Tannd";
unsigned char hienthi[100];
void hienchudo(unsigned int c)
{
  k=c*6-32*6;
hienthi[0]=0xff;
for(i=0;i<6;i++)
{
  hienthi[i+1]=mahang[k+i];
  }  
hienthi[7]=0xff;
  for(i=0;i<200;i++)
for(n=0;n<8;n++)
P2=macotd[n];
P1=hienthi[n];
//P3=hienthi[n];
delay(20); // delay mo
}
P1=0xff;   //
delay(500); // delay tat
}
void hienchuxanh(unsigned int c)
{
  k=c*6-32*6;
hienthi[0]=0xff;
for(i=0;i<6;i++)
{
  hienthi[i+1]=mahang[k+i];
  }  
hienthi[7]=0xff;
  for(i=0;i<200;i++)
for(n=0;n<8;n++)
P2=macotd[n];
//P1=hienthi[n];
P3=hienthi[n];
delay(20); // delay mo
}
P1=0xff;   //
delay(500); // delay tat
}void hienchuvang(unsigned int c)
{
  k=c*6-32*6;
hienthi[0]=0xff;
for(i=0;i<6;i++)
{
  hienthi[i+1]=mahang[k+i];
  }  
hienthi[7]=0xff;
  for(i=0;i<200;i++)
for(n=0;n<8;n++)
P2=macotd[n];
P1=hienthi[n];
P3=hienthi[n];
delay(20); // delay mo
}
P1=0xff;   //
delay(500); // delay tat
}
void main()
{

  l=strlen(xau)*6+8;
for(i=0;i<8;i++) hienthi[i]=0xff;
for(i=0;i<strlen(xau);i++)
{
n=0;
 k=*(xau+i)*6-32*6;
 for(j=k;j<k+6;j++)
 {
  m=8+i*6+n;
  hienthi[8+i*6+n]=mahang[k+n];
n++;
  }  
}
for(i=m+1;i<m+8;i++) hienthi[i]=0xff; 
while(1)
{
for(j=48;j<59;j++)
{
hienchudo(j);
}
for(j=48;j<59;j++)
{
hienchuxanh(j);
}for(j=48;j<59;j++)
{
hienchuvang(j);
}


for(j=65;j<92;j++)
{
hienchudo(j);
}
for(j=65;j<92;j++)
{
hienchuxanh(j);
}
for(j=65;j<92;j++)
{
hienchuvang(j);
}
for(i=0;i<3;i++)
{
    k=0;
for(t=0;t<l;t++)
{
for(j=1;j<100;j++)//thoi gian nhin thay ky tu
for(n=0;n<8;n++)
P2=macotd[n];
//P1=hienthi[n+k];
P3=hienthi[n+k];
delay(20); // delay mo
}
P1=0xff;   //
delay(500); // delay tat
k++;
}
}
for(i=0;i<3;i++)
{
    k=0;
for(t=0;t<l;t++)
{
for(j=1;j<100;j++)//thoi gian nhin thay ky tu
for(n=0;n<8;n++)
P2=macotd[n];
//P1=hienthi[n+k];
P3=hienthi[n+k];
delay(20); // delay mo
}
P1=0xff;   //
delay(500); // delay tat
k++;
}
}
for(i=0;i<3;i++)
{
    k=0;
for(t=0;t<l;t++)
{
for(j=1;j<100;j++)//thoi gian nhin thay ky tu
for(n=0;n<8;n++)
P2=macotd[n];
P1=hienthi[n+k];
//P3=hienthi[n+k];
delay(20); // delay mo
}
P1=0xff;   //
delay(500); // delay tat
k++;
}
}
}

Hiển thị số 0000-9999 lên led 7 thanh

Download Code Here
#include <REGX52.H>
#include<stdio.h>
#include<math.h>
sbit led1= P1^0;
sbit led2= P1^1;
sbit led3= P1^2;
sbit led4= P1^3;
unsigned char m[10]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char donvi,chuc,tram,nghin;

int num;
void delay(int time)
{
while(time--);
}

void sang(unsigned int num)
{
  int i;
//num=9876;
nghin=num/1000;
tram=(num%1000)/100;
chuc=((num%1000)%100)/10;
donvi=((num%1000)%100)%10;
for(i=0;i<50;i++)
{
P2=m[nghin];
led1=1; led2=led3=led4=0;
delay(100);

P2=0xff;
delay(100);

P2=m[tram];
led2=1; led1=led3=led4=0;
delay(100);

P2=0xff;
delay(100);

P2=m[chuc];
led3=1; led1=led2=led4=0;
delay(100);

P2=0xff;
delay(100);

P2=m[donvi];
led4=1; led1=led3=led2=0;
delay(100);

P2=0xff;
delay(100);
}
}
void main()
{
while(1)
{
int n;
for(n=0;n<9999;n++)
{
sang(n);
}
}
}

Tạo 2 xung có tần số khác nhau

Download Code Here
#include <REGX52.H>
sbit xung1= P2^0;
sbit xung2= P2^1;

void ngatT0(void) interrupt 1
{
 TR0=0;
 TH0=-10000/256;
 TL0=-10000%256;
 TR0=1;
 xung2=~xung2;
}
void ngatT1(void) interrupt 3
{
xung1=~xung1;
}
void main(void)
{
TMOD = 0x21;
TH1= TL1=-25;
TR1=1;

IE=0x8A;
IP=0;
TF0=1;
while(1);
}

Hiển thị chữ lên LCD

Download Code Here
#include <REGX52.H>
#include <stdio.h>
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^2;
char x;
void delay30ms(void)
{
TMOD = 0x10;
TH1 = 35535/256;
TL1 = 35535%256;
TR1 = 1;
while (!TF1);
TR1 = TF1=0;
}
void delay(unsigned long int t)
{
unsigned long int i;
for (i=0;i<t;i++);
}
void busy_flag(void)
{
P2 = 0xff;
RS = 0;
RW = 1;
do
{
EN = 0;
delay(10);
EN = 1;
x = P2;
x = x&0x80;
}
while(x==0x80);
}
void write_command(unsigned char LCD_command)
{
busy_flag();
P2 = LCD_command;
RS = 0;
RW = 0;
EN = 1;
delay(50);
EN = 0;
delay(50);
}
void write_data (unsigned char LCD_data)
{
busy_flag();
P2 = LCD_data;
RS = 1;
RW = 0;
EN = 1;
delay(50);
EN = 0;
delay(50);
}
void write_string(char *s)
{
while(*s)
{
write_data(*s);
s++;
}
}

void init(void)
{
write_command(0x03);
write_command(0x38);
write_command(0x06);
write_command(0x0c);
}
void main(void)
{
delay30ms();
init();


write_command(0x01);
delay(10);
write_command(0x80);
write_string(" DUY TAN ");
delay(10);
write_command(0xc0);
write_string (" KHOA DIEN TU");
  while(1);
}