Professional Documents
Culture Documents
Code Thuc Hanh Ghep Noi May Tinh
Code Thuc Hanh Ghep Noi May Tinh
#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
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 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
#define PORTB
#define PORTC
#define CRPPI
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(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
#define PORTB
#define PORTC
#define CRPPI
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
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
#define PORTB
#define PORTC
#define CRPPI
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
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
#define PORTB
#define PORTC
#define CRPPI
#define CNT0
#define CNT1
#define CNT2
#define CRPPI_CNT
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(PORTA + 8)
(PORTA + 9)
(PORTA + 10)
(PORTA + 11)
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")+11
unsigned 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 0
printf("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);
11
// Dung bo T 2 che do 3
outportb(add+10, 0x28);
xung phat ra co do rong 40us
outportb(add+10, 0);
outportb(add+11, 0x74);
outportb(add+9, 6);
40us, out1 se xuong thap
outportb(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
#define PORTB
#define PORTC
#define CRPPI
#define CNT0
#define CNT1
#define CNT2
#define CRPPI_CNT
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(PORTA + 8)
(PORTA + 9)
(PORTA + 10)
(PORTA + 11)
{
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 hieu
outportb(CRPPI_CNT, 0xD4); //0b1101 0100
byteLow = 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 hieu
outportb(CRPPI_CNT, 0xE4); //0b1110'0100
u8Return = 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: CNT2
outportb(CRPPI_CNT, 0x36); //0b0011'0110
outportb(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 bin
outportb(CRPPI_CNT, 0x70); //0b0111'0000
outportb(CNT1, 0xFF); outportb(CNT1, 0xFF); //0d65535
while ( !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 3
outportb(T0,1000); // nhap so 1000 vao bo dem T0
outportb(T0,1000/256);
outportb(DK,0x70); // chon T1, mode 0
outportb(T1,0xFF);
outportb(T1,0xFF);
while ( CountLoadedC1()==0);
16
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
#define PORTB
#define PORTC
#define CRPPI
#define CNT0
#define CNT1
#define CNT2
#define CRPPI_CNT
#define DAC
#define PORTLOW
#define PORTHIGH
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(PORTA + 8)
(PORTA + 9)
(PORTA + 10)
(PORTA + 11)
(Get_Addr("PCI_DAC"))
(DAC + 2)
(DAC + 3)
a=inportb(port_a);
printf("%d\n",a);
}
/******************************************************\
| * 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
#define PORTB
#define PORTC
#define CRPPI
#define CNT0
#define CNT1
#define CNT2
#define CRPPI_CNT
#define DAC
#define PORTLOW
#define PORTHIGH
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(PORTA + 8)
(PORTA + 9)
(PORTA + 10)
(PORTA + 11)
(Get_Addr("PCI_DAC"))
(DAC + 2)
(DAC + 3)
20
}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
#define PORTB
#define PORTC
#define CRPPI
#define CNT0
#define CNT1
#define CNT2
#define CRPPI_CNT
#define DAC
#define PORTLOW
#define PORTHIGH
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(PORTA + 8)
(PORTA + 9)
(PORTA + 10)
(PORTA + 11)
(Get_Addr("PCI_DAC"))
(DAC + 2)
(DAC + 3)
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(PORTA + 8)
(PORTA + 9)
(PORTA + 10)
(PORTA + 11)
(Get_Addr("PCI_DAC"))
(DAC + 2)
(DAC + 3)
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
#define PORTB
#define PORTC
#define CRPPI
#define CNT0
#define CNT1
#define CNT2
#define CRPPI_CNT
#define DAC
#define PORTLOW
#define PORTHIGH
(Get_Addr("PCI_8255"))
(PORTA + 1)
(PORTA + 2)
(PORTA + 3)
(PORTA + 8)
(PORTA + 9)
(PORTA + 10)
(PORTA + 11)
(Get_Addr("PCI_DAC"))
(DAC + 2)
(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..40000
unsigned 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