Micro
Micro
Micro
de
termistor###
Programa de termistor
#include "config.h"
#include "reloj.h"
#include <libpic30.h>
#include "xlcd.h"
#include <p33FJ32MC202.h>
#include <stdio.h>
#include <math.h>
char adc_vector[4]="";
int adc_result=0;
float vm=0;
float rntc=0;
double temperaturaK=0;
double temperaturaC=0;
float adc_float=0;
float rAux=10000;
float vcc=3.6;
void ADC_Init(void){
AD1CON1bits.ADON =1;
//positiva=AVdd, Negativa=AVsa
AD1CON3bits.ADCS=21;
int main(){
XLCDgotoXY(0,0);
putrsXLCD("Micro");
XLCDgotoXY(1,0);
putrsXLCD("Temp: ");
XLCDgotoXY(1,6);
while(1){
while(!AD1CON1bits.DONE);
AD1CON1bits.DONE=0;
adc_result=ADC1BUF0;
rntc=rAux/((vcc/vm)-1);
temperaturaK=beta/(log(rntc/r0)+(beta/temp0));
temperaturaC=temperaturaK-273.15;
__delay_ms(10);
sprintf(adc_vector,"%.2f",temperaturaC);
printf("%.2f\n",temperaturaC);
XLCDgotoXY(1,6);
putrsXLCD(adc_vector);
XLCDgotoXY(1,12);
putrsXLCD("C");
return 0;
Xlcd.h
#ifndef XLCD_H
#define XLCD_H
/* DATA_PORT defines the port to which the LCD data lines are connected */
/* CTRL_PORT defines the port where the control lines are connected.
* These are just samples, change to match your application.
*/
/* LINES*/
void XLCDInit(void);
/* OpenXLCD
*/
/* SetCGRamAddr
*/
/* SetDDRamAddr
*/
/* BusyXLCD
*/
*/
/* ReadDataXLCD
*/
char ReadDataXLCD(void);
/* WriteCmdXLCD
*/
/* WriteDataXLCD
*/
void WriteDataXLCD(char);
/* putcXLCD
* A putc is a write
*/
/* putsXLCD
*/
*/
#endif /* XLCD_H */
Xlcd.c
#include <xc.h>
#include "reloj.h"
#include <libpic30.h>
#include "xlcd.h"
__delay_us(1);
return;
__delay_ms(200);
return;
__delay_ms(5);
return;
return;
// Fila columna
if ( x>0 ){WriteCmdXLCD(DDRAM_LINE2+y);}
else {WriteCmdXLCD(DDRAM_LINE1+y);}
return;
void XLCDInit(void)
{ unsigned char i;
WriteCmdXLCD(CLEAR_XLCD);
SetCGRamAddr(Addres*8);
++buffer;
DATA_PORT = 0;
TRIS_DATA_PORT = 0xff;
TRIS_DATA_PORT |= 0xf0;
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
TRIS_RS = 0;
TRIS_E = 0;
DelayPORXLCD();
#endif
#endif
Delay_1us();
E_PIN = 0;
DelayXLCD();
DATA_PORT |= 0b00100000;
DATA_PORT |= 0b00000010;
#endif
#endif
Delay_1us();
E_PIN = 0;
DelayXLCD();
DATA_PORT |= 0b00100000;
DATA_PORT |= 0b00000010;
#endif
#endif
Delay_1us();
E_PIN = 0;
#endif
#endif
return;
while(BusyXLCD());
RS_PIN = 0;
Delay_1us();
Delay_1us();
E_PIN = 0;
Delay_1us();
TRIS_DATA_PORT = 0xff; // Make data port inputs
#endif
RS_PIN = 0;
Delay_1us();
Delay_1us();
E_PIN = 0;
DATA_PORT |= ((CGaddr<<4)&0xf0);
DATA_PORT |= (CGaddr&0x0f);
#endif
Delay_1us();
Delay_1us();
E_PIN = 0;
#endif
#endif
return;
while(BusyXLCD());
RS_PIN = 0;
Delay_1us();
Delay_1us();
E_PIN = 0;
Delay_1us();
RS_PIN = 0;
Delay_1us();
Delay_1us();
E_PIN = 0;
DATA_PORT |= ((DDaddr<<4)&0xf0);
DATA_PORT |= (DDaddr&0x0f);
#endif
Delay_1us();
Delay_1us();
E_PIN = 0;
#endif
#endif
return;
RS_PIN = 0;
Delay_1us();
Delay_1us();
{ // If high
#endif
Delay_1us();
Delay_1us();
E_PIN = 0;
}
else // Busy bit is low
Delay_1us();
Delay_1us();
E_PIN = 0;
#endif
while(BusyXLCD());
RS_PIN = 0;
Delay_1us();
Delay_1us();
E_PIN = 0;
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock data out of the LCD controller
Delay_1us();
data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data
data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data
#endif
Delay_1us();
Delay_1us();
data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data
data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data
#endif
E_PIN = 0;
#endif
return (data&0x7f); // Return the address, Mask off the busy bit
char ReadDataXLCD(void)
char data;
while(BusyXLCD());
RW_PIN = 1;
Delay_1us();
E_PIN = 1; // Clock the data out of the LCD
Delay_1us();
E_PIN = 0;
RW_PIN = 0;
RW_PIN = 1;
RS_PIN = 1;
Delay_1us();
Delay_1us();
#endif
Delay_1us();
Delay_1us();
#endif
E_PIN = 0;
RW_PIN = 0;
#endif
return(data); // Return the data byte
while(BusyXLCD());
Delay_1us();
Delay_1us();
E_PIN = 0;
Delay_1us();
DATA_PORT |= cmd&0xf0;
DATA_PORT |= (cmd>>4)&0x0f;
#endif
RS_PIN = 0;
Delay_1us();
E_PIN = 0;
DATA_PORT |= (cmd<<4)&0xf0;
DATA_PORT |= cmd&0x0f;
#endif
Delay_1us();
Delay_1us();
E_PIN = 0;
TRIS_DATA_PORT |= 0xf0;
#else
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
while(BusyXLCD());
RW_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock data into LCD
Delay_1us();
E_PIN = 0;
DATA_PORT |= data&0xf0;
DATA_PORT |= ((data>>4)&0x0f);
#endif
RW_PIN = 0;
Delay_1us();
Delay_1us();
E_PIN = 0;
Delay_1us();
DATA_PORT |= ((data<<4)&0xf0);
DATA_PORT |= (data&0x0f);
#endif
Delay_1us();
E_PIN = 1; // Clock nibble into LCD
Delay_1us();
E_PIN = 0;
TRIS_DATA_PORT |= 0xf0;
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
return;
//DelayExecution();
return;
Config.h
// FBS
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment
may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No
Boot program Flash segment)
// FGS
#pragma config GWRP = ON // General Code Segment Write Protect (User program
memory is write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program
memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = PRI // Oscillator Mode (Primary Oscillator (XT, HS, EC))
#pragma config IESO = ON // Internal External Switch Over Mode (Start-up device with
FRC, then automatically switch to user-selected oscillator source when ready)
// FOSC
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow Only One
Re-configuration)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching
and Fail-Safe Clock Monitor are disabled)
// FWDT
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-
Window mode)
// FPOR
#pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
#pragma config LPOL = ON // Motor Control PWM Low Side Polarity bit (PWM module
low side output pins have active-high output polarity)
#pragma config HPOL = ON // Motor Control PWM High Side Polarity bit (PWM
module high side output pins have active-high output polarity)
#pragma config PWMPIN = ON // Motor Control PWM Module Pin Mode bit (PWM
module pins controlled by PORT register at device Reset)
// FICD
#include <xc.h>
Reloj.h
#define FCY 4000000
//FCY=FOSC/2
//FOSC= FIN*(M/N1*N2)
#include "libpic30.h"
#include "p33fj32mc202.h"
#include "config.h"
#include <xc.h>
#include <stdio.h>
int t=500;
void init_INT0(void);
void main(void) {
init_INT0();
while(1){
/*_LATB1=1;
__delay_ms(t);
_LATB2=1;
__delay_ms(t);
_LATB3=1;
__delay_ms(t);
_LATB4=1;
__delay_ms(t);*/
/*
_LATB6 = 0X0001;
__delay_ms(t);
_LATB1=0x0001;
__delay_ms(t);
_LATB2=0x0001;
__delay_ms(t);
_LATB3=0x0001;
__delay_ms(t);
_LATB4=0x0001;
__delay_ms(t);
*/
/*
LATB=0x0004;
__delay_ms(t);
LATB=0x0006;
__delay_ms(t);
LATB=0x0002;
__delay_ms(t); */
/*
LATB=0x0010;
__delay_ms(t);
LATB=0x0008;
__delay_ms(t);
LATB=0x0008;
__delay_ms(t);
LATB=0x000C;
__delay_ms(t);
LATB=0x0004;
__delay_ms(t);
LATB=0x0006;
__delay_ms(t);
LATB=0x0002;
__delay_ms(t);*/
_LATA0 = 0X0000;
__delay_ms(t);
_LATA4 = 0X0001;
__delay_ms(t);
LATB=0x0012;
__delay_ms(t);
LATB=0x0010;
__delay_ms(t);
LATB=0x0008;
__delay_ms(t);
LATB=0x0008;
__delay_ms(t);
LATB=0x000C;
__delay_ms(t);
LATB=0x0004;
__delay_ms(t);
LATB=0x0006;
__delay_ms(t);
LATB=0x0002;
__delay_ms(t);
return;
void init_INT0(void){
IPC0bits.INT0IP=4; //Prioridad 1
//Proceso de interrupción
LATAbits.LATA0=1;
LATAbits.LATA4=0;
LATBbits.LATB1=0;
LATBbits.LATB2=0;
LATBbits.LATB3=0;
LATBbits.LATB4=0;
__delay_ms(8000);
IFS0bits.INT0IF=0;
}
Librerías
Config.h
// DSPIC33FJ32MC202 Configuration Bit Settings
// FBS
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment
may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No
Boot program Flash segment)
// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program
memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program
memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = PRI // Oscillator Mode (Primary Oscillator (XT, HS, EC))
#pragma config IESO = ON // Internal External Switch Over Mode (Start-up device with
FRC, then automatically switch to user-selected oscillator source when ready)
// FOSC
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow Only One
Re-configuration)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching
and Fail-Safe Clock Monitor are disabled)
// FWDT
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-
Window mode)
// FPOR
#pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
#pragma config LPOL = ON // Motor Control PWM Low Side Polarity bit (PWM module
low side output pins have active-high output polarity)
#pragma config HPOL = ON // Motor Control PWM High Side Polarity bit (PWM
module high side output pins have active-high output polarity)
#pragma config PWMPIN = ON // Motor Control PWM Module Pin Mode bit (PWM
module pins controlled by PORT register at device Reset)
// FICD
#include <xc.h>