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

📄 2543.c

📁 AD转换器TCL2543的测试源代码 经过测试 比较好用
💻 C
字号:
#include <reg51.h>
#include <stdlib.h>
#include <intrins.h>
#include <stdio.h>

sbit CS =P1^0;
sbit SID=P1^1;
sbit SCK=P1^2;

/************************************** 
2543控制引脚宏定义 
*************************************/ 
sbit CLOCK = P2^3; /*2543时钟*/ 
sbit D_IN = P2^2; /*2543输入*/ 
sbit D_OUT = P2^1; /*2543输出*/ 
sbit _CS  = P2^0; /*2543片选*/ 

#define uint unsigned int 
#define uchar unsigned char 
float ac;
 
unsigned char code AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,     //第一行汉字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,     //第二行汉字位置
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,     //第三行汉字位置
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,     //第四行汉字位置
};
unsigned char code str1[] = "零通道:";
unsigned char  str[10];


//串口发送一个字节
void SendByte(unsigned char Dbyte)
{
  unsigned char i;
  for(i=0;i<8;i++)
  {
      SCK = 0;
      Dbyte=Dbyte<<1;     //左移一位
      SID = CY;         //移出的位给SID
      SCK = 1;
      SCK = 0;
  }
}

//串口接收一个字节
//仅在读取数据的时候用到
//而读出的数据是一次只能读出4bit的
unsigned char ReceiveByte(void)
{
  unsigned char i,temp1,temp2;
  temp1=temp2=0;
  for(i=0;i<8;i++)
  {
      temp1=temp1<<1;
      SCK = 0;
      SCK = 1;         
      SCK = 0;
      if(SID) temp1++;
  }
  for(i=0;i<8;i++)
  {
      temp2=temp2<<1;
      SCK = 0;
      SCK = 1;
      SCK = 0;
      if(SID) temp2++;
  }
  return ((0xf0&temp1)+(0x0f&temp2));
}

void CheckBusy( void )
{
  do   SendByte(0xfc);     //11111,RW(1),RS(0),0
  while(0x80&ReceiveByte());     //BF(.7)=1 Busy
}

void WriteCommand( unsigned char Cbyte )
{
  CS = 1;
  CheckBusy();
  SendByte(0xf8);         //11111,RW(0),RS(0),0
  SendByte(0xf0&Cbyte);     //高四位
  SendByte(0xf0&Cbyte<<4);//低四位(先执行<<)
  CS = 0;
}

void WriteData( unsigned char Dbyte )
{
  CS = 1;
  CheckBusy();
  SendByte(0xfa);         //11111,RW(0),RS(1),0
  SendByte(0xf0&Dbyte);     //高四位
  SendByte(0xf0&Dbyte<<4);//低四位(先执行<<)
  CS = 0;
}

unsigned char ReadData( void )
{
  CheckBusy();
  SendByte(0xfe);         //11111,RW(1),RS(1),0
  return ReceiveByte();
}


void LcmInit( void )
{
  WriteCommand(0x30);     //8BitMCU,基本指令集合
  WriteCommand(0x03);     //AC归0,不改变DDRAM内容
  WriteCommand(0x0C);     //显示ON,游标OFF,游标位反白OFF
  WriteCommand(0x01);     //清屏,AC归0
  WriteCommand(0x06);     //写入时,游标右移动
}

//文本区清RAM函数
void LcmClearTXT( void )
{
  unsigned char i;
  WriteCommand(0x30);     //8BitMCU,基本指令集合
  WriteCommand(0x80);     //AC归起始位
  for(i=0;i<64;i++)
    WriteData(0x20);
}

//图形区和文本区显示在两个不同的RAM区
//图形区清RAM函数


void PutStr(unsigned char row,unsigned char col,unsigned char *puts)
{
  WriteCommand(0x30);     //8BitMCU,基本指令集合
  WriteCommand(AC_TABLE[8*row+col]);     //起始位置
  while(*puts != '\0')     //判断字符串是否显示完毕
  {
      if(col==8)         //判断换行
      {         //若不判断,则自动从第一行到第三行
          col=0;
          row++;
      }
      if(row==4) row=0;     //一屏显示完,回到屏左上角
      WriteCommand(AC_TABLE[8*row+col]);
      WriteData(*puts);     //一个汉字要写两次
      puts++;
      WriteData(*puts);
      puts++;
      col++;
  }
}



/*************************************/ 
void delay2(uchar n) 
{ 
uchar i; 
for(i=0;i<n;i++) 
{ 
_nop_(); 
} 
} 

/************************************** 
名称:read2543 
功能:TLC2543驱动模块 
输入参数:port通道号 
输出参数:ad转换值 
*************************************/ 
uint read2543(uchar port) 
{ 
uint ad=0,i; 
CLOCK=0; 
_CS=0; 
port<<=4; 
for(i=0;i<12;i++) 
{ 
if(D_OUT) ad|=0x01; 
D_IN=(bit)(port&0x80); 
CLOCK=1; 
delay2(3); 
CLOCK=0; 
delay2(3); 
port<<=1; 
ad<<=1; 
} 
_CS=1; 
ad>>=1; 
return(ad); 
} 


void delayms(unsigned int MS)
{
  unsigned char us,usn;
  while(MS!=0)         //for 12M
      { usn = 2;
          while(usn!=0)
              {
                  us=0xf5;
                  while (us!=0){us--;};
                  usn--;
              }
          MS--;
      }
}

void main( void )
{
  LcmInit();
  LcmClearTXT();
  PutStr(0,0,str1);
  while(1)
  {
  ac = read2543(1);
  ac = ac*5/4096;
  sprintf(str,"%0.3f",ac);
  PutStr(0,3,str);  
  delayms(100);
  }

}

⌨️ 快捷键说明

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