36
Beta document. Last update: 20/11/2010, (c)opyleft MMX, by ATP, CH-K15D1, UET (coltech), VNU. *************************************************** CHU Y - Ma nguon sau su dung cho mon hoc "Thuc hanh Ghep noi may tinh", phong thi nghiem Ghep noi may tinh (P316- G2), bo mon Dien tu va ky thuat may tinh, khoa Dien tu vien thong, truong Dai hoc Cong nghe, Dai hoc Quoc gia Ha Noi. - Code duoc toi tap hop tu cac khoa hoc truoc, co sua chua, bo sung, toi uu... hi vong co bo code chuan, hoan thien cho cac ban khoa sau (ke tu 2010). Tuy nhien cung co the con loi vi co bai toi chua test (do thoi luong thuc hanh it). - Ma nguon duoc gom lai trong file text nay cho de doc (de trong khi thi nua :) - Lap trinh chi la phu, de the hien ly thuyet, vi vay cac ban hay nam vung ly thuyet truoc khi lam bai. - Toi thuong go code theo style K&R C, dat dau { (begin) nam cung hang tren, vi du: if (true) { foo(); } - Neu ban nao co bo sung, gop y, email cho toi: [email protected] ********************************************** CACH CHAY CHUONG TRINH (GUIDE STEP BY STEP) - Chay chuong trinh "Visual C++ 6.0" - Chon menu File / New... (hoac an Ctrl+N) - Hien ra cua so New: O tab Projects, chon Win32 Console Application. O Project name, go ten project, vi du: bai_01_01. Location, co the an nut [...] de chon noi luu project, vi du: D:\thuc_hanh 1

Code Thuc Hanh Ghep Noi May Tinh

Embed Size (px)

Citation preview

Beta document. Last update: 20/11/2010, (c)opyleft MMX, by ATP, CH-K15D1, UET (coltech), VNU.

***************************************************CHU Y

- Ma nguon sau su dung cho mon hoc "Thuc hanh Ghep noi may tinh", phong thi nghiem Ghep noi may tinh (P316-G2),bo mon Dien tu va ky thuat may tinh, khoa Dien tu vien thong, truong Dai hoc Cong nghe, Dai hoc Quoc gia Ha Noi.- Code duoc toi tap hop tu cac khoa hoc truoc, co sua chua, bo sung, toi uu... hi vong co bo code chuan, hoan thien cho cac ban khoa sau (ke tu 2010).Tuy nhien cung co the con loi vi co bai toi chua test (do thoi luong thuc hanh it).- Ma nguon duoc gom lai trong file text nay cho de doc (de trong khi thi nua :)- Lap trinh chi la phu, de the hien ly thuyet, vi vay cac ban hay nam vung ly thuyet truoc khi lam bai.- Toi thuong go code theo style K&R C, dat dau { (begin) nam cung hang tren, vi du:if (true) { foo();}- Neu ban nao co bo sung, gop y, email cho toi: [email protected]

**********************************************

CACH CHAY CHUONG TRINH (GUIDE STEP BY STEP)

- Chay chuong trinh "Visual C++ 6.0"- Chon menu File / New... (hoac an Ctrl+N)- Hien ra cua so New:O tab Projects, chon Win32 Console Application.O Project name, go ten project, vi du: bai_01_01.Location, co the an nut [...] de chon noi luu project, vi du: D:\thuc_hanhAn OK- Hien ra cua so Step 1 of 1:Chon "A simple application."An OK- O cua so chuong trinh chinh co xuat hien Workspace 'bai_01_01' duoi dang cay thu muc:Chon (kich dup chuot) bai_01_01 files / Sources Files / bai_01_01.cpp. Xoa sach noi dung trong file do, va paste code bai thuc hanh o duoi vao.Kich chuot phai vao "Header Files" chon "Add Files to Folder...", tim den file "PCIXP.h", vi du: "C:\Program Files\Microsoft Visual Studio\VC98\Include"Kich chuot phai vao "Source Files" chon "Add Files to Folder...", tim den file "DecLib.lib", vi du: "C:\Program Files\Microsoft Visual Studio\VC98\Lib"(chu y khi tim file phai chon Files of type = Library Files (.lib) thi file "DecLib.lib" moi hien len)

1

- An F5 hoac Ctrl+F5 de chay chuong trinh.- Neu van loi, luc nay hay hoi giao vien huong dan Th.S Pham Dinh Tuan de giai quyet ^^

***************************************************| * Bai 1.1: Dem cac so 0..255 (0x00..0xFF). Ghi ra cong A, 1 giay 1 lan || |\******************************************************/

#include "stdafx.h"#include "PCIXP.h" //khai bao thu vien PCI

int x = Get_Addr("PCI_8255");

#define port_A x#define port_B x+1#define port_C x+2#define CRPPI x+3

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

unsigned char i=0;outportb(CRPPI,0x80);for( i=0; i<=255; i++){

outportb(port_A,i);Sleep(10);

}return (0);

}/*********************************************************\

| * Bai 1.2: Doc tu cong A, ghi ra cong C. Thoat khi an phim hoac cac byte || cong A deu = 1 |

\***************************************************/

#include "stdafx.h"#include "PCIXP.h"#include "conio.h"

int x = Get_Addr("PCI_8255");

#define port_a x#define port_b x+1#define port_c x+2#define crppi x+3

2

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

outportb(crppi,0x90);int i;do {

i= inportb(x);outportb(port_c,i);

}while((i!=0xff)&&!kbhit());

return 0;}/**************************************************\

| * Bai 1.3: Nang bit PA5 va PA7 len 1 ma khong anh huong cac bit con lai || Den sang theo quy luat || |\*********************************************/

#include "stdafx.h"#include "pcixp.h"#include <conio.h>

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)

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

unsigned char bitIndex = 0, ch;

printf("\nPress any key to swap LEDs\n");outportb(CRPPI, 0x80);while ( !kbhit() ) {

outportb(PORTA, 1<<bitIndex); Sleep(100);outportb(PORTC, 1<<(7-bitIndex)); Sleep(100);if (++bitIndex == 8) bitIndex = 0;

}fflush(stdin); ch = getch();while ( !kbhit() ) {

outportb(PORTA, 1<<(7-bitIndex)); Sleep(100);outportb(PORTC, 1<<bitIndex); Sleep(100);if (++bitIndex == 8) bitIndex = 0;

}

3

return 0;}/***************************************************\

| * Bai 1.4: Ghi ra cong A 2 so BCD tuong ung 0..99. Tre 1s. |

\*****************************************************/

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)

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

unsigned char i, j;

outportb(CRPPI, 0x80);for (i=0; i<10; i++)

for (j=0; j<10; j++) {outportb(PORTA, (i<<4)+j);Sleep(1000);if ( kbhit() ) exit;

}

return 0;}

/*********************************************************\

| * Bai 1.5: Kiem tra va thong bao gia tri cac bit cong A, che do 0, mode vao || |\********************************************************/

#include "stdafx.h"#include "pcixp.h"#include <conio.h>

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)

4

#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)

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

unsigned char u8Data, bitIndex;

outportb(CRPPI, 0x90);while ( !kbhit() ) {

u8Data = inportb(PORTA);for (bitIndex=0; j<8; bitIndex++) {

printf("Bit %u = %u \n", bitIndex, ((u8Data)&(1<<bitIndex)) == (1<<bitIndex) );

}Sleep(1000);

}

return 0;}

/**************************************************************\

| * Bai 1.6: Den giao thong lap 3 bit cong C || Do -> Xanh -> Vang -> Do -> Xanh || Moi den sang 2 phut || Den sang khi bit = 1 || PA7 == 1, cac den tat het, ngung hoat dong || |\**********************************************************/

#include "stdafx.h"#include "pcixp.h"#include <conio.h>

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)

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

unsigned char u8Data, bitIndex = 0;

outportb(CRPPI, 0x90); //0b1001'0000 // A in, C outdo {

u8Data = inportb(PORTA); Sleep(100);

5

if (++bitIndex == 3) j = 0;outportb(PORTC, 1<<j); Sleep(2000);

} while ( !kbhit() && ((u8Data&0x80)==0) );

return 0;}

/********************************************************\

Bai 1.7: Thiet ke dk den giao thong tai nga tu(he gom 4 bo den)

***************************

#include "stdafx.h"#include "PCIXP.h"#include "conio.h"

#define port_a Get_Addr("PCI_8255")#define port_b Get_Addr("PCI_8255")+1#define port_c Get_Addr("PCI_8255")+2#define crppi Get_Addr("PCI_8255")+3

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

unsigned char a[3];a[0]=9;a[1]=2;a[2]=20;unsigned char x=0;outportb(crppi,0x90);while(inportb(port_a)!=0x80){

if(x==2) x=0;else x++;outportb(port_c,a[x]);Sleep(250);

}return(0);

}\***********************************************************/

| * Bai 1.8: Motor buoc || |\***********************************************************/

#include "stdafx.h"#include "pcixp.h"

6

#include "conio.h"

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)

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

unsigned char ch;char bitIndex = 0;unsigned int iDelay = 50;char flagAutomode = 0;

printf("[q]uit [a]uto [l]eft [r]ight [-]Faster(delay-10) [=]Slower(delay+10) \n");

printf("Mode is Manual \n");outportb(CRPPI, 0x80);while ( (ch=getch()) != 'q' ){

if ( (ch=='-')&&(iDelay>10) ) {iDelay-=10; printf("Delay = %d \n", iDelay);

}if ( (ch=='=')&&(iDelay<200) ) {

iDelay+=10; printf("Delay = %d \n", iDelay);}if ( (ch=='a')||(ch=='A') ) {

if (flagAutomode) { flagAutomode = 0; printf("Mode is Manual \n"); }

else { flagAutomode = 1; printf("Mode is Auto \n"); }}if ( (ch=='l')||(ch=='L') ) {

if (flagAutomode) while ( !kbhit() ) {if (--bitIndex<0) bitIndex = 3;outportb(PORTA, 1<<bitIndex); Sleep(iDelay);

}else {

if (--bitIndex<0) bitIndex = 3;outportb(PORTA, 1<<bitIndex); Sleep(iDelay);

}}if ( (ch=='r')||(ch=='R') ) {

if (flagAutomode) while ( !kbhit() ) {if (++bitIndex==4) bitIndex = 0;outportb(PORTA, 1<<bitIndex); Sleep(iDelay);

}else {

7

if (++bitIndex==4) bitIndex = 0;outportb(PORTA, 1<<bitIndex); Sleep(iDelay);

}}fflush(stdin);

}

return 0;}

/**********************************************************\

| * Bai 2.1: Phat xung vuong qua bo dem 2 trong chip PIT-8253 voi tan so bki nhap tu ban phim || |\*******************************************************/

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)

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

unsigned int freq, N;unsigned char ch;

//S C R W M M M B//SC=01:chon CNT1; RW=11:bytelow-bytehigh;//M=011:che do 3 xung vuong; BCD=0:dem bin//36: CNT0//76: CNT1//B6: CNT2outportb(CRPPI_CNT, 0x76); //0b1011'0110printf("[Esc]Exit [t]ang [g]iam [T]ang manh [G]iam manh \n");printf("Freq : "); scanf("%d", &freq);while ( (ch=getch()) != 27 ) {

if ( (ch=='T')&&(freq<=64535) ) freq+=1000;if ( (ch=='G')&&(freq>=1000) ) freq-=1000;

8

if ( (ch=='t')&&(freq<=65435) ) freq+=100;if ( (ch=='g')&&(freq>=100) ) freq-=100;N = int(1E6 / freq + 0.5);printf("Freq = %d N = %d \n", freq, N);if (N<2) printf("Frequency is too high. \n");else if (N>65535) printf("Frequency is too low. \n");else {

outportb(CNT1, N); Sleep(100);outportb(CNT1, N>>8); Sleep(100);

}}

return 0;}

/*********************************************************\

Bai 2.2: Tuong tu bai 2.1 nhug tan so co the d/c tang hoac giam lien tuc tung buoc 100Hz bang cach nhan phim “T” hoac “G”

\*******************************************************/#include "stdafx.h"#include "PCIXP.h"#include "conio.h"

#define C0 Get_Addr("PCI_8253")+8#define C1 Get_Addr("PCI_8253")+9#define C2 Get_Addr("PCI_8253")+10#define crppi Get_Addr("PCI_8253")+11unsigned char y;char i;

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

unsigned int a,b, tanso;outportb(crppi,0xB6);printf("Nhap tan so\n");scanf("%d",&a);b=int(1000000/a+0.5);if(b<2 || b>65535) printf("loi\n");

else{outportb(C2,b);}

while(1){

y=getchar();

9

if (y=='t'){

b+=100;

}else if(y=='g'){

b-=100;}if(b<2 || b>65535) {

printf("loi\n");}else {

tanso=int(1000000/(b));if(y!='\n') printf("tan so%d\n",tanso);

}

};return 0;

}--------------Hoac ---------#include "StdAfx.h"#include "PCIXP.h"#define PCI Get_Addr("PCI_8255")#define C0 PCI+8#define C1 PCI+9#define C2 PCI+10#define CRPIT (PCI+11)

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

int f,n;printf("%x\n",PCI);outportb(CRPIT,0xb6);// 00 11 011 0printf("nhap tan so:");scanf("%d",&f);

n=int(1000000/f+0.5);printf("He so chia = %d\n",n);if(n<2)

{ printf("tan so qua cao");

10

}if(n>65535)

{printf("tan so qua thap");

}else

do{

//printf("a= ");scanf("%c",&a);

if(a=='t'){

f=f+100;}if(a=='g'){

f=f-1000;

}printf("tan so f = %d\n",f);

outportb(C2,n);Sleep(400);outportb(C2,n>>8);Sleep(2000);

}while(1);

//printf("tan so",f);return 0;}

*****************Bai 2.3: CT phat ra cac xung ty le lap lai co do rong xung thoa man dk: muc thap 40,us, muc cao 200,us

******************#include "stdafx.h"#include<conio.h>#include<stdio.h>#include<dos.h>#include<PCIXP.h>void main(){

int add=Get_Addr("PCI_8255");outportb(add+11, 0xB6); // Dung bo T 2 che do 3

11

outportb(add+10, 0x28); // Chia 40 lan (0x28 = 40) , dam bao xung phat ra co do rong 40us

outportb(add+10, 0);

outportb(add+11, 0x74); // Bo dem T1, che do 2outportb(add+9, 6); // Chia 6 lan, dam bao sau 5 xung clock

40us, out1 se xuong thapoutportb(add+9, 0);

printf("\n\nChuong trinh tao song vuong co muc thap 40us, muc cao 200us.");

getch();}

************************Bai 2.4: Nhap va cho chay CT lien tuc hien thi so xung dem dc. Nhan cong tac de tao xung

*********************

#include <stdio.h>#include "stdafx.h"#include "conio.h"#include "PCIXP.h"#include "stdlib.h"int x=Get_Addr("PCI_8255");

void set_up_PIT(void){

outportb(x+11,0x76);outportb(x+9,0xE8);outportb(x+9,0x03);outportb(x+11,0x30);outportb(x+8,0xFF);outportb(x+8,0xFF);outportb(x+11,0x00);

}int getcount(int protid)

{int thap,cao;

thap= inportb(protid);cao= inportb(protid);return (0xFFFF-((thap)+(cao<<8)));

}

12

int main(void){

int count;do

{

set_up_PIT();count=getcount(x+8);printf("so dem la %d \n",(count));Sleep(1000);

}while(!kbhit()); return (0);

}

***********************

| * Bai 2.6: Do thoi gian giua 2 su kien || |\*************************************************************/

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)

unsigned char Cnt1OutLevel(){

unsigned char u8Return;//SC=11:doc CRPPI; COUNT=1; STATUS=0:chot trang thai;

COUNT#=010:Cnt1; 0:bat buoc//outportb(CRPPI_CNT, 0xE4); //0b1110 0100u8Return = inportb(CNT1);return u8Return >> 7;

}unsigned int Cnt1Value()

13

{unsigned char byteLow, byteHigh;//SC=11:doc CRPPI; STATUS=0; COUNT=1:chot gtri dem;

COUNT#=010:Cnt1; 0:bat buoc//doi vi tri 2 bit STATUS va COUNT cho de hieuoutportb(CRPPI_CNT, 0xD4); //0b1101 0100byteLow = inportb(CNT1);byteHigh = inportb(CNT1);return ((byteHigh<<8) + byteLow);

}unsigned char Cnt1Loaded(){

unsigned char u8Return;//SC=11:doc CRPPI; STATUS=1:chot trang thai; COUNT=0;

COUNT#=010:Cnt1; 0:bat buoc//doi vi tri 2 bit STATUS va COUNT cho de hieuoutportb(CRPPI_CNT, 0xE4); //0b1110'0100u8Return = inportb(CNT1);return (u8Return&0x40 == 0);

}

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

unsigned int u16Data;//S C R W M M M B//SC=00:Cnt0; RW=11:bytelow-bytehigh; M=011:mode3 xung vuong;

BCD=0:dem bin//36: CNT0//76: CNT1//B6: CNT2outportb(CRPPI_CNT, 0x36); //0b0011'0110outportb(CNT0, 0x03); outportb(CNT0, 0xE8); //0d1000//SC=01:Cnt1; RW=11:bytelow-bytehigh; M=000:mode0 dem lui 0 nhay

len cao; BCD=0:dem binoutportb(CRPPI_CNT, 0x70); //0b0111'0000outportb(CNT1, 0xFF); outportb(CNT1, 0xFF); //0d65535while ( !Cnt1Loaded() );while (Cnt1Value == 65535);

do {u16Data = Cnt1Value();while ( Cnt1OutLevel == 0 );while ( Cnt1OutLevel );

} while (Cnt1Value() != u16Data);

if ( Cnt1OutLevel() ) printf("Overflow!\n");else printf("Time = %d ms \n", 65535 - u16Data);

14

return 0;}

/***************************************************\

Bai 2.5:

*****************************8

#include "stdafx.h"#include "stdafx.h"#include<dos.h>#include<pcixp.h>#include<stdio.h>#include<conio.h>#include<iostream.h>

#define x Get_Addr("PCI_8255")#define C0 x+8#define C1 x+9#define C2 x+10#define CRPIT x+11

int getcount(int portid);void set_up_PIT(void);

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

int count;set_up_PIT();do{

//system("cls");count=getcount(C0);printf("so dem la %d\n",count);Sleep(1000);

}while (!kbhit());return 0;

}

void set_up_PIT(void){

outportb(CRPIT,0x30);outportb(C0,0xFF);

15

outportb(C0,0xFF);outportb(CRPIT,0x00);

}

int getcount(int portid){

int lsb,msb;lsb=inportb(portid);msb=inportb(portid);return(0xFFFF-(lsb+(msb<<8)));

}

***********************

Bai 2.6

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

#define P Get_Addr("PCI_8255")#define T0 P + 8#define T1 P + 9#define T2 P + 10#define DK P + 11

int CountLoadedC1();int CountValueC1();int CheckOutC0();

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

int x;

outportb(DK,0x36); // Chon T0 , mode 3outportb(T0,1000); // nhap so 1000 vao bo dem T0outportb(T0,1000/256);

outportb(DK,0x70); // chon T1, mode 0outportb(T1,0xFF);outportb(T1,0xFF);

while ( CountLoadedC1()==0);

16

// doi cho den khi C1 ngung dem while ( CountValueC1() >= 65535 );

while(1){

do{

x = CountValueC1();Sleep(100);if(CheckOutC0()==1) break;

} while ( CountValueC1() != x );if(x = 0) printf(" qua tai \n");else printf(" thoi gian troi qua la %d ms \n",65535 - x);

}getch();return 0;

}

int CountLoadedC1() // Ham kiem tra bo dem da nap hay chua{

unsigned char status,value;outportb(DK,0xE4); //1110 0100 chot trang thai T1status = inportb(T1); //doc trang thai thanh ghi tu dieu khienvalue = ((status &0x40 ) == 0 ); //kiem tra xem da nap gia tri dem chuareturn value; // value = 1 chua nap, value = 0 da nap va

doc duoc

}

int CountValueC1() // Ham doc gia tri tu T1{

unsigned char High_value,Low_value;unsigned int Value;outportb(DK,0xD4); //1101 0100 Chot so dem T1High_value = inportb(T1);Low_value = inportb(T1);Value = High_value<<8;Value |= Low_value;return Value;

}

int CheckOutC0() // Ham xac dinh trang thai PIN 7{

int status;outportb(DK,0xE2); // Chot trang thai bo dem 1status = inportb(T0); // Doc trang thai thanh ghi tu dieu khienstatus = status>>7;

17

return status;}

*************************

| * Bai 3.1: Doi 0, 255, 511, 1023, 2047, 4095 thanh tin hieu tuog tu o loi ra DAC. || |\*******************************************************/

#include "stdafx.h"#include "pcixp.h"#include <conio.h>

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)#define DAC (Get_Addr("PCI_DAC"))#define PORTLOW (DAC + 2)#define PORTHIGH (DAC + 3)

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

int iVal;do {

printf("i (1 to quit) : "); scanf("%d", &iVal);outportb(PORTLOW, iVal); Sleep(100);outportb(PORTHIGH, iVal >> 8); Sleep(100);

} while (iVal != 1);

return 0;}

-------------------------Cach khac

// Bai311.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "conio.h"#include "PCIXP.h"

18

#define port_a Get_Addr("PCI_8255")#define port_b Get_Addr("PCI_8255")+1#define port_c Get_Addr("PCI_8255")+2#define crppi Get_Addr("PCI_8255")+3#define ad Get_Addr("PCI_DAC")#define adlow Get_Addr("PCI_DAC")+4#define adhigh Get_Addr("PCI_DAC")+5

int main(int argc, char* argv[]){// unsigned int a;

//outportb(crppi, 0x90);// while(a!=inportb(port_a))// {

outportb(adlow,0x00);outportb(adhigh,0x00);printf("0");getchar();outportb(adlow,0xFF);outportb(adhigh,0x00);printf("255");getchar();outportb(adlow,0xFF);outportb(adhigh,0x01);printf("511");getchar();outportb(adlow,0xFF);outportb(adhigh,0x03);printf("1023");getchar();outportb(adlow,0xFF);outportb(adhigh,0x07);printf("2047");getchar();outportb(adlow,0xFF);outportb(adhigh,0x0F);printf("4095");getchar();

// a=inportb(port_a);// printf("%d\n",a);// };return 0;

}/******************************************************\

| * Bai 3.2: Phat song rang cua . |

19

| |\****************************************************/

#include "stdafx.h"#include <PCIXP.h>#include <iostream.h>#include <conio.h>

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

int x,bt,bc,i;int a[] = {0,255,511,1023,2047,41095};x = Get_Addr("PCI_ADC");while(!kbhit()){

for(i=0;i<=5;i++){

bt = a[i]%255;bc = a[i]/255;outportb(x+4,bt);outportb(x+5,bc);// Sleep(1000);

}// Sleep(3000);}getch();return 0;

}

***************************Cach khac

#include "stdafx.h"#include "pcixp.h"#include <conio.h>

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)#define DAC (Get_Addr("PCI_DAC"))#define PORTLOW (DAC + 2)#define PORTHIGH (DAC + 3)

20

void delay(unsigned long u32Delay){

while (u32Delay--);}

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

char ch; unsigned int iVal = 0;

while ( ch != 27 ) {if ( kbhit() ) ch = getch();iVal += 20; if (iVal >= 4095) iVal = 0;outportb(PORTLOW, iVal);outportb(PORTHIGH, iVal >> 8);delay(10000);

}

return 0;}

************************// Cách khác _AAAA:

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

int x=Get_Addr("PCI_ADC");

void delay(int t){

while(t--);}

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

int i;do{

for(i=0;i<=4095;i=i+5){

outportb(x+4,i);outportb(x+5,i>>8);delay(500);

}

21

}while(!kbhit());

return 0;}/*****************************************************\

| * Bai 3.3: Tao xung rang cua. Tan so, bien do thay doi tu ban phim. || An t: tang bien do, g: giam bien do, i: tang tan so, d: giam tan so || |\**********************************************************/

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

int x=Get_Addr("PCI_ADC");

void delay(int t){

while(t--);}

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

int bd=4000,ts=50000;

lap:while(!kbhit()){

outportb(x+4,bd);outportb(x+5,bd>>8);delay(ts);outportb(x+4,0);outportb(x+5,0);delay(ts);

}printf("\nchon: t g u d");i=getchar();if(i=='t') bd=bd+500;if(i=='g') bd=bd-500;if(i=='u') ts=ts+5000; if(i=='d') ts=ts-5000; goto lap;

return 0;}

22

*********************************Cach khac:

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)#define DAC (Get_Addr("PCI_DAC"))#define PORTLOW (DAC + 2)#define PORTHIGH (DAC + 3)

void delay(unsigned long u32Delay){

while (u32Delay--);}

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

char ch; unsigned int iVal = 0;unsigned long cky = 10000; //cky=1000..40000unsigned int bdo = 2000;

printf("[Esc]Quit [T]ang bien do 1000 [g]iam bien do 100 [i]tang chu ky 100 [D]Giam chu ky 1000 \n");

while ( ch != 27 ) {if ( kbhit() ) {

ch = getch();if ( (ch=='t')&&(bdo<=3995) ) bdo+=100;if ( (ch=='g')&&(bdo>=100) ) bdo-=100;if ( (ch=='T')&&(bdo<=3095) ) bdo+=1000;if ( (ch=='G')&&(bdo>=1000) ) bdo-=1000;if ( (ch=='i')&&(cky<=0xFFFFFFFF-100) ) cky+=100;if ( (ch=='d')&&(cky>=100) ) cky-=100;if ( (ch=='I')&&(cky<=0xFFFFFFFF-1000) ) cky+=1000;if ( (ch=='D')&&(cky>=1000) ) cky-=1000;printf("Bien do = %d Chu ky = %d \n", bdo, cky);

}iVal += 20; if (iVal >= bdo) iVal = 0;outportb(PORTLOW, iVal);

23

outportb(PORTHIGH, iVal >> 8);delay(cky);

}

return 0;}

/*********************************************************\

| * Bai 3.4: Tao xung vuong. Tan so, bien do thay doi tu ban phim. || An t: tang bien do, g: giam bien do, i: tang tan so, d: giam tan so || |\*******************************************************/

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)#define DAC (Get_Addr("PCI_DAC"))#define PORTLOW (DAC + 2)#define PORTHIGH (DAC + 3)

void delay(unsigned long u32Delay){

while (u32Delay--);}

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

char ch; unsigned int iVal = 0;unsigned long cky = 10000; //cky=1000..40000unsigned int bdo = 2000;

printf("[Esc]Quit [T]ang bien do 1000 [g]iam bien do 100 [i]tang chu ky 100 [D]Giam chu ky 1000 \n");

while ( ch != 27 ) {if ( kbhit() ) {

ch = getch();if ( (ch=='t')&&(bdo<=3995) ) bdo+=100;

24

if ( (ch=='g')&&(bdo>=100) ) bdo-=100;if ( (ch=='T')&&(bdo<=3095) ) bdo+=1000;if ( (ch=='G')&&(bdo>=1000) ) bdo-=1000;if ( (ch=='i')&&(cky<=0xFFFFFFFF-100) ) cky+=100;if ( (ch=='d')&&(cky>=100) ) cky-=100;if ( (ch=='I')&&(cky<=0xFFFFFFFF-1000) ) cky+=1000;if ( (ch=='D')&&(cky>=1000) ) cky-=1000;printf("Bien do = %d Chu ky = %d \n", bdo, cky);

}if (iVal == bdo) iVal = 0;else iVal = bdo;outportb(PORTLOW, iVal);outportb(PORTHIGH, iVal >> 8);delay(cky);

}

return 0;}

/*********************************************************\

| * Bai 3.5: Tao xung sin. Tan so, bien do thay doi tu ban phim. || An t: tang bien do, g: giam bien do, i: tang tan so, d: giam tan so || |\*********************************************************/

#include "stdafx.h"#include "pcixp.h"#include <conio.h>#include <math.h>

#define PORTA (Get_Addr("PCI_8255"))#define PORTB (PORTA + 1)#define PORTC (PORTA + 2)#define CRPPI (PORTA + 3)#define CNT0 (PORTA + 8)#define CNT1 (PORTA + 9)#define CNT2 (PORTA + 10)#define CRPPI_CNT (PORTA + 11)#define DAC (Get_Addr("PCI_DAC"))#define PORTLOW (DAC + 2)#define PORTHIGH (DAC + 3)

const PI = 3.1415926535;

void delay(unsigned long u32Delay){

25

while (u32Delay--);}

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

char ch; unsigned int iVal = 0, iTmp = 0;unsigned long cky = 10000; //cky=1000..40000unsigned int bdo = 2000;

while ( ch != 27 ) {if ( kbhit() ) {

ch = getch();if ( (ch=='t')&&(bdo<=3995) ) bdo+=100;if ( (ch=='g')&&(bdo>=100) ) bdo-=100;if ( (ch=='T')&&(bdo<=3095) ) bdo+=1000;if ( (ch=='G')&&(bdo>=1000) ) bdo-=1000;if ( (ch=='i')&&(cky<=0xFFFFFFFF-100) ) cky+=100;if ( (ch=='d')&&(cky>=100) ) cky-=100;if ( (ch=='I')&&(cky<=0xFFFFFFFF-1000) ) cky+=1000;if ( (ch=='D')&&(cky>=1000) ) cky-=1000;printf("Bien do = %d Chu ky = %d \n", bdo, cky);

}iTmp += 10; if (iTmp >= 4095) iTmp = 0;iVal = bdo * int( sin( 2*PI*iTmp/4095 ) ) + bdo;outportb(PORTLOW, iVal);outportb(PORTHIGH, iVal >> 8);delay(cky);

}

return 0;}

*************************Cach khac:

#include "stdafx.h"#include "pcixp.h"#include "math.h"#include "conio.h"

int x;

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

x=Get_Addr("PCI_ADC");float i;int gt;

26

while(1){for(i=0;i<2*3.14;i+=0.01){

gt=2000+2000*sin(i);

outportb(x+4,gt);outportb(x+5,gt>>8);Sleep(0.03);

}}return 0;

}

**********************************Bai 3.6: Cap the 1chieu DC co do lon bien doi duoc (+-10V) tren bo front-end toi loi vao A/D. Viet CT bien doi tuong tu-so A/D dien ap nay theo pp bac thang. Do lien tuc 0.5s/lan va hien gtri len man hinh.

******************

#include "stdafx.h"#include "pcixp.h"#include "conio.h"

int x=Get_Addr("PCI_ADC");

void delay(int t){

while(t--);}

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

int bd=4000,ts=50000;

lap:while(!kbhit()){

outportb(x+4,bd);outportb(x+5,bd>>8);delay(ts);outportb(x+4,0);outportb(x+5,0);delay(ts);

27

}printf("\nchon: t g u d");i=getchar();if(i=='t') bd=bd+500;if(i=='g') bd=bd-500;if(i=='u') ts=ts+5000; if(i=='d') ts=ts-5000; goto lap;

return 0;}

************************

28