Video funcional
http://youtu.be/Cqz0c6A4w-c
BLOG DE ELECTRONICA - DOMOTICA
jueves, 18 de julio de 2013
Implementación de la rutina de un código para desarrollar una aplicación tipo Hardware-Software atreves del IDE de visual Basic y el código en firmware en PIC C necesarios para establecer una comunicación USB con un dispositivo HID LAB 8
MATERIALES :
- PBC
- LEDS
- POTENCIOMETROS
- RESISTENCIAS
- MOTOR
- RS232
- PROTOBOARD
- COMPUTADOR
- VISUAL BASIC 2010
CODIGO FIRMWARE :
#include <18F2550.h>
#device adc=8//La resolución del módulo A/D es de 8 bits.
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN
#use delay (clock=12000000)//12Mhz
#USE RS232 (baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7)//Defincion RS-232
#use standard_io(a)
#use fast_io(b)// Aqui están las señales del encoder
#byte port_a = 0xF80
#byte port_b = 0xF81
// CCS Library dynamic defines
#DEFINE USB_HID_DEVICE TRUE //Tells the CCS PIC USB firmware to include HID handling code.
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 FOR IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE 64 //allocate 64 bytes in the hardware FOR transmission
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 FOR OUT bulk/interrupt transfers
#define USB_EP1_RX_SIZE 64 //allocate 64 bytes in the hardware FOR reception
// CCS USB Libraries
#include <pic18_usb.h> //Microchip 18Fxx5x hardware layer FOR usb.c
#include <usb_desc_hid 8-byte.h> //USB Configuration and Device descriptors FOR this UBS device
#include <usb.c> //handles usb setup tokens and get descriptor reports
void usb_debug_task(VOID)
{
STATIC int8 last_connected;
STATIC int8 last_enumerated;
INT8 new_connected;
INT8 new_enumerated;
new_connected = usb_attached ();
new_enumerated = usb_enumerated ();
last_connected = new_connected;
last_enumerated = new_enumerated;
}
// ----------------------------------Declaración de variables----------------------------------
int dato4;
int dato1;
//Variables USB-----------------------------------
int16 tx;
//int16 tx1=0;
#byte txl=43
#byte txh=44
#byte tx1l=45
#byte tx1h=46
#byte port_a = 0xF80
#byte port_b = 0xF81
int8 in_data[8];
int8 out_data[8];
// ----------------------------------Declaración de los prototipos de las funciones----------------------------------
#INT_TIMER1
void int_TMR1()//retardo programado a un segundo
{
}
#int_RDA
RDA_isr()
{
dato4=GETC();
}
void main(){
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
set_tris_b(0x00);
SETUP_ADC_PORTS(AN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
usb_init();
for (;;) {
usb_task ();
usb_debug_task ();
usb_get_packet (1, in_data, 8);
txh=(in_data[1]);
txl=(in_data[2]);
tx1h=(in_data[3]);
dato1=(in_data[4]);
PUTC(dato1);
output_b(dato1);
out_data[0]=30 ;
//out_data[1]=q;
//out_data[2]=q1;
set_adc_channel(0);
delay_us(20);
dato4 = read_adc();
out_data[3]=dato4;
usb_put_packet (1, out_data, 8, USB_DTS_TOGGLE);
ENABLE_INTERRUPTS(int_timer0);
set_timer0(tx);
}
}
CODIGO SOFTWARE:
Public Class Form1
Dim val1, y1, y2 As Int16
Dim sensor, setpoint As Byte
Dim cuenta, dato4 As Byte
Dim bajoconta, bajo, bajoy1, bajoy2 As Byte
Dim uno, dos, tres, cuatro, cinco, seis, siete, ocho As Byte
Private Sub HIDComm1_ConnectSuccess(ByVal sender As System.Object, ByVal e As AxVBHIDComm.__HIDComm_ConnectSuccessEvent) Handles HIDComm1.ConnectSuccess
Me.Label4.Text = "Control de port B" & Me.HIDComm1.Manufacturer
End Sub
Private Sub HIDComm1_Disconnected(ByVal sender As System.Object, ByVal e As AxVBHIDComm.__HIDComm_DisconnectedEvent) Handles HIDComm1.Disconnected
Me.Label4.Text = "Esperando conexion"
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.HIDComm1.Connect()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim buffer() As Byte
ReDim buffer(8)
If HIDComm1.Connected = False Then
HIDComm1.Connect()
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim outbuffer() As Byte
ReDim outbuffer(8)
Dim inbuffer() As Byte
ReDim inbuffer(8)
outbuffer(0) = 1
outbuffer(1) = 15
outbuffer(2) = 2
outbuffer(3) = 1
outbuffer(4) = cuenta
outbuffer(5) = y1
'outbuffer(6) = 0
'outbuffer(7) = 0
HIDComm1.WriteTo(outbuffer, 8)
inbuffer = HIDComm1.ReadFrom(8)
dato4 = inbuffer(3)
Label3.Text = dato4
ProgressBar1.Value = dato4
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
HIDComm1.Uninit()
End
End Sub
Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
cuenta = 70
Label1.Text = 3410
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
cuenta = 140
Label1.Text = 5500
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
cuenta = 185
Label1.Text = 7250
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
cuenta = 255
Label1.Text = "APAGADO"
End Sub
Private Sub Label1_Click_1(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
cuenta = 20
Label1.Text = "ENCENDIDO"
End Sub
Private Sub GroupBox1_Enter_1(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
End Class
- PBC
- LEDS
- POTENCIOMETROS
- RESISTENCIAS
- MOTOR
- RS232
- PROTOBOARD
- COMPUTADOR
- VISUAL BASIC 2010
CODIGO FIRMWARE :
#include <18F2550.h>
#device adc=8//La resolución del módulo A/D es de 8 bits.
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN
#use delay (clock=12000000)//12Mhz
#USE RS232 (baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7)//Defincion RS-232
#use standard_io(a)
#use fast_io(b)// Aqui están las señales del encoder
#byte port_a = 0xF80
#byte port_b = 0xF81
// CCS Library dynamic defines
#DEFINE USB_HID_DEVICE TRUE //Tells the CCS PIC USB firmware to include HID handling code.
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 FOR IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE 64 //allocate 64 bytes in the hardware FOR transmission
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 FOR OUT bulk/interrupt transfers
#define USB_EP1_RX_SIZE 64 //allocate 64 bytes in the hardware FOR reception
// CCS USB Libraries
#include <pic18_usb.h> //Microchip 18Fxx5x hardware layer FOR usb.c
#include <usb_desc_hid 8-byte.h> //USB Configuration and Device descriptors FOR this UBS device
#include <usb.c> //handles usb setup tokens and get descriptor reports
void usb_debug_task(VOID)
{
STATIC int8 last_connected;
STATIC int8 last_enumerated;
INT8 new_connected;
INT8 new_enumerated;
new_connected = usb_attached ();
new_enumerated = usb_enumerated ();
last_connected = new_connected;
last_enumerated = new_enumerated;
}
// ----------------------------------Declaración de variables----------------------------------
int dato4;
int dato1;
//Variables USB-----------------------------------
int16 tx;
//int16 tx1=0;
#byte txl=43
#byte txh=44
#byte tx1l=45
#byte tx1h=46
#byte port_a = 0xF80
#byte port_b = 0xF81
int8 in_data[8];
int8 out_data[8];
// ----------------------------------Declaración de los prototipos de las funciones----------------------------------
#INT_TIMER1
void int_TMR1()//retardo programado a un segundo
{
}
#int_RDA
RDA_isr()
{
dato4=GETC();
}
void main(){
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
set_tris_b(0x00);
SETUP_ADC_PORTS(AN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
usb_init();
for (;;) {
usb_task ();
usb_debug_task ();
usb_get_packet (1, in_data, 8);
txh=(in_data[1]);
txl=(in_data[2]);
tx1h=(in_data[3]);
dato1=(in_data[4]);
PUTC(dato1);
output_b(dato1);
out_data[0]=30 ;
//out_data[1]=q;
//out_data[2]=q1;
set_adc_channel(0);
delay_us(20);
dato4 = read_adc();
out_data[3]=dato4;
usb_put_packet (1, out_data, 8, USB_DTS_TOGGLE);
ENABLE_INTERRUPTS(int_timer0);
set_timer0(tx);
}
}
CODIGO SOFTWARE:
Public Class Form1
Dim val1, y1, y2 As Int16
Dim sensor, setpoint As Byte
Dim cuenta, dato4 As Byte
Dim bajoconta, bajo, bajoy1, bajoy2 As Byte
Dim uno, dos, tres, cuatro, cinco, seis, siete, ocho As Byte
Private Sub HIDComm1_ConnectSuccess(ByVal sender As System.Object, ByVal e As AxVBHIDComm.__HIDComm_ConnectSuccessEvent) Handles HIDComm1.ConnectSuccess
Me.Label4.Text = "Control de port B" & Me.HIDComm1.Manufacturer
End Sub
Private Sub HIDComm1_Disconnected(ByVal sender As System.Object, ByVal e As AxVBHIDComm.__HIDComm_DisconnectedEvent) Handles HIDComm1.Disconnected
Me.Label4.Text = "Esperando conexion"
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.HIDComm1.Connect()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim buffer() As Byte
ReDim buffer(8)
If HIDComm1.Connected = False Then
HIDComm1.Connect()
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim outbuffer() As Byte
ReDim outbuffer(8)
Dim inbuffer() As Byte
ReDim inbuffer(8)
outbuffer(0) = 1
outbuffer(1) = 15
outbuffer(2) = 2
outbuffer(3) = 1
outbuffer(4) = cuenta
outbuffer(5) = y1
'outbuffer(6) = 0
'outbuffer(7) = 0
HIDComm1.WriteTo(outbuffer, 8)
inbuffer = HIDComm1.ReadFrom(8)
dato4 = inbuffer(3)
Label3.Text = dato4
ProgressBar1.Value = dato4
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
HIDComm1.Uninit()
End
End Sub
Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
cuenta = 70
Label1.Text = 3410
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
cuenta = 140
Label1.Text = 5500
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
cuenta = 185
Label1.Text = 7250
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
cuenta = 255
Label1.Text = "APAGADO"
End Sub
Private Sub Label1_Click_1(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
cuenta = 20
Label1.Text = "ENCENDIDO"
End Sub
Private Sub GroupBox1_Enter_1(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
End Class
jueves, 4 de julio de 2013
IMPLEMENTACION DE LA RUTINA DEL CODIGO PARA DESARROLLAR UNA APLICACION EN SOFTWARE ATRAVEZDEL IDE DE VISUAL BASIC (LAB -7)
MATERIALES :
- COMPUTADOR
- VISUAL BASIC 2010
CODIGO :
Public Class Form1
Dim SUMA As Double // VARIABLE 1
Dim RESTA As Double // VARIABLE 2
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
SUMA = TrackBar1.Value + TrackBar2.Value //EL RESULTADO ENTRE LA SUMA DE TRACKBAR 1 Y 2 SERAALAMACENADO EN SUMA
Label1.Text = SUMA // TEXTO SUMA
RESTA = TrackBar2.Value - TrackBar1.Value //EL RESULTADO ENTRE LA RESTA DE TRACKBAR 1 Y 2 SERAALAMACENADO EN RESTA
Label2.Text = RESTA // TEXTO RESTA
ProgressBar1.Value = TrackBar1.Value //BARRA DE PROGRESO1 IGUAL A TRACKBAR1
If ProgressBar1.Value > ProgressBar2.Value Then // COMPARAR BARRA DE PROGRESO 1 Y 2
Button1.BackColor = Color.Green // SI ES MAYOR BARRA DE PROGRESO 1 CONTRA 2 SERA VERDE
Else
Button1.BackColor = Color.Red // SI NO ES MAYOR BARRA 1 PROGRESO 1 CONTRA 2 SERA ROJO
End If
End Sub
Private Sub TrackBar2_Scroll(sender As Object, e As EventArgs) Handles TrackBar2.Scroll
SUMA = TrackBar1.Value + TrackBar2.Value //EL RESULTADO ENTRE LA SUMA DE TRACKBAR 1 Y 2 SERAALAMACENADO EN SUMA
Label1.Text = SUMA // TEXTO SUMA
RESTA = TrackBar2.Value - TrackBar1.Value //EL RESULTADO ENTRE LA RESTA DE TRACKBAR 1 Y 2 SERAALAMACENADO EN RESTA
Label2.Text = RESTA // TEXTO RESTA
ProgressBar2.Value = TrackBar2.Value //BARRA DE PROGRESO1 IGUAL A TRACKBAR1
If ProgressBar1.Value > ProgressBar2.Value Then // COMPARAR BARRA DE PROGRESO 1 Y 2
Button1.BackColor = Color.Green // SI ES MAYOR BARRA DE PROGRESO 1 CONTRA 2 SERA VERDE
Else
Button1.BackColor = Color.Red // SI NO ES MAYOR BARRA 1 PROGRESO 1 CONTRA 2 SERA ROJO
End If
End Sub
End Class
IMPLEMENTACION DE LA RUTINA DEL MODULO PWM, ADC, RS 232, Y LCD; POTENCIOMETRO Y ACTUADOR Y COMUNICACION TTL DE COMUNICACION SERIAL (LAB - 6)
MATERIALES:
- MICROCONTROLADOR 18F2550
- MOTOR
- - LCD 16X2
- -
RESISTENCIAS
- -
PROTOBOARD
- -
POTENCIOMETRO
- CABLE RS 232
CODIGO (1) :
# include <18F2550.h> //DEFINIR PROCESADOR
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //FUSIBLES
#use delay (clock=12000000)//12Mhz
#USE RS232 (baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7) //CONFIG RS232
#byte portb = 0x06 //DIRECCION DE ALMACENAMIENTO
INT VALORT; //VARIABLE 1
int VALORR; //VARIABLE 2
#int_RDA
void RDA_isr()
{
VALORR=GETC(); //ALMACENAR EN VARIABLE 2 EL GETC
}
void main() {
SET_TRIS_B(0X00); //SALIDAS PUERTOS B
SETUP_ADC_PORTS(AN0|VSS_VDD) ; //PUERTO A
setup_adc(ADC_CLOCK_INTERNAL); // RELOJ INTERNO
enable_interrupts(INT_RDA); // ACTIVAR INTERRUPCIONES
enable_interrupts(GLOBAL); // ACTIVAR INTERRUPCIONES
for (;;) {
set_adc_channel(0); //ACTIVAR CANAL CONVERSION
delay_us(20); //RETARDO
VALORT = read_adc(); //ALAMACENAR EN VARIABLE 1 VALOR
PUTC(VALORT);
output_b(VALORR); //MOSTRAR EN PUERTO B
}
}
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //FUSIBLES
#use delay (clock=12000000)//12Mhz
#USE RS232 (baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7) //CONFIG RS232
#byte portb = 0x06 //DIRECCION DE ALMACENAMIENTO
INT VALORT; //VARIABLE 1
int VALORR; //VARIABLE 2
#int_RDA
void RDA_isr()
{
VALORR=GETC(); //ALMACENAR EN VARIABLE 2 EL GETC
}
void main() {
SET_TRIS_B(0X00); //SALIDAS PUERTOS B
SETUP_ADC_PORTS(AN0|VSS_VDD) ; //PUERTO A
setup_adc(ADC_CLOCK_INTERNAL); // RELOJ INTERNO
enable_interrupts(INT_RDA); // ACTIVAR INTERRUPCIONES
enable_interrupts(GLOBAL); // ACTIVAR INTERRUPCIONES
for (;;) {
set_adc_channel(0); //ACTIVAR CANAL CONVERSION
delay_us(20); //RETARDO
VALORT = read_adc(); //ALAMACENAR EN VARIABLE 1 VALOR
PUTC(VALORT);
output_b(VALORR); //MOSTRAR EN PUERTO B
}
}
HARDWARE1 :
CODIGO (2) :
#include <18F2550.h> //DEFINIR PROCESADOR
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //FUSIBLES
#use delay (clock=12000000)//12Mhz
#include <flex_lcd.c> //LIBRERIA LCD
#USE RS232 (baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7) //CONFIG RS232
#byte portb = 0x06 //DIRECCION DE ALMACENAMIENTO
INT VALORTx; //VARIABLE 1
int VALORRx; //VARIABLE 2
#int_RDA
void RDA_isr()
{
VALORRx=GETC(); //ALMACENAR EN VARIABLE 2 EL GETC
}
void main() {
SET_TRIS_B(0X00); //SALIDAS PUERTOS B
SETUP_ADC_PORTS(AN0|VSS_VDD) ; //PUERTO A
setup_adc(ADC_CLOCK_INTERNAL); // RELOJ INTERNO
enable_interrupts(INT_RDA); // ACTIVAR INTERRUPCIONES
enable_interrupts(GLOBAL); // ACTIVAR INTERRUPCIONES
lcd_init(); // INICIALIZAR LCD
for (;;) {
set_adc_channel(0); //ACTIVAR CANAL CONVERSION
delay_us(20); //RETARDO
VALORTx = read_adc(); //ALAMACENAR EN VARIABLE 1 VALOR
PUTC(VALORTx);
lcd_gotoxy(1,2); // IR A POCISION LCD 2,2
printf(lcd_putc, "ADC=%u", VALORRx ); // MOSTRAR EN PANTALLA EL VALORRX
delay_ms(100); //RETARDO
}
}
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //FUSIBLES
#use delay (clock=12000000)//12Mhz
#include <flex_lcd.c> //LIBRERIA LCD
#USE RS232 (baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7) //CONFIG RS232
#byte portb = 0x06 //DIRECCION DE ALMACENAMIENTO
INT VALORTx; //VARIABLE 1
int VALORRx; //VARIABLE 2
#int_RDA
void RDA_isr()
{
VALORRx=GETC(); //ALMACENAR EN VARIABLE 2 EL GETC
}
void main() {
SET_TRIS_B(0X00); //SALIDAS PUERTOS B
SETUP_ADC_PORTS(AN0|VSS_VDD) ; //PUERTO A
setup_adc(ADC_CLOCK_INTERNAL); // RELOJ INTERNO
enable_interrupts(INT_RDA); // ACTIVAR INTERRUPCIONES
enable_interrupts(GLOBAL); // ACTIVAR INTERRUPCIONES
lcd_init(); // INICIALIZAR LCD
for (;;) {
set_adc_channel(0); //ACTIVAR CANAL CONVERSION
delay_us(20); //RETARDO
VALORTx = read_adc(); //ALAMACENAR EN VARIABLE 1 VALOR
PUTC(VALORTx);
lcd_gotoxy(1,2); // IR A POCISION LCD 2,2
printf(lcd_putc, "ADC=%u", VALORRx ); // MOSTRAR EN PANTALLA EL VALORRX
delay_ms(100); //RETARDO
}
}
HARDWARE2 :
IMPLEMENTACION DE LA RUTINA DEL MODULO PWM, ADC Y LCD; POTENCIOMETRO Y ACTUADOR (LAB - 5)
MATERIALES:
- MICROCONTROLADOR 18F2550
-
MOTOR
- LCD 16X2
-
RESISTENCIAS
-
PROTOBOARD
-
POTENCIOMETRO
CODIGO (1) :
#include <18F2550.h> //DEFINIR PROCESADOR
#device adc=8 //NUMERO DE BIT’S A UTILIZAR
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //FUSIBLES
#use delay (clock=12000000) //CISTAL A UTILIZAR
#include <flex_lcd.c> //LIBRERIA LCD
#byte portb=0x06 //DIRECCION DE ALMACENAMIENTO
int VALOR; //VARIABLE 1
INT VALOR2; //VARIABLE 2
void main ()
{
lcd_init();
SETUP_ADC_PORTS(AN0_TO_AN1|VSS_VDD); // PUERTO A
setup_adc(ADC_CLOCK_INTERNAL); // RELOJ INTERNO
while(TRUE)
{
set_adc_channel(0); //ACTIVAR CANAL CONVERSION
delay_us(100000); //RETARDO
VALOR=read_adc(); //ALAMACENAR EN VARIABLE 1 VALOR
lcd_gotoxy(1,2); // IR A POCISION LCD 1,2
set_adc_channel(1); //ACTIVAR CANAL CONVERSION
delay_us(100000); //RETARDO
VALOR2=read_adc(); //ALAMACENAR EN VARIABLE 2 VALOR
lcd_gotoxy(2,2); // IR A POCISION LCD 2,2
printf(lcd_putc,"\f ADC0=%u\n ADC1=%u", VALOR, VALOR2); //MOSTRAR EN PANTALLA
delay_us(100); // RETARDO
}
}
FUNCIONAMIENTO:
TENEMOS DOS ENTRADAS A NUESTRO CONVERSOR ANALOGO DIGITAL,EL CUAL ATRAVEZ DE LA CONVERSION NOS MUESTRO UN VALOR MKIN IMO DE 0 Y MAXIMO DE 255 Y CADA UNO DE ELLOS SE VISUALIZA EN LA PANTALLA LCD.
HARDWARE:
CODIGO (2) :
include <18F2550.h> //DEFINIR PROCESADOR
#device adc=8 //NUMERO DE BIT’S A UTILIZAR
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //FUSIBLES
#use delay (clock=12000000) //CISTAL A UTILIZAR
#include <math.h> //LIBRERIA PARA OPERACIONES MATEMATICAS
#include <flex_lcd2.c> //LIBRERIA LCD
#byte portb=0x06 //DIRECCION DE ALMACENAMIENTO
void main() {
setup_timer_2(t2_div_by_16,1023,16); // CONFIGURACION TMR2
setup_ccp1(ccp_pwm); // CCP1 EN MODO PWM
lcd_init();
SETUP_ADC_PORTS(AN0|VSS_VDD); //PUERTO A
setup_adc(ADC_CLOCK_INTERNAL); // RELOJ INTERNO
while(true) {
float VALOR; // VARIABLE 1
float VALOR2; // VARIABLE 2
set_adc_channel(0); //ACTIVAR CANAL CONVERSION
delay_us(20); //RETARDO
VALOR= read_adc(); //ALAMACENAR EN VARIABLE 1 VALOR
lcd_gotoxy(2,2); // IR A POCISION LCD 2,2
VALOR2=((VALOR*0.392)); //VARIABLE 2 SE CARGA CON EL VALOR DEL DUTY SEGUN OPERACION
set_pwm1_duty(VALOR); //ACTIVAR PWM
lcd_gotoxy(1,2); // IR A POCISION LCD 2,2
printf(lcd_putc,"\f ADC=%f \n ",VALOR); // MOSTRAR EN PANTALLA EL VALOR ADC
printf(lcd_putc,"\DUTY=%f\n",VALOR2); // MOSTRAR EN PANTALLA EL VALOR DUTY
delay_ms(100); // RETARDO
}
}
FUNCIONAMIENTO :
ESTA VEZ TENEMOS DOS SALIDAS UNA ES EL RESULTADOANOLOGO DIGITAL Y LA OTRA ES NUESTRO DUTY. PODEMOS VARIAR ESTOS VALORES CON LAS ENTRADAS Y VERLOS EHN LA PANTALLA
HARDAWARE :
martes, 25 de junio de 2013
IMPLEMENTACION DE LA RUTINA DEL MODULO PWM CON ADC, POTENCIOMETRO Y ACTUADOR (LAB 4)
IMPLEMENTACION DE LA RUTINA DEL MODULO PWM
CON ADC, POTENCIOMETRO Y ACTUADOR (LAB 4)
MATERIALES:
·
MICROCONTROLADOR 18F2550
·
MOTOR
·
TIP31C
·
RESISTENCIAS
·
PROTOBOARD
·
POTENCIOMETRO
·
PUENTE H (L293)
·
COMPUERTA NEGADORA(LM7404)
PROGRAMA:
#include
<18F2550.h> //
DEFINIR PROCESADOR
#device
adc=8 //
NUMERO DE BIT’S A UTILIZAR
#fuses HS, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV,
PLL3, CPUDIV1, VREGEN
#use delay(clock=12000000) //
CRISTAL 12MHz
int VALOR; //
VARIABLE ENTERA
void main() {
SETUP_ADC_PORTS(AN0|VSS_VDD); //
PUERTO A ANALOGICO
setup_adc(ADC_CLOCK_INTERNAL); //
RELOJ INTERNO ADC
setup_timer_2(t2_div_by_16,1023,16); //
CONFIGURACION TMR2
setup_ccp1(ccp_pwm); //
CCP1 EN MODO PWM
while(true) {
set_adc_channel(0); //ACTIVA
CANAL DE CONVERSION
delay_us(20); //
RETARDO MUESTREO
VALOR= read_adc(); // RESULTADO DE LA CONVERSION EN
// VALOR
set_pwm1_duty(VALOR); //
A DUTY DE PWM
}
}
DIAGRAMA
DE FLUJO:
HARDWARE 1
HARDWARE 2
Suscribirse a:
Entradas (Atom)