21ic问答首页 - 我这个程序用单片机数码管显示相关内容 反映很慢
我这个程序用单片机数码管显示相关内容 反映很慢
kbkbk2022-03-06
#include "reg52.h"
#include "HX711.h"
#include "uart.h"
#include "LCD1602.h"
#include "temp.h"
typedef unsigned char u8;
typedef unsigned int u16;
u16 KEY_NUM=0;
unsigned long HX711_Buffer=0;
unsigned long Weight_Maopi=0;Weight_Shiwu=0;
bit Flag_OK=0;
sbit key_stop = P3^0;
sbit key_star = P3^1;
sbit key_fen = P3^2;
sbit key_shi = P3^3;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 DisplayDataTemp[8];
u8 DisplayWeight[8];
u8 DisplayData[8];
u8 code smgduan[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,0x6f};
u8 miao=0;
u8 fen=0;
u8 shi=0;
u8 miao_L,miao_H,fen_L,fen_H,shi_L,shi_H;
u8 State=1;
u16 counter=0;
void delay(u16 x){ //ÑÓʱº¯Êý
while(x--)
;
}//delay
void Delay_ms(u16 n)
{
u16 i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
void key_delay(int xms){ //¡ã¡ä?¨¹?¨®¨º¡Ào¡¥¨ºy x ms
u16 i, j;
for(i=0; i<xms; ++i)
for(j=0; j<110; ++j)
;
}//key_delay
void T0_Init(){ //¶¨Ê±Æ÷³õʼ»¯
TMOD = 0x01;
TH0 = 0x3c;
TL0 = 0xb0;
EA = 1; //¿ª×ÜÖжÏ
ET0 = 1;
TR0 = 1;
}//T0_Init
void dataprosTemp(int temp)
{
float tp;
if(temp< 0) //µ±Î¶ÈֵΪ¸ºÊý
{
DisplayDataTemp[0] = 0x40; // -
//ÒòΪ¶ÁÈ¡µÄζÈÊÇʵ¼ÊζȵIJ¹Â룬ËùÒÔ¼õ1£¬ÔÙÈ¡·´Çó³öÔÂë
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//ÁôÁ½¸öСÊýµã¾Í*100£¬+0.5ÊÇËÄÉáÎåÈ룬ÒòΪCÓïÑÔ¸¡µãÊýת»»ÎªÕûÐ͵Äʱºò°ÑСÊýµã
//ºóÃæµÄÊý×Ô¶¯È¥µô£¬²»¹ÜÊÇ·ñ´óÓÚ0.5£¬¶ø+0.5Ö®ºó´óÓÚ0.5µÄ¾ÍÊǽø1ÁË£¬Ð¡ÓÚ0.5µÄ¾Í
//Ëã¼ÓÉÏ0.5£¬»¹ÊÇÔÚСÊýµãºóÃæ¡£
}
else
{
DisplayDataTemp[0] = 0x00;
tp=temp;//ÒòΪÊý¾Ý´¦ÀíÓÐСÊýµãËùÒÔ½«Î¶ȸ³¸øÒ»¸ö¸¡µãÐͱäÁ¿
//Èç¹ûζÈÊÇÕýµÄÄÇô£¬ÄÇôÕýÊýµÄÔÂë¾ÍÊDz¹ÂëËü±¾Éí
temp=tp*0.0625*100+0.5;
//ÁôÁ½¸öСÊýµã¾Í*100£¬+0.5ÊÇËÄÉáÎåÈ룬ÒòΪCÓïÑÔ¸¡µãÊýת»»ÎªÕûÐ͵Äʱºò°ÑСÊýµã
//ºóÃæµÄÊý×Ô¶¯È¥µô£¬²»¹ÜÊÇ·ñ´óÓÚ0.5£¬¶ø+0.5Ö®ºó´óÓÚ0.5µÄ¾ÍÊǽø1ÁË£¬Ð¡ÓÚ0.5µÄ¾Í
//Ëã¼ÓÉÏ0.5£¬»¹ÊÇÔÚСÊýµãºóÃæ¡£
}
DisplayDataTemp[1] = smgduan[temp / 10000];
DisplayDataTemp[2] = smgduan[temp % 10000 / 1000];
DisplayDataTemp[3] = smgduan[temp % 1000 / 100] | 0x80;
DisplayDataTemp[4] = smgduan[temp % 100 / 10];
DisplayDataTemp[5] = smgduan[temp % 10];
}
void datapros()
{
DisplayData[0] = smgduan[miao_L];
DisplayData[1] = smgduan[miao_H];
DisplayData[2] = 0x40;
DisplayData[3] = smgduan[fen_L];
DisplayData[4] = smgduan[fen_H];
DisplayData[5] = 0x40;
DisplayData[6] = smgduan[shi_L];
DisplayData[7] = smgduan[shi_H];
}
void DigDisplayTemp()
{
u8 j;
for(j=0;j<6;j++)
{
switch(j) //λѡ£¬Ñ¡ÔñµãÁÁµÄÊýÂë¹Ü£¬
{
case(0):
LSA=0;LSB=0;LSC=0; break;//ÏÔʾµÚ0λ
case(1):
LSA=1;LSB=0;LSC=0; break;//ÏÔʾµÚ1λ
case(2):
LSA=0;LSB=1;LSC=0; break;//ÏÔʾµÚ2λ
case(3):
LSA=1;LSB=1;LSC=0; break;//ÏÔʾµÚ3λ
case(4):
LSA=0;LSB=0;LSC=1; break;//ÏÔʾµÚ4λ
case(5):
LSA=1;LSB=0;LSC=1; break;//ÏÔʾµÚ5λ
}
P0=DisplayDataTemp[5-j];//·¢ËÍÊý¾Ý
delay(100); //¼ä¸ôÒ»¶Îʱ¼äɨÃè
P0=0x00;//ÏûÒþ
}
}
void DigDisplay()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i)
{
case(0):
LSA=0;LSB=0;LSC=0; break;
case(1):
LSA=1;LSB=0;LSC=0; break;
case(2):
LSA=0;LSB=1;LSC=0; break;
case(3):
LSA=1;LSB=1;LSC=0; break;
case(4):
LSA=0;LSB=0;LSC=1; break;
case(5):
LSA=1;LSB=0;LSC=1; break;
case(6):
LSA=0;LSB=1;LSC=1; break;
case(7):
LSA=1;LSB=1;LSC=1; break;
}
P0=DisplayData[i];
delay(50);
P0=0x00;
}
}
void keyscan(){ //°´¼üº¯Êý
if(key_stop == 0){
key_delay(20);
if(key_stop == 0){
EA = ~EA; //¹Ø×ÜÖжϣ¬¼´Í£Ö¹
while(!key_stop);
}
}//key_stop
if(key_star == 0){
key_delay(20);
if(key_star == 0){
State = State+1;
if(State==4)
{
State=1;
} //¿ª×ÜÖжÏ
while(!key_star);
}
}//key_star
if(key_fen == 0){
key_delay(20);
if(key_fen == 0){
fen++;
while(!key_fen);
}
}//key_fen++
if(key_shi == 0){
key_delay(20);
if(key_shi == 0){
shi++;
while(!key_shi);
}
}//key_shi++
}
void dataweight()
{
DisplayWeight[1]=smgduan[Weight_Shiwu/1000];
DisplayWeight[2]=smgduan[Weight_Shiwu%1000/100];
DisplayWeight[3]=smgduan[Weight_Shiwu%100/10];
DisplayWeight[4]=smgduan[Weight_Shiwu%10];
DisplayWeight[5]=0x6f;
}
void DigDisplayWeight()
{
u8 j;
for(j=0;j<6;j++)
{
switch(j) //λѡ£¬Ñ¡ÔñµãÁÁµÄÊýÂë¹Ü£¬
{
case(0):
LSA=0;LSB=0;LSC=0; break;//ÏÔʾµÚ0λ
case(1):
LSA=1;LSB=0;LSC=0; break;//ÏÔʾµÚ1λ
case(2):
LSA=0;LSB=1;LSC=0; break;//ÏÔʾµÚ2λ
case(3):
LSA=1;LSB=1;LSC=0; break;//ÏÔʾµÚ3λ
case(4):
LSA=0;LSB=0;LSC=1; break;//ÏÔʾµÚ4λ
case(5):
LSA=1;LSB=0;LSC=1; break;//ÏÔʾµÚ5λ
}
P0=DisplayWeight[5-j];//·¢ËÍÊý¾Ý
delay(50); //¼ä¸ôÒ»¶Îʱ¼äɨÃè
P0=0x00;//ÏûÒþ
}
}
void Get_Weight()
{
HX711_Buffer=HX711_Read();
HX711_Buffer=HX711_Buffer/100;
if(HX711_Buffer>Weight_Maopi)
{
Weight_Shiwu=HX711_Buffer;
Weight_Shiwu=Weight_Shiwu - Weight_Maopi;
Weight_Shiwu=(unsigned int)((float)Weight_Shiwu/4.40+0.05);
}
}
void Get_Maopi()
{
HX711_Buffer=HX711_Read();
Weight_Maopi=HX711_Buffer/100;
}
void main()
{
char k=10;
T0_Init();
Send_Word("Welcome to use!\n");
Send_Word("Made by CZQ Group!\n");
Delay_ms(800);//ÑÓʱ0.8Ãë
Get_Maopi();
Delay_ms(1000);//ÑÓʱ1s
// LcdWriteCom(0x80+0x40+6);
// LcdWriteData(0x5c); ½ðÇ®·ûºÅ
// LcdWriteCom(0x80+0x40+15);
// LcdWriteData(0x5c);
Get_Maopi();
while(1)
{
keyscan();
dataprosTemp(Ds18b20ReadTemp());
k--;
if( Flag_OK==0)
{
if(k==0)
{
Get_Weight();
k--;
}
if(k<0)
{
k=10;
}
Send_ASCII(Weight_Shiwu/1000+0x30);
Send_ASCII(".");
Send_ASCII(Weight_Shiwu%1000/100+0x30);
Send_ASCII(Weight_Shiwu%100/10+0x30);
Send_ASCII(Weight_Shiwu%10+0x30);
Send_Word(" kg \n");
}
if(State==3)
{
dataweight();
DigDisplayWeight();
}
if(State==1)
{
DigDisplay();
datapros();
}
if(State==2)
{
DigDisplayTemp();
}
}
}
void timer0_Init() interrupt 1{ //ÖжϺ¯Êý
counter++;
if(counter == 20){
counter = 0;
miao++; //i = 100
if(miao == 60){
miao = 0;
fen++;
if(fen == 60){
fen = 0;
shi++;
if(shi == 24){
shi = 0;
fen = 0;
miao = 0;
}
}
}
miao_L = miao%10;
miao_H = miao/10;
fen_L = fen%10;
fen_H = fen/10;
shi_L = shi%10;
shi_H = shi/10;
}
}//timer0_Init
#include "HX711.h"
#include "uart.h"
#include "LCD1602.h"
#include "temp.h"
typedef unsigned char u8;
typedef unsigned int u16;
u16 KEY_NUM=0;
unsigned long HX711_Buffer=0;
unsigned long Weight_Maopi=0;Weight_Shiwu=0;
bit Flag_OK=0;
sbit key_stop = P3^0;
sbit key_star = P3^1;
sbit key_fen = P3^2;
sbit key_shi = P3^3;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 DisplayDataTemp[8];
u8 DisplayWeight[8];
u8 DisplayData[8];
u8 code smgduan[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,0x6f};
u8 miao=0;
u8 fen=0;
u8 shi=0;
u8 miao_L,miao_H,fen_L,fen_H,shi_L,shi_H;
u8 State=1;
u16 counter=0;
void delay(u16 x){ //ÑÓʱº¯Êý
while(x--)
;
}//delay
void Delay_ms(u16 n)
{
u16 i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
void key_delay(int xms){ //¡ã¡ä?¨¹?¨®¨º¡Ào¡¥¨ºy x ms
u16 i, j;
for(i=0; i<xms; ++i)
for(j=0; j<110; ++j)
;
}//key_delay
void T0_Init(){ //¶¨Ê±Æ÷³õʼ»¯
TMOD = 0x01;
TH0 = 0x3c;
TL0 = 0xb0;
EA = 1; //¿ª×ÜÖжÏ
ET0 = 1;
TR0 = 1;
}//T0_Init
void dataprosTemp(int temp)
{
float tp;
if(temp< 0) //µ±Î¶ÈֵΪ¸ºÊý
{
DisplayDataTemp[0] = 0x40; // -
//ÒòΪ¶ÁÈ¡µÄζÈÊÇʵ¼ÊζȵIJ¹Â룬ËùÒÔ¼õ1£¬ÔÙÈ¡·´Çó³öÔÂë
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//ÁôÁ½¸öСÊýµã¾Í*100£¬+0.5ÊÇËÄÉáÎåÈ룬ÒòΪCÓïÑÔ¸¡µãÊýת»»ÎªÕûÐ͵Äʱºò°ÑСÊýµã
//ºóÃæµÄÊý×Ô¶¯È¥µô£¬²»¹ÜÊÇ·ñ´óÓÚ0.5£¬¶ø+0.5Ö®ºó´óÓÚ0.5µÄ¾ÍÊǽø1ÁË£¬Ð¡ÓÚ0.5µÄ¾Í
//Ëã¼ÓÉÏ0.5£¬»¹ÊÇÔÚСÊýµãºóÃæ¡£
}
else
{
DisplayDataTemp[0] = 0x00;
tp=temp;//ÒòΪÊý¾Ý´¦ÀíÓÐСÊýµãËùÒÔ½«Î¶ȸ³¸øÒ»¸ö¸¡µãÐͱäÁ¿
//Èç¹ûζÈÊÇÕýµÄÄÇô£¬ÄÇôÕýÊýµÄÔÂë¾ÍÊDz¹ÂëËü±¾Éí
temp=tp*0.0625*100+0.5;
//ÁôÁ½¸öСÊýµã¾Í*100£¬+0.5ÊÇËÄÉáÎåÈ룬ÒòΪCÓïÑÔ¸¡µãÊýת»»ÎªÕûÐ͵Äʱºò°ÑСÊýµã
//ºóÃæµÄÊý×Ô¶¯È¥µô£¬²»¹ÜÊÇ·ñ´óÓÚ0.5£¬¶ø+0.5Ö®ºó´óÓÚ0.5µÄ¾ÍÊǽø1ÁË£¬Ð¡ÓÚ0.5µÄ¾Í
//Ëã¼ÓÉÏ0.5£¬»¹ÊÇÔÚСÊýµãºóÃæ¡£
}
DisplayDataTemp[1] = smgduan[temp / 10000];
DisplayDataTemp[2] = smgduan[temp % 10000 / 1000];
DisplayDataTemp[3] = smgduan[temp % 1000 / 100] | 0x80;
DisplayDataTemp[4] = smgduan[temp % 100 / 10];
DisplayDataTemp[5] = smgduan[temp % 10];
}
void datapros()
{
DisplayData[0] = smgduan[miao_L];
DisplayData[1] = smgduan[miao_H];
DisplayData[2] = 0x40;
DisplayData[3] = smgduan[fen_L];
DisplayData[4] = smgduan[fen_H];
DisplayData[5] = 0x40;
DisplayData[6] = smgduan[shi_L];
DisplayData[7] = smgduan[shi_H];
}
void DigDisplayTemp()
{
u8 j;
for(j=0;j<6;j++)
{
switch(j) //λѡ£¬Ñ¡ÔñµãÁÁµÄÊýÂë¹Ü£¬
{
case(0):
LSA=0;LSB=0;LSC=0; break;//ÏÔʾµÚ0λ
case(1):
LSA=1;LSB=0;LSC=0; break;//ÏÔʾµÚ1λ
case(2):
LSA=0;LSB=1;LSC=0; break;//ÏÔʾµÚ2λ
case(3):
LSA=1;LSB=1;LSC=0; break;//ÏÔʾµÚ3λ
case(4):
LSA=0;LSB=0;LSC=1; break;//ÏÔʾµÚ4λ
case(5):
LSA=1;LSB=0;LSC=1; break;//ÏÔʾµÚ5λ
}
P0=DisplayDataTemp[5-j];//·¢ËÍÊý¾Ý
delay(100); //¼ä¸ôÒ»¶Îʱ¼äɨÃè
P0=0x00;//ÏûÒþ
}
}
void DigDisplay()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i)
{
case(0):
LSA=0;LSB=0;LSC=0; break;
case(1):
LSA=1;LSB=0;LSC=0; break;
case(2):
LSA=0;LSB=1;LSC=0; break;
case(3):
LSA=1;LSB=1;LSC=0; break;
case(4):
LSA=0;LSB=0;LSC=1; break;
case(5):
LSA=1;LSB=0;LSC=1; break;
case(6):
LSA=0;LSB=1;LSC=1; break;
case(7):
LSA=1;LSB=1;LSC=1; break;
}
P0=DisplayData[i];
delay(50);
P0=0x00;
}
}
void keyscan(){ //°´¼üº¯Êý
if(key_stop == 0){
key_delay(20);
if(key_stop == 0){
EA = ~EA; //¹Ø×ÜÖжϣ¬¼´Í£Ö¹
while(!key_stop);
}
}//key_stop
if(key_star == 0){
key_delay(20);
if(key_star == 0){
State = State+1;
if(State==4)
{
State=1;
} //¿ª×ÜÖжÏ
while(!key_star);
}
}//key_star
if(key_fen == 0){
key_delay(20);
if(key_fen == 0){
fen++;
while(!key_fen);
}
}//key_fen++
if(key_shi == 0){
key_delay(20);
if(key_shi == 0){
shi++;
while(!key_shi);
}
}//key_shi++
}
void dataweight()
{
DisplayWeight[1]=smgduan[Weight_Shiwu/1000];
DisplayWeight[2]=smgduan[Weight_Shiwu%1000/100];
DisplayWeight[3]=smgduan[Weight_Shiwu%100/10];
DisplayWeight[4]=smgduan[Weight_Shiwu%10];
DisplayWeight[5]=0x6f;
}
void DigDisplayWeight()
{
u8 j;
for(j=0;j<6;j++)
{
switch(j) //λѡ£¬Ñ¡ÔñµãÁÁµÄÊýÂë¹Ü£¬
{
case(0):
LSA=0;LSB=0;LSC=0; break;//ÏÔʾµÚ0λ
case(1):
LSA=1;LSB=0;LSC=0; break;//ÏÔʾµÚ1λ
case(2):
LSA=0;LSB=1;LSC=0; break;//ÏÔʾµÚ2λ
case(3):
LSA=1;LSB=1;LSC=0; break;//ÏÔʾµÚ3λ
case(4):
LSA=0;LSB=0;LSC=1; break;//ÏÔʾµÚ4λ
case(5):
LSA=1;LSB=0;LSC=1; break;//ÏÔʾµÚ5λ
}
P0=DisplayWeight[5-j];//·¢ËÍÊý¾Ý
delay(50); //¼ä¸ôÒ»¶Îʱ¼äɨÃè
P0=0x00;//ÏûÒþ
}
}
void Get_Weight()
{
HX711_Buffer=HX711_Read();
HX711_Buffer=HX711_Buffer/100;
if(HX711_Buffer>Weight_Maopi)
{
Weight_Shiwu=HX711_Buffer;
Weight_Shiwu=Weight_Shiwu - Weight_Maopi;
Weight_Shiwu=(unsigned int)((float)Weight_Shiwu/4.40+0.05);
}
}
void Get_Maopi()
{
HX711_Buffer=HX711_Read();
Weight_Maopi=HX711_Buffer/100;
}
void main()
{
char k=10;
T0_Init();
Send_Word("Welcome to use!\n");
Send_Word("Made by CZQ Group!\n");
Delay_ms(800);//ÑÓʱ0.8Ãë
Get_Maopi();
Delay_ms(1000);//ÑÓʱ1s
// LcdWriteCom(0x80+0x40+6);
// LcdWriteData(0x5c); ½ðÇ®·ûºÅ
// LcdWriteCom(0x80+0x40+15);
// LcdWriteData(0x5c);
Get_Maopi();
while(1)
{
keyscan();
dataprosTemp(Ds18b20ReadTemp());
k--;
if( Flag_OK==0)
{
if(k==0)
{
Get_Weight();
k--;
}
if(k<0)
{
k=10;
}
Send_ASCII(Weight_Shiwu/1000+0x30);
Send_ASCII(".");
Send_ASCII(Weight_Shiwu%1000/100+0x30);
Send_ASCII(Weight_Shiwu%100/10+0x30);
Send_ASCII(Weight_Shiwu%10+0x30);
Send_Word(" kg \n");
}
if(State==3)
{
dataweight();
DigDisplayWeight();
}
if(State==1)
{
DigDisplay();
datapros();
}
if(State==2)
{
DigDisplayTemp();
}
}
}
void timer0_Init() interrupt 1{ //ÖжϺ¯Êý
counter++;
if(counter == 20){
counter = 0;
miao++; //i = 100
if(miao == 60){
miao = 0;
fen++;
if(fen == 60){
fen = 0;
shi++;
if(shi == 24){
shi = 0;
fen = 0;
miao = 0;
}
}
}
miao_L = miao%10;
miao_H = miao/10;
fen_L = fen%10;
fen_H = fen/10;
shi_L = shi%10;
shi_H = shi/10;
}
}//timer0_Init
赞0
评论
2022-03-07
赞0
评论
2022-03-07
赞0
速度慢,肯定是架构不对,软件死延时等待。
评论
2022-03-06
您需要登录后才可以回复 登录 | 注册