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

📄 dianji.c

📁 基于S51单片机的C源码 对学习S51单片机的同学很有帮助 包含了实验室的和多模块程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/********************************************************/
/*用单片机通过变频器控制三相电机进行温度控制*/
/*作者:陈广大;*/
/*四个键P2^3(设置),     P2^4,     P2^5,     P2^6  */
/*键盘,液晶屏,看门狗,继电器好使,232好使,D/A好使*/
/**/
/********************************************************/
#pragma SMALL
#include <absacc.h>
#include <reg52.h>
#include <intrins.h>
#include <string.h>

#define uchar unsigned char
#define uint unsigned int
/*---------LCD命令----------*/
#define clear   0x01  //清屏幕,约1.6ms
#define home    0x02   //光标回零(左上角)
#define f_set   0x30  //并行、基本指令
#define dis_on  0x0f  //打开显示,光标,闪烁功能
#define in_mode 0x06  //输入方式,地址增1,光标右移
/*------------LCD-----------*/
sbit P_RS=P1^3;
sbit P_RW=P1^4;
sbit P_E= P1^5;
/**********12232*************
sbit P_RS=P0^2; //12232是液晶屏
sbit P_RW=P0^1;
sbit P_E= P0^0;
/*************************/
sbit bee=P3^6;
sbit r=P2^7;
/*-----------x5045p-----------*/
sbit      x25_clk=P1^6;
sbit      dout25=P0^3;
sbit      x25_din=P1^7;
sbit      x25_cs=P0^4;
/*--------TLC5618---------*/
sbit DIN=P2^2;//定义P2.2为串行数据口
sbit CLK=P2^1;//P2.1为串行时钟端
sbit CS=P2^0;///P2^0为片选端
/*--------18b20接线引脚-------*/
sbit Inda=P1^1;  //一共使用了5个18b20
sbit Inda1=P1^2;
sbit Inda2=P1^3;
sbit Inda3=P1^4;
sbit Inda4=P1^5;
/*----------------------------*/
idata uint  Tg1,Tg2,Tg3,Tg4,Tg5;//设置的参数
bit flag_232C,flag_cursor;
bit f_exit0,f_exit1,f_exit2,f_exit3,f_exit4,flag_sign1,flag_sign2,
flag_sign3,flag_sign4,flag_sign5;//温度采集用的标志
data uint wendu[12];//存放最近两分钟内的温度值
idata uchar T_ptr,T_ptr1,T_ptr2,T_ptr3,T_ptr4;
idata uchar T_err,T_err1,T_err2,T_err3,T_err4;
idata  uchar str2[6];
/*----------------------------------------*/
union{
     uchar  T[10];
     uint   ttt[5];
     }idata SYS;
union {
 uchar tp[10];
      uint result[5];
}idata tt;
/*----------------------------------------*/
/*t0_int() using:*/
data uchar  T_50ms,ms_timing,second,second1,second2,second3,second4,second5,
second6,second7,minute1,minute,minuter,tune;
/*-----------------------------------------*/
bdata uchar ab,bc,cd;
sbit abit0=ab^0;sbit abit1=ab^1;sbit abit2=ab^2;sbit abit3=ab^3;
sbit abit4=ab^4;sbit abit5=ab^5;sbit abit6=ab^6;sbit abit7=ab^7;
sbit bbit0=bc^0;sbit bbit1=bc^1;sbit bbit2=bc^2;sbit bbit3=bc^3;
sbit bbit4=bc^4;sbit bbit5=bc^5;sbit bbit6=bc^6;sbit bbit7=bc^7;
sbit cbit0=cd^0;sbit cbit1=cd^1;sbit cbit2=cd^2;sbit cbit3=cd^3;
sbit cbit4=cd^4;sbit cbit5=cd^5;sbit cbit6=cd^6;sbit cbit7=cd^7;
/*-------------延时程序---------------*/
void delay10(uchar j)    /*  time=j*10+7  */
{
uchar ch;
    while(j--)
    {
    ch++; ch++;
    }
}
/*-----------------------------------------------------------------*/
void delay10ms(uchar j)
{
  uchar i,jj;
  for(jj=0;jj<j;jj++) {
    for(i=0;i<13;i++) delay10(94);
  }
}
/*----------------蜂鸣器--------------------*/
void BELL()
{  bee = 0;
   delay10ms(3);
   bee = 1;
}
/*看门狗加E2PROM程序*/
/*------------------------------------------*/
void watch_dog(void)
{
x25_cs=0;
x25_cs=1;
}
/*-----------------------------------------*/
void outbyt(uchar ib)
{
uchar i;
ab=ib;
for(i=0;i<8;i++){
        x25_clk=0;
        x25_din=abit7;
        ab<<=1;
        x25_clk=1;
        }
x25_din=0;
}
/*-----------------------------------------*/
uchar inbyt(void)
{
uchar i;
dout25=1;
for(i=0;i<8;i++){
        x25_clk=1;
        x25_clk=0;
        ab<<=1;
        abit0=dout25;
        }
return ab;
}
/*--------------从指定寄存器读一个字节数-----------------*/
uchar byte_read(uint loc)
{
uchar ch;
union{
     uchar ch[2];
     uint  i;
     }u;
u.i=loc;
x25_clk=0;x25_cs=0;u.ch[0]=((u.ch[0]&0x01)<<3)|0x03;
outbyt(u.ch[0]);
outbyt(u.ch[1]);
ch=inbyt();
x25_clk=0;
x25_cs=1;
return ch;
}
/*--------------------------------------*/
uchar rdsr_cmd(void)
{
uchar ch;
        x25_clk=0;
        x25_cs=0;
        outbyt(5);
        ch=inbyt();
        x25_clk=0;
        x25_cs=1;
        return ch;
}
/*---------------------------------------*/
void wip_poll(void)
{
uchar i;
for(i=0;i<99;i++){
        ab=rdsr_cmd();
        if(!abit0) break;
        }
i=0;
}
/*---------------向指定地址写入一个字节------------------*/
void byte_write(uint loc,uchar x)
{
union{
	uchar ch[2];
	uint  i;
	}u;
x25_clk=0;
x25_cs=0;
outbyt(0x06);
x25_cs=1;
x25_cs=0;
u.i=loc;
x25_clk=0;
x25_cs=0;
u.ch[0]=((u.ch[0]&0x01)<<3)|0x02;
outbyt(u.ch[0]);
outbyt(u.ch[1]);
outbyt(x);
x25_clk=0;
x25_cs=1;
wip_poll();
}
/*------------------取数据---------------------*/
void get_buff(uchar *buff1,uchar n)
{
data uchar i,*buff;
data uint base;
buff=buff1;
base=0x60+(n-1)*12;
for(i=0;i<=9;i++){
	buff[i]=byte_read(base+i);
	}
}
/*---------------存设定完的数据------------------*/
void save_buff(uchar *buff1,uchar n1)
{
data uchar i,*buff,n;
data uint base;
buff=buff1;  n=n1;
base=0x60+(n-1)*12;
for(i=0;i<=9;i++){
	byte_write(0x60+(n-1)*12+i,buff[i]);
	}
}
/*-------------------取系统用参数------------------------*/
void sub_get_parameter(void)//取设置的参数
{
uchar buff[10];
get_buff(buff,1);
Tg1=(buff[0]&0x0f);
get_buff(buff,2);
Tg2=((buff[0]&0x0f)*10+(buff[1]&0x0f))*10+(buff[3]&0x0f);//buff[2]里是小数点
get_buff(buff,3);
Tg3=((buff[0]&0x0f)*10+(buff[1]&0x0f))*10+(buff[3]&0x0f);
get_buff(buff,4);
Tg4=((buff[0]&0x0f)*10+(buff[1]&0x0f))*10+(buff[3]&0x0f);
get_buff(buff,5);
Tg5=((buff[0]&0x0f)*10+(buff[1]&0x0f))*10;+(buff[3]&0x0f);
}
/*------------------------------------------*/
/* 液晶屏程序 */
/*------------------------------------------*/
void bit8_serial_input(uchar ix)
{
data uchar i,j,nc;
P_E=0;    //clk=0;
ab=ix;
for(i=0;i<8;i++){
	P_RW=abit7;    //din=ebit7;
	ab<<=1;
	P_E=1;
	for(j=0;j<10;j++)nc=0;
	P_E=0;
	}
}
/*----------------------------------*/
void instruct(uchar dat)
{
data uchar ch;
P_RS=1;   //选通
ch=0xf8;
bit8_serial_input(ch);
ch=dat&0xf0;
bit8_serial_input(ch);
ch=dat<<4;
bit8_serial_input(ch);
P_RS=0;  //结束作业
}
void write_abyte(uchar dat)
{
data uchar ch;
P_RS=1;  //读数据--不选通
ch=0xfa;  //RS=1 RW=0;
bit8_serial_input(ch);
ch=dat&0xf0;  //RS=1 RW=0;
bit8_serial_input(ch);
ch=dat<<4;
bit8_serial_input(ch);
P_RS=0;  //结束作业
}
/*
   写一个汉字到x,Y坐标处其中X为8列y为4行
   */
void  write_word(uint w)
{
data  uchar ch,cl;
ch=w>>8;
cl=w;
write_abyte(ch);
write_abyte(cl);
}
/*---------------------------------*/
dis_hz_str(uchar x, uchar y, uint *p_hz)
{
data uchar loc;
data uint *p;
code uchar xline[5]={0,1,3,2,4};
x=xline[x];
instruct(home);
loc=((x<<3)+y-9)|0x80;
instruct(loc); p=p_hz;
while(*p){
     write_word(*p++);
     }
}
/*------显示字母数字在x,y处------*/
void dis_str(uchar x, uchar y, uchar *str)
{
data uchar loc,*p;
code uchar xline[5]={0,1,3,2,4};
p=str;
x=xline[x];
instruct(home);
loc=(16*(x-1)+y-1);
if(loc&0x01){
       loc/=2;
       instruct(loc|0x80);
       //ch=' ';   //字符串前加空格(0x20 )
       write_abyte(' ');
       }else{
       loc/=2;
       instruct(loc|0x80);
       }
while(*p){
       write_abyte(*p++);
       }
}
/*------------------------------*/
void initial(void)
{
data uchar i,j;
P2=0xff;
P_RS=0;           //串行--不选通
P_E=0;   //串行方式
instruct(f_set);    //0x30;     //8位并行接口 基本指令
for(i=0;i<30;i++){}
for(i=0;i<250;i++){}
for(i=0;i<250;i++){}
instruct(f_set);    //0x30;     //8位并行接口 基本指令
for(i=0;i<30;i++){}
for(i=0;i<250;i++){}
for(i=0;i<250;i++){}
instruct(dis_on);     //0x0f);     //开启屏幕 光标 闪烁
for(i=0;i<30;i++){}
instruct(clear);     //0x01);     //清屏幕 clear
for(j=0;j<10;j++){
for(i=0;i<250;i++){}
   }
instruct(in_mode);     //0x06);    //光标右移、地址增1 方式
for(i=0;i<30;i++){}
instruct(home);       //0x02   //光标回零(左上角)
}
/*----------------------------------------*/
void itoa(int x,unsigned char *str2)
{
data unsigned int n;
idata unsigned char chn,*p,*p2,str1[5];
data unsigned char i,j;
p2=str2;
if(x<0){
	*p2++='-';
	x=-x;
	}
n=x;
p=&str1;
j=0;
while(n!=0){
	chn=n%10;
	n=n/10;
	*p++=chn+'0';
	j++;
	}
if(j!=0){
	p--;
	for(i=0;i<j;i++)*p2++=*p--;
	}
else    *p2++='0';
*p2=0;
return;
}
/*------------------------------------------*/
void display_run_data(uchar lin,uchar colum,int x)
{
uchar str2[6],i,L;
bit flag_neg;
if(x<0){flag_neg=1; x=-x;}else flag_neg=0;
if(x>999)x=999;
itoa(x,str2);
L=strlen(str2);
switch(L){
   case 1:
        str2[4]=str2[0]; str2[3]='.';str2[2]='0'; str2[1]='0'; str2[0]=' ';
        break;
   case 2:
        str2[4]=str2[1]; str2[3]='.';str2[2]=str2[0]; str2[1]='0'; str2[0]=' ';
        break;
   case 3:
        str2[4]=str2[2]; str2[3]='.';str2[2]=str2[1]; str2[1]=str2[0]; str2[0]=' ';
        break;
   case 4:
        str2[4]=str2[3]; str2[3]='.';
        break;
   }
   if(flag_neg){  //负数处理
       switch(L){
           case 1:
           case 2:
                str2[1]='-';
                break;
           case 3:
                str2[0]='-';
                break;
           case 4:
           case 5:
                for(i=4;i>0;i--)str2[i]=str2[i-1]; str2[0]='-';
                break;
           }
        }
   str2[5]=0;  //字符串结束
   dis_str(lin,colum,str2);
}
/*---------------固定值显示--------------------*/
void initial_xs(void)
{
data uchar i;
data uint str1[10];
code uint *Va={"各点温度值"};
P_RS=0;

for(i=0;i<10;i++)str1[i]=0;
strcpy(str1,Va);
dis_hz_str(1,3,str1);//dis_str(1,7,"℃");
dis_str(2,1,"1.");
dis_str(3,1,"2.");
dis_str(4,1,"3.") ;
dis_str(2,9,"4.");
dis_str(3,9,"5.") ;
/*for(i=0;i<10;i++)str1[i]=0;
strcpy(str1,Ia);
dis_hz_str(3,1,str1);  //dis_str(1,15,"℃");
for(i=0;i<10;i++)str1[i]=0;
strcpy(str1,Ta);
dis_hz_str(3,1,str1); //dis_str(3,15,"℃");
for(i=0;i<10;i++)str1[i]=0;
strcpy(str1,Ha);
dis_hz_str(4,1,str1); //dis_str(4,15,"℃");
for(i=0;i<10;i++)str1[i]=0;  */
instruct(0x0c);
}
/*------------------------------------------*/
/*五路18B20程序*/
/*------------------------------------------*/
uchar inti(void)
{
data uchar i=3;
do{                 //连续检测4次,看是否存在温度传感器
    Inda=0;
    delay10(50);     //发511us复位脉冲
    Inda=1;          //等待应答脉冲
    delay10(6);
    if (!Inda)      //存在应答脉冲返回1
         return(1);
    delay10(250);
    }
while(i--);
return(0);        //4次检测失败返回无传感器
}
/*-------------------------------------*/
uchar inti1(void)
{
data uchar i=3;
do{                 //连续检测4次,看是否存在温度传感器
    Inda1=0;
    delay10(50);     //发511us复位脉冲
    Inda1=1;          //等待应答脉冲
    delay10(6);
    if (!Inda1)      //存在应答脉冲返回1
         return(1);
    delay10(250);

⌨️ 快捷键说明

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