本人是一名業余無線電愛好者,最近想著給本地朋友做一套中繼臺的信標系統,就是整點報時加語音提示,想了好多方案,差了好多資料,最后決定用GPS授時,單片機讀取時間,然后比較,再通過串口輸出指令來控制MP3播放模塊,現在問題是,GPS讀取時間已經完成,串口整點發指令正常,唯獨是,只能發一次指令,發一次指令后,單片機不再讀取更新時間,就無法進行下一個時間的報時,請大家多多指點,我沒學過單片機,程序都是抄的,不好意思了。
#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};//播放器發送指令
unsigned char buf[80]; //請把GPS那貨發來的數據放到我的肚子里
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位異步收發8位數據,波特率可變(由T1的溢出率控制)
AUXR |= 0x40; //定時器時鐘1T模式
AUXR &= 0xFE; //串口1選擇定時器1為波特率發生器
TMOD &= 0x0F; //設置定時器模式
TL1 = 0xE0; //設置定時初始值
TH1 = 0xFE; //設置定時初始值
ET1 = 0; //禁止定時器%d中斷
TR1 = 1; //定時器1開始計時
ES = 1;
EA=1;
}
void Display(unsigned char hour,min) //數碼管顯示
{
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);//定時器兄弟簡單而單調的工作,很輕松的。每4ms刷新下數碼管,全年無休哦
}
/*********************串口發送*******************************/
void fasong()
{
TI=0;
for(h=0;h<6;h++)
{
SBUF=fs[h];
delayms(5);
TI=0;
}
}
/*************************串口數據接收**************************/
void Uart_Receive(void) interrupt 4
{
unsigned char ch,num;
ES = 0;
if (RI)//如果接收完成則進入
{
ch = SBUF;
if (ch == '$') //如果收到字符'$',便開始接收
{
rev_start = 1;
rev_stop = 0; //接收停止標志
}
if (rev_start == 1) //標志位為1,開始接收
{
buf[num++] = ch; //字符存到數組中
if (ch == '\n') //如果接收到換行
{
buf[num] = '\n';
rev_start = 0;
rev_stop = 1; //接收停止標志
num = 0;
if( hour==0x07 && min==0x00 && sec==0x00) //早上7點
{
fasong(); //發送播放指令前4字節 (頭幀及命令幀)
delayms(5);
SBUF=0x01; //發送播放指令第5字節(第一首)
delayms(5);
SBUF=0xef; //發送播放指令前6字節(尾幀)
}
if( hour==0x08 && min==0x00 && sec==0x00) //上午8點
{
fasong();
delayms(5);
SBUF=0x02;
delayms(5);
SBUF=0xef;
}
if( hour==0x09 && min==0x00 && sec==0x00) //上午9點
{
fasong();
delayms(5);
SBUF=0x03;
delayms(5);
SBUF=0xef;
}
if( hour==0x10 && min==0x00 && sec==0x00) //上午10點
{
fasong();
delayms(5);
SBUF=0x04;
delayms(5);
SBUF=0xef;
}
if( hour==0x11 && min==0x00 && sec==0x00) //上午11點
{
fasong();
delayms(5);
SBUF=0x05;
delayms(5);
SBUF=0xef;
}
if( hour==0x12 && min==0x00 && sec==0x00) //中午12點
{
fasong();
delayms(5);
SBUF=0x06;
delayms(5);
SBUF=0xef;
}
if( hour==0x13 && min==0x00 && sec==0x00) //下午1點
{
fasong();
delayms(5);
SBUF=0x07;
delayms(5);
SBUF=0xef;
}
if( hour==0x14 && min==0x00 && sec==0x00) //下午2點
{
fasong();
delayms(5);
SBUF=0x08;
delayms(5);
SBUF=0xef;
}
if( hour==0x15 && min==0x00 && sec==0x00) //下午3點
{
fasong();
delayms(5);
SBUF=0x09;
delayms(5);
SBUF=0xef;
}
if( hour==0x14 && min==0x00 && sec==0x00) //下午4點
{
fasong();
delayms(5);
SBUF=0x0a;
delayms(5);
SBUF=0xef;
}
if( hour==0x17 && min==0x54 && sec==0x00) //下午5點
{
fasong();
delayms(5);
SBUF=0x0b;
delayms(5);
SBUF=0xef;
}
if( hour==0x18 && min==0x00 && sec==0x00) //晚上6點
{
fasong();
delayms(5);
SBUF=0x0c;
delayms(5);
SBUF=0xef;
}
if( hour==0x19 && min==0x00 && sec==0x00) //晚上7點
{
fasong();
delayms(5);
SBUF=0x0d;
delayms(5);
SBUF=0xef;
}
if( hour==0x20 && min==0x00 && sec==0x00) //晚上8點
{
fasong();
delayms(5);
SBUF=0x0e;
delayms(5);
SBUF=0xef;
}
if( hour==0x21 && min==0x00 && sec==0x00) //晚上9點
{
fasong();
delayms(5);
SBUF=0x0e;
delayms(5);
SBUF=0xef;
}
if( hour==0x22 && min==0x00 && sec==0x00) //晚上10點
{
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換算成北京時間
hour=hour/10*16+hour%10;
ES=1;
rev_stop=0;
}
}
}
|