Advertisement
DrAungWinHtut

pic7seg_ca.cpp

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