Programm on MKICROC

Discussion in 'The Projects Forum' started by splendid-electron, Feb 24, 2014.

  1. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    I'm trying to understand how this programm work can you help me
    it's about a tchometer contact less it count the speed with PIC18F2550

    Code ( (Unknown Language)):
    1.  
    2.  sbit IR_Tx at RA3_bit;
    3. // Define Messages
    4.  char message1[] = "2014TACHY.V3.COM";
    5.  char *RPM = "00000 RPM";
    6. //================================================
    7.  void Display_RPM(unsigned long num)
    8.    {
    9.       RPM[0] = num/10000 + 48;
    10.       RPM[1] = (num/1000)%10 + 48;
    11.       RPM[2] = (num/100)%10 + 48;
    12.       RPM[3] = (num/10)%10 + 48;
    13.       RPM[4] = num%10 + 48;
    14.       Lcd_Out(2,4,RPM);
    15.       //UART_Write_Text(RPM);
    16.    }
    17. //================================================
    18.  void main()
    19.    {
    20.       CMCON = 0x07;   // Disable comparators
    21.       ADCON1 = 0x0F;  // Disable Analog functions
    22.       //TRISC = 0x00;
    23.       TRISC = 0x80;        // Port C output except RC7 RX
    24.       TRISB = 0x00;
    25.       PORTA = 0x00;
    26.       TRISA = 0b00010000;
    27.       T0CON = 0b01101000; // TMR0 as 16-bit counter
    28.       Lcd_Init();        // Initialize LCD
    29.       Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
    30.       Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off
    31.       Lcd_Out(1,1,message1);            // Write message1 in 1st row
    32.       //----
    33.        UART1_Init(9600);                         // initialize UART1 module
    34.        Delay_ms(100);
    35.       //----
    36.       do
    37.        {
    38.            T0CON.TMR0ON = 1;
    39.            TMR0L = 0;
    40.            TMR0H = 0;
    41.            IR_Tx = 1;
    42.            Delay_ms(1000); // Wait for 1 sec
    43.            IR_Tx = 0;
    44.            T0CON.TMR0ON = 0;    // Stop the timer
    45.            RPM_Value = (256*TMR0H + TMR0L)*60;
    46.            Display_RPM(RPM_Value);
    47.            //----
    48.            
    49.            LongToStr(RPM_Value, txt);
    50.            UART_Write_Text("N");
    51.            Delay_ms(10); // Wait for 1 sec
    52.            //UART_Write_Text("2014TACHY.V3.COM");
    53.            UART_Write_Text(txt);
    54.            //----
    55.        } while(1);             // Infinite Loop
    56.  }
    57.  
     
    Last edited by a moderator: Feb 24, 2014
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    It is using timer 0 as a counter and counting the number of slots in the wheel it detects in one second.

    See this website for more information...
     
    Last edited: Feb 24, 2014
    splendid-electron likes this.
  3. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    void Display_RPM(unsigned long num)
    {
    RPM[0] = num/10000 + 48;
    RPM[1] = (num/1000)%10 + 48;
    RPM[2] = (num/100)%10 + 48;
    RPM[3] = (num/10)%10 + 48;
    RPM[4] = num%10 + 48;
    Lcd_Out(2,4,RPM);
    //UART_Write_Text(RPM);


    can u explain for me the use of this lines
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Please use code tags when posting code ( the "#" button - it surrounds your code with [CODE ] and [/CODE ] tags. Note that the spaces are there so it is treated as text).

    This code is converting the binary number to ASCII, by taking each number and assigning it to an element in the RPM array, which will then be displayed on a LCD screen.
     
    ErnieM and splendid-electron like this.
  5. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    Code ( (Unknown Language)):
    1.  do
    2.        {
    3.            T0CON.TMR0ON = 1;
    4.            TMR0L = 0;
    5.            TMR0H = 0;
    6.            IR_Tx = 1;
    7.            Delay_ms(1000); // Wait for 1 sec
    8.            IR_Tx = 0;
    9.            T0CON.TMR0ON = 0;    // Stop the timer
    10.            RPM_Value = (256*TMR0H + TMR0L)*60;
    11.            Display_RPM(RPM_Value);
    12.            //----
    Hello :)
    please help me to understand how this can count the number of slots
    specialy this
    Code ( (Unknown Language)):
    1.  RPM_Value = (256*TMR0H + TMR0L)*60;
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    The code
    Code ( (Unknown Language)):
    1. 256*TMR0H
    shifts the value in the most significant byte in the counter register 8 bits to the left, which is typically done as
    Code ( (Unknown Language)):
    1. TMR0H<<8
    , but that's not important...
    The shift is needed because the counter is 16 bits, but the registers are 8 bits wide, so you shift the most significant byte into position and add the least significant byte.

    Since this count is over the period of
    Code ( (Unknown Language)):
    1. Delay_ms(1000); // Wait for 1 sec
    this gives us counts per second. Multiplying by 60 gives counts per hour.
     
    splendid-electron likes this.
  7. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    Code ( (Unknown Language)):
    1. T0CON.TMR0ON = 1;
    this line?
    Code ( (Unknown Language)):
    1.  RPM_Value = (256*TMR0H + TMR0L)*60;
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    This turns the timer on..
    What is the question?
     
    splendid-electron likes this.
  9. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    what is the functionof this line exactly why 256???
     
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Did you read post #6?
     
    splendid-electron likes this.
  11. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    aaah yes thank you so much for your help it was helpful :))

    ..
     
  12. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    I need a prograam on c# that display the speed via USB on the screen of computer can u help me
     
  13. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    If you are using Visual Studio and WindowsForms, it can be pretty easy if you use a USB to TTL converter.

    Without too much scrutiny, this looks like a good reference.
     
    splendid-electron likes this.
  14. splendid-electron

    Thread Starter New Member

    Jan 26, 2014
    14
    0
    thank you so much :)
     
Loading...