接触PIC不久,有些问题和以前的单片机不太一样。先放码,说问题。
#include <htc.h>
#include <pic16f690.h>
/*define data */
#define led_on() (RB7=0)
#define led_off() (RB7=1)
#define led_flash() (RB7=!RB7)
/*define idata*/
unsigned char time_10ms_flag,API_mode,time_100ms,display_flag;
unsigned int short_time,middle_time,long_time;
unsigned char short_cnt,middle_cnt,long_cnt;
long unsigned int contact_cnt;
/*define fuction */
void init_sys(void);
void test_out(unsigned char time_mode);
/*programe*/
main()
{
init_sys();
API_mode=short_time_mode;
while(1)
{
test_out(API_mode);
}
}
//------------------
void init_sys(void)
{
OSCCON=0b01110101; //8M int0sc
OPTION_REG=0b00000101;
PORTA=0;
TRISA=0b00001011;
PORTB=0;
TRISB=0; //led is RB7.LOW VOL is on,high vol is off
PORTC=0;
TRISC=0;
T1CON = 0b00110100;//1/8 fosc
TMR1H = 0XF6;
TMR1L = 0X3B; //10ms timer1
TMR1IF=0;
TMR1IE=1;
TMR1ON=1;
PEIE=1; ///MUST SET
GIE=1;
}
//--------------------------
void test_out(unsigned char time_mode)
{
if(time_10ms_flag==1)
{
time_10ms_flag=0; //定时器扫描放这里LED就一直亮,不知道为什么
switch(time_mode)
{
case short_time_mode:
short_time++;
if(short_time<=60)
{
led_off();
}
else if(short_time<120)
{
led_on();
}
else if(short_time>=120)
{
short_time=0;
short_cnt++;
contact_cnt++;
if(short_cnt==8)
{
short_cnt=0;
API_mode=middle_time_mode ;
}
}
break;
case middle_time_mode:
middle_time++;
if(middle_time<=120)
{
led_off();
}
else if(middle_time<240)
{
led_on();
}
else if(middle_time>=240)
{
middle_time=0;
middle_cnt++;
contact_cnt++;
if(middle_cnt==8)
{
middle_cnt=0;
API_mode=long_time_mode;
}
}
break;
case long_time_mode:
long_time++;
if(long_time<=180)
{
led_off();
}
else if(long_time<360)
{
led_on();
}
else if(long_time>=360)
{
long_time=0;
long_cnt++;
contact_cnt++;
if(long_cnt==8)
{
long_cnt=0;
API_mode=short_time_mode;
}
}
break;
default:
break;
}
}
}
//----------------------------------
void interrupt ISR(void)
{
if(TMR1IE&&TMR1IF)
{
TMR1IF=0;
time_10ms_flag=1;
TMR1H = 0XF6;
TMR1L = 0X3B;
}
}
以上程序出现10MS扫TEST_OUT函数,LED出现一直亮情况.本来是希望,三种频率闪的
修改MAIN()和TEST_OUT函数就变好了,不知道为什么
main()
{
init_sys();
API_mode=short_time_mode;
while(1)
{
if(time_10ms_flag==1) //当定时标志放主函数就好了
{
time_10ms_flag=0;
test_out(API_mode);
}
}
}
void test_out(unsigned char time_mode)
{
switch(time_mode)
{
}
}
这样把10MS定时标志放主函数,再调用就好了。要是把10MS定时标志放子函数就有问题。不知道为什么,以前在别的单片机都好着呢,请高手们解释下 |