導航:首頁 > 操作系統 > 單片機的code

單片機的code

發布時間:2025-06-23 04:46:12

單片機課程設計,急求!!!

#defineLCD1602_FLAG
#defineLCD1602_PORTP0
#include<reg51.h>
#include<stddef.h>
#defineucharunsignedchar

sbitlcd1602_rs=P2^0;
sbitlcd1602_e=P2^2;
sbitlcd1602_rw=P2^1;
sbitlcd1602_busy=P0^7;

ucharstr[]="ThisisKeyTest";
ucharkeyval,keystr[16];

voiddelay(uchara)
{
uchari;
while(a--)for(i=0;i<120;i++);
}

/*
************************************
*函數名稱:lcd1602_CheckBusy()
*函數功能:狀態查詢
************************************
*/

voidlcd1602_CheckBusy()
{
do
{
lcd1602_busy=1;
lcd1602_rs=0;
lcd1602_rw=1;
lcd1602_e=0;
lcd1602_e=1;
}
while(lcd1602_busy);
}

/*
***************************************
*函數名稱:lcd1602_WriteCmd()
*函數功能:寫命令
*入口參數:命令字
*出口參數:無
***************************************
*/

voidlcd1602_WriteCmd(constucharcmd)
{
lcd1602_CheckBusy();
lcd1602_rs=0;
lcd1602_rw=0;
lcd1602_e=1;
LCD1602_PORT=cmd;
lcd1602_e=0;
}

/*
*******************************************
*函數名稱:lcd1602_WriteData()
*函數功能:寫數據
*入口參數:c--待寫數據
*出口參數:無
*********************************************
*/

voidlcd1602_WriteData(constucharc)
{
lcd1602_CheckBusy();
lcd1602_rs=1;
lcd1602_rw=0;
lcd1602_e=1;
LCD1602_PORT=c;
lcd1602_e=0;
}

/*
***********************************************
*函數名稱:lcd1602_Init()
*函數功能:初始化LCD
*入口參數:無
*出口參數:無
***********************************************
*/

voidlcd1602_Init()
{
lcd1602_WriteCmd(0x38); //顯示模式為8位2行5*7點陣
lcd1602_WriteCmd(0x0c);//displayenable,flagenable,flashenable,
lcd1602_WriteCmd(0x06);//flagmovetoright,screendon'tmove
lcd1602_WriteCmd(0x01);//clearscreen
}

/*
************************************************
*函數名稱:lcd1602_Display()
*函數功能:字元顯示
*入口參數:ptr--字元或字元串指針
*出口參數:無
*說明:用戶可通過以下方式來調用:
*1)lcd1602_Display("Hello,world!");
*2)INT8U存儲類型txt[]="要顯示的字元串";
*或者INT8U存儲類型txt[]={'t','x','t',..,''};
*INT8U*ptr;
*ptr=&txt;
*lcd1602_Display(ptr);
*或lcd1602_Display(txt);
*或lcd1602_Display(&txt);
************************************************
*/

voidlcd1602_Display(constuchar*ptr,ucharline)
{
uchardatai=0;
uchar*dataq;

q=ptr;
switch(line)
{
case0:
lcd1602_WriteCmd(0x80);
while(q!=NULL&&(*q!='')&&i<16)
{
lcd1602_WriteData(*q);
q++;
i++;
}
break;
case1:
lcd1602_WriteCmd(0xc0);
while(q!=NULL&&(*q!='')&&i<16)
{
lcd1602_WriteData(*q);
q++;
i++;
}
break;
}
}

ucharkbscan(void)
{
unsignedcharsccode,recode;
P3=0x0f;//發0掃描,列線輸入
if((P3&0x0f)!=0x0f)//有鍵按下
{
delay(20);//延時去抖動
if((P3&0x0f)!=0x0f)
{
sccode=0xef;//逐行掃描初值
while((sccode&0x01)!=0)
{
P3=sccode;
if((P3&0x0f)!=0x0f)
{
recode=(P3&0x0f)|0xf0;
while((P3&0x0f)!=0x0f);//等待鍵抬起
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return0;//無鍵按下,返回0
}
uchargetkey(void)
{
ucharkey;
key=kbscan();
if(key==0){keyval=0xff;return(0);}
switch(key)
{
case0x11:keyval=7;break;
case0x12:keyval=4;break;
case0x14:keyval=1;break;
case0x18:keyval=10;break;
case0x21:keyval=8;break;
case0x22:keyval=5;break;
case0x24:keyval=2;break;
case0x28:keyval=0;break;
case0x41:keyval=9;break;
case0x42:keyval=6;break;
case0x44:keyval=3;break;
case0x48:keyval=11;break;
case0x81:keyval=12;break;
case0x82:keyval=13;break;
case0x84:keyval=14;break;
case0x88:keyval=15;break;
default:keyval=0xff;break;
}
if(keyval!=0xff)return(1);
elsereturn(0);
}


main()
{
ucharnum;
lcd1602_Init();
lcd1602_Display(str,0);
while(1)
{
if(getkey())
{
if(keyval<10)keystr[num]=keyval+0x30;
elsekeystr[num]=keyval-10+'A';
lcd1602_Display(keystr,1);
num++;
num%=16;
}

}
}

Ⅱ 8位單片機PID控制PWM的演算法如何實現,C語言計算

PID控制在8位單片機中仍然有廣泛的應用,比如溫度控制,利用比例、積分、微分補償來做恆溫補償控制,當然由於有這些數學處理,用C語言相對方便一些,以下是一個具體的實例。

#include<reg51.h>

#include<intrins.h>

#include<math.h>

#include<string.h>

struct PID {

unsigned int SetPoint; // 設定目標 Desired Value

unsigned int Proportion; // 比例常數 Proportional Const

unsigned int Integral; // 積分常數 Integral Const

unsigned int Derivative; // 微分常數 Derivative Const

unsigned int LastError; // Error[-1]

unsigned int PrevError; // Error[-2]

unsigned int SumError; // Sums of Errors

};

struct PID spid; // PID Control Structure

unsigned int rout; // PID Response (Output)

unsigned int rin; // PID Feedback (Input)

sbit data1=P1^0;

sbit clk=P1^1;

sbit plus=P2^0;

sbit subs=P2^1;

sbit stop=P2^2;

sbit output=P3^4;

sbit DQ=P3^3;

unsigned char flag,flag_1=0;

unsigned char high_time,low_time,count=0;//占空比調節參數

unsigned char set_temper=35;

unsigned char temper;

unsigned char i;

unsigned char j=0;

unsigned int s;

/***********************************************************

延時子程序,延時時間以12M晶振為准,延時時間為30us×time

***********************************************************/

void delay(unsigned char time)

{

unsigned char m,n;

for(n=0;n<time;n++)

for(m=0;m<2;m++){}

}

/***********************************************************

寫一位數據子程序

***********************************************************/

void write_bit(unsigned char bitval)

{

EA=0;

DQ=0; /*拉低DQ以開始一個寫時序*/

if(bitval==1)

{

_nop_();

DQ=1; /*如要寫1,則將匯流排置高*/

}

delay(5); /*延時90us供DA18B20采樣*/

DQ=1; /*釋放DQ匯流排*/

_nop_();

_nop_();

EA=1;

}

/***********************************************************

寫一位元組數據子程序

***********************************************************/

void write_byte(unsigned char val)

{

unsigned char i;

unsigned char temp;

EA=0;

TR0=0;

for(i=0;i<8;i++) /*寫一位元組數據,一次寫一位*/

{

temp=val>>i; /*移位操作,將本次要寫的位移到最低位*/

temp=temp&1;

write_bit(temp); /*向匯流排寫該位*/

}

delay(7); /*延時120us後*/

// TR0=1;

EA=1;

}

/***********************************************************

讀一位數據子程序

***********************************************************/

unsigned char read_bit()

{

unsigned char i,value_bit;

EA=0;

DQ=0; /*拉低DQ,開始讀時序*/

_nop_();

_nop_();

DQ=1; /*釋放匯流排*/

for(i=0;i<2;i++){}

value_bit=DQ;

EA=1;

return(value_bit);

}

/***********************************************************

讀一位元組數據子程序

***********************************************************/

unsigned char read_byte()

{

unsigned char i,value=0;

EA=0;

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

{

if(read_bit()) /*讀一位元組數據,一個時序中讀一次,並作移位處理*/

value|=0x01<<i;

delay(4); /*延時80us以完成此次都時序,之後再讀下一數據*/

}

EA=1;

return(value);

}

/***********************************************************

復位子程序

***********************************************************/

unsigned char reset()

{

unsigned char presence;

EA=0;

DQ=0; /*拉低DQ匯流排開始復位*/

delay(30); /*保持低電平480us*/

DQ=1; /*釋放匯流排*/

delay(3);

presence=DQ; /*獲取應答信號*/

delay(28); /*延時以完成整個時序*/

EA=1;

return(presence); /*返回應答信號,有晶元應答返回0,無晶元則返回1*/

}

/***********************************************************

獲取溫度子程序

***********************************************************/

void get_temper()

{

unsigned char i,j;

do

{

i=reset(); /*復位*/

} while(i!=0); /*1為無反饋信號*/

i=0xcc; /*發送設備定位命令*/

write_byte(i);

i=0x44; /*發送開始轉換命令*/

write_byte(i);

delay(180); /*延時*/

do

{

i=reset(); /*復位*/

} while(i!=0);

i=0xcc; /*設備定位*/

write_byte(i);

i=0xbe; /*讀出緩沖區內容*/

write_byte(i);

j=read_byte();

i=read_byte();

i=(i<<4)&0x7f;

s=(unsigned int)(j&0x0f); //得到小數部分

s=(s*100)/16;

j=j>>4;

temper=i|j; /*獲取的溫度放在temper中*/

}

/*====================================================================================================

Initialize PID Structure

=====================================================================================================*/

void PIDInit (struct PID *pp)

{

memset ( pp,0,sizeof(struct PID)); //全部初始化為0

}

/*====================================================================================================

PID計算部分

=====================================================================================================*/

unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint )

{

unsigned int dError,Error;

Error = pp->SetPoint - NextPoint; // 偏差

pp->SumError += Error; // 積分

dError = pp->LastError - pp->PrevError; // 當前微分

pp->PrevError = pp->LastError;

pp->LastError = Error;

return (pp->Proportion * Error // 比例項

+ pp->Integral * pp->SumError // 積分項

+ pp->Derivative * dError); // 微分項

}

/***********************************************************

溫度比較處理子程序

***********************************************************/

void compare_temper()

{

unsigned char i;

if(set_temper>temper) //是否設置的溫度大於實際溫度

{

if(set_temper-temper>1) //設置的溫度比實際的溫度是否是大於1度

{

high_time=100; //如果是,則全速加熱

low_time=0;

}

else //如果是在1度范圍內,則運行PID計算

{

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

{

get_temper(); //獲取溫度

rin = s; // Read Input

rout = PIDCalc ( &spid,rin ); // Perform PID Interation

}

if (high_time<=100)

high_time=(unsigned char)(rout/800);

else

high_time=100;

low_time= (100-high_time);

}

}

else if(set_temper<=temper)

{

if(temper-set_temper>0)

{

high_time=0;

low_time=100;

}

else

{

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

{

get_temper();

rin = s; // Read Input

rout = PIDCalc ( &spid,rin ); // Perform PID Interation

}

if (high_time<100)

high_time=(unsigned char)(rout/10000);

else

high_time=0;

low_time= (100-high_time);

}

}

// else

// {}

}

/*****************************************************

T0中斷服務子程序,用於控制電平的翻轉 ,40us*100=4ms周期

******************************************************/

void serve_T0() interrupt 1 using 1

{

if(++count<=(high_time))

output=1;

else if(count<=100)

{

output=0;

}

else

count=0;

TH0=0x2f;

TL0=0xe0;

}

/*****************************************************

串列口中斷服務程序,用於上位機通訊

******************************************************/

void serve_sio() interrupt 4 using 2

{

/* EA=0;

RI=0;

i=SBUF;

if(i==2)

{

while(RI==0){}

RI=0;

set_temper=SBUF;

SBUF=0x02;

while(TI==0){}

TI=0;

}

else if(i==3)

{

TI=0;

SBUF=temper;

while(TI==0){}

TI=0;

}

EA=1; */

}

void disp_1(unsigned char disp_num1[6])

{

unsigned char n,a,m;

for(n=0;n<6;n++)

{

// k=disp_num1[n];

for(a=0;a<8;a++)

{

clk=0;

m=(disp_num1[n]&1);

disp_num1[n]=disp_num1[n]>>1;

if(m==1)

data1=1;

else

data1=0;

_nop_();

clk=1;

_nop_();

}

}

}

/*****************************************************

顯示子程序

功能:將占空比溫度轉化為單個字元,顯示占空比和測得到的溫度

******************************************************/

void display()

{

unsigned char code number[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};

unsigned char disp_num[6];

unsigned int k,k1;


k=high_time;

k=k%1000;

k1=k/100;

if(k1==0)

disp_num[0]=0;

else

disp_num[0]=0x60;

k=k%100;

disp_num[1]=number[k/10];

disp_num[2]=number[k%10];

k=temper;

k=k%100;

disp_num[3]=number[k/10];

disp_num[4]=number[k%10]+1;

disp_num[5]=number[s/10];

disp_1(disp_num);

}

/***********************************************************

主程序

***********************************************************/

void main()

{

unsigned char z;

unsigned char a,b,flag_2=1,count1=0;

unsigned char phil[]={2,0xce,0x6e,0x60,0x1c,2};

TMOD=0x21;

TH0=0x2f;

TL0=0x40;

SCON=0x50;

PCON=0x00;

TH1=0xfd;

TL1=0xfd;

PS=1;

EA=1;

EX1=0;

ET0=1;

ES=1;

TR0=1;

TR1=1;

high_time=50;

low_time=50;

PIDInit ( &spid ); // Initialize Structure

spid.Proportion = 10; // Set PID Coefficients 比例常數 Proportional Const

spid.Integral = 8; //積分常數 Integral Const

spid.Derivative =6; //微分常數 Derivative Const

spid.SetPoint = 100; // Set PID Setpoint 設定目標 Desired Value

while(1)

{

if(plus==0)

{

EA=0;

for(a=0;a<5;a++)

for(b=0;b<102;b++){}

if(plus==0)

{

set_temper++;

flag=0;

}

}

else if(subs==0)

{

for(a=0;a<5;a++)

for(b=0;a<102;b++){}

if(subs==0)

{

set_temper--;

flag=0;

}

}

else if(stop==0)

{

for(a=0;a<5;a++)

for(b=0;b<102;b++){}

if(stop==0)

{

flag=0;

break;

}

EA=1;

}

get_temper();

b=temper;

if(flag_2==1)

a=b;

if((abs(a-b))>5)

temper=a;

else

temper=b;

a=temper;

flag_2=0;

if(++count1>30)

{

display();

count1=0;

}

compare_temper();

}

TR0=0;

z=1;

while(1)

{

EA=0;

if(stop==0)

{

for(a=0;a<5;a++)

for(b=0;b<102;b++){}

if(stop==0)

disp_1(phil);

// break;

}

EA=1;

}

}

Ⅲ 單片機調試的時候出錯了 求大神

就這個程序而言,就純在這幾個錯誤,只要是有錯誤就無法編譯成功


錯誤一、按鍵檢測函數內,賦值語句被你寫成判斷語句了,如下圖這里

解決辦法是,將temp的變數聲明語句移到函數外,使其成為全局變數


錯誤三、既然程序的前端有各個函數的定義部分,那就要吧主函數放在最上端,不需要經常修改的函數放在最下端,雖然這不是強制性的,但這是一個良好的編程習慣


先改正以上錯誤吧!

閱讀全文

與單片機的code相關的資料

熱點內容
實惠雲伺服器租用多少錢 瀏覽:845
java黑客 瀏覽:78
linux命令關閉進程 瀏覽:176
安卓如何分辨是不是快充充電頭 瀏覽:835
php隨機生成姓名 瀏覽:46
便簽增加密碼 瀏覽:446
centOS系統命令 瀏覽:223
vb編譯教學 瀏覽:158
linuxtop含義 瀏覽:151
如何進入程序員賬號 瀏覽:603
和平區優勢單片機市價 瀏覽:636
為什麼28區的伺服器進不了 瀏覽:377
電氣專業演算法處理 瀏覽:161
linux拷貝文件到目錄命令 瀏覽:560
打開文本命令 瀏覽:161
python列印數組長度 瀏覽:598
安卓怎麼下載小雞模擬 瀏覽:989
linux命令中ls 瀏覽:218
lcd顯示屏編程 瀏覽:603
php手機網站開發 瀏覽:886