語音模塊SYN6288的51程序和12864+溫度傳感器的程序各自單獨運行就可以,加在一起就不行了,求大神求解,急用
//main.c
#include<reg52.h>
#include "12864.h"
#include"temp.h"
#include "speaker.h"
void tempDisplay(int temp) //lcd顯示
{
unsigned char datas1,datas2,datas3; //定義數組
float tp;
if(temp>0) //當溫度值為負數
{
tp=temp;
temp=tp*0.0625*100+0.5;
}
datas1= temp % 10000 / 1000;
datas2= temp % 1000 / 100;
datas3= temp % 100 / 10;
PosLCD(2,6);
WrDatLCD(' ');
WrDatLCD('0'+datas1); //十位
WrDatLCD('0'+datas2); //個位
WrDatLCD('.'); //顯示 ‘.’
WrDatLCD('0'+datas3); //顯示小數點
WrDatLCD('C');
}
void main()
{
LCD_Init();
SpeakerInit();
while(1)
{
tempDisplay(Ds18b20ReadTemp());
Speaker("哈哈");
delayMs(2000);
}
}
//12864.h
#ifndef _12864_H_
#define _12864_H_
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
sbit RS = P2^5; //數據/命令選擇端(H/L)
sbit RW = P2^6; //數/寫選擇端(H/L)
sbit EN = P2^7; //使能信號
void DelayMS(uint ValMS);
void DectectBusyBit(void);
void WrComLCD(uchar ComVal);
void WrDatLCD(uchar DatVal);
void PosLCD(uchar X,uchar Y);
void LCD_Init();
#endif
//12864.c
#include<reg52.h>
#include "12864.h"
/* ***************************************************** */
// 函數名稱:DelayMS()
// 函數功能:延時毫秒數(ValMS)
// 入口參數:無
// 出口參數:無
/* ***************************************************** */
void DelayMS(uint ValMS)
{
uint uiVal,ujVal;
for(uiVal = 0; uiVal < ValMS; uiVal++)
for(ujVal = 0; ujVal < 121; ujVal++);
}
/* ***************************************************** */
// 函數名稱:DectectBusyBit()
// 函數功能:檢測狀態標志位(判斷是忙/閑)
// 入口參數:無
// 出口參數:無
/* ***************************************************** */
void DectectBusyBit()
{
P0 = 0xff; // 讀狀態值時,先賦高電平
RS = 0;
RW = 1;
EN = 1;
DelayMS(1);
while(P0 & 0x80); // 若LCD忙,停止到這里,否則走起
EN = 0; // 之后將EN初始化為低電平
}
/* ***************************************************** */
// 函數名稱:WrComLCD()
// 函數功能:LCD寫指令
// 入口參數:指令(ComVal)
// 出口參數:無
/* ***************************************************** */
void WrComLCD(uchar ComVal)
{
DectectBusyBit();
RS = 0;
RW = 0;
EN = 1;
P0 = ComVal;
DelayMS(1);
EN = 0;
}
/* ***************************************************** */
// 函數名稱:WrDatLCD()
// 函數功能:LCD寫數據
// 入口參數:數據(DatVal)
// 出口參數:無
/* ***************************************************** */
void WrDatLCD(uchar DatVal)
{
DectectBusyBit();
RS = 1;
RW = 0;
EN = 1;
P0 = DatVal;
DelayMS(1);
EN = 0;
}
/* ***************************************************** */
// 函數名稱:PosLCD()
// 函數功能:輸入定位
// 入口參數:無
// 出口參數:無
/* ***************************************************** */
void PosLCD(uchar X,uchar Y)
{
uchar ucPos;
if(X == 1)
{ X = 0x80; } //第一行
else if(X == 2)
{ X = 0x90; } //第二行
else if(X == 3)
{ X = 0x88; } //第三行
else if(X == 4)
{ X = 0x98; } //第四行
ucPos = X + Y-1; //計算地址
WrComLCD(ucPos); //顯示地址
}
/* ***************************************************** */
// 函數名稱:LCD_Init()
// 函數功能:LCD初始化
// 入口參數:無
// 出口參數:無
/* ***************************************************** */
void LCD_Init()
{
WrComLCD(0x30); // 8位數據端口、選擇基本指令
DelayMS(10);
WrComLCD(0x01); // 顯示清屏
DelayMS(10);
WrComLCD(0x0C); // 顯示設定:整體顯示、游標關、不反白
DelayMS(10);
}
//temp.h
#ifndef __TEMP_H_
#define __TEMP_H_
//---重定義關鍵詞---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
//--定義使用的IO口--//
sbit DSPORT=P3^2;
//--聲明全局函數--//
void Delay1ms(uint y);
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar com);
uchar Ds18b20ReadByte();
void Ds18b20ChangTemp();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
#endif
//temp.c
#include<reg52.h>
#include"temp.h"
/*******************************************************************************
* 函 數 名 : Delay1ms
* 函數功能 : 延時函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
/*******************************************************************************
* 函 數 名 : Ds18b20Init
* 函數功能 : 初始化
* 輸 入 : 無
* 輸 出 : 初始化成功返回1,失敗返回0
*******************************************************************************/
uchar Ds18b20Init()
{
uchar i;
DSPORT = 0; //將總線拉低480us~960us
i = 70;
while(i--);//延時642us
DSPORT = 1; //然后拉高總線,如果DS18B20做出反應會將在15us~60us后總線拉低
i = 0;
while(DSPORT) //等待DS18B20拉低總線
{
i++;
if(i>5)//等待>5MS
{
return 0;//初始化失敗
}
Delay1ms(1);
}
return 1;//初始化成功
}
/*******************************************************************************
* 函 數 名 : Ds18b20WriteByte
* 函數功能 : 向18B20寫入一個字節
* 輸 入 : com
* 輸 出 : 無
*******************************************************************************/
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0; //每寫入一位數據之前先把總線拉低1us
i++;
DSPORT = dat & 0x01; //然后寫入一個數據,從最低位開始
i=6;
while(i--); //延時68us,持續時間最少60us
DSPORT = 1; //然后釋放總線,至少1us給總線恢復時間才能接著寫入第二個數值
dat >>= 1;
}
}
/*******************************************************************************
* 函 數 名 : Ds18b20ReadByte
* 函數功能 : 讀取一個字節
* 輸 入 : com
* 輸 出 : 無
*******************************************************************************/
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DSPORT = 0;//先將總線拉低1us
i++;
DSPORT = 1;//然后釋放總線
i++;
i++;//延時6us等待數據穩定
bi = DSPORT; //讀取數據,從最低位開始讀取
/*將byte左移一位,然后與上右移7位后的bi,注意移動之后移掉那位補0。*/
byte = (byte >> 1) | (bi << 7);
i = 4; //讀取完之后等待48us再接著讀取下一個數
while(i--);
}
return byte;
}
/*******************************************************************************
* 函 數 名 : Ds18b20ChangTemp
* 函數功能 : 讓18b20開始轉換溫度
* 輸 入 : com
* 輸 出 : 無
*******************************************************************************/
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過ROM操作命令
Ds18b20WriteByte(0x44); //溫度轉換命令
// Delay1ms(100); //等待轉換成功,而如果你是一直刷著的話,就不用這個延時了
}
/*******************************************************************************
* 函 數 名 : Ds18b20ReadTempCom
* 函數功能 : 發送讀取溫度命令
* 輸 入 : com
* 輸 出 : 無
*******************************************************************************/
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過ROM操作命令
Ds18b20WriteByte(0xbe); //發送讀取溫度命令
}
/*******************************************************************************
* 函 數 名 : Ds18b20ReadTemp
* 函數功能 : 讀取溫度
* 輸 入 : com
* 輸 出 : 無
*******************************************************************************/
int Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp(); //先寫入轉換命令
Ds18b20ReadTempCom(); //然后等待轉換完后發送讀取溫度命令
tml = Ds18b20ReadByte(); //讀取溫度值共16位,先讀低字節
tmh = Ds18b20ReadByte(); //再讀高字節
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
//speaker.h
#ifndef _SPEAKER_H_
#define _SPEAKER_H_
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
void SpeakerInit(); //語音初始化程序
char Speaker(char * pString); //語音播放程序
void delayMs(uint xms);
#endif
//speaker.c
#include <reg52.h>
#include <string.h>
#include <math.h>
#include "speaker.h"
void delayMs(uint xms)
{
uint i,j;
for(i=0;i<xms;i++)
for (j=0;j<123;j++);
}
//語音模塊初始化
void SpeakerInit()
{
/**************串口的初始化*****************/
TL1=0XFA; //在11.0592MHZ下,設置波特率9600bps,工作方式2
TH1=0XFA;
TMOD=0X20;
SCON=0X50; //串口工作方式1,允許接收
PCON=0X80;
EA=0;
REN=1;
TI=0; //發生中斷標志位置零
RI=0; //接收中斷標志位置零
TR1=1; //定時器1用做波特率發生
}
//語音播報程序
char Speaker(char * pString)
{
uchar headOfFrame[5];
uchar length; //定義字符串長度
uchar ecc = 0; //定義校驗字節
uint i=0;
if (pString == NULL) //空字符串
return -1;
/*****************發送過程**********************/
headOfFrame[0]=0XFD; //構造幀頭FD
headOfFrame[1]=0X00; //構造數據區長度的高字節
length = strlen(pString); //需要發送文本的長度
headOfFrame[2]=length+3;//構造數據區長度的低字節
headOfFrame[3]=0X01; //構造命令字:合成播放命令
headOfFrame[4]=0X00; //構造命令參數:編碼格式為GB2312
for(i=0;i<5;i++) //依次發送構造好的5個幀頭字節
{
ecc=ecc^(headOfFrame[ i]); //對發送的字節進行異或校驗
SBUF=headOfFrame[ i];
while (TI==0){;} //等待發送中斷標志置位
TI=0; //發送中斷標志位清零
}
for(i=0;i<length;i++) //依次發送待合成的文本數據
{
ecc=ecc^(*pString);
SBUF = (*pString);
pString ++;
while(TI==0){;}
TI=0;
}
SBUF=ecc;
while(TI==0){;}
TI=0;
return 0; //成功返回0
}
|