You are on page 1of 7

E:/#0-2S2017/#1-TEC_SELDI/SAP-02 - Projeto Forno MCU/SELDI_SAP-02 MCU_FW/Forno_DSET-SAP01_137-V1.X/forno.

h
/*
* File: forno.h
* Author: Sergio Cotes Eufrasio
* Comments:
* Revision history: 01 Data: 28/03/2017
*/

#ifndef XC_HEADER_TEMPLATE_H
#defineXC_HEADER_TEMPLATE_H

#ifdef__cplusplus
extern "C" {
#endif /* __cplusplus */

#ifdef__cplusplus
}
#endif /* __cplusplus */

#endif/* XC_HEADER_TEMPLATE_H */

///////////////////////////////////////////////////////////////////////////
// CONFIGURAÇÕES DO MICROCONTROLADOR
// PIC18F4550 Configuration Bit Settings

// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator (HS))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
1.1 of 7 2017.04.03 21:52:24
E:/#0-2S2017/#1-TEC_SELDI/SAP-02 - Projeto Forno MCU/SELDI_SAP-02 MCU_FW/Forno_DSET-SAP01_137-V1.X/forno.h
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 20000000

//////////////////////INCLUSÃO DE ARQUIVOS EXTERNOS////////////////////////////


#include <xc.h> //< > bibliotecas no diretório do compilador;
//" " bibl. dir projeto
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

////////////DECLARAÇÃO DE CONSTANTES E VARIÁVEIS GLOBAIS///////////////////////


//int variavel = 0x00 //pode ser char, float, signed int, etc.
int motor=0,sentido=0,recuo=0,aquec=0,avanco=0;

2.1 of 7 2017.04.03 21:52:24


E:/#0-2S2017/#1-TEC_SELDI/SAP-02 - Projeto Forno MCU/SELDI_SAP-02 MCU_FW/Forno_DSET-SAP01_137-V1.X/forno.h
float valor=0,_valorConvertido = 0,_aj_temprt = 0,_aj_tempo_s = 0,temprt = 0;
int Tdisp = 1;

#define c0 PORTBbits.RB3 //Saída para as teclas sensores inicio e fim


#define c1 PORTBbits.RB2 //saidas teclas DESL, MOTOR OFF, AQUEC OFF e
//RECUO
#define c2 PORTBbits.RB1 //saídas teclas LIGA, MOTOR ON, AQUEC ON AVANÇO
#define c3 PORTBbits.RB0
#define l0 PORTBbits.RB4 //Entradas teclas DESL e LIG
#define l1 PORTBbits.RB5 //Entrada teclas MOTOR OFF e MOTOR ON
#define l2 PORTBbits.RB6 //Entrada teclas S_INIC, AQUEC OFF e AQUEC ON
#define l3 PORTBbits.RB7 //Entrada teclas S_FORNO, RECUO e AVANCO
#define avnc PORTCbits.RC6 //ponte h
#define rec PORTCbits.RC7
#define reaq PORTCbits.RC1 //Resistência de aquecimento
#define start PORTCbits.RC0 //INICIA CICLO AUTOMÁTICO
#define liga PORTBbits.RB0 //MANUAL
#define vent PORTCbits.RC2 //MANUAL
#define t_tecl 100//tempo de reconhecimento de tecla

#define fator_ajtmprt 10.23 //////Fator de calibração potenciômetro temperatura


//#define fator_tmprt 0.68 //////Fator de calibração medição de temperatura
#define fator_tmprt 2 //Fator 0.68 para simulador e fator 2 para KIT
#define fator_ajtempo 17.05 //////Fator de calibração potenciômetro de tempo

#define RS PORTEbits.RE0 //lcd PINO RS


#define RW PORTEbits.RE2
#define EN PORTEbits.RE1
#define BF PORTDbits.RD7
#define DATAPORT LATD
#define LCD_LINE1 0x80
#define LCD_LINE2 0xC0
#define LCD_LINE3 0x94
#define LCD_LINE4 0xD4
#define CLRSCR 0x01
#define DISPLAY_ON 0x0C //Definindo o nome DISPLAY_ON possui o
//valor 0C hexa
#define DISPLAY_OFF 0x08
#define CURSOR_ON 0x0A
#define CURSOR_OFF 0x08
#define CURSOR_INC 0x06
#define MODE_8BIT 0x38
#define MODE_4BIT 0x28
#define ADC_ch0 0b00000000 //0,0,chs3,chs2,chs1,chs0,0,0
#define ADC_ch1 0b00000100
#define ADC_ch2 0b00001000

/////////////////////////Declaração de Prototipos/////////////////////////////
void LCDclean(); //Limpeza do LCD
void manual();
void automatico();

3.1 of 7 2017.04.03 21:52:24


E:/#0-2S2017/#1-TEC_SELDI/SAP-02
void automatico(); - Projeto Forno MCU/SELDI_SAP-02 MCU_FW/Forno_DSET-SAP01_137-V1.X/forno.h
void LCD_data(unsigned char data); //Envia dado de caracter para o LCD
void LCD_cmd(unsigned char cmd);
void LCD_string(const char *buffer);
void LCD_isbusy(void);
void LCD_blink(void);
void LCD_init(void);
void Delay_ms (int x);
void Delay_us (int x);
float le_adc_x(char ch, float fator_cal);
void pwm (void);

void LCD_init(void){
TRISD=0x00;
TRISE=0x00;
LCD_cmd(MODE_8BIT); // 2 Line, 5x7 display, 8 bit
LCD_cmd(CLRSCR); // clear the screen
LCD_cmd(CURSOR_INC); // Cursor Incremetns on each write
LCD_cmd(DISPLAY_ON | CURSOR_OFF); // Display on and Cursor Off
return;
}

void LCD_data(unsigned char data){


LCD_isbusy();
RS = 1;
RW = 0;
Delay_ms(1);
EN = 1;
DATAPORT = data;
EN = 0;
Delay_ms(1);

void LCD_cmd(unsigned char cmd){


LCD_isbusy();
RS = 0;
RW = 0;
EN = 1; //liga display
DATAPORT = cmd;
Delay_ms(1);
EN = 0; //desabilita display
}

void LCD_string(const char *buffer)


{
while(*buffer) // Write data to LCD up to null
{
LCD_isbusy(); // Wait while LCD is busy
4.1 of 7 2017.04.03 21:52:24
E:/#0-2S2017/#1-TEC_SELDI/SAP-02
LCD_isbusy();
- Projeto Forno MCU/SELDI_SAP-02 MCU_FW/Forno_DSET-SAP01_137-V1.X/forno.h
// Wait while LCD is busy
LCD_data(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
}

void LCD_isbusy(void)
{

// TRISDbits.TRISD7=1; // Make D7 as input


// RS = LOW;
// RW = HIGH;
// while(!BF);
// EN = HIGH;
// Delay1KTCYx(10);
// EN = LOW;
// Delay1KTCYx(10);
// TRISDbits.TRISD7=0; // Back to Output

Delay_us(40); //Este comando está substituindo as 9 linhas


//anteriores para o Proteus

void Delay_ms (int x)


{
int cont; //variavel de uso local
for(cont=0; cont<x; cont++)
{
__delay_ms(1);
}

void Delay_us (int x)


{
int cont;
for(cont=0; cont<x; cont++)
{
__delay_us(1);
}
}

////////###########FUTURAS IMPLEMENTAÇÕES##########///////////////////////////

//////////////////########TIMER COM INTERRUPÇÃO########////////////////////////


/*
* IMPLEMENTAR TEMPORIZADOR POSTERIORMENTE, PARA O MOMENTO, CONTAR TEMPO COM

* FUNÇÃO DELAY.
/////////// iniciar contagem de tempo de secagem/////////////////////
//////////Mostrar no display contagem regressiva////////////////////
5.1 of 7 2017.04.03 21:52:24
E:/#0-2S2017/#1-TEC_SELDI/SAP-02 - Projeto Forno MCU/SELDI_SAP-02 MCU_FW/Forno_DSET-SAP01_137-V1.X/forno.h
//////////Mostrar no display contagem regressiva////////////////////
TMR0H = 0; //Configurar depois atravéz de variável, valor inicial
TMR0L = 0; //configurar parte alta atravéz de cópia de variável
T0CONbits.T08BIT=1;//Configura temporizador 0 como 8 bits
T0CONbits.TOCS=0;//Informa que o incremento do contador será pelo clock
//interno, o mesmo das instruções (1/4 da freqUência
//do oscilador)
T0CONbits.PSA=0;//será utilizado um prescaller
T0CONbits.TOPS=0b001;//prescaler 4
INTCONbits.TMR0IE=0;//desabilita a interrupção do timer 0
INTCOMbits.TMR0IF=0;//zera o contador de estouro de tempo

T0C0Nbits.TMR0IF=1;//Inicia o temporizador
if(INTCONbits.TMR0IF ==1) { //PROCEDIMENTO DE ESTOURO DO TEMPORIZADOR}
*/

//////////################ PWM ###################/////////////

/* Esta rotina é do 18f452, adaptar para o 16f877A


* PWM a 50% do duty cile (velocidade = N/2)
* */
void pwm (void)
{
TRISCbits.TRISC2 = 0;// pORTA DO pwm É SAÍDA DIGITAL
T2CONbits.T2CKPS = 0b01;//Prescaler igual a 4
PR2 = 99;//Carrega o registrador PR2 com o valor calculado
T2CONbits.TMR2ON = 1;//Ativa o temporizador 2
CCP1CON:CCPR1L = 0B00110010;//Os 8 bits mais sig do valor calculado para CCPR
CCP1CONbits.DC1B1=0;//um dos 2 bits menos sig bit 1
CCP1CONbits.DC1B0=0;//bit 0
CCP1CONbits.CCP1M0 = 0b0;//Habilita o CCP1 para o módulo PWM.
CCP1CONbits.CCP1M1 = 0b0;
CCP1CONbits.CCP1M2 = 0b1;
CCP1CONbits.CCP1M3 = 0b1;
}

float le_adc_x(char ch, float fator_cal)//Leitura do conversor AD


{
TRISAbits.TRISA0 = 1; //Porta A0 (RA0) é entrada
TRISAbits.TRISA1 = 1;
TRISAbits.TRISA2 = 1;
ADCON1bits.VCFG = 0b00;//REF VCC E GND
ADCON1bits.PCFG = 0b1011;//configura AN0, AN1, AN2 e AN3 como analógicas
ADCON2bits.ADFM = 1;//Justifica resultado 10 bits AD a direita nos REGS
ADCON0 = ch; //configura canal ch como entrada para o A/D
//ADCON0 ? ? CHS3 CHS2 CHS1 CHS0 GO/DONE ADON 00XXXX00
ADCON2bits.ACQT = 0B010;//Tempo de aquisição (TAD)
ADCON2bits.ADCS = 0B101;//Tempo de conversão (Tosc)
Delay_ms(1);
ADCON0bits.ADON = 1; //inicia aquisição (sample/hold)

6.1 of 7 2017.04.03 21:52:24


E:/#0-2S2017/#1-TEC_SELDI/SAP-02
ADCON0bits.ADON = 1;
- Projeto Forno MCU/SELDI_SAP-02 MCU_FW/Forno_DSET-SAP01_137-V1.X/forno.h
//inicia aquisição (sample/hold)
Delay_us(3); //aguarda aquisição
ADCON0bits.GO_DONE = 1; //dispara conversão

while(ADCON0bits.GO_DONE == 1); //programa aguarda bit GO/DONE zerar


_valorConvertido = (ADRESH << 8) | ADRESL;
_valorConvertido=_valorConvertido/fator_cal;
ADCON1bits.PCFG = 0b1111; //As entradas voltam a ser digitais
ADCON0bits.ADON = 0;//finaliza
Delay_ms(1);
return;
}

7.1 of 7 2017.04.03 21:52:24

You might also like