|
本人是一名業(yè)余無線電愛好者,最近想著給本地朋友做一套中繼臺(tái)的信標(biāo)系統(tǒng),就是整點(diǎn)報(bào)時(shí)加語音提示,想了好多方案,差了好多資料,最后決定用GPS授時(shí),單片機(jī)讀取時(shí)間,然后比較,再通過串口輸出指令來控制MP3播放模塊,現(xiàn)在問題是,GPS讀取時(shí)間已經(jīng)完成,串口整點(diǎn)發(fā)指令正常,唯獨(dú)是,只能發(fā)一次指令,發(fā)一次指令后,單片機(jī)不再讀取更新時(shí)間,就無法進(jìn)行下一個(gè)時(shí)間的報(bào)時(shí),請(qǐng)大家多多指點(diǎn),我沒學(xué)過單片機(jī),程序都是抄的,不好意思了。
#include "stc15.h"
#define LEDPORT P2
typedef unsigned char uint8;//
#define uint unsigned int
sbit D1=P4^1;
sbit D2=P4^2;
sbit D3=P4^4;
sbit D4=P4^5;
sbit LED=P3^6;
sbit LED1=P3^7;
sbit play=P3^3;
unsigned char h,hour,min,sec;
bit rev_start,rev_stop;
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0-9,
unsigned char code fs[]={0x7e,0x04,0x41,0x00,0x01,0xef};//播放器發(fā)送指令
unsigned char buf[80]; //請(qǐng)把GPS那貨發(fā)來的數(shù)據(jù)放到我的肚子里
void delayms(int ms)
{
int i,j;
for(i=ms;i>0;i--)
for(j=845;j>0;j--);
}
void TimerInit()
{
TMOD=0x21;
TH0 = 0xF1; //4ms左右
TL0 = 0x9A;
ET0=1;
TR0=1;
SCON = 0x50;//使用串行工作方式1,10位異步收發(fā)8位數(shù)據(jù),波特率可變(由T1的溢出率控制)
AUXR |= 0x40; //定時(shí)器時(shí)鐘1T模式
AUXR &= 0xFE; //串口1選擇定時(shí)器1為波特率發(fā)生器
TMOD &= 0x0F; //設(shè)置定時(shí)器模式
TL1 = 0xE0; //設(shè)置定時(shí)初始值
TH1 = 0xFE; //設(shè)置定時(shí)初始值
ET1 = 0; //禁止定時(shí)器%d中斷
TR1 = 1; //定時(shí)器1開始計(jì)時(shí)
ES = 1;
EA=1;
}
void Display(unsigned char hour,min) //數(shù)碼管顯示
{
static unsigned char i;
LEDPORT=0xff;
switch (i)
{
case 0: if(hour/16) LEDPORT=tab[hour/16];
else LEDPORT=tab[10]; D1=0;D2=D3=D4=1;break;
case 1: LEDPORT=tab[hour%16]; D2=0;D1=D3=D4=1;break;
case 2: LEDPORT=tab[min/16]; D3=0;D1=D2=D4=1;break;
case 3: LEDPORT=tab[min%16]; D4=0;D2=D3=D1=1;break;
}
i++;
if(i==4)i=0;
}
void Timer0() interrupt 1
{
TH0 = 0xF1;
TL0 = 0x9A;
Display(hour,min);//定時(shí)器兄弟簡單而單調(diào)的工作,很輕松的。每4ms刷新下數(shù)碼管,全年無休哦
}
/*********************串口發(fā)送*******************************/
void fasong()
{
TI=0;
for(h=0;h<6;h++)
{
SBUF=fs[h];
delayms(5);
TI=0;
}
}
/*************************串口數(shù)據(jù)接收**************************/
void Uart_Receive(void) interrupt 4
{
unsigned char ch,num;
ES = 0;
if (RI)//如果接收完成則進(jìn)入
{
ch = SBUF;
if (ch == '$') //如果收到字符'$',便開始接收
{
rev_start = 1;
rev_stop = 0; //接收停止標(biāo)志
}
if (rev_start == 1) //標(biāo)志位為1,開始接收
{
buf[num++] = ch; //字符存到數(shù)組中
if (ch == '\n') //如果接收到換行
{
buf[num] = '\n';
rev_start = 0;
rev_stop = 1; //接收停止標(biāo)志
num = 0;
if( hour==0x07 && min==0x00 && sec==0x00) //早上7點(diǎn)
{
fasong(); //發(fā)送播放指令前4字節(jié) (頭幀及命令幀)
delayms(5);
SBUF=0x01; //發(fā)送播放指令第5字節(jié)(第一首)
delayms(5);
SBUF=0xef; //發(fā)送播放指令前6字節(jié)(尾幀)
}
if( hour==0x08 && min==0x00 && sec==0x00) //上午8點(diǎn)
{
fasong();
delayms(5);
SBUF=0x02;
delayms(5);
SBUF=0xef;
}
if( hour==0x09 && min==0x00 && sec==0x00) //上午9點(diǎn)
{
fasong();
delayms(5);
SBUF=0x03;
delayms(5);
SBUF=0xef;
}
if( hour==0x10 && min==0x00 && sec==0x00) //上午10點(diǎn)
{
fasong();
delayms(5);
SBUF=0x04;
delayms(5);
SBUF=0xef;
}
if( hour==0x11 && min==0x00 && sec==0x00) //上午11點(diǎn)
{
fasong();
delayms(5);
SBUF=0x05;
delayms(5);
SBUF=0xef;
}
if( hour==0x12 && min==0x00 && sec==0x00) //中午12點(diǎn)
{
fasong();
delayms(5);
SBUF=0x06;
delayms(5);
SBUF=0xef;
}
if( hour==0x13 && min==0x00 && sec==0x00) //下午1點(diǎn)
{
fasong();
delayms(5);
SBUF=0x07;
delayms(5);
SBUF=0xef;
}
if( hour==0x14 && min==0x00 && sec==0x00) //下午2點(diǎn)
{
fasong();
delayms(5);
SBUF=0x08;
delayms(5);
SBUF=0xef;
}
if( hour==0x15 && min==0x00 && sec==0x00) //下午3點(diǎn)
{
fasong();
delayms(5);
SBUF=0x09;
delayms(5);
SBUF=0xef;
}
if( hour==0x14 && min==0x00 && sec==0x00) //下午4點(diǎn)
{
fasong();
delayms(5);
SBUF=0x0a;
delayms(5);
SBUF=0xef;
}
if( hour==0x17 && min==0x54 && sec==0x00) //下午5點(diǎn)
{
fasong();
delayms(5);
SBUF=0x0b;
delayms(5);
SBUF=0xef;
}
if( hour==0x18 && min==0x00 && sec==0x00) //晚上6點(diǎn)
{
fasong();
delayms(5);
SBUF=0x0c;
delayms(5);
SBUF=0xef;
}
if( hour==0x19 && min==0x00 && sec==0x00) //晚上7點(diǎn)
{
fasong();
delayms(5);
SBUF=0x0d;
delayms(5);
SBUF=0xef;
}
if( hour==0x20 && min==0x00 && sec==0x00) //晚上8點(diǎn)
{
fasong();
delayms(5);
SBUF=0x0e;
delayms(5);
SBUF=0xef;
}
if( hour==0x21 && min==0x00 && sec==0x00) //晚上9點(diǎn)
{
fasong();
delayms(5);
SBUF=0x0e;
delayms(5);
SBUF=0xef;
}
if( hour==0x22 && min==0x00 && sec==0x00) //晚上10點(diǎn)
{
fasong();
delayms(5);
SBUF=0x0f;
delayms(5);
SBUF=0xef;
}
}
}
RI = 0; //RI清0,重新接收
ES = 1; //串口1中斷允許
}
if(TI)
{
TI = 0;
}
}
void main()
{
LED1=0;
P0M0=0X00;P0M1=0X00;
P1M0=0X00;P1M1=0X00;
P2M0=0X00;P2M1=0X00;
P3M0=0X00;P3M1=0X00;
P4M0=0X00;P4M1=0X00;
TimerInit();
while(1)
{
if((rev_stop==1)&&(buf[5]=='A'))//如果接收到GNGGA
{
ES=0;
hour= (buf [7]-0x30)* 16+ buf[8] -0x30;
min= (buf [9]-0x30)* 16+ buf[10]-0x30;
sec= (buf[11]-0x30)* 16+ buf[12]-0x30;
hour= hour / 16 * 10 + hour % 16;
LED=!LED;
LED1=!LED1;
hour= (hour+8) % 24;//UTC Time換算成北京時(shí)間
hour=hour/10*16+hour%10;
ES=1;
rev_stop=0;
}
}
}
|
|