PWM control program of stepper motor controlled by single chip microcomputer

Publisher:Meilin8888Latest update time:2016-09-07 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
PWM control program written in C51 language for 8051 microcontroller to control stepper motor

#include  
#include  
#include  
#include "UPSD3200.H" 
#include "upsd_pwm.h" 
#define uint unsigned int 
#define uchar unsigned char 
PSD_REGS PSD8xx_reg _at_ csiop; 
#define KEYIO (~(PSD8xx_reg.DATAIN_A)) & 0x07 
sbit CP=P4^7; 
sbit U_D=P1^1; 
void init_system(); // 系统初始化 
uchar keyboard(); 
void uPSD_PWM4_Variable(unsigned char PWM_Period, unsigned char PWM_PulseWidth); 
uchar Adjust_station=NO; 
unsigned char data x,flag; 
unsigned char keyboard() //键盘输入 

unsigned char a=0xff,b=0x01,c=0xf8,d;//有按键的位置 
uint i; 
do{ 
a=KEYIO|c; 
}while(a==0xff); 
if (a == 0xfe){d = '1';} 
else if (a == 0xfd){d = '2';} 
else if (a == 0xfb){d = '3';} 
else {d = '0';} 
return(d); 

void uPSD_PWM4_Init(void) 
unsigned int PWM_prescaler; 
P4SFS |= 0x80; 
PWM_prescaler = (unsigned int)((FREQ_OSC/2)/PWM4_INPUT_freq);//PWM4输入脉冲频率 K

PSCL1L = PWM_prescaler & 0x00ff; 
PSCL1H = (PWM_prescaler >> 8); 

void StepMotor_GO(bit direction,uint speed) { 
unsigned char PWM_Period, PWM_PulseWidth; 
uint temp_uint; 
U_D = direction;//方向 
temp_uint = (PWM4_INPUT_freq*1000)/speed; 
PWM_Period = (unsigned char)temp_uint; 
PWM_PulseWidth = PWM_Period/2; 
uPSD_PWM4_Variable(PWM_Period,PWM_PulseWidth); 

void StepMotor_STOP(void) 

P4SFS &= 0x7f; 

void uPSD_PWM4_Variable(unsigned char PWM_Period, unsigned char PWM_PulseWidth) 

P4SFS |= 0x80; 
PWMVP = PWM_Period; 
PWMVPW = PWM_PulseWidth; 
PWMCON |= 0x20; 

void initkey(void){ 
PSD8xx_reg.DATAOUT_A=0xFF; 
PSD8xx_reg.CONTROL_A=0x00; 
PSD8xx_reg.DIRECTION_A=0x00; 

void init_system() // 系统初始化 

WDKEY=0x55; 
P4SFS = (unsigned char) (0x08 << 3); 
uPSD_PWM_Channel_8bit(3,243); 
问题补充:(接着上面的序) 
void uPSD_PWM_Init_8bit(unsigned char PWM_channel_no, unsigned int PWM_freq8, unsigned char PWMCON_value) 

unsigned int PWM_prescaler; 
unsigned char output_polarity_mode; 
output_polarity_mode = PWMCON_value & 0x8F; 
PWMCON = (PWMCON & 0x70) | output_polarity_mode; 
P4SFS = (unsigned char) (0x08 << PWM_channel_no); 
PWM_prescaler = (unsigned int) ( (((unsigned int) FREQ_OSC) / 2) / PWM_freq8); 
PSCL0L = PWM_prescaler & 0x00ff; 
PSCL0H = (PWM_prescaler >> 8); 
PSCL1L = PWM_prescaler & 0x00ff; 
PSCL1H = (PWM_prescaler >> 8); 

void uPSD_PWM_Disable(void) 

PWMCON &= 0xDF; 

void main(void){ 
x=0; 
init_system(); 
flag=1; 
while(flag==1){ 
x=keyboard(); 
if(x=='1') {StepMotor_GO(0,100);} 
else if(x=='2') {StepMotor_GO(1,500);} 
else if(x=='3') { StepMotor_STOP();uPSD_PWM_Disable();} 
else if(x=='0') {uPSD_PWM_Disable();} 

}

Reference address:PWM control program of stepper motor controlled by single chip microcomputer

Previous article:nRF2401 learning board---AD acquisition and wireless transmission
Next article:C51 implements calendar, temperature and alarm functions

Recommended ReadingLatest update time:2024-11-16 14:34

89C2051 stepper motor driver circuit and source code, Stepper motor driver
program stepper.c stepper.hex /* * STEPPER.C * sweeping stepper's rotor cw and cww 400 steps * Copyright (c) 1999 by W.Sirichote */ #include c:mc518051io.h /* include i/o header file */ #include c:mc518051reg.h register unsigned char j,flag1,temp; register unsigned int cw_n,ccw_n; unsigned char step ={0x80,0xc0,0x40,0
[Microcontroller]
89C2051 stepper motor driver circuit and source code, Stepper motor driver
Infineon Launches PSOC™ Control MCU Family for Motor Control and Power Conversion in Industrial and Consumer Applications
Infineon Technologies AG recently launched a new PSOC™ Control microcontroller (MCU) series . This series is suitable for the next generation of industrial and consumer motor control and power conversion system applications, including home appliances, power tools, renewable energy products, industrial drive
[Industrial Control]
Infineon Launches PSOC™ Control MCU Family for Motor Control and Power Conversion in Industrial and Consumer Applications
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号