Advertisement
DrAungWinHtut

pic_lcd_timer.cpp

Aug 9th, 2023
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.56 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. // 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. void lcd_set_cursor(char col,char line);
  64. void ADC_Init();
  65. void Timer0_Init();
  66. unsigned int ADC_Read(uint8_t channel);
  67. void timer(void);
  68.  
  69.  
  70. //O2 Lvl = 3543 %
  71. //PH lvl = 3432 %
  72.  
  73. void main(void) {
  74.     TRISB = 0b00000000;
  75.     char name[16] = "Aung Win Htut";
  76.     char buffer[16];
  77.     int i = 32;
  78.     Timer0_Init();
  79.     ADC_Init();
  80.     LCD_Init();
  81.     LCD_Clear();
  82.     unsigned int motor_status=0;
  83.     __delay_ms(300);
  84.     while (1) {
  85.         unsigned int O2_level = ADC_Read(0);
  86.         O2_level = O2_level * 100 / 1023;
  87.         sprintf(buffer, "O2 lvl = %3u%%", O2_level);
  88.        
  89.         if(O2_level<=20)
  90.         {
  91.             //start motor
  92.             motor_status=1;
  93.             LATBbits.LATB0 = 1;
  94.             LCD_Clear();
  95.             lcd_set_cursor(1,1);
  96.             LCD_String("O2 is in danger!");
  97.             timer();
  98.             __delay_ms(30);
  99.            
  100.            
  101.         }
  102.        
  103.        
  104.                  
  105.         //LCD_Cmd(0x00);
  106.          LCD_Clear();
  107.         lcd_set_cursor(1,1);
  108.         LCD_String(buffer);
  109.         __delay_ms(100);
  110.        
  111.         unsigned int PH_level = ADC_Read(1);
  112.         PH_level = PH_level * 14 / 1023;
  113.         sprintf(buffer, "PH lvl = %3u%%", PH_level);
  114.         lcd_set_cursor(1,2);
  115.         LCD_String(buffer);
  116.         __delay_ms(100);        
  117.     }
  118.     return;
  119. }
  120. void Timer0_Init() {
  121.     // Set Timer0 to 16-bit mode and use the internal clock (Fosc/4)
  122.     T0CON0 = 0b10000000; // 16-bit mode, Fosc/4    
  123.     // Set the prescaler to 1:256, so each Timer0 tick is 256 instruction cycles
  124.     T0CON1 = 0b00000110; // 1:256 prescaler    
  125.     // Load Timer0 with the initial value to achieve a 1-minute delay
  126.     TMR0H = 0x85; // High byte
  127.     TMR0L = 0xEE; // Low byte
  128. }
  129. void __interrupt()isr(void)
  130. {
  131.     if(PIR0bits.TMR0IF==1)
  132.     {
  133.         PIR0bits.TMR0IF=0;
  134.         PORTBbits.RB0=0;
  135.         LCD_Clear();
  136.         lcd_set_cursor(1,1);
  137.         LCD_String("O2 is OK again!");
  138.         __delay_ms(300);
  139.          LCD_Clear();
  140.        // __delay_ms(2000);
  141.     }
  142.    
  143. }
  144.  
  145. void timer(void)
  146. {
  147.     INTCONbits.GIE = 0;
  148.     T0CON0 = 0b10000100;
  149.     T0CON1 = 0b01001011;
  150.     TMR0H = 200;
  151.     PIR0bits.TMR0IF = 0;
  152.     PIE0bits.TMR0IE = 1;
  153.     INTCONbits.GIE = 1;
  154.  
  155.  
  156. }
  157.  
  158. void ADC_Init() {
  159.     // Configure ADC module settings
  160.     // Set the ADC channel to ANA3 (RA3)
  161.     ANSELA = 0b00000011; //RA0 and RA1
  162.     TRISA =  0b11111111; //all inputs (including digital inputs)
  163.     ADREF =  0b00000000; // VREF to VDD and VSS
  164.     ADCLK =  0b00000011; // Set TAD = 2 us
  165.     ADACQ =  0b00000000;
  166.     ADCON0 = 0b10000100;  
  167.     // Optional: Allow the ADC to stabilize before reading the first value
  168.     __delay_us(20);
  169. }
  170. unsigned int ADC_Read(uint8_t channel) {    
  171.     unsigned int result;
  172.      ADPCH = channel; //0b00000011; //RA3 = 3
  173.     __delay_us(2);
  174.    
  175.     ADCON0bits.GO = 1; //Start  
  176.     // Wait for the conversion to complete
  177.     while (ADCON0bits.GO); //while (ADCON0bits.ADGO==1);
  178.     result = ((unsigned int)ADRESH << 8) | ADRESL; // ADRESH * 256 + ADRESL;
  179.     // Return the ADC result (combine ADRESH and ADRESL)
  180.     return(result);
  181. }
  182.  
  183. void LCD_Init() {
  184.  
  185.     TRISC = 0x00; //all C port pins are output
  186.     __delay_ms(15);
  187.  
  188.     LCD_Cmd(0x02);  // Return home
  189.     LCD_Cmd(0x28);  // 4-bit mode - 2 line display - 5x7 font
  190.     LCD_Cmd(0x0C);  // Display ON - Cursor OFF - Blink OFF
  191.     LCD_Cmd(0x06);  // Increment cursor - No shift
  192.     LCD_Cmd(0x80);  // Address DDRAM with 0 offset 80h
  193. }
  194.  
  195. void LCD_Cmd(unsigned char command) {
  196.     LCD_Send(0,command);
  197. }
  198.  
  199. void LCD_Char(unsigned char data) {
  200.     LCD_Send(1,data);
  201. }
  202.  
  203. void LCD_Send(int RS,unsigned char data)
  204. {
  205.    
  206.     LCD_RS = RS;     // Data mode data = 1101, 1001
  207.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | (data & 0xF0);   // Send higher nibble 1101,0000
  208.    
  209.     LCD_EN = 1;     // Enable pulse
  210.     __delay_us(1);
  211.     LCD_EN = 0;
  212.     __delay_us(200);
  213.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | ((data << 4) & 0xF0);   // Send lower nibble 1001,0000
  214.    
  215.     LCD_EN = 1;     // Enable pulse
  216.     __delay_us(1);
  217.     LCD_EN = 0;
  218.     __delay_ms(2);
  219.  
  220. }
  221.  
  222. void LCD_String(const char* text) {
  223.     while (*text != '\0') {
  224.         LCD_Char(*text++);
  225.     }
  226. }
  227.  
  228. void LCD_Clear() {
  229.     LCD_Cmd(0x01);  // Clear display
  230.     __delay_ms(2);
  231. }
  232.  
  233. void lcd_set_cursor(char col,char line)
  234. {
  235.     if(line==1){
  236.         LCD_Cmd(0b10000000 | col);        
  237.     }
  238.     else if(line==2){
  239.         LCD_Cmd(0b11000000 | col);        
  240.     }
  241. }
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement