Advertisement
stoneman

MIKS Lab 4 (Mikro C)

May 25th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. int hex2int(char *a, unsigned int len)
  2. {
  3.     int i;
  4.     int val = 0;
  5.  
  6.     for(i=0;i<len;i++)
  7.        if(a[i] <= 57)
  8.         val += (a[i]-48)*(1<<(4*(len-1-i)));
  9.        else
  10.         val += (a[i]-55)*(1<<(4*(len-1-i)));
  11.     return val;
  12. }
  13.  
  14. sbit LCD_RS at RB4_bit;
  15. sbit LCD_EN at RB5_bit;
  16. sbit LCD_D4 at RB0_bit;
  17. sbit LCD_D5 at RB1_bit;
  18. sbit LCD_D6 at RB2_bit;
  19. sbit LCD_D7 at RB3_bit;
  20.  
  21. sbit LCD_RS_Direction at TRISB4_bit;
  22. sbit LCD_EN_Direction at TRISB5_bit;
  23. sbit LCD_D4_Direction at TRISB0_bit;
  24. sbit LCD_D5_Direction at TRISB1_bit;
  25. sbit LCD_D6_Direction at TRISB2_bit;
  26. sbit LCD_D7_Direction at TRISB3_bit;
  27.  
  28. unsigned char readbuff[64] absolute 0x500;
  29. unsigned char writebuff[64] absolute 0x540;
  30.  
  31. void main()
  32. {
  33.      int ulaz_status;
  34.      char hex_cifra[3];
  35.      int tmp_broj;
  36.      int i;
  37.      int broj_konverzija = 0;
  38.      
  39.      ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
  40.      CMCON  |= 7;                            // Disable comparators
  41.      
  42.      Lcd_Init();                             // Initialize Lcd
  43.      HID_Enable(&readbuff,&writebuff);       // Enable HID communication
  44.      
  45.      Lcd_Cmd(_LCD_CLEAR);
  46.      Lcd_Out(1,1,"Broj karaktera:");
  47.      
  48.      while(1)
  49.      {
  50.          USB_Polling_Proc();
  51.          Lcd_Out(2,1, broj_konverzija);
  52.          ulaz_status = HID_Read();
  53.          if(ulaz_status != 0)
  54.          {
  55.              for(i=0; i<64; i=i+1)
  56.              {
  57.                  ByteToHex(readbuff[i], hex_cifra);
  58.                  tmp_broj = hex2int(hex_cifra, 2);
  59.                  if(tmp_broj == 0)
  60.                  {
  61.                      tmp_broj = 45;
  62.                  }
  63.                  else
  64.                  {
  65.                      if(tmp_broj >= 32 && tmp_broj <=126)
  66.                      {
  67.                          if(tmp_broj >= 97 && tmp_broj <=122)
  68.                          {
  69.                              tmp_broj -= 32;
  70.                              broj_konverzija++;
  71.                          }
  72.                      }
  73.                      else
  74.                      {
  75.                          tmp_broj = 45;
  76.                      }
  77.                  }
  78.                  
  79.                  //prevesti tmp_broj u Hex broj
  80.                  //Dobijeni Hex broj smestiti u lokaciju i
  81.                  writebuff[i]=readbuff[i];
  82.              }
  83.              Lcd_Out(//promenjiva za postavljanje kursora na pocetak drugog reda);
  84.              HID_Write(&writebuff,64);
  85.          }
  86.      }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement