前言
记录学习,仅供参考
编程前的配置工作
先把keil的文件夹加到STC-ISP中 然后可以在keil中建工程了 先新建一个文件夹放工程文件 输入工程的名字点击保存。 这里选择第二个 板子的型号选择正确,后面弹出来的选择是
现在工程就建好了,然后要加入必要的库文件。 点击这里
勾选生成hex文件
因为我们用C语言写代码,所以要加入相关的头文件。如图
这里要加的东西再学习通课程网站的资料里 下载解压好,上面图里要用到这个路径
这里如图选择好,一路点击确定。
这里新建一个文件。先保存,保存到新建工程的那个文件夹里。命名后缀要是.c
这里点开Target,右键点击Source Group 点击Manage
要加入刚保存的c文件和库文件,库文件在刚在课程网站下载的文件里面,里面的source里,选择全部文件类型才能看到库文件。记得只用点击一下add就行了,之后点close,OK。
![在这里插入图片描述](https://img-blog.csdnimg.cn/d4b410a7de1047f88e4947c390358ce3.png
可以看到都加进来了,现在准备工作就做好了,可以开始开心地写程序了。 每次建工程这些都要完成一遍,不是设置一编就好
写完之后点击这里编译
下面解决几道练习题
#include "STC15F2K60S2.H"
#include "displayer.H"
#include "sys.H"
code unsigned long SysClock=11059200L;
#ifdef _displayer_H_
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
#endif
void main()
{
displayerInit();
LedPrint(0);
SetDisplayerArea(0,6);
Seg7Print(1,2,14,15,12,2,1);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "sys.H"
#include "STC15F2K60S2.H"
#include "beep.H"
code unsigned long SysClock=11059200L;
int main(){
BeepInit();
SetBeep(1200,180);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "sys.H"
#include "STC15F2K60S2.H"
#include "music.H"
#include "Beep.H"
#define uchar unsigned char
code unsigned long SysClock=11059200L;
code uchar arr[]={
0x31,0x10,
0x32,0x10,
0x33,0x10,
0x34,0x10,
0x35,0x10};
int main(){
MusicPlayerInit();
BeepInit();
SetPlayerMode(enumModePlay);
SetMusic(100,0xFA,arr,10,enumMscNull);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "STC15F2K60S2.H"
#include "sys.H"
#include "stepmotor.h"
#include "displayer.H"
code unsigned long SysClock=11059200L;
#ifdef _displayer_H_
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
#endif
int main(){
StepMotorInit();
DisplayerInit();
SetDisplayerArea(0,7);
Seg7Print(10,10,10,10,10,10,10,10);
LedPrint(0);
SetStepMotor(enumStepMotor2,10,100);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "STC15F2K60S2.H"
#include "sys.H"
#include "displayer.H"
#include "key.H"
code unsigned long SysClock=11059200L;
#ifdef _displayer_H_
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
#endif
char a=10;
void myKey_callback()
{ char k;
k=GetKeyAct(enumKey1);
if( k == enumKeyPress ) a=14;
else if( k == enumKeyRelease ) a=10;
}
void my10mS_callback(){
Seg7Print(10,10,10,10,10,10,10,a);}
void main(){
DisplayerInit();
Key_Init();
SetDisplayerArea(0,7);
LedPrint(0);
SetEventCallBack(enumEventSys10mS, my10mS_callback);
SetEventCallBack(enumEventKey, myKey_callback);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "STC15F2K60S2.H"
#include "sys.H"
#include "beep.H"
#include "Vib.h"
code unsigned long SysClock=11059200L;
void mysv_callback()
{
if(GetVibAct()==enumVibQuake)
SetBeep(1200,100);
}
void main(){
BeepInit();
VibInit();
SetEventCallBack(enumEventVib, mysv_callback);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "STC15F2K60S2.H"
#include "sys.H"
#include "hall.H"
#include "displayer.H"
code unsigned long SysClock=11059200L;
#ifdef _displayer_H_
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
#endif
unsigned char ch=0x01;
char flag=0;
void my100mS_callback(){
if(flag){
if(ch<16) {
LedPrint(ch);
ch=ch<<1;
}
else
ch=0x01;
}
}
void myhall(){
if(GetHallAct()==enumHallGetClose)
flag=1;
else
flag=0;
}
void main(){
DisplayerInit();
HallInit();
SetDisplayerArea(0,7);
Seg7Print(10,10,10,10,10,10,10,10);
LedPrint(0);
SetEventCallBack(enumEventSys100mS, my100mS_callback);
SetEventCallBack(enumEventHall, myhall);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "STC15F2K60S2.H"
#include "sys.H"
#include "adc.h"
#include "displayer.H"
#include "key.H"
code unsigned long SysClock=11059200L;
#ifdef _displayer_H_
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
#endif
unsigned char temp[]={0xc0,0x60,0x30,0x18,0x0c,0x06,0x03};
int i=3;
void mydh_callback()
{
if(GetAdcNavAct(enumAdcNavKeyLeft)==enumKeyPress)
{
if(i>0)
i--;
}
else if(GetAdcNavAct(enumAdcNavKeyRight)==enumKeyPress)
{
if(i<6)
i++;
}
LedPrint(temp[i]);
}
void main(){
DisplayerInit();
Key_Init();
AdcInit(ADCexpEXT);
SetDisplayerArea(0,7);
LedPrint(temp[3]);
Seg7Print(10,10,10,10,10,10,10,10);
SetEventCallBack(enumEventXADC, mydh_callback);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "STC15F2K60S2.H"
#include "sys.H"
#include "displayer.H"
code unsigned long SysClock=11059200L;
#ifdef _displayer_H_
code char decode_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x08,0x40,0x01,0x76,0x38};
#endif
long ms=0,min=0,sec=0,temp=0;
char oput[8]={0};
void fun(){
temp++;
ms=temp;
sec=(temp/1000)%60000;
min=temp/60000;
if(sec>59)
sec=sec-60;
oput[0]=ms%10;
oput[1]=(ms/10)%10;
oput[2]=(ms/100)%10;
oput[3]=12;
oput[4]=sec%10;
oput[5]=(sec/10)%10;
oput[6]=12;
oput[7]=min;
Seg7Print(oput[7],oput[6],oput[5],oput[4],oput[3],oput[2],oput[1],oput[0]);
}
int main(){
DisplayerInit();
SetDisplayerArea(0,7);
Seg7Print(1,1,1,1,1,1,1,1);
LedPrint(0);
SetEventCallBack(enumEventSys1mS, fun);
MySTC_Init();
while(1)
MySTC_OS();
}
#include "STC15F2K60S2.H"
#include "sys.H"
#include "adc.h"
#include "beep.h"
code unsigned long SysClock=11059200L;
void Rop_callback(){
if(GetADC().Rop<20)
SetBeep(1200,150);
else if(GetADC().Rop>100)
SetBeep(2000,150);
}
int main(){
BeepInit();
AdcInit();
SetEventCallBack(enumEventXADC, Rop_callback);
MySTC_Init();
while(1)
MySTC_OS();
}
后记
本人测试问题不大,仅供参考,如果代码出错欢迎评论区交流
|