#include<pic16f7x.h>
unsigned int i=0,j=0,temp=0,t=0;
void tmint() //TMR0初始化程序,使TMR0工作于定时器方式
{
T0CS = 0; //TMR0时钟源选择,内部指令周期时钟
PSA = 1; //TMR0不分频,分配给WDT
T0IF = 0; //清除TMR0的中断标志
T0IE = 1; //TMR0中断允许
}
main()
{
GIE = 1 ; // 関总中断
PEIE = 0 ; // 関外設中斷
ADCON1 = 0x07 ; // A口全部配置為數字端口
TRISA = 0x00 ; // A端口6路輸出
tmint() ;
while(1)
{
if(t==1)
{
t=0;
temp=~temp;
PORTA = temp;
}
}
}
void interrupt MR0 ()
{
if(T0IF==1)
{
TMR0 = 0X08; //对TMR0写入一个调整值。因为与入TMR0后接着的
//两个周期能增量,中断需要别3个周期的响应时间,
//自动进行现场保护要消耗周期
T0IF = 0;
i=i+1;
if(i==10000)
{
i=0;
t=1;
}
}
}
请问,此方法是否正确,我在用SIM仿真时是PORTA口在680ms跳变一次。我把程序下到硬件里面,去点亮一个发光管,但没有反应。
请高手指点!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1) you should always read the datasheet / compiler manual extensively before trying to code a device.
2) you should learn good programming styles, including always commenting on your code, always use informative names for your variable / routines, etc.
more specifically:
"#include<pic16f7x.h> "
always try NOT to include specific device header files and instead include the "standard" header files and let the compiler sort it out as to which header file to include. This allows better consistency and portability. for example, in picc, the above should be
"#include <htc.h>"
always put fuse setting in your code, not in the programmer.
"void tmint() //TMR0初始化程序,使TMR0工作于定时器方式 "
something like the following would be better:
"void tmr0_init(void) //TMR0初始化程序,使TMR0工作于定时器方式 "
"main() "
instead, use:
"int main(void)"
"GIE = 1 ; // 関总中断 "
always enable global interrupt AFTER the peripheral interrupt has been enabled.
"temp=~temp; "
commend on your code.
no reason to use a shadow variable here.
"void interrupt MR0 () "
better yet:
"void interrupt tmr0_isr(void) "
"TMR0 = 0X08; //对TMR0写入一个调整值。因为与入TMR0后接着的 "
not sure why you want to load an offset but if you step it through, you may find thatTMR0 has advanced quite a low before this code is executed.
to maintain good timing consistency, use:
"TMR0 += 0X08; //对TMR0写入一个调整值。因为与入TMR0后接着的 "
I would suggest that you take an entry-level class in programming.
一周热门 更多>