/* 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 */
}; |