Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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;
- }
- 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 FullStepMode (){
- PORTCbits.RC0=0;
- PORTCbits.RC1=0;
- PORTCbits.RC2=0;
- }
- void HalfStepMode (){
- PORTCbits.RC0=1;
- PORTCbits.RC1=0;
- PORTCbits.RC2=0;
- }
- void QuarterStepMode (){
- PORTCbits.RC0=0;
- PORTCbits.RC1=1;
- PORTCbits.RC2=0;
- }
- void EighthStepMode (){
- PORTCbits.RC0=1;
- PORTCbits.RC1=1;
- PORTCbits.RC2=0;
- }
- void SixteenthStepMode (){
- PORTCbits.RC0=1;
- PORTCbits.RC1=1;
- PORTCbits.RC2=1;
- }
- void driverInit (){
- // sleep and reset set to logic high
- PORTA=0b00000110;
- }
- void setDirection (int c){
- if (c==1){
- PORTCbits.RC4=1;
- }
- if (c==0){
- PORTCbits.RC4=0;
- }
- }
- void driveMotor (int dir, int deg){
- setDirection (dir);
- for (int i=0; i<deg/1.8; i++){
- PORTCbits.RC5=1;
- __delay_us(1000);
- PORTCbits.RC5=0;
- __delay_us(1000);
- }
- }
- //OPERATION:
- // MSI -> RC2
- // MS2 -> RC1
- // MS3 -> RC0
- // DRIVER_SLEEP -> RA1
- // DRIVER_RST -> RA2
- // STEP -> RC5
- // DIR -> RC4
- // MS1 MS2 MS3
- // Full Step: L L L
- // Half Step: H L L
- // Quarter Step: L H L
- // Eighth Step: H H L
- // Sixteenth Step: H H H
- // SLEEP: logic low puts the driver into sleep mode; a logic high allows normal operation
- // RESET: logic hight sets the translator to a predefined Home state and turns off all of the FET outputs; logic high enables STEP inputs
- // DIR: sets the direction
- // STEP: PWM signal
- void main (void){
- TRISB=0b00100001;
- TRISA=0x00;
- TRISC=0x00;
- float voltage, temp;
- int tempWithCorrection;
- ADC_Init();
- UART_Init (9600);
- uint16_t temp_ADC;
- driverInit ();
- FullStepMode ();
- int dir=1;
- while (1){
- temp_ADC=ADC_Read(13);
- voltage = temp_ADC * 0.0048828;
- temp=voltage*100;
- tempWithCorrection=temp-6;
- if (tempWithCorrection>28){
- driveMotor (1, 100); //open window
- UART_Write ("temperature: ");
- UART_Write (tempWithCorrection + '0');
- UART_Write ("Windows Opened");
- PORTBbits.RB4=1; // LED indicates opened windows
- }
- if (tempWithCorrection<23){
- PORTBbits.RB4=0; // LED indicates closed windows
- driveMotor (0, 100);
- UART_Write ("temperature: ");
- UART_Write (tempWithCorrection + '0');
- UART_Write ("Windows Closed");
- }
- }
- return
Advertisement
Add Comment
Please, Sign In to add comment