We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi,
i am a student and needs some help. Can someone help solve some of my doubts?
Hmm..may i know what does it means by SYSCLK/12/10? How can i go about calculating the timer values using the SYSCLK/12/10 if i want to use the codes below to make the LED blink at 1Hz?
Thanx=)
//------------------------------------------------------------------------------------ // MAIN Routine //------------------------------------------------------------------------------------ void main (void) { // disable watchdog timer WDTCN = 0xde; WDTCN = 0xad; SFRPAGE = CONFIG_PAGE; // Switch to configuration page PORT_Init (); SFRPAGE = TMR3_PAGE; // Switch to Timer 3 page Timer3_Init (SYSCLK /120/10); // Init Timer3 to generate interrupts // at a 10 Hz rate. EA = 1; // enable global interrupts SFRPAGE = LEGACY_PAGE; // Page to sit in for now while (1) { // spin forever } } //------------------------------------------------------------------------------------ // PORT_Init //------------------------------------------------------------------------------------ // // Configure the Crossbar and GPIO ports // void PORT_Init (void) { XBR2 = 0x40; // Enable crossbar and weak pull-ups P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output } //------------------------------------------------------------------------------------ // Timer3_Init //------------------------------------------------------------------------------------ // // Configure Timer3 to auto-reload and generate an interrupt at interval // specified by <counts> using SYSCLK/12 as its time base. // // void Timer3_Init (int counts) { TMR3CN = 0x00; // Stop Timer3; Clear TF3; // use SYSCLK/12 as timebase RCAP3 = -counts; // Init reload values TMR3 = 0xffff; // set to reload immediately EIE2 |= 0x01; // enable Timer3 interrupts TR3 = 1; // start Timer3 } //------------------------------------------------------------------------------------ // Interrupt Service Routines //------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------ // Timer3_ISR //------------------------------------------------------------------------------------ // This routine changes the state of the LED whenever Timer3 overflows. // // NOTE: The SFRPAGE register will automatically be switched to the Timer 3 Page // When an interrupt occurs. SFRPAGE will return to its previous setting on exit // from this routine. // void Timer3_ISR (void) interrupt 14 { TF3 = 0; // clear TF3 LED = ~LED; // change state of LED }
i add in the variable declaration--static int b;--at the global constant..and declare b=0 in the ISR?
Almost there. 1) Yes, you must declare 'b' as static unsigned char b; (why waste one int when one char can do). This will NOT make 'b' global, its scope is still local to the function, but its lifetime will be infinite, i.e., it will retain its value across the whole execution time of the program.
2) To initialize 'b', you should declare it with initialization: static unsigned char b = 0; Thus, the first time your program runs, the value of 'b' will automagically be zero.
Andy wrote: "Actually, it has nothing to do with optimisation. The whole point of local variables is that they do cease to exist between calls to the function ..."
You are right, of course. My point is exactly that. The only reason for that particular code to run as written is that the storage allocated for the locals in interrupt handlers can't be reused for main code.
Were that function a non-interrupt function, it would crash and burn.
"Almost there. 1) Yes, you must declare 'b' as static unsigned char b; (why waste one int when one char can do)."
Hi, Thanx for ur help=)
"No, it doesn't.
Back to the textbook!"
Hi, i have checked that out already, static variable is another class of local variable.it is not destroyed on the exit of the function and will be available when function is next called.
thanx=)