Guest User

Untitled

a guest
Oct 5th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.65 KB | None | 0 0
  1. Embedded Systems Final Journal
  2.  
  3. Practical 1
  4.  
  5. Aim: Elevator using 8051
  6.  
  7. Solution:
  8. Code-
  9. #include <reg51.h>
  10.  
  11. sbit floorg=P1^0;
  12. sbit floor1=P1^1;
  13. sbit floor2=P1^2;
  14. sbit floor3=P1^3;
  15.  
  16. static int current_floor=0;
  17. static int req_floor;
  18.  
  19. void MsDelay(unsigned int values)
  20. {
  21. unsigned int x,y;
  22. for(x=0;x<1275;x++);
  23. for(y=0;y<values;y++);
  24. }
  25.  
  26. void userinput()
  27. {
  28.  
  29.  
  30. if(floorg==0)
  31. {
  32. req_floor=0;
  33. }
  34. else if(floor1==0)
  35. {
  36. req_floor=1;
  37. }
  38. else if(floor2==0)
  39. {
  40. req_floor=2;
  41. }
  42. else if(floor3==0)
  43. {
  44. req_floor=3;
  45. }
  46.  
  47. }
  48.  
  49. void reqresponse(int req_floor)
  50. {
  51.  
  52. unsigned int digi_val[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  53. unsigned int bin[]={0x01,0x03,0x02,0x06,0x04,0x06,0x08,0x09};
  54. unsigned int i;
  55.  
  56. if(req_floor==current_floor)
  57. {
  58. P2=digi_val[current_floor];
  59. P0=0x00;
  60. }
  61. else if(req_floor > current_floor)
  62. {
  63. int diff=req_floor-current_floor;
  64. P2=digi_val[current_floor+diff];
  65. MsDelay(200);
  66. current_floor=req_floor;
  67. for(i=8;i>0;i--)
  68. {
  69. P0=bin[i];
  70. MsDelay(7);
  71. }
  72.  
  73. }
  74. else if(req_floor < current_floor)
  75. {
  76. int diff=current_floor-req_floor;
  77. P2=digi_val[current_floor-diff];
  78. MsDelay(200);
  79. current_floor=req_floor;
  80. for(i=0;i<8;i++)
  81. {
  82. P0=bin[i];
  83. MsDelay(7);
  84. }
  85. }
  86.  
  87. }
  88.  
  89. void main()
  90. {
  91.  
  92. while(1)
  93. {
  94. reqresponse(req_floor);
  95. userinput();
  96. }
  97. }
  98.  
  99. Design-
  100.  
  101.  
  102.  
  103. Working-
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. Practical 2 Design Traffic Simulator
  111.  
  112. Code:
  113. #### Traffic Light #####
  114. #include<reg51.h>
  115. sbit R1=P1^0;
  116. sbit Y1=P1^1;
  117. sbit G1=P1^2;
  118. sbit R2=P1^3;
  119. sbit Y2=P1^4;
  120. sbit G2=P1^5;
  121. sbit R3=P2^0;
  122. sbit Y3=P2^1;
  123. sbit G3=P2^2;
  124. sbit R4=P2^3;
  125. sbit Y4=P2^4;
  126. sbit G4=P2^5;
  127.  
  128.  
  129. void msdelay(unsigned int t)
  130. {
  131. int i,j;
  132. int k=100*t;
  133. for(i=0;i<k;i++)
  134. for(j=0;j<1275;j++);
  135. }
  136.  
  137. void clear()
  138. {
  139. R1=0;
  140. R2=0;
  141. R3=0;
  142. R4=0;
  143. Y1=0;
  144. Y2=0;
  145. Y3=0;
  146. Y4=0;
  147. G1=0;
  148. G2=0;
  149. G3=0;
  150. G4=0;
  151. }
  152.  
  153.  
  154. void phase1()
  155. {
  156. R1=1;
  157. R2=1;
  158. G3=1;
  159. R4=1;
  160. msdelay(25);
  161. Y3=1;
  162. Y1=1;
  163. msdelay(5);
  164. }
  165.  
  166. void phase2()
  167. {
  168. G1=1;
  169. R2=1;
  170. R3=1;
  171. R4=1;
  172. msdelay(25);
  173. Y2=1;
  174. Y1=1;
  175. Y4=1;
  176. msdelay(5);
  177. }
  178.  
  179. void phase3()
  180. {
  181. R1=1;
  182. G2=1;
  183. R3=1;
  184. G4=1;
  185. msdelay(25);
  186. Y2=1;
  187. Y4=1;
  188. msdelay(5);
  189. }
  190.  
  191. void phase4()
  192. {
  193. R1=1;
  194. G2=1;
  195. R3=1;
  196. R4=1;
  197. msdelay(25);
  198. Y1=1;
  199. Y2=1;
  200. Y3=1;
  201. msdelay(5);
  202. }
  203.  
  204. void phase5()
  205. {
  206. G1=1;
  207. R2=1;
  208. G3=1;
  209. R4=1;
  210. msdelay(25);
  211. Y1=1;
  212. Y3=1;
  213. Y4=1;
  214. msdelay(5);
  215. }
  216.  
  217. void phase6()
  218. {
  219. R1=1;
  220. R2=1;
  221. R3=1;
  222. G4=1;
  223. msdelay(25);
  224. Y3=1;
  225. Y4=1;
  226. msdelay(5);
  227. }
  228. void main()
  229. {
  230. P1=0x00;
  231. P2=0x00;
  232. while(1)
  233. {
  234. phase1();
  235. clear();
  236. phase2();
  237. clear();
  238. phase3();
  239. clear();
  240. phase4();
  241. clear();
  242. phase5();
  243. clear();
  244. phase6();
  245. clear();
  246. } }
  247.  
  248.  
  249. Design:
  250.  
  251.  
  252.  
  253.  
  254. Simulation:
  255.  
  256.  
  257.  
  258. Practical No:-3
  259. Analog to Digital Convertor
  260.  
  261. Main.c
  262. #include "Includes.h"
  263. // Main function
  264. void main()
  265. {
  266. unsigned char ADC_Value = 0; // To capture ADC value
  267. unsigned char Digit[3] = { 0,0,0 }; // To make digits to display on LCD
  268.  
  269. InitADC(); // Initialize ADC
  270. InitLCD(); // Initialize LCD display
  271. WriteStringToLCD("Starting..."); // Show Welcome Message
  272. delay_sec(1); // 1 Sec delay
  273. ClearLCDScreen(); // Clear LCD
  274.  
  275. while(1)
  276. {
  277. ADC_Value = ReadADC(AN1); // Read ADC value from Channel 1
  278.  
  279. Digit[2] = (unsigned char)( ADC_Value/100); // Find out first digit
  280. Digit[1] = (unsigned char)( ADC_Value/10) - Digit[2]*10; // Find out second digit
  281. Digit[0] = ADC_Value - Digit[2]*100 - Digit[1]*10; // Find out third digit
  282.  
  283. ClearLCDScreen(); // Clear LCD
  284. WriteStringToLCD("ADC value = "); // Display string
  285. WriteDataToLCD(Digit[2]+0x30); // Display first digit
  286. WriteDataToLCD(Digit[1]+0x30); // Display second digit
  287. WriteDataToLCD(Digit[0]+0x30); // Display third digit
  288.  
  289. delay_sec(1); // Delay of one second
  290. }
  291. }
  292.  
  293. LED.c
  294. #include "Includes.h"
  295.  
  296. // Function Purpose: Produce approximate delay in Secs.
  297. void delay_sec(unsigned int d)
  298. {
  299. unsigned int i;
  300.  
  301. for(i=0;i<(d*20);i++)
  302. __delay_us(50000);
  303. }
  304.  
  305.  
  306. // Function Purpose: Produce approximate delay in given uSecs.
  307. void __delay_us(unsigned int d)
  308. {
  309. unsigned int i, limit;
  310. limit = d/15;
  311.  
  312. for(i=0;i<limit;i++);
  313. }
  314.  
  315. void ToggleEpinOfLCD(void)
  316. {
  317. E = 1; // Give a pulse on E pin
  318. __delay_us(E_Delay); // so that LCD can latch the
  319. E = 0; // data from data bus
  320. __delay_us(E_Delay);
  321. }
  322.  
  323. void WriteDataToLCD(char t)
  324. {
  325. RS = 1; // This is data
  326.  
  327. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  328. P1 |= (t&0xF0); // Write Upper nibble of data
  329. ToggleEpinOfLCD(); // Toggle E pin to send data
  330.  
  331. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  332. P1 |= ((t<<4)&0xF0);// Write Lower nibble of data
  333. ToggleEpinOfLCD(); // Toggle E pin to send data
  334. }
  335.  
  336.  
  337. void WriteCommandToLCD(int z)
  338. {
  339. RS = 0; // This is command
  340.  
  341. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  342. P1 |= (z&0xF0); // Write Upper nibble of data
  343. ToggleEpinOfLCD(); // Toggle E pin to send data
  344.  
  345. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  346. P1 |= ((z<<4)&0xF0);// Write Lower nibble of data
  347. ToggleEpinOfLCD(); // Toggle E pin to send data
  348. }
  349.  
  350. void InitLCD(void)
  351. {
  352. RS = 0; // Make pin zero
  353. E = 0; // Make Pin zero
  354.  
  355. ///////////// Reset process from datasheet /////////
  356. __delay_us(15000);
  357.  
  358. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  359. P1 |= 0x30; // Write 0x3
  360. ToggleEpinOfLCD(); // Toggle E pin to send data
  361.  
  362. __delay_us(4500);
  363.  
  364. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  365. P1 |= 0x30; // Write 0x3
  366. ToggleEpinOfLCD(); // Toggle E pin to send data
  367.  
  368. __delay_us(300);
  369.  
  370. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  371. P1 |= 0x30; // Write 0x3
  372. ToggleEpinOfLCD(); // Toggle E pin to send data
  373.  
  374. __delay_us(650);
  375.  
  376. P1 &= 0x0F; // Make P1.4 to P1.7 zero
  377. P1 |= 0x20; // Write 0x2
  378. ToggleEpinOfLCD(); // Toggle E pin to send data
  379.  
  380. __delay_us(650);
  381.  
  382. /////////////////////////////////////////////////////
  383. WriteCommandToLCD(0x28); //function set
  384. WriteCommandToLCD(0x0c); //display on,cursor off,blink off
  385. WriteCommandToLCD(0x01); //clear display
  386. WriteCommandToLCD(0x06); //entry mode, set increment
  387. }
  388.  
  389.  
  390. void WriteStringToLCD(const char *s)
  391. {
  392. while(*s)
  393. WriteDataToLCD(*s++); // print first character on LCD
  394. }
  395.  
  396.  
  397. void ClearLCDScreen(void) // Clear the Screen and return cursor to zero position
  398. {
  399. WriteCommandToLCD(0x01); // Clear the screen
  400. __delay_us(2000); // Delay for cursor to return at zero position
  401. }
  402.  
  403. ADC0808.c
  404. #include "Includes.h"
  405.  
  406.  
  407. void InitADC(void)
  408. {
  409. Add_A = 0; // Make output
  410. Add_B = 0; // Make output
  411. Add_C = 0; // Make output
  412. ALE = 0; // Make output
  413. EOC = 1; // Make input
  414. OE = 0; // Make output
  415. START = 0; // Make output
  416. CLK = 0; // Make output
  417.  
  418. Data_Bus = 0xFF; // Make Inputs
  419. }
  420.  
  421.  
  422.  
  423. unsigned char ReadADC(unsigned char Channel)
  424. {
  425. unsigned int i = 0;
  426. unsigned int ADC_value = 0;
  427.  
  428. // Select Channel
  429. switch(Channel)
  430. {
  431. case AN0: Add_C = 0; Add_B = 0; Add_A = 0; break;
  432. case AN1: Add_C = 0; Add_B = 0; Add_A = 1; break;
  433. case AN2: Add_C = 0; Add_B = 1; Add_A = 0; break;
  434. case AN3: Add_C = 0; Add_B = 1; Add_A = 1; break;
  435. case AN4: Add_C = 1; Add_B = 0; Add_A = 0; break;
  436. case AN5: Add_C = 1; Add_B = 0; Add_A = 1; break;
  437. case AN6: Add_C = 1; Add_B = 1; Add_A = 0; break;
  438. case AN7: Add_C = 1; Add_B = 1; Add_A = 1; break;
  439. }
  440.  
  441. __delay_us(HalfCycleDelay); // 250kHz Frequency
  442. ALE = 1; // Enable Address Latch
  443. CLK = 1; // Make CLK High
  444. __delay_us(HalfCycleDelay); // 250kHz Frequency
  445. CLK = 0; // Make CLK Low
  446. START = 1; // Start ADC Conversion
  447. __delay_us(HalfCycleDelay); // 250kHz Frequency
  448. CLK = 1; // Make CLK High
  449. ALE = 0; // Disable Address Latch
  450. __delay_us(HalfCycleDelay); // 250kHz Frequency
  451. CLK = 0; // Make CLK Low
  452. START = 0; // Complete the start pulse
  453.  
  454. for(i=0;i<2000;i++)
  455. {
  456. CLK = !CLK; // Toggle Clock
  457. __delay_us(HalfCycleDelay); // 250kHz Frequency
  458.  
  459. if(!EOC) // Wait for EOC to be low
  460. break;
  461. }
  462.  
  463. for(i=0;i<2000;i++)
  464. {
  465. CLK = !CLK; // Toggle Clock
  466. __delay_us(HalfCycleDelay); // 250kHz Frequency
  467.  
  468. if(EOC) // Wait for EOC to be High
  469. break;
  470. }
  471.  
  472. CLK = 0; // Make CLK Low
  473. OE = 1; // Enable Output
  474. __delay_us(HalfCycleDelay); // 250kHz Frequency
  475. CLK = 1; // Make CLK High
  476. __delay_us(HalfCycleDelay); // 250kHz Frequency
  477. CLK = 0; // Make CLK Low
  478. __delay_us(HalfCycleDelay); // 250kHz Frequency
  479. CLK = 1; // Make CLK High
  480.  
  481. ADC_value = Data_Bus; // Read value
  482.  
  483. __delay_us(HalfCycleDelay); // 250kHz Frequency
  484. OE = 0; // Disable Output
  485. CLK = 0; // Make CLK Low
  486. __delay_us(HalfCycleDelay); // 250kHz Frequency
  487.  
  488. return ADC_value; // Return ADC value
  489. }
  490.  
  491. ADC0808.h
  492.  
  493. #ifndef __ADC0808_H
  494. #define __ADC0808_H
  495.  
  496. // Define ADC Channels
  497. #define AN0 0
  498. #define AN1 1
  499. #define AN2 2
  500. #define AN3 3
  501. #define AN4 4
  502. #define AN5 5
  503. #define AN6 6
  504. #define AN7 7
  505.  
  506. // Define Pins
  507. sbit Add_A = P2^0; // Address Pin A
  508. sbit Add_B = P2^1; // Address Pin B
  509. sbit Add_C = P2^2; // Address Pin C
  510. sbit ALE = P2^3; // Address Latch Enable
  511. sbit EOC = P2^4; // End Of Conversion
  512. sbit OE = P2^5; // Output Enable
  513. sbit START = P2^6; // Start Conversion
  514. sbit CLK = P2^7; // Clock for AD0808
  515.  
  516. // Define Data Bus
  517. #define Data_Bus P0
  518.  
  519. #define HalfCycleDelay 10 // usecs
  520.  
  521. // Function Declarations
  522. void InitADC(void);
  523. unsigned char ReadADC(unsigned char);
  524.  
  525. #endif
  526.  
  527. Includes.h
  528.  
  529. #ifndef __INCLUDES_H
  530. #define __INCLUDES_H
  531.  
  532. #include<reg51.h>
  533. #include "LCD.h"
  534. #include "ADC0808.h"
  535.  
  536.  
  537. #endif
  538.  
  539. LCD.h
  540.  
  541. #ifndef __LCD_H
  542. #define __LCD_H
  543.  
  544. //*******************
  545. //Pin description
  546. /*
  547. P1.4 to P1.7 is data bus
  548. P1.1 is RS
  549. P1.0 is E
  550. */
  551. //********************
  552.  
  553. // Defines Pins
  554. sbit RS = P1^1;
  555. sbit E = P1^0;
  556.  
  557. // Constants
  558. #define E_Delay 250
  559.  
  560. // Function Declarations
  561. void delay_sec(unsigned int);
  562. void __delay_us(unsigned int);
  563. void InitLCD(void);
  564. void WriteCommandToLCD(int);
  565. void WriteDataToLCD(char);
  566. void WriteStringToLCD(const char*);
  567. void ClearLCDScreen(void);
  568.  
  569. #endif
  570. reg51.h
  571.  
  572. /*--------------------------------------------------------------------------
  573. REG51.H
  574.  
  575. Header file for generic 80C51 and 80C31 microcontroller.
  576. Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
  577. All rights reserved.
  578. --------------------------------------------------------------------------*/
  579.  
  580. #ifndef __REG51_H__
  581. #define __REG51_H__
  582.  
  583. /* BYTE Register */
  584. sfr P0 = 0x80;
  585. sfr P1 = 0x90;
  586. sfr P2 = 0xA0;
  587. sfr P3 = 0xB0;
  588. sfr PSW = 0xD0;
  589. sfr ACC = 0xE0;
  590. sfr B = 0xF0;
  591. sfr SP = 0x81;
  592. sfr DPL = 0x82;
  593. sfr DPH = 0x83;
  594. sfr PCON = 0x87;
  595. sfr TCON = 0x88;
  596. sfr TMOD = 0x89;
  597. sfr TL0 = 0x8A;
  598. sfr TL1 = 0x8B;
  599. sfr TH0 = 0x8C;
  600. sfr TH1 = 0x8D;
  601. sfr IE = 0xA8;
  602. sfr IP = 0xB8;
  603. sfr SCON = 0x98;
  604. sfr SBUF = 0x99;
  605.  
  606.  
  607. /* BIT Register */
  608. /* PSW */
  609. sbit CY = 0xD7;
  610. sbit AC = 0xD6;
  611. sbit F0 = 0xD5;
  612. sbit RS1 = 0xD4;
  613. sbit RS0 = 0xD3;
  614. sbit OV = 0xD2;
  615. sbit P = 0xD0;
  616.  
  617. /* TCON */
  618. sbit TF1 = 0x8F;
  619. sbit TR1 = 0x8E;
  620. sbit TF0 = 0x8D;
  621. sbit TR0 = 0x8C;
  622. sbit IE1 = 0x8B;
  623. sbit IT1 = 0x8A;
  624. sbit IE0 = 0x89;
  625. sbit IT0 = 0x88;
  626.  
  627. /* IE */
  628. sbit EA = 0xAF;
  629. sbit ES = 0xAC;
  630. sbit ET1 = 0xAB;
  631. sbit EX1 = 0xAA;
  632. sbit ET0 = 0xA9;
  633. sbit EX0 = 0xA8;
  634.  
  635. /* IP */
  636. sbit PS = 0xBC;
  637. sbit PT1 = 0xBB;
  638. sbit PX1 = 0xBA;
  639. sbit PT0 = 0xB9;
  640. sbit PX0 = 0xB8;
  641.  
  642. /* P3 */
  643. sbit RD = 0xB7;
  644. sbit WR = 0xB6;
  645. sbit T1 = 0xB5;
  646. sbit T0 = 0xB4;
  647. sbit INT1 = 0xB3;
  648. sbit INT0 = 0xB2;
  649. sbit TXD = 0xB1;
  650. sbit RXD = 0xB0;
  651.  
  652. /* SCON */
  653. sbit SM0 = 0x9F;
  654. sbit SM1 = 0x9E;
  655. sbit SM2 = 0x9D;
  656. sbit REN = 0x9C;
  657. sbit TB8 = 0x9B;
  658. sbit RB8 = 0x9A;
  659. sbit TI = 0x99;
  660. sbit RI = 0x98;
  661.  
  662. #endif
  663. KEIL
  664.  
  665. Proteous
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680. Practical 4
  681.  
  682. Stepper Motor
  683.  
  684. Code -
  685. #include<reg51.h>
  686. void MSDelay(unsigned int value);
  687. void main()
  688. {
  689. unsigned int i,j;
  690. const char bin[]= {0x01, 0x02, 0x04, 0x08};
  691. P1 = 0x00;
  692. while(1)
  693. {
  694. if(P1^4)
  695. {
  696. for(i=0;i<4;i++)
  697. {
  698. P1 = bin[i];
  699. MSDelay(2);
  700. }
  701. }
  702. else
  703. P1 = 0x00;
  704. }
  705. }
  706. void MSDelay(unsigned int value)
  707. {
  708. unsigned int x,y;
  709. for(x=0; x<1200; x++)
  710. for(y=0; y<value; y++);
  711. }
  712.  
  713.  
  714. Circuit -
  715.  
  716.  
  717.  
  718.  
  719. Practical No 5
  720.  
  721. Create a push button interface
  722.  
  723. Code:
  724.  
  725. #include<reg51.h>
  726. void delay(int dly)
  727. {
  728. int i,j;
  729. for(i=0;i<=dly;i++)
  730. for(j=0;j<=500;j++)
  731. {};
  732. }
  733.  
  734. sbit l1=P2^0;
  735. sbit l2=P2^1;
  736. sbit l3=P2^2;
  737. sbit l4=P2^3;
  738. sbit l5=P2^4;
  739. sbit l6=P2^5;
  740. sbit l7=P2^6;
  741. sbit l8=P2^7;
  742. sbit s1 =P1^0;
  743. sbit s2 =P1^1;
  744. sbit s3 =P1^2;
  745. sbit s4 =P1^3;
  746. sbit s5 =P1^4;
  747. sbit s6 =P1^5;
  748. sbit s7 =P1^6;
  749. sbit s8 =P1^7;
  750. main()
  751. {
  752. unsigned int digit_val[]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0x00,0x10};
  753. P1=0x00;
  754. P2=0x00;
  755. while(1)
  756. {
  757. if(s1==1)
  758. {
  759. l1=1;
  760. delay(50);
  761. P3=0x40;
  762. }
  763. else if(s2==1)
  764. {
  765. l2=1;
  766. delay(50);
  767. P3=0xF9;
  768. }
  769. else if(s3==1)
  770. {
  771. l3=1;
  772. delay(50);
  773. P3=0x24;
  774. }
  775. else if(s4==1)
  776. {
  777. l4=1;
  778. delay(50);
  779. P3= 0x30;
  780. }
  781. else if(s5==1)
  782. {
  783. l5=1;
  784. delay(50);
  785. P3=0x19;
  786. }
  787. else if(s6==1)
  788. {
  789. l6=1;
  790. delay(50);
  791. P3=0x12;
  792. }
  793. else if(s7==1)
  794. {
  795. l7=1;
  796. delay(50);
  797. P3=0x02;
  798. }
  799. else if(s8==1)
  800. {
  801. l8=1;
  802. delay(50);
  803. P3=0xF8;
  804. }
  805. }
  806. }
  807.  
  808. Proteous simulation:
  809.  
  810.  
  811.  
  812. Practical 6
  813.  
  814. SerialCommuncation
  815.  
  816. #include <reg51.h>
  817.  
  818. void cct_init(void);
  819.  
  820.  
  821. void SerialInitialize(void);
  822.  
  823. void uart_msg(unsigned char *c);
  824.  
  825. void uart_tx(unsigned char);
  826.  
  827. sbit led1 = P1^0;
  828.  
  829. sbit led2 = P1^1;
  830.  
  831. sbit led3 = P1^2;
  832.  
  833. sbit led4 = P1^3;
  834.  
  835.  
  836.  
  837.  
  838. void main()
  839.  
  840. {
  841.  
  842. cct_init();
  843.  
  844. SerialInitialize();
  845.  
  846. EA = 1;
  847.  
  848. ES = 1;
  849.  
  850. uart_msg("Initializing Serial Communication");
  851.  
  852. uart_tx(0x0d);
  853.  
  854. uart_msg("1,2,3,4 key can on leds and a,b,c,d can off them respectively.");
  855.  
  856. uart_tx(0x0d); //next line
  857.  
  858. uart_msg("Press the key for particular LED");
  859.  
  860. uart_tx(0x0d);
  861.  
  862. while(1);
  863.  
  864. }
  865.  
  866. void cct_init(void) //initialize cct
  867.  
  868. {
  869.  
  870. P0 = 0x00; //not used
  871.  
  872. P1 = 0x00; //output port used for leds
  873.  
  874. P2 = 0x00; //not used
  875.  
  876. P3 = 0x03; //used for serial communication
  877.  
  878. }
  879.  
  880. void SerialInitialize(void) //Initialize Serial Port
  881.  
  882. {
  883.  
  884. TMOD = 0x20; //Timer 1 In Mode 2 -Auto Reload to Generate Baud Rate
  885.  
  886. SCON = 0x50; //Serial Mode 1, 8-Data Bit, REN Enabled
  887.  
  888. TH1 = 0xFD; //Load Baud Rate 9600 To Timer Register
  889.  
  890. TR1 = 1; //Start Timer
  891.  
  892. }
  893. void uart_msg(unsigned char *c)
  894.  
  895. {
  896.  
  897. while(*c != 0)
  898.  
  899. {
  900.  
  901. uart_tx(*c++);
  902.  
  903. }
  904.  
  905. }
  906.  
  907. void uart_tx(unsigned char serialdata)
  908.  
  909. {
  910.  
  911. SBUF = serialdata; //Load Data to Serial Buffer Register
  912.  
  913. while(TI == 0); //Wait Until Transmission To Complete
  914.  
  915. TI = 0; //Clear Transmission Interrupt Flag
  916.  
  917. }
  918.  
  919. void serial_ISR (void) interrupt 4
  920.  
  921. {
  922.  
  923. char chr; //receive character
  924.  
  925. if(RI==1)
  926.  
  927. {
  928.  
  929. chr = SBUF;
  930.  
  931. RI = 0;
  932.  
  933. }
  934.  
  935. P0 = ~P0; //Show the data has been updated
  936.  
  937. switch(chr)
  938.  
  939. {
  940.  
  941. case '1': led1 = 1; uart_msg("1st on"); uart_tx(0x0d); break;
  942.  
  943. case '2': led2 = 1; uart_msg("2nd on"); uart_tx(0x0d); break;
  944.  
  945. case '3': led3 = 1; uart_msg("3rd on"); uart_tx(0x0d); break;
  946.  
  947. case '4': led4 = 1; uart_msg("4th on"); uart_tx(0x0d); break;
  948.  
  949. case 'a': led1 = 0; uart_msg("1st off"); uart_tx(0x0d); break;
  950.  
  951. case 'b': led2 = 0; uart_msg("2nd off"); uart_tx(0x0d); break;
  952.  
  953. case 'c': led3 = 0; uart_msg("3rd off"); uart_tx(0x0d); break;
  954.  
  955. case 'd': led4 = 0; uart_msg("4th off"); uart_tx(0x0d); break;
  956.  
  957.  
  958. default: ; break; //do nothing
  959.  
  960. }
  961.  
  962. RI = 0;
  963.  
  964. }
  965.  
  966. Build new project in keil
  967.  
  968.  
  969.  
  970.  
  971. Right click on virtual terminal/edit properties/baud rate = 9600
  972.  
  973. Right click on 8051/edit properties/clock frequency = 11.0592
  974.  
  975.  
  976.  
  977.  
  978.  
  979. Practical 7
  980.  
  981. 7 Segment display
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003. Practical 8
  1004.  
  1005. Binary counter
  1006. #include <reg51.h>
  1007.  
  1008.  
  1009. void main()
  1010. {
  1011. unsigned char x,y;
  1012. unsigned int i;
  1013. P1=0x00;
  1014.  
  1015. while(1)
  1016. {
  1017. x=0x01;
  1018. for(y=0;y<9;y++)
  1019. {
  1020. P1=x;
  1021. for(i=0;i<60000;i++);
  1022. x=x<<1;
  1023.  
  1024. }
  1025. }
  1026. }
  1027.  
  1028.  
  1029.  
  1030. Big Data Final Journal
  1031.  
  1032. Practical No 1
  1033.  
  1034. TimeSeries analysis
  1035.  
  1036. mydata<-read.csv("D:/jobs.csv")
  1037.  
  1038. mydata
  1039.  
  1040.  
  1041.  
  1042. attach(mydata)
  1043.  
  1044. x<-Month
  1045.  
  1046. Y<-Total.Filled.Jobs
  1047.  
  1048. X
  1049.  
  1050.  
  1051. d.y<-diff(Y)
  1052.  
  1053. plot(x,Y)
  1054.  
  1055.  
  1056. acf(Y)
  1057.  
  1058.  
  1059. pacf(Y)
  1060.  
  1061.  
  1062. acf(d.y)
  1063.  
  1064.  
  1065. arima(x=Y, order=c(1,0,1))
  1066.  
  1067.  
  1068. arima(x=Y, order=c(0,0,1))
  1069.  
  1070.  
  1071.  
  1072. show(mydata)
  1073.  
  1074. mydata.arima001<-arima(Y,order=c(0,0,1))
  1075.  
  1076. mydata.pred1<-predict(mydata.arima001,n.ahead=100)
  1077.  
  1078. plot(Y)
  1079.  
  1080.  
  1081.  
  1082.  
  1083. lines(mydata.pred1$pred,col="blue")
  1084.  
  1085.  
  1086. attach(mydata.pred1)
  1087.  
  1088. head(mydata.pred1)
  1089.  
  1090.  
  1091. tail(mydata.pred1$pred)
  1092.  
  1093. head(mydata.pred1$pred)
  1094.  
  1095.  
  1096. Practical No 2
  1097.  
  1098. Topic Modeling
  1099.  
  1100. library(tm)
  1101. library(topicmodels)
  1102. setwd("D:/british-fiction-corpus")
  1103. filenames<-list.files(path="D:/british-fiction-corpus",pattern="*.txt")
  1104. filenames
  1105.  
  1106. filetext<-lapply(filenames,readLines)
  1107. mycorpus<-Corpus(VectorSource(filetext))
  1108. mycorpus<-tm_map(mycorpus,removeNumbers)
  1109. mycorpus<-tm_map(mycorpus,removePunctuation)
  1110. mycorpus
  1111.  
  1112.  
  1113. mystopwords=c("of","a","and","the","in","to","for","that","is","on","are","with","as","by"
  1114. ,"be","an","which","it","from","or","can","have","these","has","such","you")
  1115. mycorpus<-tm_map(mycorpus,tolower)
  1116. mycorpus<-tm_map(mycorpus,removeWords,mystopwords)
  1117. dtm<-DocumentTermMatrix(mycorpus)
  1118. k<-3
  1119.  
  1120.  
  1121. #lda_output_3<-LDA(dtm,k,method="VEM",control=control_VEM)
  1122. # control_VEM
  1123.  
  1124. lda_output_3<-LDA(dtm,k,method="VEM",control=NULL)
  1125. lda_output_3<-LDA(dtm,k,method="VEM")
  1126. dtm
  1127.  
  1128.  
  1129. lda_output_3<-LDA(dtm,k,method="VEM")
  1130. lda_output_3@Dim
  1131. lda_output_3<-LDA(dtm,k,method="VEM")
  1132. show (dtm)
  1133.  
  1134.  
  1135. topics(lda_output_3)
  1136. terms(lda_output_3,10)
  1137.  
  1138.  
  1139. library(tm)
  1140. library(topicmodels)
  1141.  
  1142. setwd("D:/wd")
  1143.  
  1144.  
  1145. filenames<-list.files(path="D:/wd",pattern="*.txt")
  1146.  
  1147. filenames
  1148.  
  1149. filetext<-lapply(filenames,readLines)
  1150. mycorpus<-Corpus(VectorSource(filetext))
  1151.  
  1152. mycorpus<-tm_map(mycorpus,removeNumbers)
  1153. mycorpus<-tm_map(mycorpus,removePunctuation)
  1154.  
  1155. mycorpus
  1156. mystopwords=c("of","a","and","the","in","to","for","that","is","on","are","with","as","by"
  1157. ,"be","an","which","it","from","or","can","have","these","has","such","you")
  1158. mycorpus<-tm_map(mycorpus,tolower)
  1159. mycorpus<-tm_map(mycorpus,removeWords,mystopwords)
  1160. dtm<-DocumentTermMatrix(mycorpus)
  1161. k<-2
  1162. #lda_output_3<-LDA(dtm,k,method="VEM",control=control_VEM)
  1163.  
  1164. # control_VEM
  1165. #lda_output_3<-LDA(dtm,k,method="VEM",control=NULL)
  1166. lda_output_3<-LDA(dtm,k,method="VEM")
  1167. dtm
  1168.  
  1169. lda_output_3<-LDA(dtm,k,method="VEM")
  1170. lda_output_3@Dim
  1171. lda_output_3<-LDA(dtm,k,method="VEM")
  1172. show (dtm)
  1173. topics(lda_output_3)
  1174. terms(lda_output_3,10)
  1175.  
  1176.  
  1177.  
  1178.  
  1179. Practical 3
  1180.  
  1181. Sentiment Analysis
  1182.  
  1183. weatherdata<-read.csv("D:/weather.csv")
  1184. library(tm)
  1185. corpus<-iconv(weatherdata$tweet_text,to="utf-8")
  1186. corpus<-Corpus(VectorSource(corpus))
  1187. corpus<tm_map(corpus,tolower)
  1188. corpus<-tm_mapcorpus,tolower)
  1189. corpus<-tm_map(corpus,removePunctuation)
  1190. inspect(corpus[1:5])
  1191. corpus<-tm_map(corpus,removeNumbers)
  1192. inspect(corpus[1:5])
  1193. corpus<-tm_map(corpus,removeWords,stopwords('english'))
  1194. corpus<-tm_map(corpus,stripWhitespace)
  1195. tdm<-TermDocumentMatrix(corpus)
  1196. tdm
  1197. tdm<-as.matrix(tdm)
  1198. tdm[1:10,1:2]
  1199. w<-rowSums(tdm)
  1200. library(wordcloud)
  1201. w<-sort(rowSums(tdm),decreasing = TRUE)
  1202. set.seed(222)
  1203. wordcloud(words = names(w),freq =
  1204. w,max.words=150,random.order=F,min.freq = 5,colors =
  1205. brewer.pal(8,'Dark2'))
  1206. wordcloud(words = names(w),freq =
  1207. w,max.words=150,random.order=F,min.freq = 5,colors =
  1208. brewer.pal(8,'Dark2'),rot.per=0.3)
  1209.  
  1210. library(syuzhet)
  1211. library(lubridate)
  1212. library(ggplot2)
  1213. library(scales)
  1214. library(reshape2)
  1215. library(dplyr)
  1216. weatherdata<-read.csv("D:/weather.csv")
  1217. tweets<-iconv(weatherdata$tweet_text,to="utf-8")
  1218. s<-get_nrc_sentiment(tweets)
  1219. head(s)
  1220. tweets[4]
  1221. barplot(colSums(s),las=2,col=rainbow(10),ylab='count',main='Sentiments
  1222. for weather')
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239. Practical 4
  1240.  
  1241. Kshingles
  1242. readinteger<- function()
  1243. {
  1244.  n <-readline(prompt = "enter value of k-1: ")
  1245.  k <- as.integer(n)
  1246.  u1 <- readLines("D:\\wine.txt")
  1247.  Shingle <- 0
  1248.  i<- 0
  1249.  
  1250.  while(i<nchar(u1)-k+1)
  1251.  {
  1252.    Shingle[i] <- substr(u1,start = i,stop = i+k)
  1253.    print(Shingle[i])
  1254.    i=i+1
  1255.  }
  1256. }
  1257. Type command after running the above code.
  1258. readinteger()
  1259. Output
  1260.  
  1261.            Practical 5
  1262.  
  1263. Document similarity
  1264.  
  1265. Code -
  1266.  
  1267. library(textreuse)
  1268. library(graphics)
  1269. minhash<- minhash_generator(200, seed = 235)
  1270. ats<- TextReuseCorpus(dir="D:/R/french-plays",n=5,minhash_func = minhash)
  1271. buckets<- lsh(ats, bands = 50 ,progress = interactive())
  1272. candidates<- lsh_candidates(buckets)
  1273. my.df<- lsh_compare(candidates, ats , jaccard_similarity)
  1274. my.df
  1275. color <- c("red" , "green" , "blue" , "orange" , "yellow" , "pink")
  1276. barplot(as.matrix(my.df),col=color)
  1277.  
  1278.  
  1279. Output -
  1280.  
  1281.  
  1282.  
  1283.  
  1284. Practical 6
  1285. Setting up environment for Big Data Hadoop Sandbox:
  1286. 1. Download HDP Sandbox
  1287. Give full access control in file property
  1288. Use vitualbox 5.2 above
  1289. Restart machine
  1290.  
  1291. 2. Import Appliance from VirtualBox
  1292. once extraction is complete
  1293.  
  1294.  
  1295. Visit localhost:1080
  1296. 3. Install WinSCP
  1297. 4. Install putty
  1298. 5. Install/ launch Eclipse
  1299.  
  1300. 6.reset Password
  1301.  
  1302. user id: root
  1303. old pass: hadoop
  1304. new pass: ********
  1305.  
  1306. Admin password
  1307.  
  1308. to reset admin password type command:
  1309. ambari-admin-password-reset
  1310. new: ********
  1311.  
  1312.  
  1313.  
  1314. WinSCP
  1315. accessing on Cent OS File sys
  1316. host name: 127.0.0.1 port 2222
  1317. user: root Pass: jd@ruia19
  1318.  
  1319. accessing on Cent OS File sys using shell in box.
  1320. Ambari file explorer for Hadoop file system
  1321.  
  1322. at web shell cleint
  1323. login with : root pass: ********
  1324.  
  1325. If any alerts found restart processes
  1326.  
  1327.  
  1328. Practical 7 Wordcount
  1329.  
  1330. Start Virtual box and hadoop sandbox
  1331. Go to the given virtualbox link
  1332.  
  1333. Login in web shell client
  1334.  
  1335. Open ambari
  1336.  
  1337. Go to files view
  1338.  
  1339.  
  1340. Create program in eclipse
  1341.  
  1342. WordCount Code -
  1343. import java.io.IOException;
  1344. import java.util.StringTokenizer;
  1345.  
  1346. import org.apache.hadoop.conf.Configuration;
  1347. import org.apache.hadoop.fs.Path;
  1348. import org.apache.hadoop.io.IntWritable;
  1349. import org.apache.hadoop.io.Text;
  1350. import org.apache.hadoop.mapreduce.Job;
  1351. import org.apache.hadoop.mapreduce.Mapper;
  1352. import org.apache.hadoop.mapreduce.Reducer;
  1353. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  1354. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  1355. import org.apache.hadoop.util.GenericOptionsParser;
  1356.  
  1357. public class WordCount {
  1358.  
  1359. public static class TokenizerMapper
  1360. extends Mapper<Object, Text, Text, IntWritable>{
  1361.  
  1362. private final static IntWritable one = new IntWritable(1);
  1363. private Text word = new Text();
  1364.  
  1365. public void map(Object key, Text value, Context context
  1366. ) throws IOException, InterruptedException {
  1367. StringTokenizer itr = new StringTokenizer(value.toString());
  1368. while (itr.hasMoreTokens()) {
  1369. word.set(itr.nextToken());
  1370. context.write(word, one);
  1371. }
  1372. }
  1373. }
  1374.  
  1375. public static class IntSumReducer
  1376. extends Reducer<Text,IntWritable,Text,IntWritable> {
  1377. private IntWritable result = new IntWritable();
  1378.  
  1379. public void reduce(Text key, Iterable<IntWritable> values,
  1380. Context context
  1381. ) throws IOException, InterruptedException {
  1382. int sum = 0;
  1383. for (IntWritable val : values) {
  1384. sum += val.get();
  1385. }
  1386. result.set(sum);
  1387. context.write(key, result);
  1388. }
  1389. }
  1390.  
  1391. public static void main(String[] args) throws Exception {
  1392. Configuration conf = new Configuration();
  1393.  
  1394. Job job = new Job(conf, "word count");
  1395. job.setJarByClass(WordCount.class);
  1396. job.setMapperClass(TokenizerMapper.class);
  1397. job.setCombinerClass(IntSumReducer.class);
  1398. job.setReducerClass(IntSumReducer.class);
  1399. job.setOutputKeyClass(Text.class);
  1400. job.setOutputValueClass(IntWritable.class);
  1401. FileInputFormat.addInputPath(job, new Path(args[0]));
  1402. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  1403. System.exit(job.waitForCompletion(true) ? 0 : 1);
  1404. }
  1405. }
  1406.  
  1407. Add jar files
  1408. Right click on project name (wordcount) / properties / java build path / libraries / add external jar files
  1409. Export jar file
  1410. Right click on project name (wordcount) / Export / java / jar files / select your jar file / select main class
  1411. Create a wordlist on notepad or take any wordlist
  1412. open winscp
  1413. login with web shell client username and password
  1414. set host name 127.0.0.1 and port 2222
  1415.  
  1416.  
  1417. Copy the jar file you exported (wordcount.jar) in web shell
  1418. Change permissions in winscp - select the file (wordcount) right click/ properties/ chose all read write execute permissions
  1419.  
  1420. go to web shell and check if wordcount.jar file is uploaded
  1421. Command - ls
  1422.  
  1423. go to ambari
  1424. create a new folder - Wordcount
  1425. inside Wordcount create a new folder - Input
  1426.  
  1427.  
  1428. Change permissions to all folders to read write and execute - select folder and click on permissions
  1429. Then upload your wordlist in Input folder and give all permissions
  1430. go to web shell and type command -
  1431. hadoop jar wordcount.jar /Wordcount/Input /Wordcount/Output
  1432.  
  1433. Note - hadoop jar wordcount.jar(is the name of your jar file you uploaded)
  1434. New Output folder will be created with the output of your wordlist
  1435.  
  1436. Now go in the Wordcount/Output folder
  1437.  
  1438. Select and open the part-r-00000 file. This file shows the output of the wordcount
  1439.  
  1440.  
  1441. Practical 8
  1442. Top 5 categories
  1443.  
  1444. Start Virtual box and hadoop sandbox
  1445. Go to the given virtualbox link
  1446.  
  1447. Login in web shell client
  1448.  
  1449. Open ambari
  1450.  
  1451. Go to files view
  1452.  
  1453.  
  1454.  
  1455. Create program in eclipse
  1456.  
  1457. Top 5 categories of videos Code -
  1458. import java.io.IOException;
  1459.  
  1460. import org.apache.hadoop.fs.Path;
  1461. import org.apache.hadoop.conf.*;
  1462. import org.apache.hadoop.io.*;
  1463. import org.apache.hadoop.mapreduce.*;
  1464. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  1465. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  1466. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  1467. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  1468.  
  1469. public class Top5_categories {
  1470.  
  1471. public static class Map extends Mapper<LongWritable, Text, Text,
  1472. IntWritable> {
  1473.  
  1474. private Text category = new Text();
  1475. private final static IntWritable one = new IntWritable(1);
  1476. public void map(LongWritable key, Text value, Context context )
  1477. throws IOException, InterruptedException {
  1478. String line = value.toString();
  1479. String str[]=line.split("\t");
  1480.  
  1481. if(str.length > 5){
  1482. category.set(str[3]);
  1483. }
  1484.  
  1485. context.write(category, one);
  1486. }
  1487.  
  1488. }
  1489.  
  1490. public static class Reduce extends Reducer<Text, IntWritable,
  1491. Text, IntWritable> {
  1492.  
  1493. public void reduce(Text key, Iterable<IntWritable> values,
  1494. Context context)
  1495. throws IOException, InterruptedException {
  1496. int sum = 0;
  1497. for (IntWritable val : values) {
  1498.  
  1499. sum += val.get();
  1500. }
  1501. context.write(key, new IntWritable(sum));
  1502. }
  1503. }
  1504.  
  1505. public static void main(String[] args) throws Exception {
  1506. Configuration conf = new Configuration();
  1507.  
  1508. @SuppressWarnings("deprecation")
  1509. Job job = new Job(conf, "categories");
  1510. job.setJarByClass(Top5_categories.class);
  1511.  
  1512. job.setMapOutputKeyClass(Text.class);
  1513. job.setMapOutputValueClass(IntWritable.class);
  1514. //job.setNumReduceTasks(0);
  1515. job.setOutputKeyClass(Text.class);
  1516. job.setOutputValueClass(IntWritable.class);
  1517.  
  1518. job.setMapperClass(Map.class);
  1519. job.setReducerClass(Reduce.class);
  1520.  
  1521. job.setInputFormatClass(TextInputFormat.class);
  1522. job.setOutputFormatClass(TextOutputFormat.class);
  1523.  
  1524. FileInputFormat.addInputPath(job, new Path(args[0]));
  1525. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  1526. Path out=new Path(args[1]);
  1527. out.getFileSystem(conf).delete(out);
  1528. job.waitForCompletion(true);
  1529. }
  1530.  
  1531. }
  1532. public static void main(String[] args) throws Exception {
  1533. Configuration conf = new Configuration();
  1534.  
  1535. Job job = new Job(conf, "word count");
  1536. job.setJarByClass(WordCount.class);
  1537. job.setMapperClass(TokenizerMapper.class);
  1538. job.setCombinerClass(IntSumReducer.class);
  1539. job.setReducerClass(IntSumReducer.class);
  1540. job.setOutputKeyClass(Text.class);
  1541. job.setOutputValueClass(IntWritable.class);
  1542. FileInputFormat.addInputPath(job, new Path(args[0]));
  1543. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  1544. System.exit(job.waitForCompletion(true) ? 0 : 1);
  1545. }
  1546. }
  1547.  
  1548. Add jar files
  1549. Right click on project name (Top5) / properties / java build path / libraries / add external jar files
  1550. Export jar file
  1551. Right click on project name (Top5) / Export / java / jar files / select your jar file / select main class
  1552. Create a wordlist on notepad or take any wordlist or text file with appropriate data
  1553. open winscp
  1554. login with web shell client username and password
  1555. set host name 127.0.0.1 and port 2222
  1556.  
  1557.  
  1558. Copy the jar file you exported (Top5.jar) in web shell
  1559. Change permissions in winscp - select the file (Top5) right click/ properties/ chose all read write execute permissions
  1560.  
  1561. go to web shell and check if Top5_categories.jar file is uploaded
  1562. Command - ls
  1563.  
  1564. go to ambari
  1565. create a new folder - Top 5
  1566. inside Top 5 create a new folder - Input
  1567.  
  1568.  
  1569. Change permissions to all folders to read write and execute - select folder and click on permissions
  1570. Then upload your wordlist in Input folder and give all permissions
  1571. go to web shell and type command -
  1572. hadoop jar Top5_categories.jar /Top5/input /Top5/output
  1573. Note - hadoop jar Top5_categories.jar(is the name of your jar file you uploaded)
  1574. New Output folder will be created with the output of your wordlist
  1575.  
  1576.  
  1577. Now go in the Top5/Output folder
  1578.  
  1579. Select and open the part-r-00000 file. This file shows the output of the Top5
  1580.  
  1581.  
  1582. Practical 9
  1583. Top 10 rating of youtube videos
  1584. Start Virtual box and hadoop sandbox
  1585. Go to the given virtualbox link
  1586.  
  1587. Login in web shell client
  1588.  
  1589. Open ambari
  1590.  
  1591. Go to files view
  1592.  
  1593.  
  1594.  
  1595. Create program in eclipse
  1596.  
  1597. Top 10 rating of youtube videos Code -
  1598. package ratedvid;
  1599.  
  1600. import java.io.IOException;
  1601.  
  1602. import org.apache.hadoop.fs.Path;
  1603. import org.apache.hadoop.conf.*;
  1604. import org.apache.hadoop.io.*;
  1605. import org.apache.hadoop.mapreduce.*;
  1606. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  1607. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  1608. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  1609. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  1610.  
  1611. public class VideoRating {
  1612.  
  1613. public static class Map extends Mapper<LongWritable, Text, Text,
  1614. FloatWritable> {
  1615.  
  1616. private Text video_name = new Text();
  1617. private FloatWritable rating = new FloatWritable();
  1618. public void map(LongWritable key, Text value, Context context )
  1619. throws IOException, InterruptedException {
  1620. String line = value.toString();
  1621. String str[]=line.split("\t");
  1622.  
  1623. if(str.length > 7){
  1624. video_name.set(str[0]);
  1625. if(str[6].matches("\\d+.+")){ //this regular expression
  1626.  
  1627. float f=Float.parseFloat(str[6]); //typecasting string to float
  1628. rating.set(f);
  1629. }
  1630. }
  1631.  
  1632. context.write(video_name, rating);
  1633. }
  1634.  
  1635. }
  1636.  
  1637. public static class Reduce extends Reducer<Text, FloatWritable,
  1638. Text, FloatWritable> {
  1639.  
  1640. public void reduce(Text key, Iterable<FloatWritable> values,
  1641. Context context)
  1642. throws IOException, InterruptedException {
  1643. float sum = 0;
  1644. int l=0;
  1645. for (FloatWritable val : values) {
  1646. l+=1; //counts number of values are there for that key
  1647. sum += val.get();
  1648. }
  1649. sum=sum/l; //takes the average of the sum
  1650. context.write(key, new FloatWritable(sum));
  1651. }
  1652. }
  1653.  
  1654. public static void main(String[] args) throws Exception {
  1655. Configuration conf = new Configuration();
  1656.  
  1657. @SuppressWarnings("deprecation")
  1658. Job job = new Job(conf, "videorating");
  1659. job.setJarByClass(VideoRating.class);
  1660.  
  1661. job.setMapOutputKeyClass(Text.class);
  1662. job.setMapOutputValueClass(FloatWritable.class);
  1663. //job.setNumReduceTasks(0);
  1664. job.setOutputKeyClass(Text.class);
  1665. job.setOutputValueClass(FloatWritable.class);
  1666.  
  1667. job.setMapperClass(Map.class);
  1668. job.setReducerClass(Reduce.class);
  1669.  
  1670. job.setInputFormatClass(TextInputFormat.class);
  1671. job.setOutputFormatClass(TextOutputFormat.class);
  1672.  
  1673. FileInputFormat.addInputPath(job, new Path(args[0]));
  1674. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  1675. Path out=new Path(args[1]);
  1676. out.getFileSystem(conf).delete(out);
  1677. job.waitForCompletion(true);
  1678. }
  1679.  
  1680. }
  1681. Add jar files
  1682. Right click on project name (Top10) / properties / java build path / libraries / add external jar files
  1683. Export jar file
  1684. Right click on project name (Top10) / Export / java / jar files / select your jar file / select main class
  1685. Create a wordlist on notepad or take any wordlist
  1686. open winscp
  1687. login with web shell client username and password
  1688. set host name 127.0.0.1 and port 2222
  1689.  
  1690.  
  1691. Copy the jar file you exported (Top10.jar) in web shell
  1692. Change permissions in winscp - select the file (Top10) right click/ properties/ chose all read write execute permissions
  1693.  
  1694. go to web shell and check if Top10.jar file is uploaded
  1695. Command - ls
  1696.  
  1697. go to ambari
  1698. create a new folder - Top10
  1699. inside Top10 create a new folder - Input
  1700.  
  1701.  
  1702. Change permissions to all folders to read write and execute - select folder and click on permissions
  1703. Then upload your wordlist in Input folder and give all permissions
  1704. go to web shell and type command -
  1705. hadoop jar Top10.jar /Top10/input /Top10/output
  1706. Note - hadoop jar Top10.jar(is the name of your jar file you uploaded)
  1707. New Output folder will be created with the output of your wordlist
  1708.  
  1709.  
  1710.  
  1711. Now go in the Top10/Output folder
  1712.  
  1713. Select and open the part-r-00000 file. This file shows the output of the Top10
Add Comment
Please, Sign In to add comment