This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What's wrong with my program?

I'm trying to generate a square frequency with C51. After playing around for
some hours with the debuger I Know which line is giving me a headacke.

The important lines (in bold) of the algorithm are as follows

 new_timer_reload = 0xfe9b;                          
 new_timer_reload_high = new_timer_reload >>8;          
 TR0=1;                                          //run timer 0
 divisions=38;                                   //The high poti is chossen to be divided in 7 steps (rough tunning)
                                                     //not like the low poti
 low_poti=0x32;
 high_poti=0x39;
}                   

void main()
{
init();
while(1)
 {
  add_high=high_poti/divisions;                                 //According to the value in high poti (0 to 255)
                                                                          //gives me the value I should add to TL0 (0 to 6)
  add_high=add_high<<8;                             // times 256
  new_timer_reload = 0xfe9b+low_poti+add_high;
  new_timer_reload_high = new_timer_reload >> 8;
 }
}

#pragma RB(1)
timer0() interrupt 1 using 1
{
TR0=0;                                                                         //stop the timer   
EAL=0;                                                                         //disable all interrupts 
frequen_1=~frequen_1;                                                  //invert the signal 
TL0 = new_timer_reload;                                     //load the new values
TH0 = new_timer_reload_high;                             
TR0=1;                                                                      
EAL=1;                                
}

high_poti is a value that comes from a A/D conversion, it's between 0 and 255
and so add_high is between 0 and 6 and then multipled by 256 so I add it to the
higher byte of new_timer_reload

the problem is that in the instrcution
new_timer_reload = 0xfe9b+low_poti+add_high;
if I executed without adding add_high is working properly and I obtain
values of 332 microseconds for the timer as expected.
But when I add add_high (for my example equals 256), I obtain values of roughly 64000 microseconds
which means I have TH0=00, but in this case I should expect TH0=0xFF
and having frequencies around 332-256=76 microseconds.

I hope you could understand what I'm trying to explain.

What's wrong with my program?

Thanks a lot.

Parents Reply Children
No data