您的位置:首页 > 理论基础

分享一个计算机控制的实验例程

2014-05-12 22:41 351 查看
程序是老师写好的,但是我觉得很有参考价值,毕竟之前没用C写过采集及AD转换之类的程序



程序如下:

#include <reg52.h>

#define  uchar unsigned char

#define  uint unsigned int

#define  ulong unsigned long

sbit DAT=P2^5;

sbit CLK=P2^4;

sbit CS=P2^3;

uint LTC1292(void)

{
uint i,x;
CLK=0; DAT=1; CS=0;
for(i=0;i<14;i++)
{
CLK=1; x<<=1;
if(DAT==1) x++;
CLK=0; 
}
CS=1;
return (x&0xfff);

}

void delay(uint t)

{

  while(t--);

}

#define LCD_COM 0  // Command

#define LCD_DAT  1  // Data

sbit LcdRS=P2^0;

sbit LcdRW=P2^1;

sbit LcdEN=P2^2;

void time(unsigned int t)

{

  unsigned int i;

  for(i=0;i<t;i++);

}

void LCD_WRITE(unsigned char x,bit WS)

{

  P0=x; 

  LcdRW=0;  LcdRS=WS;

  LcdEN=1;  time(50);  LcdEN=0;

}

void LCD_Initial()

{

  LCD_WRITE(0x38,LCD_COM);   time(120);

  LCD_WRITE(0x38,LCD_COM);   time(120);

  LCD_WRITE(0x01,LCD_COM);   time(120);

  LCD_WRITE(0x06,LCD_COM);   time(120);

  LCD_WRITE(0x0c,LCD_COM);   time(120);

}

void GotoXY(unsigned char x,unsigned char y)

{

  unsigned char code table[4]={0x00,0x40,0x10,0x50};

  LCD_WRITE(0x80+table[x]+y, LCD_COM); 

}

void PutCh(char m)

{

  LCD_WRITE(m,LCD_DAT);

}

void Print(unsigned char *str)

{
while(*str!='\0')
{
PutCh(*str);
str++;
}

}

void main()

{

  uint m;

  ulong y;

  LCD_Initial();

  GotoXY(0,0); Print("LTC1292 Test....");

  GotoXY(1,0); Print("AD=  ");

  GotoXY(1,8); Print("V=    v");

  while(1) 

  {

    delay(2000);

    y=LTC1292();

    GotoXY(1,3);

    PutCh(y/1000+'0');

    PutCh((y/100)%10+'0');

    PutCh((y/10)%10+'0');

    PutCh(y%10+'0');
y=y*125; 
m=y/4095;

    GotoXY(1,10);

    PutCh((m/100)%10+'0');

   // PutCh('.');

    PutCh((m/10)%10+'0');

    PutCh(m%10+'0');

  }

}

改变TCK的温度就可以采集相应的电压
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C51