项目场景:
51用定时器同时实现动态数码管和流水灯
问题描述
1.不能实现流水灯,只有一个灯常亮
2.报错:source\main.c(83): error C141: syntax error near ‘unsigned’, expected ‘__asm’
#include "include/ca51f155_config.h"
#include "include/ca51f155sfr.h"
#include "include/ca51f155xsfr.h"
#include "include/gpiodef_f155.h"
#include "include/system_clock.h"
#include "include/uart.h"
#include "include/delay.h"
#include <intrins.h>
#include "include/lcd_led.h"
#define INT_TIME 100000 //注意范围,超了的话会导致下面两个值计算错误
#define TH_VAL (unsigned char)((0x10000 - (INT_TIME*(FOSC/1000))/12000)>>8)
#define TL_VAL (unsigned char)((0x10000 - (INT_TIME*(FOSC/1000))/12000))
#define SMIT_EN(N) (N<<6)
#define SINK(N) (N<<3)
#define SINK_EN(N) (N<<2)
#define DRV(N) (N<<1)
#define SR(N) (N<<0)
unsigned char gsmg_code[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
unsigned char g_code[]={0x04,0x02,0x01};
static unsigned int j;
static unsigned int k;
void lighten (unsigned char a)
{
P37=a&(1<<0);P41(a&(1<<1));P42(a&(1<<2));
return ;
}
void lighten_duan (unsigned char a)
{
P35=a&(1<<0);P30=a&(1<<1);P17=a&(1<<2);P13=a&(1<<3);P14=a&(1<<4);P34=a&(1<<5);P15=a&(1<<6);P16=a&(1<<7);
}
void TIMER0_ISR (void) interrupt 1
{
static unsigned int i;
TH0 = TH_VAL;
TL0 = TL_VAL;
i++;
if(i==3){
lighten(g_code[i]);
i=0;
}//这种写法只有当i为3的时候才执行点灯程序
}
void Timer0_Init(void)
{
TMOD = (TMOD&0xFC)|0x01;
TH0 = TH_VAL;
TL0 = TL_VAL;
TR0 = 1;
ET0 = 1;
}
void System_Init(void)
{
#ifdef SYSCLK_12MHZ
CKDIV = 0; //没有这句定时器时间间隔会变大,因为默认是6MHZ
#endif
}
void main(void)
{
System_Init();
EA = 1;
Timer0_Init();
P43F=2;P43(0);
P42F=2;P41F=2;P37F=2;
P31F = 2;//3&8
P30F = 2;P34F = 2;P17F = 2;P35F = 2;P13F = 2;P14F = 2;P15F = 2;P16F = 2;//other
P43F = 2;P43(0);P42F=2;P41F=2;P37F=2;//3LED
unsigned char a;//位置错误
for(a = 0; a < 9; a++)
{
INDEX = a;
LXDAT = 0;
}
LXDIVH = 0;
LXDIVL = 112;
LXCFG = COMHV(COM_L) | SEGHV(SEG_H) | BLNK(0) | LDRV(LDRV_7);
LXCON = LEN(LEN_IRCH) | LMOD(LMOD_led);
while(1)
{
}
}
原因分析:
1.这种写法只有当i为3的时候才执行点灯程序
2.keil中变量定义只能放在最前面
解决方案:
#include "include/ca51f155_config.h"
#include "include/ca51f155sfr.h"
#include "include/ca51f155xsfr.h"
#include "include/gpiodef_f155.h"
#include "include/system_clock.h"
#include "include/uart.h"
#include "include/delay.h"
#include <intrins.h>
#include "include/lcd_led.h"
#define INT_TIME 10000 //调整参数
#define TH_VAL (unsigned char)((0x10000 - (INT_TIME*(FOSC/1000))/12000)>>8)
#define TL_VAL (unsigned char)((0x10000 - (INT_TIME*(FOSC/1000))/12000))
#define SMIT_EN(N) (N<<6)
#define SINK(N) (N<<3)
#define SINK_EN(N) (N<<2)
#define DRV(N) (N<<1)
#define SR(N) (N<<0)
unsigned char gsmg_code[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
unsigned char g_code[]={0x04,0x02,0x01};
static unsigned int j;
static unsigned int k;
void lighten (unsigned char a)
{
P37=a&(1<<0);P41(a&(1<<1));P42(a&(1<<2));
return ;
}
void lighten_duan (unsigned char a)
{
P35=a&(1<<0);P30=a&(1<<1);P17=a&(1<<2);P13=a&(1<<3);P14=a&(1<<4);P34=a&(1<<5);P15=a&(1<<6);P16=a&(1<<7);
}
void TIMER0_ISR (void) interrupt 1
{
static unsigned int i;
TH0 = TH_VAL;
TL0 = TL_VAL;
i++;
if(i==100){
j++;k++;//加两个标志位,把点灯和数码管的操作放在了主函数中
i=0;
} //通过这个if循环可以使得定时器的时间变得随心所欲
}
void Timer0_Init(void)
{
TMOD = (TMOD&0xFC)|0x01;
TH0 = TH_VAL;
TL0 = TL_VAL;
TR0 = 1;
ET0 = 1;
}
void System_Init(void)
{
#ifdef SYSCLK_12MHZ
CKDIV = 0;
#endif
}
void main(void)
{
unsigned char a;//变量定义只能放在最前面
System_Init();
EA = 1;
Timer0_Init();
P43F=2;P43(0);
P42F=2;P41F=2;P37F=2;
P31F = 2;//3&8
P30F = 2;P34F = 2;P17F = 2;P35F = 2;P13F = 2;P14F = 2;P15F = 2;P16F = 2;//other
P43F = 2;P43(0);P42F=2;P41F=2;P37F=2;//3LED
for(a = 0; a < 9; a++)
{
INDEX = a;
LXDAT = 0;
}
LXDIVH = 0;
LXDIVL = 112;
LXCFG = COMHV(COM_L) | SEGHV(SEG_H) | BLNK(0) | LDRV(LDRV_7);
LXCON = LEN(LEN_IRCH) | LMOD(LMOD_led);
while(1)
{
lighten(g_code[j]);
if(j>=3){j=0;}
lighten_duan(gsmg_code[k]);
if(k>=10){k=0;}
}
}