- /* 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、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|
|