Advertisement
DrAungWinHtut

lcd_retest.cpp

Aug 13th, 2023 (edited)
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.77 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.RC0  //
  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. #define DIGIT1  LATE0
  57. #define DIGIT2  LATE2
  58.  
  59. // Define 7-segment display pins
  60. #define SEG_A   PORTD0
  61. #define SEG_B   PORTD1
  62. #define SEG_C   PORTD2
  63. #define SEG_D   PORTD3
  64. #define SEG_E   PORTD4
  65. #define SEG_F   PORTD5
  66. #define SEG_G   PORTD6
  67. #define SEG_DP  PORTD7
  68.  
  69. // Function prototypes
  70. void LCD_Init();
  71. void LCD_Cmd(unsigned char);
  72. void LCD_Char(unsigned char);
  73. void LCD_String(const char*);
  74. void LCD_Clear();
  75. void LCD_Send(int RS,unsigned char data);
  76. void lcd_set_cursor(char col,char line);
  77. void ADC_Init();
  78. void Timer0_Init();
  79. unsigned int ADC_Read(uint8_t channel);
  80. void timer(void);
  81.  
  82. void init7Segment(void);
  83. //void displayDigit(unsigned char display, unsigned char digit);
  84. char hexvalue[10]= {0xC0,0xC9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
  85. void seven_seg_Show(int i);
  86. void seven_seg_All(int i); //i=0 off, 1 on
  87. void initLCD();
  88. void lcdwritectrl(char x);
  89.  
  90. //O2 Lvl = 3543 %
  91. //PH lvl = 3432 %
  92.  
  93. void main(void) {
  94.     TRISB = 0b00000000;
  95.     char name[16] = "Temasat";
  96.     char buffer[16];
  97.     int i = 32;
  98.     Timer0_Init();
  99.     ADC_Init();
  100.     init7Segment();
  101.     //LCD_Init();
  102.     initLCD();
  103.     LCD_Clear();
  104.     LCD_String(name);
  105.     unsigned int motor_status=0;
  106.     __delay_ms(300);
  107.    
  108.    
  109.    
  110.    
  111.     while (1) {
  112.         unsigned int Temperature = ADC_Read(0);
  113.         Temperature = Temperature * 100 / 1023;
  114.         Temperature = Temperature * 25 / 60;
  115.         sprintf(buffer, "Temperature = %3u%%", Temperature);
  116.         // Display the formatted string on the 2-digit 7-segment display
  117.         if (Temperature>30)
  118.         {
  119.             seven_seg_All(1); //1 - all on
  120.         }
  121.         else{
  122.             seven_seg_All(0);  //0 - all off
  123.         }
  124.         if(Temperature<=20)
  125.         {
  126.             //start motor
  127.             motor_status=1;
  128.             LATBbits.LATB0 = 1;
  129.             LCD_Clear();
  130.             lcd_set_cursor(1,1);
  131.             LCD_String("O2 is in danger!");
  132.             timer(); //timer ON (start counting time!)
  133.             __delay_ms(30);
  134.            
  135.            
  136.         }
  137.        
  138.        
  139.                  
  140.         //LCD_Cmd(0x00);
  141.         LCD_Clear();
  142.         lcd_set_cursor(1,1);
  143.         LCD_String(buffer);
  144.         //__delay_ms(100);
  145.        
  146.         unsigned int PH_level = ADC_Read(1);
  147.         PH_level = PH_level * 14 / 1023;
  148.         sprintf(buffer, "PH lvl = %3u%%", PH_level);
  149.         lcd_set_cursor(1,2);
  150.         LCD_String(buffer);
  151.         __delay_ms(100);        
  152.     }
  153.     return;
  154. }
  155. void Timer0_Init() {
  156.     // Set Timer0 to 16-bit mode and use the internal clock (Fosc/4)
  157.     T0CON0 = 0b10000000; // 16-bit mode, Fosc/4    
  158.     // Set the prescaler to 1:256, so each Timer0 tick is 256 instruction cycles
  159.     T0CON1 = 0b00000110; // 1:256 prescaler    
  160.     // Load Timer0 with the initial value to achieve a 1-minute delay
  161.     TMR0H = 0x85; // High byte
  162.     TMR0L = 0xEE; // Low byte
  163. }
  164.  
  165.  
  166.  
  167. void __interrupt()isr(void) //timer on interrupt
  168. {
  169.     if(PIR0bits.TMR0IF==1)   //Timer 0 time up
  170.     {
  171.         PIR0bits.TMR0IF=0;   //Timer 0 OFF
  172.         PORTBbits.RB0=0;
  173.         LCD_Clear();
  174.         lcd_set_cursor(1,1);
  175.         LCD_String("O2 is OK again!");
  176.         __delay_ms(300);
  177.          LCD_Clear();
  178.        // __delay_ms(2000);
  179.     }
  180.     else if(PIR0bits.TMR0IF==1)
  181.     {
  182.         PIR0bits.TMR0IF=0;
  183.         PORTBbits.RB0=0;
  184.         LCD_Clear();
  185.         lcd_set_cursor(1,1);
  186.         LCD_String("O2 is OK again!");
  187.         __delay_ms(300);
  188.          LCD_Clear();
  189.        // __delay_ms(2000);
  190.     }
  191.    
  192.    
  193. }
  194.  
  195. void timer(void)
  196. {
  197.     INTCONbits.GIE = 0;
  198.     T0CON0 = 0b10000100;
  199.     T0CON1 = 0b01001011;
  200.     TMR0H = 200;
  201.     PIR0bits.TMR0IF = 0;
  202.     PIE0bits.TMR0IE = 1;
  203.     INTCONbits.GIE = 1;
  204.  
  205.  
  206. }
  207.  
  208. void ADC_Init() {
  209.     // Configure ADC module settings
  210.     // Set the ADC channel to ANA0(RA0) and ANA1(RA1)
  211.     ANSELA = 0b00000011; //RA0 and RA1
  212.     TRISA =  0b11111111; //all inputs (including digital inputs)
  213.     ADREF =  0b00000000; // VREF to VDD and VSS
  214.     ADCLK =  0b00000011; // Set TAD = 2 us
  215.     ADACQ =  0b00000000;
  216.     ADCON0 = 0b10000100;  
  217.     // Optional: Allow the ADC to stabilize before reading the first value
  218.     __delay_us(20);
  219. }
  220. unsigned int ADC_Read(uint8_t channel) {    
  221.     unsigned int result;
  222.     ADPCH = channel; //0b00000011; RA3 = 3 | 0b00000000; RA0 | 0b00000001; RA1
  223.     __delay_us(2);
  224.    
  225.     ADCON0bits.GO = 1; //Start  
  226.     // Wait for the conversion to complete
  227.     while (ADCON0bits.GO); //while (ADCON0bits.ADGO==1);
  228.     result = ((unsigned int)ADRESH << 8) | ADRESL; // ADRESH * 256 + ADRESL;
  229.     // Return the ADC result (combine ADRESH and ADRESL) by moving High byte to left and add with low byte
  230.     // ADRESH + ADRESL
  231.     return(result);
  232. }
  233.  
  234. void LCD_Init() {
  235.  
  236.     TRISC = 0x00; //all C port pins are output
  237.     __delay_ms(15);
  238.     LCD_Cmd(0x02);  // Return home
  239.     LCD_Cmd(0x28);  // 4-bit mode - 2 line display - 5x7 font
  240.     LCD_Cmd(0x0C);  // Display ON - Cursor OFF - Blink OFF
  241.     LCD_Cmd(0x06);  // Increment cursor - No shift
  242.     LCD_Cmd(0x80);  // Address DDRAM with 0 offset 80h
  243. }
  244.  
  245.  
  246. void initLCD()
  247. {
  248.     __delay_ms(15);
  249.     lcdwritectrl(0b00000011);
  250.     __delay_ms(15);
  251.     lcdwritectrl(0b00000010);
  252.     lcdwritectrl(0b00001000);
  253.     lcdwritectrl(0b00001100);
  254.     lcdwritectrl(0b00000110);
  255.     lcdwritectrl(0b00000001);
  256. }
  257. void lcdwritectrl(char x)
  258. {
  259.     LCD_RS = 0;
  260.     PORTC =x;
  261.     LCD_EN = 1;
  262.     __delay_ms(1);
  263.     LCD_EN = 0;
  264.     __delay_ms(1);
  265.     PORTC =x<<4;
  266.     LCD_EN = 1;
  267.     __delay_ms(1);
  268.     LCD_EN =0;
  269.     __delay_ms(1);
  270.  
  271. }
  272.  
  273.  
  274.  
  275. void LCD_Cmd(unsigned char command) {
  276.     LCD_Send(0,command);
  277. }
  278.  
  279. void LCD_Char(unsigned char data) {
  280.     LCD_Send(1,data);
  281. }
  282.  
  283. void LCD_Send(int RS,unsigned char data)
  284. {
  285.    
  286.     LCD_RS = RS;     // Data mode data = 1101, 1001
  287.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | (data & 0xF0);   // Send higher nibble 1101,0000
  288.    
  289.     LCD_EN = 1;     // Enable pulse
  290.     __delay_us(1);
  291.     LCD_EN = 0;
  292.     __delay_us(200);
  293.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | ((data << 4) & 0xF0);   // Send lower nibble 1001,0000
  294.    
  295.     LCD_EN = 1;     // Enable pulse
  296.     __delay_us(1);
  297.     LCD_EN = 0;
  298.     __delay_ms(2);
  299.  
  300. }
  301.  
  302. void LCD_String(const char* text) {
  303.     while (*text != '\0') {
  304.         LCD_Char(*text++);
  305.     }
  306. }
  307.  
  308. void LCD_Clear() {
  309.     LCD_Cmd(0x01);  // Clear display
  310.     __delay_ms(2);
  311. }
  312.  
  313. void lcd_set_cursor(char col,char line)
  314. {
  315.     if(line==1){
  316.         LCD_Cmd(0b10000000 | col);        
  317.     }
  318.     else if(line==2){
  319.         LCD_Cmd(0b11000000 | col);        
  320.     }
  321. }
  322. void init7Segment(void) {
  323.     // Initialize digit pins
  324.     TRISE &= 0b11110010;    
  325.     TRISD=0;
  326.     PORTD =0;
  327.     DIGIT1 = 0;
  328.     DIGIT2 = 0;
  329. }
  330.  
  331.  
  332.  
  333.  void seven_seg_Show(int i){
  334.      
  335.     unsigned char tens_digit = (i / 10) % 10;
  336.     unsigned char ones_digit = i % 10;
  337.    
  338.     // Display tens digit on first digit
  339.     PORTEbits.RE0 = 0;  // Set first digit high
  340.     PORTD = hexvalue[tens_digit];
  341.     PORTEbits.RE2 = 1;  // Set second digit low
  342.     __delay_ms(30);
  343.    
  344.      // Display ones digit on second digit
  345.     PORTEbits.RE2 = 0;  // Set second digit high
  346.     PORTD = hexvalue[ones_digit];
  347.     PORTEbits.RE0 = 1;  // Set first digit low
  348.     __delay_ms(30);
  349.      
  350.    
  351. }
  352.  
  353.   void seven_seg_All(int i){  
  354.            
  355.       if(i==1) //turn on
  356.       {
  357.         for(int k=0;k<10;k++)
  358.         {
  359.             PORTEbits.RE0 = 0;  // Set first digit high
  360.             PORTD = 0x00;
  361.             PORTEbits.RE2 = 1;  // Set second digit low
  362.             __delay_ms(30);
  363.        
  364.             PORTEbits.RE0 = 1;  // Set first digit high
  365.             PORTD = 0x00;
  366.             PORTEbits.RE2 = 0;  // Set second digit low
  367.             __delay_ms(30);          
  368.         }        
  369.       }
  370.       else
  371.       {
  372.           PORTEbits.RE0 = 1;
  373.           PORTEbits.RE2 = 1;
  374.       }
  375.       // Display tens digit on first digit
  376.        
  377.    
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement