BSP末期训练

1. 串口1收发。

计算机上利用串口助手,设置串口参数:“2400,8,N,1”(即:波特率2400bps,8个数据位。无奇偶校验位,一个停止位),顺序发送10字节的HEX数据到STC-B板,STC-B板将接收到的10字节数据再以倒序方式经串口1发送回计算机。

#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
char old_da[10];

void myrxd_callback(){
	int i;
	char new_da[10];
	for(i=0;i<10;i++){
		new_da[i] = old_da[9-i];
	}
	Uart1Print(new_da,10);
}
void main() 
{ 
	Uart1Init(2400);
	SetUart1Rxd(old_da,10,old_da,0);
	SetEventCallBack(enumEventUart1Rxd,myrxd_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

2. 串口2通信(RS485连接,或TTL Uart连接)。

两块STC-B板1和2通过串口2连接(485接口上:A、B、GND,或EXT上:P1.0(RXD)、P1.1(TXD)、GND),设置串口2参数:“1200,8,N,1”。STC板1往STC板2发送5字节数据,STC板2接收数据,计算它们的累加和,并将累加和的低8位通过LED灯显示,验证结果是否正确?(STC-B板1需多换几组数据验证)。

#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
char n1[5] = {125,125,125,125,125};
void myKey_callback(){
	char k = GetKeyAct(enumKey1);
	if(k==enumKeyPress){
		Uart2Print(n1,5);
		Seg7Print(0,0,0,0,0,0,0,0);
	}
	else if(k==enumKeyRelease){
			Seg7Print(10,10,10,10,10,10,10,10);
	}
}

void main() 
{ 
	DisplayerInit();
	KeyInit();
	Uart2Init(1200,Uart2Usedfor485);
	LedPrint(0x00);
	SetDisplayerArea(0,7);
	Seg7Print(10,10,10,10,10,10,10,10);
	SetEventCallBack(enumEventKey, myKey_callback);
	
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    
#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
char old_da[5];
int array[8];
void myrxd_callback(){
	int result = 0;
	int i;
	for(i=0;i<5;i++){
		result=result+old_da[i];
	}
	LedPrint(result);
	for(i=0;i<8;i++){
		array[i]=(result%10);
		result=result/10;
	}
	Seg7Print(array[7],array[6],array[5],array[4],array[3],array[2],array[1],array[0]);
}
void main() 
{ 
	DisplayerInit();
	SetDisplayerArea(0,7);
	Uart2Init(1200,Uart2Usedfor485);
	SetUart2Rxd(old_da,5,old_da,0);
	SetEventCallBack(enumEventUart2Rxd,myrxd_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

3. 红外无线通信。

与第2题的操作一致,仅两块STC-B板通信方式选用IR红外无线连接(而不是串口2)。注意:同一房间内,同时开启红外通信可能会互相干扰。

#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
char sets[1] = {1};
char n1[5] = {25,125,125,125,125};
void myKey_callback(){
	char k = GetKeyAct(enumKey1);
	if(k==enumKeyPress){
		IrPrint(n1,5);
		Seg7Print(0,0,0,0,0,0,0,0);
	}
	else if(k==enumKeyRelease){
			Seg7Print(10,10,10,10,10,10,10,10);
	}
}

void main() 
{ 
	DisplayerInit();
	KeyInit();
	IrInit(NEC_R05d);
	IrTxdSet(sets,1);
	LedPrint(0x00);
	SetDisplayerArea(0,7);
	Seg7Print(10,10,10,10,10,10,10,10);
	SetEventCallBack(enumEventKey, myKey_callback);
	
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    
#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
char old_da[5];
int array[8];
void myir_callback(){
	int result = 0;
	int i;
	for(i=0;i<5;i++){
		result=result+old_da[i];
	}
	
	for(i=0;i<8;i++){
		if(result==0)
			array[i]=10;
		else{
			array[i]=(result%10);
		 	result=result/10;
		}
	}
	Seg7Print(array[7],array[6],array[5],array[4],array[3],array[2],array[1],array[0]);
}
void main() 
{ 
	DisplayerInit();
	SetDisplayerArea(0,7);
	LedPrint(0x00);
	Seg7Print(10,10,10,10,10,10,10,10);
	IrInit(NEC_R05d);
	SetIrRxd(old_da,5);
	SetEventCallBack(enumEventIrRxd,myir_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

4. 实时时钟。

初始化DS1302实时时钟芯片,并将其“时分秒”信息以“时时—分分—秒秒”格式显示在数码管上。然后验证“STC-B学习板”上的实时时钟在断电后,其时钟靠板上的纽扣电池仍能正常走时。

#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
struct_DS1302_RTC t={0x55,0x15,0x15,0x23,8,2,0x22}; 
struct_DS1302_RTC set_t={0x00,0x36,0x12,0x23,8,2,0x22}; 
void my1S_callback(){
	t.second=t.second+1;
	if(t.second%16==10){
		t.second=t.second+6;
		if(t.second/16==6){
			t.second=0;
			t.minute=t.minute+1;
			if(t.minute%16==10){
				t.minute=t.minute+6;
				if(t.minute/16==6){
					t.minute=0;
					t.hour=t.hour+1;
				}
			}
		}
	}
	RTC_Write(t);
	Seg7Print(t.hour/16,t.hour%16,10,t.minute/16,t.minute%16,10,t.second/16,t.second%16);
}
void mykey_callback(){
	char k = GetKeyAct(enumKey1);
	if(k==enumKeyPress){
		RTC_Write(set_t);
		t = set_t;
	}
}
void main() 
{ 
	
	DS1302Init(t);
	//struct_DS1302_RTC tmp = RTC_Read();
	//if(tmp.year!=t.year||)
	t = RTC_Read();
	//数码管和按键设置
	DisplayerInit();
	KeyInit();
	LedPrint(0x00);
	SetDisplayerArea(0,7);
	Seg7Print(t.hour/16,t.hour%16,10,t.minute/16,t.minute%16,10,t.second/16,t.second%16);
	RTC_Write(t);
	
	SetEventCallBack(enumEventSys1S, my1S_callback);
	SetEventCallBack(enumEventKey, mykey_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

5. 非易失存储。

数据可以在掉电情况下保留在非易失存储器(M24C02或DS1302)中的某个单元上。设计一段小程序:上电后,读取出非易失存储内某个单元数据,并将其值显示在LED灯上,再将这个数据+1后写回这个单元。分析这样的程序,如果拔插“STC—B学习板”电源(或按板上“RST”复位按键),会出现什么现象?(说明:DS1302需要靠纽扣电池才能在掉电时保存数据)

#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
struct_DS1302_RTC t;
unsigned char tmp;
unsigned char set_da=1;
void mykey_callback(){
	char k1 = GetKeyAct(enumKey1);
	char k2 = GetKeyAct(enumKey2);
	if(k1==enumKeyPress){
		tmp = NVM_Read(0);
		NVM_Write(0,tmp+1);
		Seg7Print(tmp+1,10,10,10,10,10,10,10);
	}
	if(k2==enumKeyPress){
		NVM_Write(0,set_da);
		Seg7Print(set_da,10,10,10,10,10,10,10);
	}
}
void main() 
{ 
	//
	DS1302Init(t);
	//数码管和按键设置
	DisplayerInit();
	KeyInit();
	LedPrint(0x00);
	SetDisplayerArea(0,0);
	Seg7Print(NVM_Read(0),10,10,10,10,10,10,10);
	SetEventCallBack(enumEventKey, mykey_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

6. 收音机。

初始化启用FM_radio模块。收音机参数设定为91.8MHz,音量6。PHONE接口上插上耳机验证是否正确收到电台?

#include "STC15F2K60S2.H"        //必须。
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //必须。
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //必须。
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
struct_FMRadio FM; 
void main() 
{ 
	FM.frequency=918;
	FM.volume=6;
	FMRadioInit(FM);
	
	DisplayerInit();
	KeyInit();
	LedPrint(0x00);
	SetDisplayerArea(0,7);
	Seg7Print(10,10,10,10,10,10,10,10);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

7. 音乐播放器。

用Music模块提供的API实现播放一段音乐。

#include "STC15F2K60S2.H"        //???
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //???
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //???
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //?????????????(Hz),??????????????(??????)??
#ifdef _displayer_H_                          //??????????(????????,?????????) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* ??:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* ??:   0   1    2    3    4     5    6    7   8    9  (?)   ?-  ?-  ?-  ??-  ??-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* ????     0         1         2         3         4         5         6         7         8         9        */
#endif
//晴天
unsigned char code sound[]={
0x16 ,0x08 ,0x21 ,0x08 ,0x25 ,0x08 ,
0x21 ,0x08 ,0x14 ,0x08 ,0x15 ,0x04 ,
0x16 ,0x04 ,0x25 ,0x08 ,0x21 ,0x08 ,
0x11 ,0x08 ,0x15 ,0x08 ,0x25 ,0x08 ,
0x21 ,0x08 ,0x11 ,0x08 ,0x25 ,0x08 ,
0x17 ,0x08 ,0x25 ,0x08 ,0x16 ,0x08 ,
0x21 ,0x08 ,0x25 ,0x08 ,0x16 ,0x08 ,
0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  0x16,0x08,0x17,0x08,
    0x21,0x08,0x22,0x08,
    0x17,0x08,0x21,0x08,
    0x21,0x10,0x21,0x08,
    0x17,0x08,0x21,0x08,
    0x22,0x08,0x17,0x08,
    0x21,0x08,0x21,0x10,
    0x21,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,
    0x23,0x10,0x23,0x08,
    0x22,0x08,0x23,0x10,
    0x25,0x10,0x23,0x10,
    0x16,0x08,0x17,0x08,
    0x21,0x08,0x22,0x08,
    0x17,0x08,0x21,0x08,

    0x21,0x10,0x21,0x08,
    0x17,0x08,0x21,0x08,
    0x22,0x08,0x17,0x08,
    0x21,0x08,0x21,0x10,
    0x21,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,
    0x23,0x08,0x22,0x08,

    0x23,0x10,0x23,0x08,
    0x22,0x08,0x23,0x10,
    0x25,0x10,0x23,0x10,
    0x25,0x10,0x23,0x14,
    0x25,0x08,0x23,0x14,
    0x25,0x08,0x23,0x08,
    0x25,0x08,0x26,0x08,
    0x23,0x08,0x25,0x10,
    0x25,0x08,0x23,0x14,
    0x25,0x08,0x23,0x14,
    0x25,0x08,0x23,0x08,
    0x25,0x08,0x26,0x08,
    0x23,0x08,0x25,0x10,
    0x25,0x08,0x25,0x08,
    0x23,0x08,0x22,0x08,
    0x22,0x10,0x22,0x10,
    0x21,0x08,0x23,0x08,
    0x23,0x08,0x22,0x08,
    0x22,0x10,0x22,0x10,
    0x21,0x08,0x21,0x08,
    0x16,0x20,0x25,0x08,
    0x25,0x08,0x23,0x08,
    0x22,0x08,0x22,0x10,

    0x22,0x10,0x21,0x08,
    0x23,0x08,0x23,0x08,
    0x22,0x08,0x22,0x10,
    0x22,0x10,0x21,0x08,
    0x21,0x08,0x16,0x20,
    0x00
};
void myKey_callback(){
	char k1 = GetKeyAct(enumKey1);
	if(k1 == enumKeyPress){
		char state = GetPlayerMode();
		if(state == enumModePlay){
			SetPlayerMode(enumModePause);
		}
		else{
			SetPlayerMode(enumModePlay);
		}
	}
}
void main() 
{ 
	DisplayerInit();
	BeepInit();
	MusicPlayerInit();
	SetMusic(80,0xFF,sound,sizeof(sound)/sizeof(sound[0]),enumMscNull);
	SetPlayerMode(enumModePause);
	//????????
	//
	KeyInit();
	
	SetEventCallBack(enumEventKey, myKey_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

8. 温度值计算。

10位精度采集热敏电阻ADC值,编写程序(查表、或线性插值方法等)换算出正确温度值,并在数码管显示出来。热敏电阻参数10K/3950,(具体见“案例测试”中提供的参考资料。可设有效换算温度范围-5°C~+85°C)。

#include "STC15F2K60S2.H"        //???
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //???
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //???
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
//温度值对应表
int code tempdata[]={239,197,175,160,150,142,135,129,124,120,116,113,109,107,104,101, 
										  99, 97, 95, 93, 91, 90, 88, 86, 85, 84, 82, 81, 80, 78, 77, 76, 
										  75, 74, 73, 72, 71, 70, 69, 68, 67, 67, 66, 65, 64, 63, 63, 62, 
										  61, 61, 60, 59, 58, 58, 57, 57, 56, 55, 55, 54, 54, 53, 52, 52, 
										  51, 51, 50, 50, 49, 49, 48, 48, 47, 47, 46, 46, 45, 45, 44, 44, 
										  43, 43, 42, 42, 41, 41, 41, 40, 40, 39, 39, 38, 38, 38, 37, 37, 
										  36, 36, 36, 35, 35, 34, 34, 34, 33, 33, 32, 32, 32, 31, 31, 31, 
										  30, 30, 29, 29, 29, 28, 28, 28, 27, 27, 27, 26, 26, 26, 25, 25,
										  24, 24, 24, 23, 23, 23, 22, 22, 22, 21, 21, 21, 20, 20, 20, 19, 
										  19, 19, 18, 18, 18, 17, 17, 16, 16, 16, 15, 15, 15, 14, 14, 14, 
										  13, 13, 13, 12, 12, 12, 11, 11, 11, 10, 10, 9, 9, 9, 8, 8, 8, 7, 
										  7, 7, 6, 6,5, 5, 54,4, 3, 3,3, 2, 2, 1, 1, 1, 0, 0, -1, -1, -1, 
										  -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -7, -8, -8, -9, -9, 
										  -10, -10, -11, -11, -12, -13, -13, -14, -14, -15, -16, -16, -17, 
										  -18, -19, -19, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, 
										  -30, -32, -33, -35, -36, -38, -40, -43, -46, -50, -55, -63, 361};
struct_ADC adc;
unsigned int rt;
int temp;
void my1s_callback(){
	adc = GetADC();
	rt = adc.Rt;
	rt = rt>>2;
	temp = tempdata[rt];
	if(temp<0){
		if(temp>-10){
			Seg7Print(10,10,12,temp%10,10,10,10,10);
		}
		else{
			Seg7Print(10,10,12,temp/10%10,temp%10,10,10,10);
		}
	}
	else{
		if(temp<10){
			Seg7Print(10,10,10,temp%10,10,10,10,10);
		}
		else if(temp>=10&&temp<100){
			Seg7Print(10,10,10,temp/10%10,temp%10,10,10,10);
		}
		else{
			Seg7Print(10,10,10,temp/100%10,temp/10%10,temp%10,10,10);
		}
	}
}
void main() 
{ 
	AdcInit(ADCexpEXT);
	DisplayerInit();
	SetDisplayerArea(0,7);
	Seg7Print(10,10,10,10,10,10,10,10);
	LedPrint(0x00);
	SetEventCallBack(enumEventSys1S, my1s_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

9. 扩展模板。

外设模块中超声波、编码器、电子尺…选取一个,能在数码管上显示相应物理量数值。

#include "STC15F2K60S2.H"        //???
#include "sys.H"
#include "adc.H" 
#include "beep.H"        //???
#include "displayer.H" 
#include "DS1302.H" 
#include "EXT.H" 
#include "FM_Radio.H"
#include "hall.H"
#include "ir.H" 
#include "key.H"        //???
#include "m24c02.H" 
#include "music.H" 
#include "vib.H" 
#include "uart1.H"
#include "uart2.h"
code unsigned long SysClock=11059200;         //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
#ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等) 
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01, 0x41, 0x48, 
	              /* 序号:   0   1    2	   3    4	    5    6	  7   8	   9	 10	   11		12   13    14     15     */
                /* 显示:   0   1    2    3    4     5    6    7   8    9  (无)   下-  中-  上-  上中-  中下-   */  
	                       0x3f|0x80,0x06|0x80,0x5b|0x80,0x4f|0x80,0x66|0x80,0x6d|0x80,0x7d|0x80,0x07|0x80,0x7f|0x80,0x6f|0x80 };  
             /* 带小数点     0         1         2         3         4         5         6         7         8         9        */
#endif
int distan;
void my1s_callback(){
	distan = GetUltraSonic();
	if(distan<10){
		Seg7Print(10,10,10,10,distan,10,10,10);
	}
	else if(distan>=10&&distan<100){
		Seg7Print(10,10,10,distan/10,distan%10,10,10,10);
	}
	else if(distan>=100&&distan<1000){
		Seg7Print(10,10,10,distan/100,distan/10%10,distan%10,10,10);
	}
	else{
		Seg7Print(10,10,10,12,12,10,10,10);
	}
}
void main() 
{ 
	EXTInit(enumEXTUltraSonic);
	DisplayerInit();
	SetDisplayerArea(0,7);
	Seg7Print(10,10,10,10,10,10,10,10);
	LedPrint(0x00);
	
	SetEventCallBack(enumEventSys1S, my1s_callback);
  MySTC_Init();	 
	while(1)             	
		{ MySTC_OS();    
		}	             
}    

10. 直流电机。

设计简单程序段,用两组参数(“50%速度、正转”,和“30%速度、反转”)分别设置直流电机,并接上直流电机观察不同参数时电机转动情况。


#include "STC15F2K60S2.H"
#include "sys.H"
#include "displayer.H"
#include "EXT.h"
#include "Key.H"

code unsigned long SysClock = 11059200;
#ifdef _displayer_H_

void callback_forkey()
{
    if (GetKeyAct(enumKey1) == enumKeyPress)
        SetPWM(0, 0, 50, 500); 
    else if (GetKeyAct(enumKey2) == enumKeyPress)
        SetPWM(50, 300, 0, 0); 
}

void main()
{
    DisplayerInit();
    EXTInit(enumEXTPWM);
    KeyInit();
    SetEventCallBack(enumEventKey, callback_forkey);
    SetDisplayerArea(0, 7);
    Seg7Print(28, 25, 32, 32, 35, 10, 10, 10);
    LedPrint(0);
    MySTC_Init();
    while (1)
    {
        MySTC_OS();
    }
}

猜你喜欢

转载自blog.csdn.net/a45667/article/details/127156320
BSP