入门STM8 没多久。
以前写程序都是一口气 写在一个main.c 里面
现在程序渐渐的有点多了。想分开来放。
今天在家试了好久,都没成功。
请问程序分开来放 有什么规则
上传一段程序,请各位高手帮我 示范一下 分成2个文件即可
#include<iostm8s207sb.h>
#define LED_CS_1 PE_ODR_ODR0
#define LED_CS_2 PE_ODR_ODR5
#define LED_CS_3 PE_ODR_ODR6
#define LED_CS_4 PG_ODR_ODR0
#define LED_CS_5 PG_ODR_ODR1
#define LED_CS_6 PC_ODR_ODR5
#define LED_CS_7 PC_ODR_ODR6
#define LED_CS_8 PC_ODR_ODR7
#define LED_A PB_ODR_ODR0
#define LED_B PB_ODR_ODR1
#define LED_C PB_ODR_ODR2
#define LED_D PB_ODR_ODR3
#define LED_E PB_ODR_ODR4
#define LED_F PB_ODR_ODR5
#define LED_G PB_ODR_ODR6
#define LED_DP PB_ODR_ODR7
unsigned int ms_count; // 1ms 变量
unsigned int buffer[]={0xff,0x45,0x34,0x01,0x56};
unsigned int buffer1[5];
unsigned int LED_NUM[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};
void GPIO_INIT (void)
{
PA_DDR= 0xff;
PA_CR1= 0xff;
PB_DDR= 0xff;
PB_CR1= 0xff;
PC_DDR= 0xff;
PC_CR1= 0xff;
PD_DDR= 0xff;
PD_CR1= 0xff;
PE_DDR= 0xff;
PE_CR1= 0xff;
PG_DDR= 0xff;
PG_CR1= 0xff;
PE_DDR= 0xff;
PE_CR1= 0xff;
LED_CS_1 = 1; // 片选
LED_CS_2 = 1;
LED_CS_3 = 1;
LED_CS_4 = 1;
LED_CS_5 = 1;
LED_CS_6 = 1;
LED_CS_7 = 1;
LED_CS_8 = 1;
}
void LED_CS (unsigned int a) //LED片选
{
switch (a)
{
case 0: LED_CS_1=1;LED_CS_2=1;LED_CS_3=1;LED_CS_5=1;LED_CS_6=1;LED_CS_7=1;LED_CS_8=1;break;
case 1: LED_CS_1=0;LED_CS_2=1;LED_CS_3=1;LED_CS_5=1;LED_CS_6=1;LED_CS_7=1;LED_CS_8=1;break;
case 2: LED_CS_1=1;LED_CS_2=0;LED_CS_3=1;LED_CS_5=1;LED_CS_6=1;LED_CS_7=1;LED_CS_8=1;break;
case 3: LED_CS_1=1;LED_CS_2=1;LED_CS_3=0;LED_CS_5=1;LED_CS_6=1;LED_CS_7=1;LED_CS_8=1;break;
case 4: LED_CS_1=1;LED_CS_2=1;LED_CS_3=1;LED_CS_5=0;LED_CS_6=1;LED_CS_7=1;LED_CS_8=1;break;
case 5: LED_CS_1=1;LED_CS_2=1;LED_CS_3=1;LED_CS_5=1;LED_CS_6=0;LED_CS_7=1;LED_CS_8=1;break;
case 6: LED_CS_1=1;LED_CS_2=1;LED_CS_3=1;LED_CS_5=1;LED_CS_6=1;LED_CS_7=0;LED_CS_8=1;break;
case 7: LED_CS_1=1;LED_CS_2=1;LED_CS_3=1;LED_CS_5=1;LED_CS_6=1;LED_CS_7=1;LED_CS_8=0;break;
default:break;
}
}
void delay (unsigned int k) //延时
{
unsigned int a,b;
for (a=0;a<k;a++)
{
for(b=0;b<50;b++);
}
}
void LED_DUANXUAN (unsigned int led_number) // LED段选
{
LED_A = ((LED_NUM[led_number]>>7)&0x01);
LED_B = ((LED_NUM[led_number]>>6)&0x01);
LED_C = ((LED_NUM[led_number]>>5)&0x01);
LED_D = ((LED_NUM[led_number]>>4)&0x01);
LED_E = ((LED_NUM[led_number]>>3)&0x01);
LED_F = ((LED_NUM[led_number]>>2)&0x01);
LED_G = ((LED_NUM[led_number]>>1)&0x01);
LED_DP =(LED_NUM[led_number]&0x01);
}
unsigned int LED_GUI_weishupanduan (unsigned long x)//位数判断
{
unsigned int i=1;
while(x/10)
{
x/=10;
i++;
}
return i;
}
void LED_GUI (unsigned long LED_NUM)
{
unsigned int k;
k=LED_GUI_weishupanduan(LED_NUM);
switch (k)
{
case 1:
LED_DUANXUAN(LED_NUM);LED_CS (1);break;
case 2:
LED_DUANXUAN(LED_NUM%10);
LED_CS (1);
delay (20);
LED_DUANXUAN(LED_NUM/10);
LED_CS (2);delay (20);
break;
case 3:
LED_DUANXUAN(LED_NUM%10);
LED_CS (1);delay (20);
LED_DUANXUAN((LED_NUM/10)%10);
LED_CS (2);delay (20);
LED_DUANXUAN(LED_NUM/100);
LED_CS (3);delay (20);
break;
case 4:
LED_DUANXUAN(LED_NUM%10);
LED_CS (1);delay (20);
LED_DUANXUAN((LED_NUM/10)%10);
LED_CS (2);delay (20);
LED_DUANXUAN((LED_NUM/100)%10);
LED_CS (3);delay (20);
LED_DUANXUAN(LED_NUM/1000);
LED_CS (4);delay (20);
break;
case 5:
LED_DUANXUAN(LED_NUM%10);
LED_CS (1);delay (20);
LED_DUANXUAN((LED_NUM/10)%10);
LED_CS (2);delay (20);
LED_DUANXUAN((LED_NUM/100)%10);
LED_CS (3);delay (20);
LED_DUANXUAN((LED_NUM/1000)%10);
LED_CS (4);delay (20);
LED_DUANXUAN(LED_NUM/10000);
LED_CS (5);delay (20);
break;
case 6:
LED_DUANXUAN(LED_NUM%10);
LED_CS (1);delay (20);
LED_DUANXUAN((LED_NUM/10)%10);
LED_CS (2);delay (20);
LED_DUANXUAN((LED_NUM/100)%10);
LED_CS (3);delay (20);
LED_DUANXUAN((LED_NUM/1000)%10);
LED_CS (4);delay (20);
LED_DUANXUAN((LED_NUM/10000)%10);
LED_CS (5);delay (20);
LED_DUANXUAN(LED_NUM/100000);
LED_CS (6);delay (20);
break;
case 7:
LED_DUANXUAN(LED_NUM%10);
LED_CS (1);delay (20);
LED_DUANXUAN((LED_NUM/10)%10);
LED_CS (2);delay (20);
LED_DUANXUAN((LED_NUM/100)%10);
LED_CS (3);delay (20);
LED_DUANXUAN((LED_NUM/1000)%10);
LED_CS (4);delay (20);
LED_DUANXUAN((LED_NUM/10000)%10);
LED_CS (5);delay (20);
LED_DUANXUAN((LED_NUM/100000)%10);
LED_CS (6);delay (20);
LED_DUANXUAN(LED_NUM/1000000);
LED_CS (7);delay (20);
break;
default :break;
}
}
/*/////////////////////////////////////////////////////////////////////////////////
TIM2 初始化部分
*//////////////////////////////////////////////////////////////////////////////////
void TIM2_INIT ()
{
// TIM2_IER |= 0x00;//禁止中断
// TIM2_EGR = 0x01; // 允许产生更新事件
TIM2_PSCR = 0x00; //预分频器 。CK_PSC/TIMx_PSCR=计数机频率。16mm
TIM2_ARRH = 0x3e; // 配置TIM2周期,1ms中断
TIM2_ARRL = 0x80;
TIM2_CNTRH = 0x3e;
TIM2_CNTRL = 0x80;
TIM2_CR1 |= 0x81; //使能TIM2.
TIM2_IER |= 0x01; //使能TIM2溢出中断
}
main ()
{
unsigned int i;
asm("sim"); // 关全局中断
CLK_CKDIVR=0x00; //16M
TIM2_INIT ();
GPIO_INIT ();
asm("rim"); //开全局中断
while(1)
{
for (i=0;i<5;i++)
{
ms_count=0;
while(ms_count<1000)
{
LED_GUI(buffer[i]);
}
}
}
}
/*/////////////////////////////////////////////////////////////////////////////////
TIM2 中断部分
*//////////////////////////////////////////////////////////////////////////////////
#pragma vector=TIM2_OVR_UIF_vector
__interrupt void TIM2_OVR_UIF(void)
{
TIM2_SR1 =0; /* 清中断标志 */
ms_count++;
} |