why it dose not work (timer0 in 18f452)

Discussion in 'Embedded Systems and Microcontrollers' started by mhghost, May 16, 2013.

  1. mhghost

    Thread Starter New Member

    May 16, 2013
    6
    0
    hi i am a beginner with micro so i wanted to try to build a clock and here is the code


    Code ( (Unknown Language)):
    1.  
    2. sbit LCD_RS at RB4_bit;
    3. sbit LCD_EN at RB5_bit;
    4. sbit LCD_D4 at RB0_bit;
    5. sbit LCD_D5 at RB1_bit;
    6. sbit LCD_D6 at RB2_bit;
    7. sbit LCD_D7 at RB3_bit;
    8.  
    9. sbit LCD_RS_Direction at TRISB4_bit;
    10. sbit LCD_EN_Direction at TRISB5_bit;
    11. sbit LCD_D4_Direction at TRISB0_bit;
    12. sbit LCD_D5_Direction at TRISB1_bit;
    13. sbit LCD_D6_Direction at TRISB2_bit;
    14. sbit LCD_D7_Direction at TRISB3_bit;
    15.  
    16.   char txt2[10];
    17.   char txt1[10];
    18.   int  x=0;
    19.   int y=0;
    20.    void main(){lcd_cmd( _LCD_CURSOR_OFF);
    21.    TRISB =0;
    22.    T0CON =0b10001111;
    23.     Lcd_Init();
    24.     while(tmr0h < 0b000011111){}
    25.     while(tmr0l < 0b01000011){}
    26.     x++;
    27.     T0CON =0b00001111;
    28.     tmr0h=0;
    29.     tmr0l=0;
    30.   if(x==60){x=0;y++;}
    31.   lcd_out (1,1,"time is ");
    32.   inttostr(x,txt1);
    33.   inttostr(y,txt2);
    34.   ltrim(txt1);
    35.   ltrim(txt2);
    36.   lcd_out(2,1,"m=");
    37.   lcd_out_cp(txt2);
    38.   lcd_out(2,9,"s=");
    39.   lcd_out_cp(txt1);
    40.  
    41.  
    42. }
    43.  
    i don't know what is wrong but every time i come near a timer i mess up could some one please help me i am using a 18f452 pic and a 4 MHz cristeal
     
    Last edited by a moderator: May 17, 2013
  2. mhghost

    Thread Starter New Member

    May 16, 2013
    6
    0
    can any one help me please
     
  3. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Hi mhghost, I saw your other thread too. :)

    I think the very first thing you need to do is work out HOW to get exact seconds, and make sure that works.

    Personally I prefer the simplicity and tunability of a Bresenham type system, which can make perfect seconds from any xtal speed.

    This page has a lot of C examples;
    http://romanblack.com/one_sec.htm

    Here is one, to give actual seconds from a TMR0 and your 4MHz xtal;
    Code ( (Unknown Language)):
    1.  
    2.   // uses 1 variable; unsigned long bres
    3.   // gets here every TMR0 int (every 256 ticks)
    4.  
    5.   bres += 256;         // add 256 ticks to bresenham total
    6.  
    7.   if(bres >= 1000000)  // if reached 1 second!
    8.   {
    9.     bres -= 1000000;   // subtract 1 second, retain error
    10.     do_1sec_event();   // update clock, etc
    11.   }
    That code goes inside your TMR0 overflow interrupt, and TMR0 prescaler set to 1:1.

    For now I would concentrate on getting the interrupt and that code working, and just make it toggle a LED every second.

    Then you can compare that to a household clock, and if the flashing LED is keeping good time you can then add more code, to count seconds and minutes.
     
  4. mhghost

    Thread Starter New Member

    May 16, 2013
    6
    0
    thanks man

    i will try it
     
Loading...