找回密码
 注册

QQ登录

只需一步,快速开始

搜索

stm8 stvd驱动a4988驱动程序源码

[复制链接]
路漫漫 发表于 2020-5-20 23:46:46 | 显示全部楼层 |阅读模式
  1. /* MAIN.C file
  2. *
  3. * Copyright (c) 2002-2005 STMicroelectronics
  4. */
  5. #include <stm8s105k4.h>  //驱动A4988
  6. #define u8     unsigned char
  7. #define u16    unsigned short
  8. #define u32    unsigned long
  9. #define v8     unsigned int


  10. void delay_ms(v8 ms);
  11. void delay_us(u8 t);
  12. u8 key_scan(void);
  13. void GPIO_Init(void);
  14. void TIM1_Init(void);

  15. u8 num;
  16. int DIR;
  17. main()
  18. {
  19.         CLK_SWCR =0x02;
  20.         CLK_SWR =0xb4;
  21.         GPIO_Init();
  22.         TIM1_Init();
  23.         _asm("rim");
  24.         delay_ms(20);
  25.         
  26.         
  27.         
  28.         while (1)
  29.         {
  30.                 u8 i;
  31.                 i=key_scan();
  32.          if(i==1)
  33.                 {
  34.                         PD_ODR|=0x08;
  35.                 }
  36.                 else
  37.                 {
  38.                         i==0;
  39.                         PD_ODR&=0xf7;
  40.                 }
  41.         }

  42. }

  43. void delay_ms(v8 ms)
  44. {
  45.         v8 x,y;
  46.         for(x=ms;x>0;x--)
  47.         {
  48.                 for(y=300;y>0;y--)
  49.             {
  50.                                 
  51.             }
  52.   }
  53. }

  54. void delay_us(u8 t)
  55. {
  56.         u8 m = t;
  57.         while(m--);
  58. }


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

  65.         
  66.         PD_DDR |=0x08;  //PD3 为输出引脚 1   DIR
  67.         PD_CR1 |=0x08;  //PD3 为推挽输出 1
  68.         PD_CR2 &=0xf7;  //PD3 为2MHZ速率 0
  69.         
  70.         
  71.         PD_DDR &=0xcf;  //PD4,5 为输入引脚        0
  72.   PD_CR1 |=0x30;  //PD4,5 为若上拉输入模式  1
  73.   PD_CR2 &=0xcf;        //PD4,5 关外部中断        0
  74. }
  75.         
  76. void TIM1_Init(void)        
  77. {
  78.         TIM1_CR1 =0x80;         // TIM1寄存器由预装载寄存器缓冲
  79.         
  80.         TIM1_PSCRH =0x00;       //预分频F(CK_CNT)=F(CK_PSC)/(PSCR[15:0]+1)
  81.         TIM1_PSCRL =0x9f;        //159+1==160, 100khz
  82.         
  83.         TIM1_IER=0x01;       //中断使能
  84.         
  85.         TIM1_ARRH =125/256;        //自动重装载计数器125
  86.         TIM1_ARRL=125%256;
  87.         
  88.         TIM1_CR1|=0x01;     //TIM1寄存器CEN位为1

  89. }

  90. u8 key_scan()
  91. {
  92.           u8 i;int c;
  93.           i=PD_IDR;
  94.           i&=0x30;
  95.           if(i==0x20)
  96.           {
  97.                         delay_ms(10);
  98.                         if(i ==0x20)
  99.                         {
  100.                         c==0;
  101.                   }
  102.                         return 1;
  103.           }        
  104.    
  105.    if(i ==0x10)
  106.          {
  107.                  delay_ms(10);
  108.                  if(i ==0x10)
  109.                  {
  110.                    c==1;   //正
  111.            }
  112.                  return 0;
  113.          }
  114.         
  115. }



  116.   
  117. @far @interrupt void TIM1_UPD_OVF_HandledInterrupt(void)//中断
  118. {
  119.         PD_ODR^=0x04;
  120.        TIM1_SR1&=0xfe;
  121.                         }


  122. /*        BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
  123. *        Copyright (c) 2007 STMicroelectronics
  124. */

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

  126. struct interrupt_vector {
  127.         unsigned char interrupt_instruction;
  128.         interrupt_handler_t interrupt_handler;
  129. };

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

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

  138. extern @far @interrupt void TIM1_UPD_OVF_HandledInterrupt(void);

  139. struct interrupt_vector const _vectab[] = {
  140.         {0x82, (interrupt_handler_t)_stext}, /* reset */
  141.         {0x82, NonHandledInterrupt}, /* trap  */
  142.         {0x82, NonHandledInterrupt}, /* irq0  */
  143.         {0x82, NonHandledInterrupt}, /* irq1  */
  144.         {0x82, NonHandledInterrupt}, /* irq2  */
  145.         {0x82, NonHandledInterrupt}, /* irq3  */
  146.         {0x82, NonHandledInterrupt}, /* irq4  */
  147.         {0x82, NonHandledInterrupt}, /* irq5  */
  148.         {0x82, NonHandledInterrupt}, /* irq6  */
  149.         {0x82, NonHandledInterrupt}, /* irq7  */
  150.         {0x82, NonHandledInterrupt}, /* irq8  */
  151.         {0x82, NonHandledInterrupt}, /* irq9  */
  152.         {0x82, NonHandledInterrupt}, /* irq10 */
  153.         {0x82, TIM1_UPD_OVF_HandledInterrupt}, /* irq11 */
  154.         {0x82, NonHandledInterrupt}, /* irq12 */
  155.         {0x82, NonHandledInterrupt}, /* irq13 */
  156.         {0x82, NonHandledInterrupt}, /* irq14 */
  157.         {0x82, NonHandledInterrupt}, /* irq15 */
  158.         {0x82, NonHandledInterrupt}, /* irq16 */
  159.         {0x82, NonHandledInterrupt}, /* irq17 */
  160.         {0x82, NonHandledInterrupt}, /* irq18 */
  161.         {0x82, NonHandledInterrupt}, /* irq19 */
  162.         {0x82, NonHandledInterrupt}, /* irq20 */
  163.         {0x82, NonHandledInterrupt}, /* irq21 */
  164.         {0x82, NonHandledInterrupt}, /* irq22 */
  165.         {0x82, NonHandledInterrupt}, /* irq23 */
  166.         {0x82, NonHandledInterrupt}, /* irq24 */
  167.         {0x82, NonHandledInterrupt}, /* irq25 */
  168.         {0x82, NonHandledInterrupt}, /* irq26 */
  169.         {0x82, NonHandledInterrupt}, /* irq27 */
  170.         {0x82, NonHandledInterrupt}, /* irq28 */
  171.         {0x82, NonHandledInterrupt}, /* irq29 */
  172. };
复制代码


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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-25 14:13

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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