stm8S103 手轮DIY 代码如下:
#include "IOSTM8S103F3.h"
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
#define LED10 PA_ODR_ODR2
#define LED100 PA_ODR_ODR3
#define LEDX PD_ODR_ODR4
#define LEDY PD_ODR_ODR5
#define LEDZ PD_ODR_ODR6
#define LEDA PA_ODR_ODR1
#define KZ10 PD_ODR_ODR2
#define KZ100 PD_ODR_ODR3
#define KZX PC_ODR_ODR4
#define KZY PC_ODR_ODR5
#define KZZ PC_ODR_ODR6
#define KZA PC_ODR_ODR7
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
u8 XYZA=0;
void Delayms(unsigned int ms)
{
u8 i,y;
while(ms != 0)
{
for(y=0;y<100;y++)
{
}
for(i=0;i<200;i++)
{
}
ms--;
}
}
//****************io口初始化****************/
void GPIO_INIT()
{
PA_DDR = 0xFF;//
PA_CR1 = 0xFF;
PA_CR2 = 0xFF;
PA_ODR = 0xFF;
PB_DDR = 0x00;//按钮
PB_CR1 = 0x00;
PB_CR2 = 0xFF;
EXTI_CR1=0xAA;
PC_DDR = 0xFF;//
PC_CR1 = 0xFF;
PC_CR2 = 0xFF;
PC_ODR = 0xFF;
PD_DDR = 0xFF;//
PD_CR1 = 0xFF;
PD_CR2 = 0xFF;
PD_ODR = 0xFF;
}
//*********************************主函数************************************/
main()
{
//Delayms(50);//稳定
CLK_CKDIVR=0x00; //时钟不分频 16M
Delayms(100);//稳定
GPIO_INIT();
KZX=0;
LEDX=0;
asm("rim");
while (1)
{
}
}
//PB中断服务函数
#pragma vector=0x06 //PB 中断响应
__interrupt void EXTI_PORTB_IRQHandler(void) //PB 服务函数
{
if((PB_IDR&0x20)==0)
{
Delayms(100);// 消按钮抖动
if((PB_IDR&0x20)==0)
{
if(LED10==1&&LED100==1) //10倍
{
LED10=0;
KZ10=0;
KZ100=1;
}
else
{
if(LED10==0&&LED100==1)//100倍
{
LED10=0;
LED100=0;
KZ10=1;
KZ100=0;
}
else
if(LED10==0&&LED100==0)//1倍
{
LED10=1;
LED100=1;
KZ10=1;
KZ100=1;
}
}
}
while((PB_IDR&0x20)==0x20);//释放按钮
}
if((PB_IDR&0x10)==0)//4选1
{
Delayms(100);// 消按钮抖动
if((PB_IDR&0x10)==0)
{
if(XYZA==0)
{
LEDX=0;
LEDY=1;
LEDZ=1;
LEDA=1;
KZX=0;
KZY=1;
KZZ=1;
KZA=1;
}
if(XYZA==1)
{
LEDX=1;
LEDY=0;
LEDZ=1;
LEDA=1;
KZX=1;
KZY=0;
KZZ=1;
KZA=1;
}
if(XYZA==2)
{
LEDX=1;
LEDY=1;
LEDZ=0;
LEDA=1;
KZX=1;
KZY=1;
KZZ=0;
KZA=1;
}
if(XYZA==3)
{
LEDX=1;
LEDY=1;
LEDZ=1;
LEDA=0;
KZX=1;
KZY=1;
KZZ=1;
KZA=0;
XYZA=0;
}
else XYZA++;
while((PB_IDR&0x10)==0x10);//释放按钮
}
}
}
|