我使用的STM8S103K3, 批号是MYS932, 现在发现PB口都不能输出高电平 以下是我的程序
#include"iostm8s103k3.h"
#include"compatible.H"
#define _BV(A) (1<<(A))
//LED1 PD3
//LED2 PD2
//LED3 PD0
#define LED_SA PD_ODR_ODR5
#define LED_SB PD_ODR_ODR6
#define LED_SRCK PD_ODR_ODR2
#define LED_RCK PD_ODR_ODR3
#define LED_SER PD_ODR_ODR4
// a BIT5
// b BIT0
// c BIT1
// D BIT3
// E BIT2
// f BIT4
// G BIT7
//DP BIT6
char LED_DATA[10]=
{
0xff-BIT6-BIT7,//0
BIT0+BIT1,//1
0xff-BIT1-BIT4-BIT6,//2
0xff-BIT2-BIT4-BIT6,//3
BIT4+BIT7+BIT0+BIT1,//4
0xff-BIT0-BIT2-BIT6,//5
0xff-BIT0-BIT6,//6
BIT5+BIT0+BIT1,//7
0xff-BIT6,//8
0xff-BIT2-BIT6,//9
};
void delay(long m)
{
volatile unsigned long i;
for(i=0;i<m;i++);
}
//把数据送入HC595
void HC_595_input(unsigned char data)
{
unsigned char i;
for(i=0;i<8;i++)
{
LED_SRCK=0;//移位寄存器时钟拉低时钟
if(data&0x80) //最高位判断
{
LED_SER=1; //最高位为1,则写1到DS口
}
else
{
LED_SER=0; //最高位为0,则写0到DS口
}
LED_SRCK=1;//移位寄存器时钟拉高时钟
//delay_1us();
delay(1);
data<<=1;
}
}
//数据从595的并行口输出
void HC_595_output(unsigned char data)
{
LED_RCK=0;//存储寄存器时钟拉低时钟
HC_595_input(data);
LED_RCK=1;//存储寄存器时钟拉高时钟
}
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: TIM_Init */
/* INPUT/OUTPUT: None. */
/* DESCRIPTION: Initialize the TIM4 as LED timebase. */
/* -------------------------------------------------------------------------- */
void TIM2_Init(void)
{
__asm("sim"); //sim是禁止中断
TIM2_PSCR =0x07; //设置时钟分频
TIM2_ARRH = 0x00; //初值
TIM2_ARRL = 0xf6; //100uS,内部16M
TIM2_IER = 0x01; //允许中断
TIM2_CR1 = 0x01; //开启定时器
__asm("rim"); //rim是使能中断
}
void GPIO_Init(void)
{
PB_DDR = 0X00;//_BV(0)|_BV(1)|_BV(4)|_BV(5)|_BV(6);
PB_CR1 = 0Xff;//_BV(0)|_BV(1)|_BV(4)|_BV(5)|_BV(6);
PB_CR2 = 0x00;
PC_DDR = 0XFF;//_BV(0)|_BV(1)|_BV(4)|_BV(2)|_BV(3);
PC_CR1 = 0XFF;// _BV(0)|_BV(1)|_BV(4)|_BV(2)|_BV(3);
PC_CR2 = 0x00;
PD_DDR = 0XFF;// _BV(6)|_BV(5)|_BV(0)|_BV(2)|_BV(3);
PD_CR1 = 0XFF;//_BV(6)|_BV(5)|_BV(0)|_BV(2)|_BV(3);
PD_CR2 = 0x00;
PF_DDR = 0XFF;
PF_CR1 = 0XFF;
PF_CR2 = 0x00;
}
void CLK_Init(void)
{
CLK_CKDIVR &= ~0x10; /* 01: fHSI= fHSI RC output/2. */
// CLK_CKDIVR |= 0x01; /* 001: fCPU=fMASTER/2. */
}
long test=0;
char test1=100;
char test2=123;
void main( void )
{
char i;
CLK_Init();
GPIO_Init();
TIM2_Init();
while(1)
{
//PD_ODR|=_BV(6)|_BV(5);
//PC_ODR=_BV(0)|_BV(1)|_BV(3);
PC_ODR=0XFF;
delay(4000);
test1++;
test2++;
//PD_ODR=_BV(2);
//PC_ODR=_BV(2);
PC_ODR=0;
delay(4000);
//PD_ODR=_BV(0);
PC_ODR=_BV(4);
delay(4000);
//LED_SA=1;
//LED_SB=1;
//PB_ODR|=_BV(0);
PB_ODR=0XFF;
PF_ODR=0XFF;
delay(4000);
PB_ODR=0X00;
PF_ODR=0X00;
}
}
/*******************************************************************************
* ==中断服务函数 函数==
*******************************************************************************/
#pragma vector=TIM2_OVR_UIF_vector
__interrupt void TIM2_UPD_OVF_BRK_IRQHandler(void)
{
char i;
//LED1=~LED1; //翻转 管脚电平
TIM2_SR1 = 0x0E; //中断标志位,它不会自动清零
if(test>2)
{
test=0;
}
if(test==1)
{
//PD_ODR|=_BV(6);
LED_RCK=0;//存储寄存器时钟拉低时钟
for(i=0;i<10;i++)
{
HC_595_input(LED_DATA[test1%10]);
}
LED_RCK=1;//存储寄存器时钟拉高时钟
LED_SA=0;
LED_SB=1;
}
else if(test==0)
{
LED_RCK=0;//存储寄存器时钟拉低时钟
for(i=0;i<10;i++)
{
HC_595_input(LED_DATA[test2%10]);
}
LED_RCK=1;//存储寄存器时钟拉高时钟
LED_SA=1;
LED_SB=0;
}
test++;
}
|