Advertisement
DrAungWinHtut

pic16f18877lcd_ok.cpp

Aug 8th, 2023
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.59 KB | None | 0 0
  1.  
  2. // PIC16F18877 Configuration Bit Settings
  3.  
  4. // 'C' source line config statements
  5.  
  6. // CONFIG1
  7. #pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
  8. #pragma config RSTOSC = HFINT1  // Power-up default value for COSC bits (HFINTOSC (1MHz))
  9. #pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
  10. #pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
  11. #pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
  12.  
  13. // CONFIG2
  14. #pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
  15. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  16. #pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
  17. #pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
  18. #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
  19. #pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
  20. #pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
  21. #pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
  22.  
  23. // CONFIG3
  24. #pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
  25. #pragma config WDTE = OFF       // WDT operating mode (WDT Disabled, SWDTEN is ignored)
  26. #pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
  27. #pragma config WDTCCS = SC      // WDT input clock selector (Software Control)
  28.  
  29. // CONFIG4
  30. #pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
  31. #pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
  32. #pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
  33.  
  34. // CONFIG5
  35. #pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
  36. #pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)
  37.  
  38. // #pragma config statements should precede project file includes.
  39. // Use project enums instead of #define for ON and OFF.
  40.  
  41.  
  42. #include <xc.h>
  43. #include <stdint.h>
  44. #include <stdio.h>
  45.  
  46. // LCD module connections
  47. #define LCD_RS PORTCbits.RC2
  48. #define LCD_EN PORTCbits.RC3
  49. #define LCD_D4 PORTCbits.RC4
  50. #define LCD_D5 PORTCbits.RC5
  51. #define LCD_D6 PORTCbits.RC6
  52. #define LCD_D7 PORTCbits.RC7
  53. #define LCD_DATA_PORT PORTC
  54. #define _XTAL_FREQ  4000000
  55.  
  56. // Function prototypes
  57. void LCD_Init();
  58. void LCD_Cmd(unsigned char);
  59. void LCD_Char(unsigned char);
  60. void LCD_String(const char*);
  61. void LCD_Clear();
  62. void LCD_Send(int RS,unsigned char data);
  63.  
  64.  
  65.  
  66. void main(void) {
  67.     char name[16] = "Aung Win Htut";
  68.     int i = 32;
  69.     LCD_Init();
  70.     LCD_Clear();
  71.     while (1) {
  72.         i++;
  73.         if(i>127)
  74.         {
  75.             i = 32;
  76.         }
  77.         LCD_Clear();
  78.         LCD_Cmd(0x00);
  79.         LCD_Char((char)i);
  80.         __delay_ms(100);        
  81.     }
  82.     return;
  83. }
  84.  
  85. void LCD_Init() {
  86.  
  87.     TRISC = 0x00; //all C port pins are output
  88.     __delay_ms(15);
  89.  
  90.     LCD_Cmd(0x02);  // Return home
  91.     LCD_Cmd(0x28);  // 4-bit mode - 2 line display - 5x7 font
  92.     LCD_Cmd(0x0C);  // Display ON - Cursor OFF - Blink OFF
  93.     LCD_Cmd(0x06);  // Increment cursor - No shift
  94.     LCD_Cmd(0x80);  // Address DDRAM with 0 offset 80h
  95. }
  96.  
  97. void LCD_Cmd(unsigned char command) {
  98.     LCD_Send(0,command);
  99. }
  100.  
  101. void LCD_Char(unsigned char data) {
  102.     LCD_Send(1,data);
  103. }
  104.  
  105. void LCD_Send(int RS,unsigned char data)
  106. {
  107.    
  108.     LCD_RS = RS;     // Data mode data = 1101, 1001
  109.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | (data & 0xF0);   // Send higher nibble 1101,0000
  110.    
  111.     LCD_EN = 1;     // Enable pulse
  112.     __delay_us(1);
  113.     LCD_EN = 0;
  114.     __delay_us(200);
  115.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | ((data << 4) & 0xF0);   // Send lower nibble 1001,0000
  116.    
  117.     LCD_EN = 1;     // Enable pulse
  118.     __delay_us(1);
  119.     LCD_EN = 0;
  120.     __delay_ms(2);
  121.  
  122. }
  123.  
  124. void LCD_String(const char* text) {
  125.     while (*text != '\0') {
  126.         LCD_Char(*text++);
  127.     }
  128. }
  129.  
  130. void LCD_Clear() {
  131.     LCD_Cmd(0x01);  // Clear display
  132.     __delay_ms(2);
  133. }
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement