Advertisement
DrAungWinHtut

pic_temp_threshold.cpp

Aug 12th, 2023
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.73 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.  
  88. //O2 Lvl = 3543 %
  89. //PH lvl = 3432 %
  90.  
  91. void main(void) {
  92.     TRISB = 0b00000000;
  93.     char name[16] = "Aung Win Htut";
  94.     char buffer[16];
  95.     int i = 32;
  96.     Timer0_Init();
  97.     ADC_Init();
  98.     init7Segment();
  99.     LCD_Init();
  100.     LCD_Clear();
  101.     unsigned int motor_status=0;
  102.     __delay_ms(300);
  103.    
  104.    
  105.    
  106.    
  107.     while (1) {
  108.         unsigned int Temperature = ADC_Read(0);
  109.         Temperature = Temperature * 100 / 1023;
  110.         Temperature = Temperature * 25 / 60;
  111.         sprintf(buffer, "Temperature = %3u%%", Temperature);
  112.         // Display the formatted string on the 2-digit 7-segment display
  113.         if (Temperature>30)
  114.         {
  115.             seven_seg_All(1); //1 - all on
  116.         }
  117.         else{
  118.             seven_seg_All(0);  //0 - all off
  119.         }
  120.         if(Temperature<=20)
  121.         {
  122.             //start motor
  123.             motor_status=1;
  124.             LATBbits.LATB0 = 1;
  125.             LCD_Clear();
  126.             lcd_set_cursor(1,1);
  127.             LCD_String("O2 is in danger!");
  128.             timer();
  129.             __delay_ms(30);
  130.            
  131.            
  132.         }
  133.        
  134.        
  135.                  
  136.         //LCD_Cmd(0x00);
  137.         LCD_Clear();
  138.         lcd_set_cursor(1,1);
  139.         LCD_String(buffer);
  140.         //__delay_ms(100);
  141.        
  142.         unsigned int PH_level = ADC_Read(1);
  143.         PH_level = PH_level * 14 / 1023;
  144.         sprintf(buffer, "PH lvl = %3u%%", PH_level);
  145.         lcd_set_cursor(1,2);
  146.         LCD_String(buffer);
  147.         __delay_ms(100);        
  148.     }
  149.     return;
  150. }
  151. void Timer0_Init() {
  152.     // Set Timer0 to 16-bit mode and use the internal clock (Fosc/4)
  153.     T0CON0 = 0b10000000; // 16-bit mode, Fosc/4    
  154.     // Set the prescaler to 1:256, so each Timer0 tick is 256 instruction cycles
  155.     T0CON1 = 0b00000110; // 1:256 prescaler    
  156.     // Load Timer0 with the initial value to achieve a 1-minute delay
  157.     TMR0H = 0x85; // High byte
  158.     TMR0L = 0xEE; // Low byte
  159. }
  160. void __interrupt()isr(void)
  161. {
  162.     if(PIR0bits.TMR0IF==1)
  163.     {
  164.         PIR0bits.TMR0IF=0;
  165.         PORTBbits.RB0=0;
  166.         LCD_Clear();
  167.         lcd_set_cursor(1,1);
  168.         LCD_String("O2 is OK again!");
  169.         __delay_ms(300);
  170.          LCD_Clear();
  171.        // __delay_ms(2000);
  172.     }
  173.    
  174. }
  175.  
  176. void timer(void)
  177. {
  178.     INTCONbits.GIE = 0;
  179.     T0CON0 = 0b10000100;
  180.     T0CON1 = 0b01001011;
  181.     TMR0H = 200;
  182.     PIR0bits.TMR0IF = 0;
  183.     PIE0bits.TMR0IE = 1;
  184.     INTCONbits.GIE = 1;
  185.  
  186.  
  187. }
  188.  
  189. void ADC_Init() {
  190.     // Configure ADC module settings
  191.     // Set the ADC channel to ANA3 (RA3)
  192.     ANSELA = 0b00000011; //RA0 and RA1
  193.     TRISA =  0b11111111; //all inputs (including digital inputs)
  194.     ADREF =  0b00000000; // VREF to VDD and VSS
  195.     ADCLK =  0b00000011; // Set TAD = 2 us
  196.     ADACQ =  0b00000000;
  197.     ADCON0 = 0b10000100;  
  198.     // Optional: Allow the ADC to stabilize before reading the first value
  199.     __delay_us(20);
  200. }
  201. unsigned int ADC_Read(uint8_t channel) {    
  202.     unsigned int result;
  203.      ADPCH = channel; //0b00000011; //RA3 = 3
  204.     __delay_us(2);
  205.    
  206.     ADCON0bits.GO = 1; //Start  
  207.     // Wait for the conversion to complete
  208.     while (ADCON0bits.GO); //while (ADCON0bits.ADGO==1);
  209.     result = ((unsigned int)ADRESH << 8) | ADRESL; // ADRESH * 256 + ADRESL;
  210.     // Return the ADC result (combine ADRESH and ADRESL)
  211.     return(result);
  212. }
  213.  
  214. void LCD_Init() {
  215.  
  216.     TRISC = 0x00; //all C port pins are output
  217.     __delay_ms(15);
  218.  
  219.     LCD_Cmd(0x02);  // Return home
  220.     LCD_Cmd(0x28);  // 4-bit mode - 2 line display - 5x7 font
  221.     LCD_Cmd(0x0C);  // Display ON - Cursor OFF - Blink OFF
  222.     LCD_Cmd(0x06);  // Increment cursor - No shift
  223.     LCD_Cmd(0x80);  // Address DDRAM with 0 offset 80h
  224. }
  225.  
  226. void LCD_Cmd(unsigned char command) {
  227.     LCD_Send(0,command);
  228. }
  229.  
  230. void LCD_Char(unsigned char data) {
  231.     LCD_Send(1,data);
  232. }
  233.  
  234. void LCD_Send(int RS,unsigned char data)
  235. {
  236.    
  237.     LCD_RS = RS;     // Data mode data = 1101, 1001
  238.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | (data & 0xF0);   // Send higher nibble 1101,0000
  239.    
  240.     LCD_EN = 1;     // Enable pulse
  241.     __delay_us(1);
  242.     LCD_EN = 0;
  243.     __delay_us(200);
  244.     LCD_DATA_PORT = (LCD_DATA_PORT & 0x0F) | ((data << 4) & 0xF0);   // Send lower nibble 1001,0000
  245.    
  246.     LCD_EN = 1;     // Enable pulse
  247.     __delay_us(1);
  248.     LCD_EN = 0;
  249.     __delay_ms(2);
  250.  
  251. }
  252.  
  253. void LCD_String(const char* text) {
  254.     while (*text != '\0') {
  255.         LCD_Char(*text++);
  256.     }
  257. }
  258.  
  259. void LCD_Clear() {
  260.     LCD_Cmd(0x01);  // Clear display
  261.     __delay_ms(2);
  262. }
  263.  
  264. void lcd_set_cursor(char col,char line)
  265. {
  266.     if(line==1){
  267.         LCD_Cmd(0b10000000 | col);        
  268.     }
  269.     else if(line==2){
  270.         LCD_Cmd(0b11000000 | col);        
  271.     }
  272. }
  273. void init7Segment(void) {
  274.     // Initialize digit pins
  275.     TRISE &= 0b11110010;    
  276.     TRISD=0;
  277.     PORTD =0;
  278.     DIGIT1 = 0;
  279.     DIGIT2 = 0;
  280. }
  281.  
  282.  
  283.  
  284.  void seven_seg_Show(int i){
  285.      
  286.     unsigned char tens_digit = (i / 10) % 10;
  287.     unsigned char ones_digit = i % 10;
  288.    
  289.     // Display tens digit on first digit
  290.     PORTEbits.RE0 = 0;  // Set first digit high
  291.     PORTD = hexvalue[tens_digit];
  292.     PORTEbits.RE2 = 1;  // Set second digit low
  293.     __delay_ms(30);
  294.    
  295.      // Display ones digit on second digit
  296.     PORTEbits.RE2 = 0;  // Set second digit high
  297.     PORTD = hexvalue[ones_digit];
  298.     PORTEbits.RE0 = 1;  // Set first digit low
  299.     __delay_ms(30);
  300.      
  301.    
  302. }
  303.  
  304.   void seven_seg_All(int i){  
  305.            
  306.       if(i==1) //turn on
  307.       {
  308.         for(int k=0;k<10;k++)
  309.         {
  310.             PORTEbits.RE0 = 0;  // Set first digit high
  311.             PORTD = 0x00;
  312.             PORTEbits.RE2 = 1;  // Set second digit low
  313.             __delay_ms(30);
  314.        
  315.             PORTEbits.RE0 = 1;  // Set first digit high
  316.             PORTD = 0x00;
  317.             PORTEbits.RE2 = 0;  // Set second digit low
  318.             __delay_ms(30);          
  319.         }        
  320.       }
  321.       else
  322.       {
  323.           PORTEbits.RE0 = 1;
  324.           PORTEbits.RE2 = 1;
  325.       }
  326.       // Display tens digit on first digit
  327.        
  328.    
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement