Cau Truc May Vi Tinh Va Ktghn

Embed Size (px)

Citation preview

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    1/9

    CNG N TP K THUT GHP NIo Vn Hot: K54V

    Bi 3.1:Vit chng trnh cho php m t 0 n 255 (00h n FFh) v vit chng ra cng Avi thi gian 1 giy mt ln m.

    #include "stdafx.h"#include "conio.h"#include "dos.h"#include "stdio.h"#include "PCIXP.h"#include "stdlib.h"#include "math.h"

    int main(int argc, char* argv[]){

    int base,i;base=Get_Addr("PCI_8255");

    outportb(base+3,0x80);

    do{

    for(i=0;i

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    2/9

    do{

    i=inportb(base);outportb(base+2,i);Sleep(100);

    }while(!kbhit()&&i!=255);getch();

    }Bi 3.3.a) Vit chng trnh nng 2 bit PA5 v PA7 ln 1 m khng lm nh hng ti cc bit cn

    li.

    #include "stdafx.h"#include "conio.h"#include "dos.h"#include "stdio.h"#include "PCIXP.h"#include "stdlib.h"

    int main(int argc, char* argv[]){

    int byte, base;base=Get_Addr("PCI_8255");outportb(base+3,0x90);byte=inportb(base);byte=byte|0xA0;outportb(base+2,byte);printf("\n\nDa dat 2 bit PA5 va PA7 len 1.");getch();

    }b) Trn 2 cng A v C lp sn cc b ch th logic bng cc n LED; hy to mt im

    sng chy vng theo 2 cng A v C theo quy lut ln lt PA0 sng -> PA1 sngPA7sng -> PC7 sng -> PC6 sng -> PC0 sng.

    #include "stdafx.h"#include "conio.h"#include "dos.h"#include "stdio.h"#include "PCIXP.h"#include "stdlib.h"#include "math.h"

    int main(int argc, char* argv[]){

    int base, i;base=Get_Addr("PCI_8255");outportb(base+3,0x80);

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    3/9

    do{

    for(i=0;i=0;i--){

    outportb(base+2,pow(2,i));Sleep(1000);

    }outportb(base+2,0x00);

    }while(!kbhit());

    }

    c) Yu cu nh cu b nhng chy vng ngc tr li.

    #include "stdafx.h"#include "conio.h"#include "dos.h"#include "stdio.h"#include "PCIXP.h"#include "stdlib.h"#include "math.h"

    int main(int argc, char* argv[]){int base, i;base=Get_Addr("PCI_8255");outportb(base+3,0x80);do{

    for(i=7;i

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    4/9

    }while(!kbhit());

    }

    Bi 3.7: iu khin n giao thng theo quy lut:- n vng lun sng sau n xanh, n lun sng sau n vng v n xanh lun sng

    sau n - Mi n sng trong 2 pht- Cc n s sng khi mc logic ca cc bit tng ng bng 1- H s tt ht n v ngng hot ng khi bit PA7 bng 1

    #include "stdafx.h"#include "conio.h"#include "dos.h"#include "stdio.h"#include "PCIXP.h"#include "stdlib.h"

    #include "math.h"int main(int argc, char* argv[]){

    int base,u;base=Get_Addr("PCI_8255");outportb(base+3,0x90);do{

    u=inportb(base);for(int i=0;i

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    5/9

    #include "math.h"

    int main(int argc, char* argv[]){

    int base, i;char ch;int delay=50;

    base=Get_Addr("PCI_8255");outportb(base+3,0x80);printf("nhan phim [q]:thoat,[t]:quay trai,[p]:quay phai,[+]:delay tang,[-]:delay giam\n");do{

    ch=getch();if(ch=='t'){

    do{

    for(i=0;i=0;i--){outportb(base+2,pow(2,i));Sleep(delay);

    }}while(!kbhit());

    }if(ch=='+'){

    delay+=10;printf("delay=%d\n",delay);

    }if(ch=='-'){

    delay-=10;printf("delay=%d\n",delay);

    }}

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    6/9

    while(ch!='q');getch();

    }

    Bi 3.11: Ni li ra ca my pht xung nhp 1MHz ti li vo CLK2 ca b m 2 chip PIT 8253/54; ni li vo ca GATE2 ln +5V lun cho php m.Nhp v cho chy chngtrnh pht sng vung qua b m 2 trong chip PIT -8253/54 vi tn s bt k c nhp t bnphm.Ch : Cng GATE ca b m C2 ni vi ngun +5V, cng CLOCK ni vi tn hiu 1MHz, cngOUT ni vi dao ng k

    #include "stdafx.h"#include "conio.h"#include "dos.h"

    #include "stdio.h"#include "PCIXP.h"#include "stdlib.h"

    int main(int argc, char* argv[])

    {int n,f;int add= Get_Addr(PCI_8255);do{

    cout > f;if(f > pow(10,6)) cout pow(10,6)); //Gii hn tn s nhp khng qu 1MHzn=pow(10,6)/f; //Tnh s chia np vo b moutportb(add+11,0xB6); // Chn b m C2, ch vit byte thp trc, byte cao sau

    //Ch 3 pht xung vungoutportb(add+10,n%256); //Np byte thpoutportb(add+10,n/256); //Np byte cao

    }

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    7/9

    Bi 3.13: Dng cc b m trong 8253/8254 v ngun xung nhp 1MHz, vit chng trnh pht ra cc xung t l lp li c rng xung tha mn iu kin sau: Mc thp 40m mccao 200m. Quan st v o trn dao ng k.

    #include "stdafx.h"#include "conio.h"#include "dos.h"#include "stdio.h"#include "PCIXP.h"#include "stdlib.h"

    int main(int argc, char* argv[]){

    int a,b;int base;base=Get_Addr("PCI_8255");outportb(base+11,0x16);printf("nhap so dem ban dau\n");// dinh dang dem nhi phanscanf("%d",&a);outportb(base+8,a);outportb(base+11,0x54);

    printf("nhap so chia\n");scanf("%d",&b);outportb(base+9,b);getch();

    3.13.Pht ra cc xung t l: mc thp 40 micro giy, mc cao 200 micro giy(code chng trnh y dng 2 b m C0 v C1)Ch : Cng GATE ca 2 b m C0 v C1 ni vi ngun +5V, cng CLOCK ca C0 ni vi cngtn hiu 1MHz, cng OUT ca C0 ni vi cng CLOCK ca C1, cng CLOCK ca C1 ni vi daong k

    #include "stdafx.h"#include "conio.h"#include "dos.h"#include "stdio.h"#include "PCIXP.h"#include "stdlib.h"

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    8/9

    int main(int argc, char* argv[])

    {int add = Get_Addr(PCI_8255);outportb(add+11,0x16); //Chn b m C0, vit byte thp, ch to 3 to xung

    vung

    outportb(add+8,40); //Np s chia to xung 40 micro giyoutportb(add+11,0x54); //Chn b m C1, vit byte thp, ch 2 to xung tl

    outportb(add+9,6); //Np h s t l (200 + 40) / 40}

    3.14. Lin tc hin th s xung c m. Nhn cng tc to xung

    #include "stdafx.h"#include "conio.h"#include "dos.h"

    #include "stdio.h"#include "PCIXP.h"#include "stdlib.h"

    int main(int argc, char* argv[]){

    int count1,count2,byte_thap,byte_cao;int add = Get_Addr(PCI_8255);outportb(add+11,0x70); //dung bo dem C1, vit byte thp trc, cao sau, ch 0outportb(add+9,0xFF); //Vit byte thpoutportb(add+9,0xFF); //Vit byte cao

    count1 = 0xFFFF; //Gi tr ban u ca b m trc khi moutportb(add+11,0x40); //Lnh cht b mdo{

    byte_thap = inportb(add+9);byte_cao = inportb(add+9);count2 = count1 - ((byte_thap&0xff) + (byte_cao

  • 7/30/2019 Cau Truc May Vi Tinh Va Ktghn

    9/9

    {int miliseconds;int HighByte=0,LowByte=0,value1, value2;int add=Get_Addr("PCI_8255");

    // Dung bo dem C0 cua 8254 de tao ra clock co chu ky 1ms.

    // Out cua C0 ket noi vao Clock cua C1

    outportb(add+11, 0x36); //dung bo T0 che do 3outportb(add+8, 0xE8); //np vo s chia 1000outportb(add+8, 0x03);

    // Bat dau chuong trinh chinhoutportb(add+11, 0xB2); // Dung bo dem T2 che do 1outportb(add+10, 0xFF); // Nap gia tri dem toi daoutportb(add+10, 0xFF);

    outportb(add+11, 0x80); // Chot bo dem T2LowByte = inportb(add+10); // Doc byte thapHighByte = inportb(add+10); // Doc byte caovalue1 = (LowByte&0xFF) + (HighByte