Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: dnymr
- *
- * Created on March 24, 2022, 5:47 PM
- */
- // PIC16F883 Configuration Bit Settings
- // 'C' source line config statements
- // CONFIG1
- #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
- #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
- #pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
- #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
- #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
- #pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
- #pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
- #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
- #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
- // CONFIG2
- #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
- #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
- // #pragma config statements should precede project file includes.
- // Use project enums instead of #define for ON and OFF.
- #include <xc.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define _XTAL_FREQ 8000000
- #define F_CPU 8000000/64 // value to calculate baud rate
- unsigned char LATA;
- unsigned char LATB;
- unsigned char LATC;
- void ADC_Init (){
- ADCON0=0x41;
- ADCON1=0x80;
- ///ADRESH = 0; //Flush ADC output Register
- // ADRESL = 0; //Flush ADC output Register
- }
- uint16_t ADC_Read (uint8_t channel){
- ADCON0 &= 0x01000101; // Clear The Channel Selection Bits
- ADCON0 |= channel<<2;
- __delay_us(30);
- GO_nDONE = 1; //Initializes A/D Conversion
- while(ADCON0bits.GO_DONE); //Wait for A/D Conversion to complete
- return ((ADRESH<<8)+ADRESL); //Returns Result
- }
- void UART_Init(long baud_rate){
- float bps;
- TRISC=0b11000000;
- bps = (( (float) (F_CPU) / (float) baud_rate) - 1); // sets baud rate
- SPBRG=(int)bps; // store value for 9600 baud rate
- TXSTAbits.CSRC = 0; // Don't care for asynchronous mode
- TXSTAbits.TX9 = 0; // Selects 8-bit data transmission
- TXSTAbits.TXEN = 1; // Enable Data tranmission on RC6 pin
- TXSTAbits.SYNC = 0; // Selects Asynchronous Serial Communication Mode
- TXSTAbits.BRGH = 0; // Default Baud rate speed
- BAUDCTLbits.BRG16 = 0; // selects only 8 bit register for baud rate
- RCSTA = 0x90; // Enables UART Communication PORT
- }
- void UART_Write(char *tx_str){
- char ch;
- while(*tx_str != 0x00){
- ch = *tx_str;
- while(!TRMT);
- TXREG = ch;
- tx_str++;
- }
- }
- void displayDigit1 (unsigned int digit1){
- if (digit1==0){
- LATA=0b00000111;
- LATB=0b00000110;
- LATC=0b00001000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==1){
- LATA=0b00000100;
- LATB=0b00000010;
- LATC=0b00000000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==2){
- LATA=0b00000011;
- LATB=0b00001110;
- LATC=0b00000000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==3){
- LATA=0b00000110;
- LATB=0b00001110;
- LATC=0b00000000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==4){
- LATA=0b00000100;
- LATB=0b00001010;
- LATC=0b00001000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==5){
- LATA=0b00000110;
- LATB=0b00001100;
- LATC=0b00001000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==6){
- LATA=0b00000111;
- LATB=0b00001100;
- LATC=0b00001000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==7){
- LATA=0b00000100;
- LATB=0b00000110;
- LATC=0b00000000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==8){
- LATA=0b00000111;
- LATB=0b00001110;
- LATC=0b00001000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit1==9){
- LATA=0b00000110;
- LATB=0b00001110;
- LATC=0b00001000;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- }
- void displayDigit2 (unsigned int digit2){
- if (digit2==0){
- LATA|=0b00110000;
- LATB|=0b00100001;
- LATC|=0b00110010;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==1){
- LATA|=0b00000000;
- LATB|=0b00000000;
- LATC|=0b00010010;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==2){
- LATA|=0b00110000;
- LATB|=0b00000000;
- LATC|=0b00110001;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==3){
- LATA|=0b00100000;
- LATB|=0b00000000;
- LATC|=0b00110011;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==4){
- LATA|=0b00000000;
- LATB|=0b00000001;
- LATC|=0b00010011;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==5){
- LATA|=0b00100000;
- LATB|=0b00000001;
- LATC|=0b00100011;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==6){
- LATA|=0b00110000;
- LATB|=0b00000001;
- LATC|=0b00100011;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==7){
- LATA|=0b00000000;
- LATB|=0b00000000;
- LATC|=0b00110010;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==8){
- LATA|=0b00110000;
- LATB|=0b00000001;
- LATC|=0b00110011;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- if (digit2==9){
- LATA|=0b00100000;
- LATB|=0b00000001;
- LATC|=0b00110011;
- PORTA=LATA;
- PORTB=LATB;
- PORTC=LATC;
- }
- }
- void displayTemperature (unsigned int temp){
- LATA=0x00;
- LATB=0x00;
- LATC=0x00;
- if (temp>=10){
- int digit1=temp/10;
- int digit2=temp%10;
- displayDigit1 (digit1);
- displayDigit2 (digit2);
- }
- if (temp>=0 && temp <10)
- displayDigit2 (temp);
- }
- void main(void) {
- TRISA=0x00;
- TRISB=0b00100000;
- TRISC=0b11000000;
- float temp_old, temp, voltage;
- //UART_Init(9600);
- ADC_Init();
- uint16_t temp_ADC=ADC_Read(13);
- voltage = temp_ADC * 0.0048828;
- temp_old = voltage / 0.01;
- displayTemperature((int)temp);
- while (1){
- temp_ADC=ADC_Read(13);
- voltage = temp_ADC * 0.0048828;
- temp = voltage / 0.01;
- if (abs(temp-temp_old)>=0.6){
- displayTemperature((int)temp);
- temp_old=temp;
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement