Dani_info

Proiect_CIM

Apr 19th, 2026
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.38 KB | None | 0 0
  1.  
  2. // PIC16F883 Configuration Bit Settings
  3.  
  4. // 'C' source line config statements
  5.  
  6. // CONFIG1
  7. #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
  8. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  9. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  10. #pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
  11. #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
  12. #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
  13. #pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
  14. #pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
  15. #pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
  16. #pragma config LVP = OFF         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
  17.  
  18. // CONFIG2
  19. #pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
  20. #pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
  21.  
  22. // #pragma config statements should precede project file includes.
  23. // Use project enums instead of #define for ON and OFF.
  24.  
  25. #include <xc.h>
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. #define _XTAL_FREQ 8000000
  31. #define F_CPU 8000000/64 // value to calculate baud rate
  32.  
  33. unsigned char LATA;
  34. unsigned char LATB;
  35. unsigned char LATC;
  36.  
  37. void ADC_Init (){
  38.    ADCON0=0x41;
  39.    ADCON1=0x80;
  40. }
  41.  
  42. uint16_t ADC_Read (uint8_t channel){
  43.    ADCON0 &= 0x01000101; // Clear The Channel Selection Bits
  44.    ADCON0 |= channel<<2;
  45.    __delay_us(30);  
  46.    GO_nDONE = 1; //Initializes A/D Conversion
  47.    while(ADCON0bits.GO_DONE); //Wait for A/D Conversion to complete
  48.    return ((ADRESH<<8)+ADRESL); //Returns Result
  49. }
  50.  
  51. void UART_Init(long baud_rate){
  52.    float bps;
  53.    TRISC=0b11000000;
  54.    bps = (( (float) (F_CPU) / (float) baud_rate) - 1); // sets baud rate
  55.    SPBRG=(int)bps; // store value for 9600 baud rate
  56.    TXSTAbits.CSRC = 0; // Don't care for asynchronous mode
  57.    TXSTAbits.TX9 = 0; // Selects 8-bit data transmission
  58.    TXSTAbits.TXEN = 1; // Enable Data tranmission on RC6 pin
  59.    TXSTAbits.SYNC = 0; // Selects Asynchronous Serial Communication Mode
  60.    TXSTAbits.BRGH = 0; // Default Baud rate speed
  61.    BAUDCTLbits.BRG16 = 0; // selects only 8 bit register for baud rate
  62.    RCSTA = 0x90; // Enables UART Communication PORT
  63. }
  64.  
  65.  
  66.  
  67. void UART_Write(char *tx_str){
  68.     char ch;
  69.     while(*tx_str != 0x00){
  70.         ch = *tx_str;
  71.         while(!TRMT);
  72.         TXREG = ch;
  73.         tx_str++;
  74.     }
  75. }
  76.  
  77. void FullStepMode (){
  78.    PORTCbits.RC0=0;
  79.    PORTCbits.RC1=0;
  80.    PORTCbits.RC2=0;
  81. }
  82.  
  83. void HalfStepMode (){
  84.    PORTCbits.RC0=1;
  85.    PORTCbits.RC1=0;
  86.    PORTCbits.RC2=0;
  87. }
  88.  
  89. void QuarterStepMode (){
  90.    PORTCbits.RC0=0;
  91.    PORTCbits.RC1=1;
  92.    PORTCbits.RC2=0;
  93. }
  94.  
  95. void EighthStepMode (){
  96.    PORTCbits.RC0=1;
  97.    PORTCbits.RC1=1;
  98.    PORTCbits.RC2=0;
  99. }
  100.  
  101. void SixteenthStepMode (){
  102.    PORTCbits.RC0=1;
  103.    PORTCbits.RC1=1;
  104.    PORTCbits.RC2=1;
  105. }
  106.  
  107. void driverInit (){
  108.     // sleep and reset set to logic high
  109.     PORTA=0b00000110;
  110. }
  111.  
  112.  
  113. void setDirection (int c){
  114.     if (c==1){
  115.         PORTCbits.RC4=1;
  116.     }
  117.     if (c==0){
  118.         PORTCbits.RC4=0;
  119.     }
  120. }
  121.  
  122. void driveMotor (int dir, int deg){
  123.     setDirection (dir);
  124.     for (int i=0; i<deg/1.8; i++){
  125.         PORTCbits.RC5=1;
  126.         __delay_us(1000);
  127.         PORTCbits.RC5=0;
  128.         __delay_us(1000);
  129.     }
  130. }
  131. //OPERATION:
  132. // MSI -> RC2
  133. // MS2 -> RC1
  134. // MS3 -> RC0
  135. // DRIVER_SLEEP -> RA1
  136. // DRIVER_RST -> RA2
  137. // STEP -> RC5
  138. // DIR -> RC4
  139.  
  140. //                 MS1  MS2  MS3
  141. // Full Step:       L    L    L
  142. // Half Step:       H    L    L
  143. // Quarter Step:    L    H    L
  144. // Eighth Step:     H    H    L
  145. // Sixteenth Step:  H    H    H
  146.  
  147. // SLEEP: logic low puts the driver into sleep mode; a logic high allows normal operation
  148. // RESET: logic hight sets the translator to a predefined Home state and turns off all of the FET outputs; logic high enables STEP inputs
  149. // DIR: sets the direction
  150. // STEP: PWM signal
  151.  
  152.  
  153.  
  154.  
  155. void main (void){
  156.     TRISB=0b00100001;
  157.     TRISA=0x00;
  158.     TRISC=0x00;
  159.     float voltage, temp;
  160.     int tempWithCorrection;
  161.     ADC_Init();
  162.     UART_Init (9600);
  163.     uint16_t temp_ADC;
  164.     driverInit ();
  165.     FullStepMode ();
  166.     int dir=1;
  167.     while (1){
  168.         temp_ADC=ADC_Read(13);
  169.         voltage = temp_ADC * 0.0048828;
  170.         temp=voltage*100;
  171.         tempWithCorrection=temp-6;
  172.         if (tempWithCorrection>28){
  173.             driveMotor (1, 100); //open window
  174.             UART_Write ("temperature: ");
  175.             UART_Write (tempWithCorrection + '0');
  176.             UART_Write ("Windows Opened");
  177.             PORTBbits.RB4=1; // LED indicates opened windows
  178.         }
  179.         if (tempWithCorrection<23){
  180.             PORTBbits.RB4=0; // LED indicates closed windows
  181.             driveMotor (0, 100);
  182.             UART_Write ("temperature: ");
  183.             UART_Write (tempWithCorrection + '0');
  184.             UART_Write ("Windows Closed");
  185.         }
  186.     }
  187.    return
  188.  
  189.  
  190.  
Advertisement
Add Comment
Please, Sign In to add comment