Advertisement
Dani_info

temp sensor - workshop

May 14th, 2022
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.98 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: dnymr
  4.  *
  5.  * Created on March 24, 2022, 5:47 PM
  6.  */
  7.  
  8.  
  9. // PIC16F883 Configuration Bit Settings
  10.  
  11. // 'C' source line config statements
  12.  
  13. // CONFIG1
  14. #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
  15. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  16. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  17. #pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
  18. #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
  19. #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
  20. #pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
  21. #pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
  22. #pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
  23. #pragma config LVP = OFF         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
  24.  
  25. // CONFIG2
  26. #pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
  27. #pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
  28.  
  29. // #pragma config statements should precede project file includes.
  30. // Use project enums instead of #define for ON and OFF.
  31.  
  32. #include <xc.h>
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36.  
  37. #define _XTAL_FREQ 8000000
  38. #define F_CPU 8000000/64 // value to calculate baud rate
  39.  
  40. unsigned char LATA;
  41. unsigned char LATB;
  42. unsigned char LATC;
  43.  
  44. void ADC_Init (){
  45.    ADCON0=0x41;
  46.    ADCON1=0x80;
  47.    ///ADRESH = 0; //Flush ADC output Register
  48.   // ADRESL = 0; //Flush ADC output Register
  49. }
  50.  
  51. uint16_t ADC_Read (uint8_t channel){
  52.    ADCON0 &= 0x01000101; // Clear The Channel Selection Bits
  53.    ADCON0 |= channel<<2;
  54.    __delay_us(30);  
  55.    GO_nDONE = 1; //Initializes A/D Conversion
  56.    while(ADCON0bits.GO_DONE); //Wait for A/D Conversion to complete
  57.    return ((ADRESH<<8)+ADRESL); //Returns Result
  58. }
  59.  
  60. void UART_Init(long baud_rate){
  61.    float bps;
  62.    TRISC=0b11000000;
  63.    bps = (( (float) (F_CPU) / (float) baud_rate) - 1); // sets baud rate
  64.    SPBRG=(int)bps; // store value for 9600 baud rate
  65.    TXSTAbits.CSRC = 0; // Don't care for asynchronous mode
  66.    TXSTAbits.TX9 = 0; // Selects 8-bit data transmission
  67.    TXSTAbits.TXEN = 1; // Enable Data tranmission on RC6 pin
  68.    TXSTAbits.SYNC = 0; // Selects Asynchronous Serial Communication Mode
  69.    TXSTAbits.BRGH = 0; // Default Baud rate speed
  70.    BAUDCTLbits.BRG16 = 0; // selects only 8 bit register for baud rate
  71.    RCSTA = 0x90; // Enables UART Communication PORT
  72. }
  73.  
  74.  
  75. void UART_Write(char *tx_str){
  76.     char ch;
  77.     while(*tx_str != 0x00){
  78.         ch = *tx_str;
  79.           while(!TRMT);
  80.      TXREG = ch;
  81.         tx_str++;
  82.     }
  83. }
  84.  
  85. void displayDigit1 (unsigned int digit1){
  86.     if (digit1==0){
  87.             LATA=0b00000111;
  88.             LATB=0b00000110;
  89.             LATC=0b00001000;
  90.             PORTA=LATA;
  91.             PORTB=LATB;
  92.             PORTC=LATC;
  93.         }
  94.         if (digit1==1){
  95.             LATA=0b00000100;
  96.             LATB=0b00000010;
  97.             LATC=0b00000000;
  98.             PORTA=LATA;
  99.             PORTB=LATB;
  100.             PORTC=LATC;
  101.         }
  102.         if (digit1==2){
  103.             LATA=0b00000011;
  104.             LATB=0b00001110;
  105.             LATC=0b00000000;
  106.             PORTA=LATA;
  107.             PORTB=LATB;
  108.             PORTC=LATC;
  109.         }
  110.         if (digit1==3){
  111.             LATA=0b00000110;
  112.             LATB=0b00001110;
  113.             LATC=0b00000000;
  114.             PORTA=LATA;
  115.             PORTB=LATB;
  116.             PORTC=LATC;
  117.         }
  118.         if (digit1==4){
  119.             LATA=0b00000100;
  120.             LATB=0b00001010;
  121.             LATC=0b00001000;
  122.             PORTA=LATA;
  123.             PORTB=LATB;
  124.             PORTC=LATC;
  125.         }
  126.         if (digit1==5){
  127.             LATA=0b00000110;
  128.             LATB=0b00001100;
  129.             LATC=0b00001000;
  130.             PORTA=LATA;
  131.             PORTB=LATB;
  132.             PORTC=LATC;
  133.         }
  134.         if (digit1==6){
  135.             LATA=0b00000111;
  136.             LATB=0b00001100;
  137.             LATC=0b00001000;
  138.             PORTA=LATA;
  139.             PORTB=LATB;
  140.             PORTC=LATC;
  141.         }
  142.         if (digit1==7){
  143.             LATA=0b00000100;
  144.             LATB=0b00000110;
  145.             LATC=0b00000000;
  146.             PORTA=LATA;
  147.             PORTB=LATB;
  148.             PORTC=LATC;
  149.         }
  150.         if (digit1==8){
  151.             LATA=0b00000111;
  152.             LATB=0b00001110;
  153.             LATC=0b00001000;    
  154.             PORTA=LATA;
  155.             PORTB=LATB;
  156.             PORTC=LATC;
  157.         }
  158.         if (digit1==9){
  159.             LATA=0b00000110;
  160.             LATB=0b00001110;
  161.             LATC=0b00001000;
  162.             PORTA=LATA;
  163.             PORTB=LATB;
  164.             PORTC=LATC;
  165.         }
  166. }
  167.  
  168. void displayDigit2 (unsigned int digit2){
  169.     if (digit2==0){
  170.             LATA|=0b00110000;
  171.             LATB|=0b00100001;
  172.             LATC|=0b00110010;
  173.             PORTA=LATA;
  174.             PORTB=LATB;
  175.             PORTC=LATC;
  176.         }
  177.         if (digit2==1){
  178.             LATA|=0b00000000;
  179.             LATB|=0b00000000;
  180.             LATC|=0b00010010;
  181.             PORTA=LATA;
  182.             PORTB=LATB;
  183.             PORTC=LATC;
  184.         }
  185.         if (digit2==2){
  186.             LATA|=0b00110000;
  187.             LATB|=0b00000000;
  188.             LATC|=0b00110001;
  189.             PORTA=LATA;
  190.             PORTB=LATB;
  191.             PORTC=LATC;
  192.         }
  193.         if (digit2==3){
  194.             LATA|=0b00100000;
  195.             LATB|=0b00000000;
  196.             LATC|=0b00110011;
  197.             PORTA=LATA;
  198.             PORTB=LATB;
  199.             PORTC=LATC;
  200.         }
  201.         if (digit2==4){
  202.             LATA|=0b00000000;
  203.             LATB|=0b00000001;
  204.             LATC|=0b00010011;
  205.             PORTA=LATA;
  206.             PORTB=LATB;
  207.             PORTC=LATC;
  208.         }
  209.         if (digit2==5){
  210.             LATA|=0b00100000;
  211.             LATB|=0b00000001;
  212.             LATC|=0b00100011;
  213.             PORTA=LATA;
  214.             PORTB=LATB;
  215.             PORTC=LATC;
  216.         }
  217.         if (digit2==6){
  218.             LATA|=0b00110000;
  219.             LATB|=0b00000001;
  220.             LATC|=0b00100011;
  221.             PORTA=LATA;
  222.             PORTB=LATB;
  223.             PORTC=LATC;
  224.         }
  225.         if (digit2==7){
  226.             LATA|=0b00000000;
  227.             LATB|=0b00000000;
  228.             LATC|=0b00110010;
  229.             PORTA=LATA;
  230.             PORTB=LATB;
  231.             PORTC=LATC;
  232.         }
  233.          if (digit2==8){
  234.             LATA|=0b00110000;
  235.             LATB|=0b00000001;
  236.             LATC|=0b00110011;
  237.             PORTA=LATA;
  238.             PORTB=LATB;
  239.             PORTC=LATC;
  240.         }
  241.         if (digit2==9){
  242.             LATA|=0b00100000;
  243.             LATB|=0b00000001;
  244.             LATC|=0b00110011;
  245.             PORTA=LATA;
  246.             PORTB=LATB;
  247.             PORTC=LATC;
  248.         }
  249. }
  250.    
  251.  
  252. void displayTemperature (unsigned int temp){
  253.     LATA=0x00;
  254.     LATB=0x00;
  255.     LATC=0x00;
  256.     if (temp>=10){
  257.      int digit1=temp/10;
  258.      int digit2=temp%10;
  259.      displayDigit1 (digit1);
  260.      displayDigit2 (digit2);
  261.     }    
  262.     if (temp>=0 && temp <10)
  263.      displayDigit2 (temp);
  264. }
  265.  
  266.  
  267. void main(void) {
  268.     TRISA=0x00;
  269.     TRISB=0b00100000;
  270.     TRISC=0b11000000;
  271.     float temp_old, temp, voltage;
  272.     //UART_Init(9600);
  273.     ADC_Init();
  274.     uint16_t temp_ADC=ADC_Read(13);
  275.     voltage = temp_ADC * 0.0048828;
  276.     temp_old = voltage / 0.01;
  277.     displayTemperature((int)temp);
  278.     while (1){
  279.         temp_ADC=ADC_Read(13);
  280.         voltage = temp_ADC * 0.0048828;
  281.         temp = voltage / 0.01;
  282.         if (abs(temp-temp_old)>=0.6){
  283.             displayTemperature((int)temp);
  284.             temp_old=temp;
  285.         }  
  286.      }
  287.      return;
  288.  }
  289.    
  290.  
  291.  
  292.  
  293.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement