⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 disadcs.cpp

📁 大学微型计算机接口技术实验源代码,直接就可以运行,包含了C++和asm代码,比较前面.
💻 CPP
字号:
//*------------------------------------------------------*/
//*  Cexp12.cpp(远程数据采集)  ```````````````````````````*/
//*  查询方式采集数据,并通过RS232来进行传输 				    */
//*  Copyright (c) 2001 by HUST                          */
//*------------------------------------------------------*/
#include <conio.h>     //kbhit();
#include <stdio.h>     //printf();
#include <dos.h>       //delay();

#define data51 0x308    //8251A数据口
#define ctrl51 0x309	   //8251A命令/状态口
#define ctrl55 0x303    //8255命令口
#define timer2 0x306    //8253的2号计数器端口
#define timctl 0x307    //8253命令口
#define factor 16	   //波特率因子16

#ifdef      __cplusplus     //if in C++ mode
    #define __CPPARGS ...
#else
    #define __CPPARGS
#endif

unsigned long int clk = 1193182;
int choose_channel()
{
	int c;
	do{
		c=getchar();                      					 //输入通道号
		c=c-48;                            	//将读入的ASCII码转换成对应的数字*/
		if(c<0||c>7)  
				printf("\nThe ad channel NO is wrong! \nBe sure it is between 0 and 7!\nPlease input the NO again!:");    		
		else 
				break;                        //通道号正确时,跳出循环
		}while(1);        
	return(c); //返回正确的通道号
}
void main()
{
	int status,ch,pc4,count,sendhigh,sendlow;
	unsigned int  sletter;
	printf("Please input the a/d channel NO(0--7):");
	ch=choose_channel();
	printf("During the process, press any key to exit!\n");
/*--------------送通道号到8255-------------------------*/
	outportb(ctrl55,0x88);                   /*初始化8255状态字,A口输出*/
	//outportb(0x300,0);                      /*A口清0*/
	outportb(0x300,ch);                     /*送通道号至A口*/
	outportb(ctrl55,0x03);              /*pc1置1*/
	delay(50);
	outportb(ctrl55,0x02);
/*----------------对接口芯片初始化-----------------------*/
	outportb(ctrl55,0x98);              /*重写8255方式字,A口输入,C口低四位输出,C口高四位输入*/
	
	//delay(100);
/*-----------------------设置波特率------------------*/	
	count=clk/4800;
	count=count/factor;//计算计数初值
	sendhigh = (count >> 8) & 0x00ff;
	sendlow  = count & 0x00ff;
	outportb(timctl,0xb6);//初始化8253
	outportb(timer2,sendlow);//装计数初值低字节
	outportb(timer2,sendhigh);//装计数初值高字节
	outportb(ctrl55,0x07);//产生波特率
/*----------------------初始化8251-------------------*/
	outportb(ctrl51,0x00);
	outportb(ctrl51,0x40);//8251复位
	delay(50);
	outportb(ctrl51,0x4e);//8251A方式命令
	delay(50);
	outportb(ctrl51,0x27);//8251A工作命令
/*---------------------进行数据采集,并且发送数据-----------*/
	do
	{ 
			outportb(ctrl55,0x01);				/*使PC0置高*/
			delay(50);
			outportb(ctrl55,0x00);				/*使PC0置低*/
		 	do
			 {
				 pc4=inportb(0x302)&0x10;
			 }while(pc4==0);  					       //查询PC4的状态,直至为低电位,即,转换完成
		 		outportb(ctrl55,0x05);             //pc2=1,允许读转换结果
		 		sletter=inportb(0x300);            //读转换结果
		 		status=inportb(ctrl51);				//获取8251 TXRDY 状态
				if((status&0x01)!=0)					//已准备好,则将采集的数据发送出去
				{
					outportb(data51,sletter);
					printf("0x%x",sletter);
					//getch();
              //delay(50);
				}
	 }while(!kbhit());                 		 //有任意键按下即退出转换
	outportb(ctrl55,0x06);								//关闭8253

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -