標(biāo)題:
自行車?yán)锍逃?jì)算單片機(jī)仿真程序遇到問題求大神幫助!
[打印本頁]
作者:
stx0923
時(shí)間:
2017-11-21 16:18
標(biāo)題:
自行車?yán)锍逃?jì)算單片機(jī)仿真程序遇到問題求大神幫助!
自行車速度已經(jīng)顯示好了,就是里程不會寫程序里沒有,求助
QQ圖片20171121161817.png
(31.08 KB, 下載次數(shù): 36)
下載附件
2017-11-21 16:18 上傳
程序在下面 求大神幫助
#include <reg51.h>
#include <intrins.h>
sbit LCM_RS=P3^0; //液晶顯示模塊
sbit LCM_RW=P3^1;//P3口來控制LCM的值
sbit LCM_EN=P3^7;
#define BUSY 0x80 //常量定義
#define DATAPORT P0 //如上
#define uchar unsigned char
#define uint unsigned int
#define L 50 //50就是L
uchar str0[16],str1[16],count;// 數(shù)組
uint speed; //速度顯示
unsigned long time;//
void ddelay(uint);
void lcd_wait(void);
void display();
void initLCM();
void WriteCommandLCM(uchar WCLCM,uchar BusyC);
void STR();
void account();
/*********延時(shí)K*1ms,12.000mhz**********/
void int0_isr(void) interrupt 0 /*遙控使用外部中斷0,接P3.2口*/
{
unsigned int temp;
time=count;
TR0=0;
temp=TH0;
temp=((temp << 8) | TL0);
TH0=0x3c;
TL0=0xaf;
count=0;
TR0=1;
time=time*50000+temp;
}
void time0_isr(void) interrupt 1 /*遙控使用定時(shí)計(jì)數(shù)器1 */
{
TH0 =0x3c;
TL0 =0xaf;
count++;
}
void main(void)
{
TMOD=0x01; /*TMOD T0選用方式1(16位定時(shí)) */
IP|=0x01; /*INT0 中斷優(yōu)先*/
TCON|=0x11; /*TCON EX0下降沿觸發(fā),啟動T0*/
IE|=0x83;
TH0=0x3c;
TL0=0xaf;
initLCM();
WriteCommandLCM(0x01,1); //清顯示屏
for(;;)
{
account();
display();
}
}
void account()
{
unsigned long a;
if (time!=0)
{
a=L*360000000/time;
}
speed=a;
}
void STR() //速度LCD設(shè)計(jì)
{
str0[0]='S';
str0[1]='p';
str0[2]='e';
str0[3]='e';
str0[4]='d';
str0[5]=' ';
str0[6]=(speed%100000)/10000+0x30;
str0[7]=(speed%10000)/1000+0x30;
str0[8]=(speed%1000)/100+0x30;
str0[9]='.';
str0[10]=(speed%100)/10+0x30;
str0[11]=speed%10+0x30;
str0[12]='k';
str0[13]='m';
str0[14]='/';
str0[15]='h';
}
void ddelay(uint k)
{
uint i,j;
for(i=0;i<k;i++)
{
for(j=0;j<60;j++)
{;}
}
}
/**********寫指令到lcd子函數(shù)************/
void WriteCommandLCM(uchar WCLCM,uchar BusyC)
{
if(BusyC)lcd_wait();
DATAPORT=WCLCM;
LCM_RS=0; /* 選中指令寄存器*/
LCM_RW=0; // 寫模式
LCM_EN=1;
_nop_();
_nop_();
_nop_();
LCM_EN=0;
}
/**********寫數(shù)據(jù)到LCD子函數(shù)************/
void WriteDataLCM(uchar WDLCM)
{
lcd_wait( ); //檢測忙信號
DATAPORT=WDLCM;
LCM_RS=1; /* 選中數(shù)據(jù)寄存器 */
LCM_RW=0; // 寫模式
LCM_EN=1;
_nop_();
_nop_();
_nop_();
LCM_EN=0;
}
/***********lcd內(nèi)部等待函數(shù)*************/
void lcd_wait(void)
{
DATAPORT=0xff; //讀LCD前若單片機(jī)輸出低電平,而讀出LCD為高電平,則沖突,Proteus就會顯示邏輯黃色
LCM_EN=1;
LCM_RS=0;
LCM_RW=1;
_nop_();
_nop_();
_nop_();
while(DATAPORT&BUSY)
{ LCM_EN=0;
_nop_();
_nop_();
LCM_EN=1;
_nop_();
_nop_();
}
LCM_EN=0;
}
/**********LCD初始化子函數(shù)***********/
void initLCM( )
{
DATAPORT=0;
ddelay(15);
WriteCommandLCM(0x38,0); //三次顯示模式設(shè)置,不檢測忙信號
ddelay(5);
WriteCommandLCM(0x38,0);
ddelay(5);
WriteCommandLCM(0x38,0);
ddelay(5);
WriteCommandLCM(0x38,1); //8bit數(shù)據(jù)傳送,2行顯示,5*7字形,檢測忙信號
WriteCommandLCM(0x08,1); //關(guān)閉顯示檢測忙信號
WriteCommandLCM(0x01,1); //清屏檢測忙信號
WriteCommandLCM(0x06,1); //顯示光標(biāo)右移設(shè)置,檢測忙信號
WriteCommandLCM(0x0c,1); //顯示屏打開,光標(biāo)不顯示,不閃爍,檢測忙信號
}
/****顯示指定坐標(biāo)的一個(gè)字符子函數(shù)****/
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40; //若y為1(顯示第二行),地址碼+0X40
X|=0x80; //指令碼為地址碼+0X80
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
/*******顯示指定坐標(biāo)的一串字符子函數(shù)*****/
void DisplayListChar(uchar X,uchar Y,uchar *DData)
{
uchar ListLength=0;
Y&=0x01;
X&=0x0f;
while(X<16)
{
DisplayOneChar(X,Y,DData[ListLength]);
ListLength++;
X++;
}
}
void display()
{
STR();
DisplayListChar(0,0,str0);
DisplayListChar(0,1,str1);
}
復(fù)制代碼
作者:
stx0923
時(shí)間:
2017-11-21 16:20
剛剛程序有亂碼我重新發(fā)一個(gè)
作者:
HC6800-ES-V2.0
時(shí)間:
2017-11-22 09:26
給你兩個(gè)建議:
第一:你的速度已經(jīng)有了,那么,里程就太太......太簡單了,找時(shí)間啊,S=Vt,初中就知道的。
這個(gè)時(shí)間可不能用定時(shí)器啊,定時(shí)器的時(shí)間太短了,用1302之類的時(shí)間芯片好了——確定計(jì)時(shí)開始時(shí)刻,確定掃描時(shí)間,即多久顯示更新一次里程,就可以算里程了。
第二:如果上面方法的程序太難(我想想都覺得麻煩),那就用簡單方法了。在自行車輪子了裝一個(gè)光電傳感器,輪子轉(zhuǎn)動一周,計(jì)數(shù)一次,這個(gè)里程就是輪子周長,確定轉(zhuǎn)動幾周顯示更新一次,就可以有里程數(shù)據(jù)了?梢杂猛獠恐袛嘟邮者@個(gè)光電傳感器的數(shù)據(jù),這樣可以計(jì)數(shù)的最大值,可以由程序設(shè)定,而不是定時(shí)/計(jì)數(shù)器,定時(shí)器的最大值有限制。
作者:
Aegean
時(shí)間:
2017-11-22 20:50
我也在做這個(gè),才開始,請教一下霍爾元件怎么產(chǎn)生高低電平的?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1