Altium Designer画的原理图和PCB图如下:(51hei附件中可下载工程文件)
制作出来的实物图如下:
单片机源程序如下:
- #include "includes.h"
- #include "API.h"
- #define X_RUN 0X01
- #define X_ADD 0X02
- #define X_SUB 0X04
- #define X_SHT 0X08
- #define Y_M1 0X01
- #define Y_M2 0X02
- #define Y_M3 0X04
- #define Y_EN 0X08
- #define Y_TQ 0X10
- #define Y_DIR 0X20
- #define Y_LAT 0X40
- uint_16 LastKeyValue,PreKeyValue,KeyValue;
- uint_8 SegData[6];
- uint_8 DivisionNum=16;
- void SegDataConver(uint_16 data)
- {
- uint_8 i;
- for(i=0;i<6;i++){
- SegData[5-i]=data%10;
- data/=10;
- }
- }
- void IO_New (void)
- {
- uint_16 i;
- LastKeyValue=PreKeyValue;
- DIN_Module.NewValue=IO_Read();
- for(i=0;i<DIN_NUM;i++){
- if( ( DIN_Module.NewValue&(1<<i) )!= ( DIN_Module.OldValue&(1<<i) ) ){
- DIN_Module.Timer[i]=20;//DIN_Module.FilterTime;
- if( DIN_Module.NewValue&(1<<i) ){
- DIN_Module.OldValue|= (1<<i);
- }else{
- DIN_Module.OldValue&=~(1<<i);
- }
- }else{
- if(DIN_Module.Timer[i]==0){
- if( DIN_Module.NewValue&(1<<i) ){
- DIN_Module.Value|= (1<<i);
- }else{
- DIN_Module.Value&=~(1<<i);
- }
- }
- }
- }
- PreKeyValue=DIN_Module.Value;
- //----------------------------
- KeyValue=0;
- for(i=0;i<4;i++){
- if( ((PreKeyValue&(1<<i))!=0) && ((LastKeyValue&(1<<i))==0) ){
- KeyValue|=(1<<i);
- }
- }
- //----------------------------
- }
- int main(void)
- {
- uint_16 MotorSpeed=0;
- uint_8 MotorState=0;
- uint_16 LastSpeed;
- uint_32 Period;
- uint_16 MotorCmd=0;
- MotorCmd|=Y_M1;
- MotorCmd|=Y_M2;
- MotorCmd&=~Y_M3;
- MotorCmd&=~Y_LAT;
- MotorCmd&=~Y_TQ;
- //1. 声明主函数使用的变量
- // uint_8 sentdata; //定义变量,用来存储SPI0要发送的数据,并赋初值为A。
- // uint_32 i;
- //2. 关总中断
- DISABLE_INTERRUPTS;
- light_init(RUN_LIGHT_BLUE,LIGHT_OFF);
- light_init(RUN_LIGHT_RED,LIGHT_OFF);
- light_init(RUN_LIGHT_GREEN,LIGHT_OFF);
- systick_init(CORE_CLOCK_DIV_16,1);//1ms
- //3. 初始化外设模块
- SPI_init(SPI_1,0,6000); //把SPI_1初始化为主机。
- IO_ReadWriteInit();
- //4. 给有关变量赋初值
- //5. 使能模块中断
- //6. 开总中断
- // pwm_init(TPM0,TPMCH0,100,50);
- ENABLE_INTERRUPTS;
- //进入主循环
- //主循环开始=============================================================
- while(1){
- if( TimerFlag&FLAG_500MS ){
- light_control(RUN_LIGHT_BLUE,LIGHT_ON);
- light_control(RUN_LIGHT_GREEN,LIGHT_ON);
- light_control(RUN_LIGHT_RED,LIGHT_ON);
- }else{
- light_control(RUN_LIGHT_BLUE,LIGHT_OFF);
- light_control(RUN_LIGHT_GREEN,LIGHT_OFF);
- light_control(RUN_LIGHT_RED,LIGHT_OFF);
- }
- if(TimerFlag&FLAG_10MS){
- IO_New();
- if(MotorState==0){
- if(KeyValue&X_RUN){
- MotorSpeed=100;
- MotorState=1;
- MotorCmd|=Y_EN;
- }
- }else{
- if(KeyValue&X_RUN){
- MotorSpeed=0;
- MotorState=0;
- MotorCmd&=~Y_EN;
- }
- if(KeyValue&X_ADD){
- MotorSpeed+=20;
- if(MotorSpeed>=500){
- MotorSpeed=500;
- }
- }
- if(KeyValue&X_SUB){
- MotorSpeed-=20;
- if(MotorSpeed<=50){
- MotorSpeed=50;
- }
- }
- if(KeyValue&X_SHT){
- MotorCmd^=Y_DIR;
- }
- }
- if(MotorSpeed!=0){
- if(MotorSpeed!=LastSpeed){
- Period=100000*3/(MotorSpeed*DivisionNum);
- // Period=MotorSpeed*10*DivisionNum/3;
- pwm_init(TPM0,TPMCH0,Period,70);
- }
- }
- IO_Write(MotorCmd);
- SegDataConver(MotorSpeed);
- LastSpeed=MotorSpeed;
- TimerFlag&=~FLAG_10MS;
- }
- }//主循环end_for
- //主循环结束==================================================================
- return 0;
- }
复制代码
所有资料51hei提供下载:
StepMotorController-V10.7z
(7.6 MB, 下载次数: 208)
|