找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6545|回复: 4
收起左侧

语音模块SYN6288(程序各自单独运行就可以,加在一起就不行了)

[复制链接]
ID:68513 发表于 2015-1-10 21:14 | 显示全部楼层 |阅读模式
语音模块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
}
回复

使用道具 举报

ID:101840 发表于 2016-5-4 23:12 来自手机 | 显示全部楼层
楼主是怎么解决的啊我也碰到类似的问题
回复

使用道具 举报

ID:93341 发表于 2021-6-30 15:51 | 显示全部楼层
是不是,通信协议和时间间隔的问题。
回复

使用道具 举报

ID:93341 发表于 2021-6-30 16:45 | 显示全部楼层
你用了多个同名延时函数
回复

使用道具 举报

ID:844772 发表于 2021-6-30 16:49 | 显示全部楼层
wangwenhai8 发表于 2021-6-30 15:51
是不是,通信协议和时间间隔的问题。

我觉得也是,别的模块工作时,关掉串口估计就可以了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表