Facebook
From a, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 3019
  1. /*
  2.  * Project name:
  3.      UART (Simple usage of UART module library functions)
  4.  * Copyright:
  5.      (c) Mikroelektronika, 2011.
  6.  * Revision History:
  7.      20110929:
  8.        - initial release (FJ);
  9.  * Description:
  10.      This code demonstrates how to use uart library routines. Upon receiving
  11.      data via RS232, MCU immediately sends it back to the sender.
  12.  * Test configuration:
  13.      MCU:             PIC18F45K22
  14.                       http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf
  15.      Dev.Board:       EasyPIC7 - ac:UART
  16.                       http://www.mikroe.com/eng/products/view/757/easypic-v7-development-system/
  17.      Oscillator:      HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
  18.      Ext. Modules:    None.
  19.      SW:              mikroC PRO for PIC
  20.                       http://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/
  21.  * NOTES:
  22.      - Turn on RX and TX UART switches (SW1.1 and SW2.1). (board specific)
  23.      - Put RX and TX UART jumpers (J3 and J4) in RS-232 or USB position,
  24.        depending on your choice (board specific).
  25.  */
  26.  
  27. sbit LCD_RS at LATB4_bit;
  28. sbit LCD_EN at LATB5_bit;
  29. sbit LCD_D4 at LATB0_bit;
  30. sbit LCD_D5 at LATB1_bit;
  31. sbit LCD_D6 at LATB2_bit;
  32. sbit LCD_D7 at LATB3_bit;
  33.  
  34. sbit LCD_RS_Direction at TRISB4_bit;
  35. sbit LCD_EN_Direction at TRISB5_bit;
  36. sbit LCD_D4_Direction at TRISB0_bit;
  37. sbit LCD_D5_Direction at TRISB1_bit;
  38. sbit LCD_D6_Direction at TRISB2_bit;
  39. sbit LCD_D7_Direction at TRISB3_bit;
  40.  
  41. char uart_rd;
  42. char uart_putty[]="I display whatever you typen";
  43. char txt[6]="Start";
  44. char txtToDisplay[200] = "";
  45. int i=0;
  46.  
  47. void main() {
  48.   //ANSELC = 0;                     // Configure PORTC pins as digital
  49.   ADCON1 = 0x0F;
  50.  
  51.  
  52.   Lcd_Init();                        // Initialize Lcd
  53.   Lcd_Cmd(_LCD_CLEAR);
  54.   Lcd_Cmd(_LCD_CURSOR_OFF);
  55.   Lcd_Out(1,1,txt);
  56.  
  57.  
  58.   UART1_Init(9600);               // Initialize UART module at 9600 bps
  59.   Delay_ms(100);                  // Wait for UART module to stabilize
  60.  
  61.  
  62.   UART1_Write_Text(uart_putty);
  63.   UART1_Write(13);
  64.   UART1_Write(10);
  65.  
  66.  
  67.   while (1) {                     // Endless loop
  68.       if (UART1_Data_Ready()) {     // If data is received,
  69.       uart_rd = UART1_Read();     // read the received data,
  70.       UART1_Write(uart_rd);
  71.       txtToDisplay[i] = uart_rd;
  72.       Lcd_Out(2,1,txtToDisplay);
  73.       i++;
  74.       // and send data via UART
  75.     }
  76.   }
  77. }
  78.  
  79.  
  80.  
  81.