作业要求
任务:编程实现系统子功能可能会使用的BSP模块功能。
BSP模块应用训练:
1. 串口1收发。计算机上利用串口助手,设置串口参数:“2400,8,N,1”(即:波特率2400bps,8个数据位。无奇偶校验位,一个停止位),顺序发送10字节的HEX数据到STC-B板,STC-B板将接收到的10字节数据再以倒序方式经串口1发送回计算机。
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需多换几组数据验证)。
3. 红外无线通信。与第2题的操作一致,仅两块STC-B板通信方式选用IR红外无线连接(而不是串口2)。注意:同一房间内,同时开启红外通信可能会互相干扰。
4. 实时时钟。初始化DS1302实时时钟芯片,并将其“时分秒”信息以“时时—分分—秒秒”格式显示在数码管上。然后验证“STC-B学习板”上的实时时钟在断电后,其时钟靠板上的纽扣电池仍能正常走时。
5. 非易失存储。数据可以在掉电情况下保留在非易失存储器(M24C02或DS1302)中的某个单元上。设计一段小程序:上电后,读取出非易失存储内某个单元数据,并将其值显示在LED灯上,再将这个数据+1后写回这个单元。分析这样的程序,如果拔插“STC—B学习板”电源(或按板上“RST”复位按键),会出现什么现象?(说明:DS1302需要靠纽扣电池才能在掉电时保存数据)
6. 收音机。初始化启用FM_radio模块。收音机参数设定为91.8MHz,音量6。PHONE接口上插上耳机验证是否正确收到电台?
7. 音乐播放器。用Music模块提供的API实现播放一段音乐。
8. 温度值计算。10位精度采集热敏电阻ADC值,编写程序(查表、或线性插值方法等)换算出正确温度值,并在数码管显示出来。热敏电阻参数10K/3950,(具体见“案例测试”中提供的参考资料。可设有效换算温度范围-5°C~+85°C)。
9. 扩展模板。外设模块中超声波、编码器、电子尺…选取一个,能在数码管上显示相应物理量数值。
10. 直流电机。设计简单程序段,用两组参数(“50%速度、正转”,和“30%速度、反转”)分别设置直流电机,并接上直流电机观察不同参数时电机转动情况。
训练题工程代码
1.串口1收发。
#include "STC15F2K60S2.H" //必须。
#include "sys.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 rcvBuffer[10];
void Uart1_callback()
{
unsigned int i;
unsigned char temp;
for (i = 0; i < 5; i++)
{
temp = rcvBuffer[i];
rcvBuffer[i] = rcvBuffer[9 - i];
rcvBuffer[9 - i] = temp;
}
while(GetUart1TxStatus() == enumUart1TxBusy)
Uart1Print(rcvBuffer, 10);
}
void main()
{
Uart1Init(2400);
SetUart1Rxd(rcvBuffer, 10, 0, 0);
SetEventCallBack(enumEventUart1Rxd, Uart1_callback);
MySTC_Init();
while(1)
{
MySTC_OS();
}
}
2.串口2通信。
发送方
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "Uart2.h"
#include "displayer.h"
#include "key.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 Senddata[5] = {88, 174, 236, 120, 75};
void Uart2RxdCallback()
{
char k = GetKeyAct(enumKey1);
if(k==enumKeyPress)
{
Uart2Print(Senddata,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();
SetDisplayerArea(0, 7);
Seg7Print(10, 10, 10, 10, 10, 10, 10, 10);
LedPrint(0);
Uart2Init(1200, Uart2Usedfor485);
SetEventCallBack(enumEventUart2Rxd, Uart2RxdCallback);
MySTC_Init();
while(1)
{
MySTC_OS();
}
}
接收方
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "Uart2.h"
#include "displayer.h"
#include "key.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 Receivedata[5];
int res[8];
void Uart2RxdCallback()
{
int sum = 0;
int i;
for(i = 0; i < 5; i++){
sum = sum + Receivedata[i];
}
LedPrint(sum);
for(i = 0; i < 8; i++){
res[i] = (sum % 10);
sum = sum/10;
}
Seg7Print(res[7],res[6],res[5],res[4],res[3],res[2],res[1],res[0]);
}
void main()
{
DisplayerInit();
SetDisplayerArea(0,7);
Uart2Init(1200, Uart2Usedfor485);
SetUart2Rxd(Receivedata, 5, 0, 0);
SetEventCallBack(enumEventUart2Rxd, Uart2RxdCallback);
MySTC_Init();
while(1)
{
MySTC_OS();
}
}
3.红外无线通信。
发送方
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "Ir.h"
#include "displayer.h"
#include "key.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 set[1] = {1};
unsigned char Senddata[5] = {88, 174, 236, 120, 75};
void myKey_callback()
{
char k = GetKeyAct(enumKey1);
if(k==enumKeyPress)
{
IrPrint(Senddata,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();
SetDisplayerArea(0, 7);
Seg7Print(10, 10, 10, 10, 10, 10, 10, 10);
LedPrint(0);
IrInit(NEC_R05d);
IrTxdSet(set,1);
SetEventCallBack(enumEventKey, myKey_callback);
MySTC_Init();
while(1)
{
MySTC_OS();
}
}
接收方
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "ir.H"
#include "displayer.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 Receivedata[5];
int res[8];
void IrRxdCallback()
{
int sum = 0;
int i;
for(i = 0; i < 5; i++){
sum = sum + Receivedata[i];
}
LedPrint(sum);
for(i = 0; i < 8; i++){
res[i] = (sum % 10);
sum = sum/10;
}
Seg7Print(res[7],res[6],res[5],res[4],res[3],res[2],res[1],res[0]);
}
void main()
{
DisplayerInit();
SetDisplayerArea(0,7);
IrInit(NEC_R05d);
SetIrRxd(Receivedata,5);
SetEventCallBack(enumEventIrRxd, IrRxdCallback);
MySTC_Init();
while(1)
{
MySTC_OS();
}
}
4.实时时钟。
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "displayer.H"
#include "DS1302.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 BCD_to_DEC(unsigned char bcd) {
return ((bcd >> 4) * 10) + (bcd & 0x0F);
}
unsigned char DEC_to_BCD(unsigned char dec) {
return ((dec / 10) << 4) + (dec % 10);
}
void my100mS_callback(void) {
struct_DS1302_RTC current_time;
unsigned char hour, minute, second;
current_time = RTC_Read();
hour = BCD_to_DEC(current_time.hour);
minute = BCD_to_DEC(current_time.minute);
second = BCD_to_DEC(current_time.second);
Seg7Print(hour / 10, hour % 10, 12, minute / 10, minute % 10, 12, second / 10, second % 10);
}
void main() {
struct_DS1302_RTC init_time;
init_time.year = DEC_to_BCD(25);
init_time.month = DEC_to_BCD(9);
init_time.day = DEC_to_BCD(8);
init_time.week = DEC_to_BCD(1);
init_time.hour = DEC_to_BCD(12);
init_time.minute = DEC_to_BCD(0);
init_time.second = DEC_to_BCD(0);
MySTC_Init();
DisplayerInit();
DS1302Init(init_time);
SetDisplayerArea(0, 7);
SetEventCallBack(enumEventSys100mS, my100mS_callback);
while (1) {
MySTC_OS();
}
}
5. 非易失存储。
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "Uart2.h"
#include "displayer.h"
#include "DS1302.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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
#define NVM_ADDR 0
unsigned char count = 0;
void main()
{
DisplayerInit();
SetDisplayerArea(0, 7);
count = NVM_Read(NVM_ADDR);
Seg7Print(0, 0, 0, 0, 0, count/100, (count%100)/10, count%10);
count++;
NVM_Write(NVM_ADDR, count);
MySTC_Init();
while(1) {
MySTC_OS();
}
}
6. 收音机。
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "FM_Radio.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 myRadio = {918, 6, 0, 0, 0};
unsigned char count = 0;
void main()
{
FMRadioInit(myRadio);
MySTC_Init();
while(1) {
MySTC_OS();
}
}
7. 音乐播放器。
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "displayer.H"
#include "Key.H"
#include "Beep.H"
#include "Music.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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
//《棠梨煎雪》
code unsigned char sound[]={0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x10,0x25,0x10,0x22,0x08,0x23,0x08,0x22,0x08,0x16,0x08,0x21,0x20,
0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x10,0x25,0x10,0x22,0x08,0x23,0x08,0x22,0x08,0x16,0x08,0x22,0x20,
0x22,0x08,0x23,0x08,0x22,0x08,0x16,0x08,0x22,0x18,0x23,0x08,0x25,0x10,0x22,0x10,0x23,0x20,
0x22,0x08,0x23,0x08,0x22,0x08,0x21,0x08,0x16,0x08,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x10,0x16,0x10,0x21,0x20,
0x00,0x40,0x23,0x10,0x16,0x10,0x21,0x10,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x22,0x10,0x15,0x20,
0x22,0x10,0x23,0x10,0x25,0x10,0x25,0x08,0x26,0x08,0x25,0x08,0x26,0x08,0x22,0x10,0x23,0x20,
0x22,0x08,0x23,0x08,0x22,0x10,0x21,0x10,0x16,0x10,0x22,0x08,0x23,0x08,0x22,0x10,0x16,0x20,
0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x22,0x0c,0x21,0x0c,0x16,0x10,0x21,0x28,
0x00,0x20,0x21,0x10,0x22,0x10,0x23,0x10,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x26,0x08,0x25,0x08,0x25,0x08,0x23,0x20,
0x15,0x08,0x16,0x08,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x26,0x08,0x25,0x08,0x22,0x08,0x25,0x08,0x23,0x20,
0x22,0x08,0x23,0x08,0x16,0x10,0x22,0x20,0x22,0x08,0x23,0x10,0x25,0x08,0x21,0x20,
0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x23,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x21,0x08,0x22,0x20,
0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x26,0x08,0x25,0x08,0x25,0x08,0x23,0x20,
0x15,0x08,0x16,0x08,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x26,0x08,0x25,0x08,0x22,0x08,0x25,0x08,0x23,0x20,
0x22,0x08,0x23,0x08,0x16,0x10,0x22,0x20,0x22,0x08,0x23,0x10,0x25,0x08,0x26,0x20,
0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x23,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x16,0x08,0x21,0x20};
void PlayorPause()
{
char k1 = GetKeyAct(enumKey1);
if(k1 == enumKeyPress)
{
char state = GetPlayerMode();
if(state == enumModePlay)
SetPlayerMode(enumModePause);
else
SetPlayerMode(enumModePlay);
}
}
void main()
{
DisplayerInit();
Seg7Print(10, 10, 10, 10, 10, 10, 10, 10);
LedPrint(0);
BeepInit();
MusicPlayerInit();
SetMusic(91, 0xFD, sound, sizeof(sound)/sizeof(sound[0]), enumMscDrvSeg7andLed);
SetPlayerMode(enumModePause);
KeyInit();
SetEventCallBack(enumEventKey, PlayorPause);
MySTC_Init();
while(1) {
MySTC_OS();
}
}
8. 温度值计算。
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "displayer.H"
#include "ADC.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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_ADC myADC;
unsigned int rt, temp;
code unsigned int 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, 4, 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};
void my1s_callback(){
myADC = GetADC();
rt = myADC.Rt;
rt = rt >> 2;
temp = tempdata[rt];
if(temp < 0)
Seg7Print(10,10,10,10,10,12,-temp/10%10,-temp%10);
else
Seg7Print(10,10,10,10,10,temp/100%10,temp/10%10,temp%10);
}
void main()
{
AdcInit(ADCexpEXT);
DisplayerInit();
SetDisplayerArea(0,7);
Seg7Print(10,10,10,10,10,10,10,10);
SetEventCallBack(enumEventSys1S, my1s_callback);
MySTC_Init();
while(1) {
MySTC_OS();
}
}
9. 扩展模板。
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "displayer.H"
#include "EXT.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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 dist;
void ShowDistance()
{
dist = GetUltraSonic();
Seg7Print(10, 10, 10, 10, dist/1000, dist/100%10, dist/10%10, dist%10);
}
void main()
{
DisplayerInit();
SetDisplayerArea(0,7);
Seg7Print(10,10,10,10,10,10,10,10);
EXTInit(enumEXTUltraSonic);
SetEventCallBack(enumEventSys1S,ShowDistance);
MySTC_Init();
while(1) {
MySTC_OS();
}
}
10. 直流电机。
#include "STC15F2K60S2.H" //必须。
#include "sys.H" //必须。
#include "Key.H"
#include "EXT.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, 0x76,
/* 序号: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
/* 显示ʾ: 0 1 2 3 4 5 6 7 8 9 (无) 下- 中- 上- 上中- 下中- H */
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
void DC_motor()
{
if (GetKeyAct(enumKey1) == enumKeyPress)
SetPWM(50, 10, 0, 10);
else if (GetKeyAct(enumKey2) == enumKeyPress)
SetPWM(0, 10, 30, 10);
}
void main()
{
EXTInit(enumEXTPWM);
KeyInit();
SetEventCallBack(enumEventKey, DC_motor);
MySTC_Init();
while(1) {
MySTC_OS();
}
}
BSP子功能筛选与实现报告
BSP子功能筛选与实现报告
|
题目名称 |
串口1收发 |
|
编程思路 |
用数组rcvBuffer[10]存储接收的10字节数据,当接收到数据时将数据逆序,仍存储在rcvBuffer[10]中,然后当uart1串口空闲时发送回上位机。 |
|
核心代码 |
unsigned char rcvBuffer[10]; void Uart1_callback() { unsigned int i; unsigned char temp; for (i = 0; i < 5; i++) { temp = rcvBuffer[i]; rcvBuffer[i] = rcvBuffer[9 - i]; rcvBuffer[9 - i] = temp; } while(GetUart1TxStatus() == enumUart1TxBusy) Uart1Print(rcvBuffer, 10); } |
|
题目名称 |
串口2通信 |
|
编程思路 |
发送方:当检测到按键按下时,通过串口 2 发送一组固定的 5 字节数据,同时清零数码管显示;当按键释放时,让数码管显示为空。 接收方:设置串口接收缓冲区,当串口成功接收到 5 字节数据后,进入回调函数,先计算这 5 个字节的和,用 LED 显示该和的低 8 位,同时把和拆成十进制各位数,依次显示在 8 位数码管上。 |
|
核心代码 |
发送方: unsigned char Senddata[5] = {88, 174, 236, 120, 75}; void Uart2RxdCallback() { char k = GetKeyAct(enumKey1); if(k==enumKeyPress) { Uart2Print(Senddata,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(); SetDisplayerArea(0, 7); Seg7Print(10, 10, 10, 10, 10, 10, 10, 10); LedPrint(0); Uart2Init(1200, Uart2Usedfor485); SetEventCallBack(enumEventUart2Rxd, Uart2RxdCallback); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
核心代码 |
接收方: unsigned char Receivedata[5]; int res[8]; void Uart2RxdCallback() { int sum = 0; int i; for(i = 0; i < 5; i++){ sum = sum + Receivedata[i]; } LedPrint(sum); for(i = 0; i < 8; i++){ res[i] = (sum % 10); sum = sum/10; } Seg7Print(res[7],res[6],res[5],res[4],res[3],res[2],res[1],res[0]); } void main() { DisplayerInit(); SetDisplayerArea(0,7); Uart2Init(1200, Uart2Usedfor485); SetUart2Rxd(Receivedata, 5, 0, 0); SetEventCallBack(enumEventUart2Rxd, Uart2RxdCallback); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
题目名称 |
红外无线通信 |
|
编程思路 |
发送方:当检测到按键按下时,通过红外无线通信发送一组固定的 5 字节数据,同时清零数码管显示;当按键释放时,让数码管显示为空。 接收方:设置红外接收缓冲区,当成功接收到 5 字节数据后,进入回调函数,先计算这 5 个字节的和,用 LED 显示该和的低 8 位,同时把和拆成十进制各位数,依次显示在 8 位数码管上。 |
|
核心代码 |
发送方: unsigned char set[1] = {1}; unsigned char Senddata[5] = {88, 174, 236, 120, 75}; void myKey_callback() { char k = GetKeyAct(enumKey1); if(k==enumKeyPress) { IrPrint(Senddata,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(); SetDisplayerArea(0, 7); Seg7Print(10, 10, 10, 10, 10, 10, 10, 10); LedPrint(0); IrInit(NEC_R05d); IrTxdSet(set,1); SetEventCallBack(enumEventKey, myKey_callback); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
核心代码 |
接收方: unsigned char Receivedata[5]; int res[8]; void IrRxdCallback() { int sum = 0; int i; for(i = 0; i < 5; i++){ sum = sum + Receivedata[i]; } LedPrint(sum); for(i = 0; i < 8; i++){ res[i] = (sum % 10); sum = sum/10; } Seg7Print(res[7],res[6],res[5],res[4],res[3],res[2],res[1],res[0]); } void main() { DisplayerInit(); SetDisplayerArea(0,7); IrInit(NEC_R05d); SetIrRxd(Receivedata,5); SetEventCallBack(enumEventIrRxd, IrRxdCallback); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
题目名称 |
实时时钟 |
|
编程思路 |
在系统初始化时设定 DS1302 实时时钟时间,并通过周期性定时回调函数读取 RTC,再经过 BCD 与十进制转换,把当前时分秒实时显示在数码管上,并且实现断电后时钟靠板上的纽扣电池仍能正常走时。 |
|
核心代码 |
unsigned char BCD_to_DEC(unsigned char bcd) { return ((bcd >> 4) * 10) + (bcd & 0x0F); } unsigned char DEC_to_BCD(unsigned char dec) { return ((dec / 10) << 4) + (dec % 10); } void my100mS_callback(void) { struct_DS1302_RTC current_time; unsigned char hour, minute, second; current_time = RTC_Read(); hour = BCD_to_DEC(current_time.hour); minute = BCD_to_DEC(current_time.minute); second = BCD_to_DEC(current_time.second); Seg7Print(hour / 10, hour % 10, 12, minute / 10, minute % 10, 12, second / 10, second % 10); } void main() { struct_DS1302_RTC init_time; init_time.year = DEC_to_BCD(25); init_time.month = DEC_to_BCD(9); init_time.day = DEC_to_BCD(8); init_time.week = DEC_to_BCD(1); init_time.hour = DEC_to_BCD(12); init_time.minute = DEC_to_BCD(0); init_time.second = DEC_to_BCD(0);
MySTC_Init(); DisplayerInit(); DS1302Init(init_time); SetDisplayerArea(0, 7); SetEventCallBack(enumEventSys100mS, my100mS_callback); while (1) { MySTC_OS(); } } |
|
题目名称 |
非易失存储 |
|
编程思路 |
定义NVM内存的起始地址为0,开机后先从NVM地址中读取上一次存储的计数值,并在数码管上显示,然后将计数值+1后存回NVM地址中,实现掉电保存。 |
|
核心代码 |
#define NVM_ADDR 0 unsigned char count = 0; void main() { DisplayerInit(); SetDisplayerArea(0, 7); count = NVM_Read(NVM_ADDR); Seg7Print(0, 0, 0, 0, 0, count/100, (count%100)/10, count%10); count++; NVM_Write(NVM_ADDR, count); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
题目名称 |
收音机 |
|
编程思路 |
定义myRadio的收音机参数设定为91.8MHz,音量6,在main函数中初始化收音机即可。 |
|
核心代码 |
struct_FMRadio myRadio = {918, 6, 0, 0, 0}; unsigned char count = 0; void main() { FMRadioInit(myRadio); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
题目名称 |
音乐播放器 |
|
编程思路 |
将音乐按音高、节拍数写入sound数组,调用SetMusic函数并设置节拍率和音调来播放音乐。一开始处于暂停模式,按一下Key开始播放,再按一下Key1又暂停播放。 |
|
核心代码 |
code unsigned char sound[]={0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x10,0x25,0x10, 0x22,0x08,0x23,0x08,0x22,0x08,0x16,0x08,0x21,0x20,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08, 0x23,0x10,0x25,0x10,0x22,0x08,0x23,0x08,0x22,0x08,0x16,0x08,0x22,0x20,0x22,0x08,0x23,0x08, 0x22,0x08,0x16,0x08,0x22,0x18,0x23,0x08,0x25,0x10,0x22,0x10,0x23,0x20,0x22,0x08,0x23,0x08, 0x22,0x08,0x21,0x08,0x16,0x08,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x10,0x16,0x10,0x21,0x20, 0x15,0x20,0x22,0x10,0x23,0x10,0x25,0x10,0x25,0x08,0x26,0x08,0x25,0x08,0x26,0x08,0x22,0x10, 0x23,0x20,0x22,0x08,0x23,0x08,0x22,0x10,0x21,0x10,0x16,0x10,0x22,0x08,0x23,0x08,0x22,0x10, 0x16,0x20,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x22,0x0c,0x21,0x0c,0x16,0x10,0x21,0x28, 0x00,0x20,0x21,0x10,0x22,0x10,0x23,0x10,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x22,0x08, 0x23,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x26,0x08,0x25,0x08,0x25,0x08,0x23,0x20,0x15,0x08, 0x16,0x08,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x26,0x08,0x25,0x08, 0x22,0x08,0x25,0x08,0x23,0x20,0x22,0x08,0x23,0x08,0x16,0x10,0x22,0x20,0x22,0x08,0x23,0x10, 0x25,0x08,0x21,0x20,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x23,0x08, 0x21,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x21,0x08,0x22,0x20,0x22,0x08,0x23,0x08,0x22,0x08, 0x23,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x23,0x08,0x25,0x08,0x26,0x08,0x25,0x08,0x25,0x08, 0x23,0x20,0x15,0x08,0x16,0x08,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x25,0x08, 0x26,0x08,0x25,0x08,0x22,0x08,0x25,0x08,0x23,0x20,0x22,0x08,0x23,0x08,0x16,0x10,0x22,0x20, 0x22,0x08,0x23,0x10,0x25,0x08,0x26,0x20,0x15,0x08,0x16,0x08,0x21,0x08,0x22,0x08,0x23,0x08, 0x25,0x08,0x23,0x08,0x21,0x08,0x22,0x08,0x23,0x08,0x22,0x08,0x16,0x08,0x21,0x20}; void PlayorPause() { char k1 = GetKeyAct(enumKey1); if(k1 == enumKeyPress) { char state = GetPlayerMode(); if(state == enumModePlay) SetPlayerMode(enumModePause); else SetPlayerMode(enumModePlay); } } void main() { DisplayerInit(); Seg7Print(10, 10, 10, 10, 10, 10, 10, 10); LedPrint(0); BeepInit(); MusicPlayerInit(); SetMusic(91, 0xFD, sound, sizeof(sound)/sizeof(sound[0]), enumMscDrvSeg7andLed); SetPlayerMode(enumModePause); KeyInit(); SetEventCallBack(enumEventKey, PlayorPause); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
题目名称 |
温度值计算 |
|
编程思路 |
用ADC模块测量温度,得到的ADC值存在rt中,由于ADC值有10位,所以要先取低8位(0-255),然后查tempdata表得到对应的实际温度值,最后用数码管显示。 |
|
核心代码 |
struct_ADC myADC; unsigned int rt, temp; code unsigned int 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, 4, 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}; void my1s_callback(){ myADC = GetADC(); rt = myADC.Rt; rt = rt >> 2; temp = tempdata[rt]; if(temp < 0) Seg7Print(10,10,10,10,10,12,-temp/10%10,-temp%10); else Seg7Print(10,10,10,10,10,temp/100%10,temp/10%10,temp%10); } void main() { AdcInit(ADCexpEXT); DisplayerInit(); SetDisplayerArea(0,7); Seg7Print(10,10,10,10,10,10,10,10); SetEventCallBack(enumEventSys1S, my1s_callback); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
题目名称 |
扩展模板 |
|
编程思路 |
利用扩展接口实现超声波测距,在 1 秒回调函数中调用GetUltraSonic函数读取测量值,再把结果实时显示在数码管上。 |
|
核心代码 |
int dist; void ShowDistance() { dist = GetUltraSonic(); Seg7Print(10, 10, 10, 10, dist/1000, dist/100%10, dist/10%10, dist%10); } void main() { DisplayerInit(); SetDisplayerArea(0,7); Seg7Print(10,10,10,10,10,10,10,10); EXTInit(enumEXTUltraSonic); SetEventCallBack(enumEventSys1S,ShowDistance); MySTC_Init(); while(1) { MySTC_OS(); } } |
|
题目名称 |
直流电机 |
|
编程思路 |
利用扩展接口控制直流电机的转向和转速,按下Key1让直流电机以50%速度,100Hz频率正转,因此设置PWM2=50,freq2=1000/100=10;按下Key2让直流电机以30%速度,100Hz频率反转,因此设置PWM1=30,freq1=1000/100=10. |
|
核心代码 |
void DC_motor() { if (GetKeyAct(enumKey1) == enumKeyPress) SetPWM(50, 10, 0, 10); else if (GetKeyAct(enumKey2) == enumKeyPress) SetPWM(0, 10, 30, 10); } void main() { EXTInit(enumEXTPWM); KeyInit(); SetEventCallBack(enumEventKey, DC_motor); MySTC_Init(); while(1) { MySTC_OS(); } } |
363

被折叠的 条评论
为什么被折叠?



