Programm on MKICROC

Thread Starter

splendid-electron

Joined Jan 26, 2014
14
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

Rich (BB code):
 sbit IR_Tx at RA3_bit;
// Define Messages
 char message1[] = "2014TACHY.V3.COM";
 char *RPM = "00000 RPM";
//================================================
 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);
   }
//================================================
 void main()
   {
      CMCON = 0x07;   // Disable comparators
      ADCON1 = 0x0F;  // Disable Analog functions
      //TRISC = 0x00;
      TRISC = 0x80;        // Port C output except RC7 RX
      TRISB = 0x00;
      PORTA = 0x00;
      TRISA = 0b00010000;
      T0CON = 0b01101000; // TMR0 as 16-bit counter
      Lcd_Init();        // Initialize LCD
      Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
      Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off
      Lcd_Out(1,1,message1);            // Write message1 in 1st row
      //----
       UART1_Init(9600);                         // initialize UART1 module
       Delay_ms(100);
      //----
      do
       {
           T0CON.TMR0ON = 1;
           TMR0L = 0;
           TMR0H = 0;
           IR_Tx = 1;
           Delay_ms(1000); // Wait for 1 sec
           IR_Tx = 0;
           T0CON.TMR0ON = 0;    // Stop the timer
           RPM_Value = (256*TMR0H + TMR0L)*60;
           Display_RPM(RPM_Value);
           //----
           
           LongToStr(RPM_Value, txt);
           UART_Write_Text("N");
           Delay_ms(10); // Wait for 1 sec
           //UART_Write_Text("2014TACHY.V3.COM");
           UART_Write_Text(txt);
           //----
       } while(1);             // Infinite Loop
 }
 
Last edited by a moderator:

Thread Starter

splendid-electron

Joined Jan 26, 2014
14
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
 

tshuck

Joined Oct 18, 2012
3,534
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.
 

Thread Starter

splendid-electron

Joined Jan 26, 2014
14
Rich (BB code):
 do
       {
           T0CON.TMR0ON = 1;
           TMR0L = 0;
           TMR0H = 0;
           IR_Tx = 1;
           Delay_ms(1000); // Wait for 1 sec
           IR_Tx = 0;
           T0CON.TMR0ON = 0;    // Stop the timer
           RPM_Value = (256*TMR0H + TMR0L)*60;
           Display_RPM(RPM_Value);
           //----
Hello :)
please help me to understand how this can count the number of slots
specialy this
Rich (BB code):
 RPM_Value = (256*TMR0H + TMR0L)*60;
 

tshuck

Joined Oct 18, 2012
3,534
The code
Rich (BB code):
256*TMR0H
shifts the value in the most significant byte in the counter register 8 bits to the left, which is typically done as
Rich (BB code):
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
Rich (BB code):
Delay_ms(1000); // Wait for 1 sec
this gives us counts per second. Multiplying by 60 gives counts per hour.
 
Top