### todo lo que está aquí es del lab.
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>
//crear variable global
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;
float beta=3977;//beta de 10kohm para NTC
float temp0=298.0;//Temp en grados Kelvin(0)
float r0=10000; //Resistencia NTC cuando la temperatura es 0
void ADC_Init(void); //Funcion prototipo para ADC
void ADC_Init(void){
TRISA = 0x0001 ; //Entrada analogica RA0
AD1PCFGL=0XFFFE; //Todas entradas anaogicas *Solo se necesita la primera
AD1CON1bits.AD12B=1; //1=12 bits resolucion y muestreo secuencial de los canales
AD1CON1bits.ADON =1;
AD1CON1bits.ASAM=0; //El muestreo inicia cuando bit SAMP=1 *muestreo manual
AD1CON1bits.SSRC=0; //Termina el muestreo y inicia la conversion
AD1CHS0bits.CH0NA=0; //La entrada negativa sera Vss
AD1CHS0bits.CH0SA=0; //Entrada positiva sera AN0
AD1CON2bits.CSCNA=0; //Muestreo secuencial (scan) deshabilitado
AD1CON2bits.VCFG=0; //Referencia para la conversion:Referencia
//positiva=AVdd, Negativa=AVsa
AD1CON2bits.BUFM=0; //configurado como buffer de 16 palabras
AD1CON2bits.SMPI=0; //Despues de la conversion se genera la interrupcion
AD1CON2bits.ALTS=0; //Solo muestra un canal
AD1CON3bits.ADRC=0; //Reloj del ADC es derivado del sistema
AD1CON3bits.ADCS=21;
int main(){
XLCDInit(); //Inicia la pantalla LCD
ADC_Init(); //Inicia el ADc
WriteCmdXLCD(CLEAR_XLCD); //Limpia la pantalla LCD al reiniciar
XLCDgotoXY(0,0);
putrsXLCD("Micro");
XLCDgotoXY(1,0);
putrsXLCD("Temp: ");
XLCDgotoXY(1,6);
while(1){
AD1CON1bits.SAMP=1; //Inicio del muestreo
__delay_ms(500); //Tiempo de muestreo
AD1CON1bits.SAMP=0; //FInalizar el muestreo
while(!AD1CON1bits.DONE);
AD1CON1bits.DONE=0;
adc_result=ADC1BUF0;
vm=(float)adc_result*3.6/4096; //0 ~ 4095 (palabra de 12 bits)
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 */
//#define WRITE_DATA_PORT PORTD
//#define READ_DATA_PORT PORTD
#define DATA_PORT PORTB
#define TRIS_DATA_PORT TRISB
/* CTRL_PORT defines the port where the control lines are connected.
* These are just samples, change to match your application.
*/
#define RW_PIN LATBbits.LATB6 /* PORT for RW */
#define TRIS_RW TRISBbits.TRISB6 /* TRIS for RW */
#define RS_PIN LATBbits.LATB5 /* PORT for RS */
#define TRIS_RS TRISBbits.TRISB5/* TRIS for RS */
#define E_PIN LATBbits.LATB7 /* PORT for E */
#define TRIS_E TRISBbits.TRISB7 /* TRIS for E */
//#define PWR_PIN PORTDbits.RD7 /* PORT for PWR */
//#define TRIS_PWR DDRDbits.RD7 /* TRIS for PWR */
/* Display ON/OFF Control defines */
#define DON 0b00001111 /* Display on */
#define DOFF 0b00001011 /* Display off */
#define CURSOR_ON 0b00001111 /* Cursor on */
#define CURSOR_OFF 0b00001101 /* Cursor off */
#define BLINK_ON 0b00001111 /* Cursor Blink */
#define BLINK_OFF 0b00001110 /* Cursor No Blink */
/* Some Command Type defines */
#define CLEAR_XLCD 0x01 /* Clears the LCD */
#define RETURN_CURSOR_HOME 0x02 /* Returns the cursor to the HOME
position */
/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT 0b00010011 /* Cursor shifts to the left */
#define SHIFT_CUR_RIGHT 0b00010111 /* Cursor shifts to the right */
#define SHIFT_DISP_LEFT 0b00011011 /* Display shifts to the left */
#define SHIFT_DISP_RIGHT 0b00011111 /* Display shifts to the right */
/* Function Set defines */
#define FOUR_BIT 0b00101111 /* 4-bit Interface */
#define EIGHT_BIT 0b00111111 /* 8-bit Interface */
#define LINE_5X7 0b00110011 /* 5x7 characters, single line */
#define LINE_5X10 0b00110111 /* 5x10 characters */
#define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */
/* LINES*/
#define DDRAM_LINE1 0b10000000 /* 4-bit Interface */
#define DDRAM_LINE2 0b11000000 /* 4-bit Interface */
#define CGRAM_ADDRESS 0b01000000 /* 4-bit Interface */
void XLCDInit(void);
void XLCDgotoXY(int x,int y);
void XLCD_WriteChr_CGRAM( const char *buffer, unsigned char Addres);
/* OpenXLCD
* Configures I/O pins for external LCD
*/
void OpenXLCD(unsigned char lcdtype);
/* SetCGRamAddr
* Sets the character generator address
*/
void SetCGRamAddr(unsigned char);
/* SetDDRamAddr
* Sets the display data address
*/
void SetDDRamAddr(unsigned char);
/* BusyXLCD
* Returns the busy status of the LCD
*/
unsigned char BusyXLCD(void);
/* ReadAddrXLCD
* Reads the current address
*/
unsigned char ReadAddrXLCD(void);
/* ReadDataXLCD
* Reads a byte of data
*/
char ReadDataXLCD(void);
/* WriteCmdXLCD
* Writes a command to the LCD
*/
void WriteCmdXLCD(unsigned char cmd);
/* WriteDataXLCD
* Writes a data byte to the LCD
*/
void WriteDataXLCD(char);
/* putcXLCD
* A putc is a write
*/
#define putcXLCD WriteDataXLCD
/* putsXLCD
* Writes a string of characters to the LCD
*/
void putsXLCD(char *);
/* putrsXLCD
* Writes a string of characters in ROM to the LCD
*/
void putrsXLCD(const char *buffer);
/* User defines these routines according to the oscillator frequency */
//extern void DelayFor18TCY(void); // 1us delay
//extern void DelayPORXLCD(void); // 15ms delay
//extern void DelayXLCD(void); // 4.1ms
extern void Delay_1us(void); // 1us delay
extern void DelayPORXLCD(void); // 15ms delay
extern void DelayXLCD(void); // 4.1ms
extern void DelayExecution(void);
#endif /* XLCD_H */
Xlcd.c
#include <xc.h>
#include "reloj.h"
#include <libpic30.h>
#include "xlcd.h"
void Delay_1us( void )
__delay_us(1);
return;
/* Provide at least a 15ms delay */
void DelayPORXLCD( void )
__delay_ms(200);
return;
/* Provide at least a 5ms delay */
void DelayXLCD( void )
__delay_ms(5);
return;
void DelayExecution( void )
__delay_us(50); // 46us to Read/Write data operation
return;
// Fila columna
//void XLCDgotoXY( int x , int y)
void XLCDgotoXY(int x,int y)
if ( x>0 ){WriteCmdXLCD(DDRAM_LINE2+y);}
else {WriteCmdXLCD(DDRAM_LINE1+y);}
return;
void XLCDInit(void)
{ unsigned char i;
/* ----------------------------- Inicializacion del LCD 2x16 --------------------------------------------------*/
DelayPORXLCD(); // retardo incial para que la tension de alimentacion se
estabilice
OpenXLCD( FOUR_BIT & LINES_5X7); // Initialize LCD
DelayXLCD(); // Retardo de por lo menos 4.1 ms
WriteCmdXLCD( DON & CURSOR_OFF & BLINK_OFF ); // Set parameters
DelayXLCD(); // Retardo de por lo menos 4.1 ms
WriteCmdXLCD(CLEAR_XLCD);
DelayXLCD(); // Retardo de por lo menos 4.1 ms
WriteCmdXLCD(0x80); // Escribe el comando para poner el cursor a una
dirección en la memoria DDRAM
DelayXLCD(); // Retardo de por lo menos 4.1 ms
void XLCD_WriteChr_CGRAM( const char *buffer, unsigned char Addres)
{ unsigned char i=0;
SetCGRamAddr(Addres*8);
for (i=0;i<8;i++) // Write data to LCD up to null
{ WriteDataXLCD(*buffer); // Write character to LCD
++buffer;
void OpenXLCD(unsigned char lcdtype)
//TRIS_PWR = 0; // PWR control pin made output
//PWR_PIN = 1; // Power LCD
DelayPORXLCD(); // Delay 15ms
// The data bits must be either a 8-bit port or the upper or
// lower 4-bits of a port. These pins are made into inputs
#ifdef BIT8 // 8-bit mode, use whole port
DATA_PORT = 0;
TRIS_DATA_PORT = 0xff;
#else // 4-bit mode
#ifdef UPPER // Upper 4-bits of the port
DATA_PORT &= 0x0f;
TRIS_DATA_PORT |= 0xf0;
#else // Lower 4-bits of the port
DATA_PORT &= 0xf0;
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
TRIS_RW = 0; // All control signals made outputs
TRIS_RS = 0;
TRIS_E = 0;
RW_PIN = 0; // R/W pin made low
RS_PIN = 0; // Register select pin made low
E_PIN = 0; // Clock pin made low
// Delay for 15ms to allow for LCD Power on reset
DelayPORXLCD();
// Setup interface to LCD
#ifdef BIT8 // 8-bit mode interface
TRIS_DATA_PORT = 0; // Data port output
DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit mode interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= 0b00100000; // Function set cmd(4-bit interface)
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= 0b00000010; // Function set cmd(4-bit interface)
#endif
#endif
E_PIN = 1; // Clock the cmd in
Delay_1us();
E_PIN = 0;
// Delay for at least 4.1ms
DelayXLCD();
// Setup interface to LCD
#ifdef BIT8 // 8-bit interface
DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00100000;
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00000010;
#endif
#endif
E_PIN = 1; // Clock the cmd in
Delay_1us();
E_PIN = 0;
// Delay for at least 100us
DelayXLCD();
// Setup interface to LCD
#ifdef BIT8 // 8-bit interface
DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00100000;
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00000010;
#endif
#endif
E_PIN = 1; // Clock cmd in
Delay_1us();
E_PIN = 0;
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0xff; // Make data port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make data nibble input
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make data nibble input
#endif
#endif
// Set data interface width, # lines, font
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(lcdtype); // Function set cmd
// Turn the display on then off
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF); // Display OFF/Blink OFF
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON); // Display ON/Blink ON
// Clear display
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(0x01); // Clear display
// Set entry mode inc, no shift
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode
// Set DD Ram address to 0
while(BusyXLCD()); // Wait if LCD busy
SetDDRamAddr(0); // Set Display data ram address to 0
return;
void SetCGRamAddr(unsigned char CGaddr)
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make data port ouput
DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port
RW_PIN = 0; // Set control signals
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock cmd and address in
Delay_1us();
E_PIN = 0;
Delay_1us();
TRIS_DATA_PORT = 0xff; // Make data port inputs
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make nibble input
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make nibble input
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((CGaddr | 0b01000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control signals
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock cmd and address in
Delay_1us();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((CGaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (CGaddr&0x0f);
#endif
Delay_1us();
E_PIN = 1; // Clock cmd and address in
Delay_1us();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make inputs
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make inputs
#endif
#endif
return;
void SetDDRamAddr(unsigned char DDaddr)
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port
RW_PIN = 0; // Set the control bits
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock the cmd and address in
Delay_1us();
E_PIN = 0;
Delay_1us();
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make port output
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make port output
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control bits
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock the cmd and address in
Delay_1us();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((DDaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (DDaddr&0x0f);
#endif
Delay_1us();
E_PIN = 1; // Clock the cmd and address in
Delay_1us();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make port input
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make port input
#endif
#endif
return;
unsigned char BusyXLCD(void)
RW_PIN = 1; // Set the control bits for read
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock in the command
Delay_1us();
#ifdef BIT8 // 8-bit interface
if(DATA_PORT&0x80) // Read bit 7 (busy bit)
{ // If high
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
else // Bit 7 low
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
if( DATA_PORT & 0x80 )
#else // Lower nibble interface
if( DATA_PORT & 0x08 )
#endif
E_PIN = 0; // Reset clock line
Delay_1us();
E_PIN = 1; // Clock out other nibble
Delay_1us();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
}
else // Busy bit is low
E_PIN = 0; // Reset clock line
Delay_1us();
E_PIN = 1; // Clock out other nibble
Delay_1us();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
#endif
unsigned char ReadAddrXLCD(void)
char data; // Holds the data retrieved from the LCD
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock data out of the LCD controller
Delay_1us();
data = DATA_PORT; // Save the data in the register
E_PIN = 0;
RW_PIN = 0; // Reset the control bits
#else // 4-bit interface
RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock data out of the LCD controller
Delay_1us();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data
#endif
E_PIN = 0; // Reset the clock
Delay_1us();
E_PIN = 1; // Clock out the lower nibble
Delay_1us();
#ifdef UPPER // Upper nibble interface
data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data
#endif
E_PIN = 0;
RW_PIN = 0; // Reset the control lines
#endif
return (data&0x7f); // Return the address, Mask off the busy bit
char ReadDataXLCD(void)
char data;
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
RS_PIN = 1; // Set the control bits
RW_PIN = 1;
Delay_1us();
E_PIN = 1; // Clock the data out of the LCD
Delay_1us();
data = DATA_PORT; // Read the data
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;
#else // 4-bit interface
RW_PIN = 1;
RS_PIN = 1;
Delay_1us();
E_PIN = 1; // Clock the data out of the LCD
Delay_1us();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data
#endif
E_PIN = 0; // Reset the clock line
Delay_1us();
E_PIN = 1; // Clock the next nibble out of the LCD
Delay_1us();
#ifdef UPPER // Upper nibble interface
data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the lower nibble of data
#endif
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;
#endif
return(data); // Return the data byte
void WriteCmdXLCD(unsigned char cmd)
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Data port output
DATA_PORT = cmd; // Write command to data port
RW_PIN = 0; // Set the control signals
RS_PIN = 0; // for sending a command
Delay_1us();
E_PIN = 1; // Clock the command in
Delay_1us();
E_PIN = 0;
Delay_1us();
TRIS_DATA_PORT = 0xff; // Data port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= cmd&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= (cmd>>4)&0x0f;
#endif
RW_PIN = 0; // Set control signals for command
RS_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock command in
Delay_1us();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= (cmd<<4)&0xf0;
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= cmd&0x0f;
#endif
Delay_1us();
E_PIN = 1; // Clock command in
Delay_1us();
E_PIN = 0;
#ifdef UPPER // Make data nibble input
TRIS_DATA_PORT |= 0xf0;
#else
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
void WriteDataXLCD(char data)
while(BusyXLCD());
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = data; // Write data to port
RS_PIN = 1; // Set control bits
RW_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock data into LCD
Delay_1us();
E_PIN = 0;
RS_PIN = 0; // Reset control bits
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= data&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= ((data>>4)&0x0f);
#endif
RS_PIN = 1; // Set control bits
RW_PIN = 0;
Delay_1us();
E_PIN = 1; // Clock nibble into LCD
Delay_1us();
E_PIN = 0;
Delay_1us();
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= ((data<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= (data&0x0f);
#endif
Delay_1us();
E_PIN = 1; // Clock nibble into LCD
Delay_1us();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
void putsXLCD(char *buffer)
while(*buffer) // Write data to LCD up to null
//while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
return;
void putrsXLCD(const char *buffer)
while(*buffer) // Write data to LCD up to null
//while(BusyXLCD()); // Wait while LCD is busy
//DelayExecution();
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
Config.h
// DSPIC33FJ32MC202 Configuration Bit Settings
// 'C' source line config statements
// 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 POSCMD = XT // Primary Oscillator Source (XT Oscillator Mode)
#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 WDTPOST = PS4096 // Watchdog Timer Postscaler (1:4,096)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-
Window mode)
#pragma config FWDTEN = ON // Watchdog Timer Enable (Watchdog timer always
enabled)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#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
#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1
and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
Reloj.h
#define FCY 4000000
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
### código del motor##
#define FCY 2000000 // frecuencia del reloj
//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) {
AD1PCFGL = 0xFFFF;//configuración de todas las salidas digitales
TRISAbits.TRISA0=0; //RB2 como salida
TRISAbits.TRISA4=0; //RB2 como salida
TRISBbits.TRISB1=0; //RB1 como salida
TRISBbits.TRISB2=0; //RB2 como salida
TRISBbits.TRISB3=0; //RB3 como salida
TRISBbits.TRISB4=0; //RB4 como salida
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){
INTCON2bits.INT0EP=0; //interrupcion INT0 cuando ocurre flanco de subida
IPC0bits.INT0IP=4; //Prioridad 1
IEC0bits.INT0IE=1; //Habilita la interrupcion
IFS0bits.INT0IF=0; //Limpiamos el flag de la interrupcion
//** rutina deATENCION interrucion externa INT0****
void __attribute__((_ interrupt__))_INT0Interrupt (void){
//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
// 'C' source line config statements
// 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 POSCMD = XT // Primary Oscillator Source (XT Oscillator Mode)
#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 WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR32 // WDT Prescaler (1:32)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-
Window mode)
#pragma config FWDTEN = ON // Watchdog Timer Enable (Watchdog timer always
enabled)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#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
#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1
and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>