Advertisement
DrAungWinHtut

7seginpic.cpp

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