HNU小学期BSP软件编程基础十道测试题

打印 上一主题 下一主题

主题 1012|帖子 1012|积分 3036

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
http://t.csdnimg.cn/Yv0R1 文章参考了这位大佬的代码,在他的基础上举行了纠错、完善等处置惩罚。
设置

编程前的准备工作按大佬的流程即可,稍有差别的是学习通课程网站的资料里没有头文件的整个压缩包了,但我们可以下载某个BSP版的工程文件,并在里面找到inc文件夹,里面包含所有头文件。


后面把这个文件夹复制到你想要的地方,然后设置的时候引用这个路径就好。

库文件也在刚刚的BSP版的工程文件里面的source里,选择全部文件范例才能看到库文件STCBSP_V3.6.LIB。
差别之处就以上两点,别的的按大佬文中的设置流程即可。

测试题(更新完成)

1、Display: 在数码管指定位置体现指定内容。编写程序,在数码管体现"  12HL- 21" 。

  1. #include "STC15F2K60S2.H"
  2. #include "displayer.H"
  3. #include "sys.H"
  4. code unsigned long SysClock=11059200;         //必须。 (定义系统工作时钟频率,用户可以修改,且必须与实际工作频率(下载时选择的)一致
  5. #ifdef _displayer_H_                          //显示模块选用时必须。(数码管显示译码表,用戶可修改、增加等)
  6. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x77,0x7c,0x39,0x5e,0x79,0x78,0x76,0x38,0x54,0x5c,0x73,0x67,0x3e,0x6e};  
  7.                       /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27 */
  8.                   /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-    中-   上-   A    b    C    d    E    F    H    L    n    o    P    q    U    y */  
  9. #endif
  10. void main()
  11. {                                 
  12.        
  13.         displayerInit();                              
  14.         LedPrint(0);
  15.     SetDisplayerArea(0,6);                           
  16.         Seg7Print(1,2,20,21,12,2,1);         
  17.         MySTC_Init();
  18.         while(1)
  19.                 MySTC_OS();
  20. }
复制代码
按老师要求加上了其他一些字母的编码,修改在decode_table[]中。

2、按键:按下K1,数码管灯体现"H"。松开后灭掉。


  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "displayer.H"            
  4. #include "key.H"
  5. code unsigned long SysClock=11059200;
  6. #ifdef _displayer_H_                          
  7. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
  8.                  /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15  */
  9.                  /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-  中-  上-   H    L  */  
  10. #endif
  11. char a=10;
  12. void myKey_callback()
  13. {   char k;
  14.         k=GetKeyAct(enumKey1);
  15.         if( k == enumKeyPress ) a=14;
  16.           else if( k == enumKeyRelease ) a=10;
  17.         Seg7Print(10,10,10,10,10,10,10,a);
  18. }
  19. //void my10mS_callback(){
  20. //Seg7Print(10,10,10,10,10,10,10,a);}
  21. void main(){
  22.          DisplayerInit();   
  23.          KeyInit();
  24.          LedPrint(0);
  25.          SetDisplayerArea(0,7);
  26.          //SetEventCallBack(enumEventSys10mS, my10mS_callback);
  27.          SetEventCallBack(enumEventKey, myKey_callback);
  28.          MySTC_Init();
  29.          while(1){
  30.                  MySTC_OS();
  31.          }
  32. }
复制代码
在系统变乱10ms即SetEventCallBack(enumEventSys10mS, my10mS_callback)那里举行了修改,去掉了那句语句,对应的处置惩罚函数也可以去掉。由于系统变乱10ms会耗费系统资源,不如只用按键变乱,并对按键变乱处置惩罚函数稍作修改即可。
同时注意,当前版本按键初始化KeyInit();中间没有下横线_

3、蜂呜器:开机后发出指定频率。开机后发出1.2KHz 1.8S的声音。

  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "beep.H"
  4. code unsigned long SysClock=11059200;
  5. int main(){
  6.        
  7.         BeepInit();
  8.         SetBeep(1200,180);
  9.         MySTC_Init();
  10.         while(1)
  11.                 MySTC_OS();
  12. }
复制代码
暂无修改。
SetBeep(1200,180);语句第一个参数指定发声频率,1200表示1.2KHz;第二个参数指定发声时长,发声时长=第二个参数×10,单元为ms,因此发声1.8s时第二个参数为180。更多细节可以看学习板说明文档。

4、音乐:开机后A调,每分钟100拍,播放1、2、3、4、5 (给简谱)。

  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "displayer.H"
  4. #include "Beep.H"
  5. #include "music.H"
  6. code unsigned long SysClock=11059200;
  7. #ifdef _displayer_H_
  8. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
  9.                  /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15  */
  10.                  /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-  中-  上-   H    L  */  
  11. #endif
  12. code unsigned char arr[]={
  13.         0x31,0x10,
  14.         0x32,0x10,
  15.         0x33,0x10,
  16.         0x34,0x10,
  17.         0x35,0x10};
  18. int main(){
  19.           DisplayerInit();
  20.           LedPrint(0);
  21.           SetDisplayerArea(0,7);
  22.           BeepInit();
  23.           MusicPlayerInit();
  24.           SetPlayerMode(enumModePlay);
  25.           SetMusic(100,0xFA,arr,10,enumMscDrvSeg7andLed);
  26.           MySTC_Init();
  27.           while(1){
  28.                         MySTC_OS();
  29.                 }
  30.        
  31. }
复制代码
增加了用7段数码管Seg7体现信息和用Led指示灯打拍的功能。
记得添加dispalyer模块,并且指定数码管范围即语句SetDisplayerArea(0,7);
数码管左侧体现每分钟拍数;右侧横线前的数字表示音调,如1代表A调等;横线后数字代表当前音是do、re、mi、fa、so、la、si中的哪一个,如1代表do等。
修改SetMusic(100,0xFA,arr,10,enumMscDrvSeg7andLed);的末了一个参数即可设置是否体现,具体可以看学习板说明文档。

5、秒表:数码管体现分、秒、毫秒。


  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "displayer.H"  
  4. code unsigned long SysClock=11059200;
  5. #ifdef _displayer_H_
  6. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};   
  7.                 /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15  */
  8.                 /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-  中-  上-   H    L  */  
  9. #endif
  10. long min=0,sec=0,ms=0,temp=0;
  11. char oput[8]={0};
  12. void fun(){
  13.         temp++;
  14.         ms=temp;
  15.         sec=(temp/1000)%60;
  16.         min=temp/60000;
  17.         oput[0]=ms%10;
  18.         oput[1]=(ms/10)%10;
  19.         oput[2]=(ms/100)%10;
  20.         oput[3]=12;
  21.         oput[4]=sec%10;
  22.         oput[5]=(sec/10)%10;
  23.         oput[6]=12;
  24.         oput[7]=min;
  25.         Seg7Print(oput[7],oput[6],oput[5],oput[4],oput[3],oput[2],oput[1],oput[0]);
  26. }
  27. int main(){
  28.         DisplayerInit();        
  29.         SetDisplayerArea(0,7);
  30.         Seg7Print(0,0,0,0,0,0,0,0);
  31.         LedPrint(0);
  32.         SetEventCallBack(enumEventSys1mS, fun);
  33.         MySTC_Init();
  34.         while(1)
  35.                 MySTC_OS();
  36. }
复制代码
大佬的原代码中,sec=(temp/1000)%60000;这一行不太对,一分钟是60s,因此temp除了1000以后应该是对60取余而不是60000。
别的,大佬代码中的if(sec>59) sec=sec-60;也不对,由于上面他的代码是取余60000,以是sec可能会比59大,因此须要用到这个if。但是这又带来了另一个问题,就是如果上面盘算得到的sec>=120的话,好比sec=121,if语句也只减了一个60,会导致数码管体现秒的部分在2分钟后变为从60开始,61、62、63...而不是从00开始,这也是个bug。
因此,简化代码即可。对应代码改为sec=(temp/1000)%60;并把后面的if语句删去,即可得到精确的逻辑。
由于数码管位数限制,这个程序只能体现10分钟以内的计时。

6、步进电机: 2号电机正转100步(速率:每秒10步)。

  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "displayer.H"
  4. #include "stepmotor.h"
  5. code unsigned long SysClock=11059200;
  6. #ifdef _displayer_H_
  7. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};   
  8.                      /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15  */
  9.                  /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-  中-  上-   H    L  */
  10. #endif
  11. int main(){
  12.        
  13.           DisplayerInit();
  14.       StepMotorInit();  
  15.           SetDisplayerArea(0,7);
  16.           Seg7Print(10,10,10,10,10,10,10,10);
  17.           LedPrint(0);
  18.           SetStepMotor(enumStepMotor2,10,100);
  19.           MySTC_Init();
  20.           while(1)
  21.                   MySTC_OS();
  22.        
  23. }
复制代码
暂无修改。
SetStepMotor(enumStepMotor2,10,100);语句第一个参数指定使用2号步进电机:此时,用L0~L3四个LED模拟一个4相步进电机;第二个参数指定速率,单元步/s;第三个参数指定转动步数,正值表示正转,负值表示反转。更多细节可以看学习板说明文档。

7、震动:震动后,蜂鸣器发出Hz频率声音。

  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "beep.H"   
  4. #include "Vib.h"
  5. code unsigned long SysClock=11059200;
  6. void mysv_callback()
  7. {
  8.         if(GetVibAct()==enumVibQuake)
  9.                 SetBeep(1200,100);
  10. }
  11. void main(){
  12.        
  13.         BeepInit();
  14.         VibInit();
  15.         SetEventCallBack(enumEventVib, mysv_callback);
  16.         MySTC_Init();
  17.         while(1)
  18.                 MySTC_OS();
  19.        
  20. }
复制代码
暂无修改。
当Vib检测到有振动发生时,将产生一个“振动变乱”enumEventVib,而SetEventCallBack(enumEventVib, mysv_callback);语句设置振动变乱用户处置惩罚函数,即发生振动变乱时,执行函数mysv_callback(),蜂鸣器发出1.2KHz,1s的声音。

8、霍尔:磁场接近时,4个LED向左流水。

  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "hall.H"
  4. #include "displayer.H"
  5. code unsigned long SysClock=11059200L;
  6. #ifdef _displayer_H_                        
  7. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};   
  8.                        /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15  */
  9.                    /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-  中-  上-   H    L  */
  10. #endif
  11. char ch[8]={0x0f,0x1e,0x3c,0x78,0xf0,0xe1,0xc3,0x87};
  12. int sum=0;
  13. char flag=0;
  14. void my100mS_callback(){
  15.         if(flag){
  16.                 if(sum==8) {
  17.                 sum=0;
  18.                 }
  19.                 LedPrint(ch[sum]);
  20.                 sum++;
  21.         }
  22.         else{
  23.                 LedPrint(0);
  24.                 sum=0;
  25.         }
  26.        
  27. }
  28. void myhall(){
  29.         if(GetHallAct()==enumHallGetClose){
  30.                 flag=1;
  31.         }
  32.         else{
  33.                 flag=0;
  34.         }               
  35. }
  36. void main(){
  37.         DisplayerInit();  
  38.         HallInit();
  39.         SetDisplayerArea(0,7);
  40.         Seg7Print(10,10,10,10,10,10,10,10);
  41.         LedPrint(0);
  42.         SetEventCallBack(enumEventSys100mS, my100mS_callback);
  43.         SetEventCallBack(enumEventHall, myhall);
  44.         MySTC_Init();
  45.         while(1)
  46.                 MySTC_OS();
  47. }
复制代码
大佬的代码是一个LED灯在L0~L3之间循环流动,这种理解方式所写代码可参考本文开头列出的文章。
而我的理解是四个灯一起向左流水移动。我按照自己的理解写了这个程序。ch数组控制着LED灯的亮灭。
磁铁靠近时,四个LED灯会向左循环流水;磁铁脱离时,LED灯会熄灭,并且下次磁铁靠近时,再次从最右端向左流水。

9、AD:左右导航按键调节初始状态在中间2个LED灯位置。

  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "displayer.H"
  4. #include "key.H"
  5. #include "adc.h"  
  6. code unsigned long SysClock=11059200;
  7. #ifdef _displayer_H_
  8. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};   
  9.                      /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15  */
  10.                  /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-  中-  上-   H    L  */
  11. #endif
  12. unsigned char temp[]={0xc0,0x60,0x30,0x18,0x0c,0x06,0x03,0x81};
  13. int i=3;
  14. void mydh_callback()
  15. {
  16.         if(GetAdcNavAct(enumAdcNavKeyLeft)==enumKeyPress)
  17.         {
  18.                 if(i>0)
  19.                         i--;
  20.                 else i=7;
  21.         }
  22.         else if(GetAdcNavAct(enumAdcNavKeyRight)==enumKeyPress)
  23.         {
  24.                 if(i<7)
  25.                         i++;
  26.                 else i=0;
  27.         }
  28.         LedPrint(temp[i]);
  29. }
  30. void main(){
  31.         DisplayerInit();
  32.         KeyInit();
  33.         AdcInit(ADCexpEXT);
  34.         SetDisplayerArea(0,7);
  35.         LedPrint(temp[3]);
  36.         Seg7Print(10,10,10,10,10,10,10,10);
  37.         SetEventCallBack(enumEventNav, mydh_callback);
  38.         MySTC_Init();
  39.         while(1)
  40.                 MySTC_OS();
  41. }
复制代码
在大佬的代码上稍作了修改,起首将SetEventCallBack(enumEventNav, mydh_callback);语句中的变乱换成了专门的导航按键变乱enumEventNav。
别的,现在移到边界以后还可以继承移动到另一头,具体可以自己执行试试。

10、AD:光照值小于20,蜂鸣器发1.2KHz频率声音。光照值大于100,蜂鸣器发2KHz频率声音。

  1. #include "STC15F2K60S2.H"
  2. #include "sys.H"
  3. #include "displayer.H"
  4. #include "beep.h"
  5. #include "adc.h"
  6. code unsigned long SysClock=11059200;
  7. #ifdef _displayer_H_
  8. code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};   
  9.                      /* 序号:   0    1    2    3    4    5    6    7    8    9    10     11         12   13   14   15  */
  10.                  /* 显示:   0    1    2    3    4    5    6    7    8    9    (无)   下-  中-  上-   H    L  */
  11. void Rop_callback(){
  12.         if(GetADC().Rop<20)
  13.                 SetBeep(1200,150);
  14.         else if(GetADC().Rop>100){
  15.                 SetBeep(2000,150);
  16.         }
  17. }
  18. void Per100ms_displayer(){
  19.         char gewei = (GetADC().Rop % 10);
  20.     char shiwei = ((GetADC().Rop) / 10) % 10;
  21.     char baiwei = ((GetADC().Rop) / 100) % 10;
  22.         if(GetADC().Rop>100){
  23.                 Seg7Print(10,10,10,10,10,baiwei,shiwei,gewei);
  24.         }
  25.         else{
  26.                 Seg7Print(10,10,10,10,10,10,shiwei,gewei);
  27.         }
  28. }
  29. int main(){
  30.         DisplayerInit();
  31.         BeepInit();
  32.         AdcInit();
  33.         SetDisplayerArea(0,7);
  34.         Seg7Print(10,10,10,10,10,10,10,10);
  35.         LedPrint(0);
  36.         SetEventCallBack(enumEventSys100mS, Per100ms_displayer);
  37.         SetEventCallBack(enumEventXADC, Rop_callback);
  38.     MySTC_Init();
  39.     while(1){
  40.      MySTC_OS();
  41.         }               
  42. }
复制代码
在原代码的基础上增加了数码管体现当前光照信息的功能。
注意Seg7Print()函数的参数是char型的。
额外用了100ms系统变乱enumEventSys100mS以及对应的用户处置惩罚函数来在数码管上体现信息,这是由于如果直接在ADC转换处置惩罚函数里来体现信息的话,数值变化就太快了,体现不清。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

盛世宏图

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表