找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1465|回复: 1
收起左侧

不欺人,复制就能用 stm8 stvd驱动a4988驱动

[复制链接]
ID:205182 发表于 2020-5-9 01:08 | 显示全部楼层 |阅读模式
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include <stm8s105k4.h>  //驱动A4988
#define u8     unsigned char
#define u16    unsigned short
#define u32    unsigned long
#define v8     unsigned int


void delay_ms(v8 ms);
void delay_us(u8 t);
u8 key_scan(void);
void GPIO_Init(void);
void TIM1_Init(void);

u8 num;
int DIR;
main()
{
        CLK_SWCR =0x02;
        CLK_SWR =0xb4;
        GPIO_Init();
        TIM1_Init();
        _asm("rim");
        delay_ms(20);
        
        
        
        while (1)
        {
                u8 i;
                i=key_scan();
         if(i==1)
                {
                        PD_ODR|=0x08;
                }
                else
                {
                        i==0;
                        PD_ODR&=0xf7;
                }
        }

}

void delay_ms(v8 ms)
{
        v8 x,y;
        for(x=ms;x>0;x--)
        {
                for(y=300;y>0;y--)
            {
                                
            }
  }
}

void delay_us(u8 t)
{
        u8 m = t;
        while(m--);
}


void GPIO_Init()
{
        PD_DDR |=0x04; //PD2 为输出引脚 1   STEP
        PD_CR1 |=0x04; //PD2 为推挽输出 1
        PD_CR2 |=0x04; //PD2 为10MHZ速率1
  PD_ODR &=0xfb; //PD2 为step输出 0

        
        PD_DDR |=0x08;  //PD3 为输出引脚 1   DIR
        PD_CR1 |=0x08;  //PD3 为推挽输出 1
        PD_CR2 &=0xf7;  //PD3 为2MHZ速率 0
        
        
        PD_DDR &=0xcf;  //PD4,5 为输入引脚        0
  PD_CR1 |=0x30;  //PD4,5 为若上拉输入模式  1
  PD_CR2 &=0xcf;        //PD4,5 关外部中断        0
}
        
void TIM1_Init(void)        
{
        TIM1_CR1 =0x80;         // TIM1寄存器由预装载寄存器缓冲
        
        TIM1_PSCRH =0x00;       //预分频F(CK_CNT)=F(CK_PSC)/(PSCR[15:0]+1)
        TIM1_PSCRL =0x9f;        //159+1==160, 100khz
        
        TIM1_IER=0x01;       //中断使能
        
        TIM1_ARRH =125/256;        //自动重装载计数器125
        TIM1_ARRL=125%256;
        
        TIM1_CR1|=0x01;     //TIM1寄存器CEN位为1

}

u8 key_scan()
{
          u8 i;int c;
          i=PD_IDR;
          i&=0x30;
          if(i==0x20)
          {
                        delay_ms(10);
                        if(i ==0x20)
                        {
                        c==0;
                  }
                        return 1;
          }        
   
   if(i ==0x10)
         {
                 delay_ms(10);
                 if(i ==0x10)
                 {
                   c==1;   //正
           }
                 return 0;
         }
        
}



  
@far @interrupt void TIM1_UPD_OVF_HandledInterrupt(void)//中断
{
        PD_ODR^=0x04;
       TIM1_SR1&=0xfe;
                        }


/*        BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
*        Copyright (c) 2007 STMicroelectronics
*/

typedef void @far (*interrupt_handler_t)(void);

struct interrupt_vector {
        unsigned char interrupt_instruction;
        interrupt_handler_t interrupt_handler;
};

@far @interrupt void NonHandledInterrupt (void)
{
        /* in order to detect unexpected events during development,
           it is recommended to set a breakpoint on the following instruction
        */
        return;
}

extern void _stext();     /* startup routine */

extern @far @interrupt void TIM1_UPD_OVF_HandledInterrupt(void);

struct interrupt_vector const _vectab[] = {
        {0x82, (interrupt_handler_t)_stext}, /* reset */
        {0x82, NonHandledInterrupt}, /* trap  */
        {0x82, NonHandledInterrupt}, /* irq0  */
        {0x82, NonHandledInterrupt}, /* irq1  */
        {0x82, NonHandledInterrupt}, /* irq2  */
        {0x82, NonHandledInterrupt}, /* irq3  */
        {0x82, NonHandledInterrupt}, /* irq4  */
        {0x82, NonHandledInterrupt}, /* irq5  */
        {0x82, NonHandledInterrupt}, /* irq6  */
        {0x82, NonHandledInterrupt}, /* irq7  */
        {0x82, NonHandledInterrupt}, /* irq8  */
        {0x82, NonHandledInterrupt}, /* irq9  */
        {0x82, NonHandledInterrupt}, /* irq10 */
        {0x82, TIM1_UPD_OVF_HandledInterrupt}, /* irq11 */
        {0x82, NonHandledInterrupt}, /* irq12 */
        {0x82, NonHandledInterrupt}, /* irq13 */
        {0x82, NonHandledInterrupt}, /* irq14 */
        {0x82, NonHandledInterrupt}, /* irq15 */
        {0x82, NonHandledInterrupt}, /* irq16 */
        {0x82, NonHandledInterrupt}, /* irq17 */
        {0x82, NonHandledInterrupt}, /* irq18 */
        {0x82, NonHandledInterrupt}, /* irq19 */
        {0x82, NonHandledInterrupt}, /* irq20 */
        {0x82, NonHandledInterrupt}, /* irq21 */
        {0x82, NonHandledInterrupt}, /* irq22 */
        {0x82, NonHandledInterrupt}, /* irq23 */
        {0x82, NonHandledInterrupt}, /* irq24 */
        {0x82, NonHandledInterrupt}, /* irq25 */
        {0x82, NonHandledInterrupt}, /* irq26 */
        {0x82, NonHandledInterrupt}, /* irq27 */
        {0x82, NonHandledInterrupt}, /* irq28 */
        {0x82, NonHandledInterrupt}, /* irq29 */
};

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:205182 发表于 2020-5-9 01:13 | 显示全部楼层
谢谢管理员提醒,发帖只为与大家交流,不为积分。
大的化小,准备给小孩做个玩具。慢慢来。ad什么以后奉上。说明很详细了。
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表