Buenas, les consulto porque estamos con mi amigo trabados, queremos manejar un brazo el problema que tenemos es que cuando pulsamos y se envía el dato para el otro lado el servo se va al extremo la idea es que cada vez que se manda un caracter se mueva de apoco. Realizamos una prueba con pulsadores sin la comunicación y los servos responden bien, por eso creemos que es un tema la comunicación, ahora en la simulación esta con dos pic pero la idea es después comunicarlo por una aplicación del celular.
La programación esta hecha en mplab xc8 y los pic 18f4620.
Desde ya muchas gracias.
TX
RX
La programación esta hecha en mplab xc8 y los pic 18f4620.
Desde ya muchas gracias.
TX
C:
// CONFIG1H
#pragma config OSC = HS // Oscillator Selection bits (XT oscillator)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)
// 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 = PORTC // 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 digital I/O on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 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
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP 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-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) 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.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#define _XTAL_FREQ 20000000
#include "Usart.h"
#define pulsador1 PORTDbits.RD0
#define pulsador1_tris TRISDbits.TRISD0
#define pulsador2 PORTDbits.RD1
#define pulsador2_tris TRISDbits.TRISD1
#define pulsador3 PORTDbits.RD2
#define pulsador3_tris TRISDbits.TRISD2
#define pulsador4 PORTDbits.RD3
#define pulsador4_tris TRISDbits.TRISD3
#define pulsador5 PORTDbits.RD4
#define pulsador5_tris TRISDbits.TRISD4
#define pulsador6 PORTDbits.RD5
#define pulsador6_tris TRISDbits.TRISD5
#define pulsador7 PORTDbits.RD6
#define pulsador7_tris TRISDbits.TRISD6
#define pulsador8 PORTDbits.RD7
#define pulsador8_tris TRISDbits.TRISD7
void main(void)
{
int i = 0;
int estado;
pulsador1_tris = 1;
pulsador2_tris = 1;
pulsador3_tris = 1;
pulsador4_tris = 1;
pulsador5_tris = 1;
pulsador6_tris = 1;
pulsador7_tris = 1;
pulsador8_tris = 1;
/*Inicializacion de comunicacion USART*/
Usart_int();
do
{
if(pulsador1 == 1)
{
__delay_ms(100);
Usart_sent_character('A');
}
if(pulsador2 == 1)
{
__delay_ms(100);
Usart_sent_character('B');
}
if(pulsador3 == 1)
{
__delay_ms(100);
Usart_sent_character('E');
}
if(pulsador4 == 1)
{
__delay_ms(100);
Usart_sent_character('F');
}
if(pulsador5 == 1)
{
__delay_ms(100);
Usart_sent_character('C');
}
if(pulsador6 == 1)
{
__delay_ms(100);
Usart_sent_character('D');
}
if(pulsador7 == 1)
{
__delay_ms(100);
Usart_sent_character('G');
}
if(pulsador8 == 1)
{
__delay_ms(100);
Usart_sent_character('H');
}
}while(1);
}
#ifndef USART_H
#define USART_H
void Usart_int();//función para iniciar el USART PIC asíncron, 8 bits, 9600 baudios
unsigned char Usart_read();//función para la recepción de caracteres
void Usart_sent_character(unsigned char);//función para la transmisión de caracteres
void Usart_sent_string(char*);//función para la transmisión de cadenas de caracteres
/*
Inicialización del módulo USART PIC modo asíncrono
en una función, a 8bits,a 9600 baudios */
void Usart_int()
{
TRISCbits.TRISC7=1;//pin RX como una entrada digital
TRISCbits.TRISC6=0;//pin TX como una salida digital
TXSTAbits.CSRC = 0;//modo asincronico
TXSTAbits.TX9 = 0;//selecion modo de transimicion 8bit.
TXSTAbits.TXEN = 1;//se habilita la transimcion
TXSTAbits.SYNC = 0;//la transmicion sera asincronica
TXSTAbits.SENDB = 0;//no habilito el control por hardware
TXSTAbits.BRGH = 1;//modo de transmicion de alta velocidad
TXSTAbits.TRMT = 1;//se lo pondrá a 1 porque se está iniciando y tendría que estar vacío.
TXSTAbits.TX9D = 0;//el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
RCSTAbits.SPEN = 1;//se pondrá a 1 para habilitar el uso del módulo USART PIC.
RCSTAbits.RX9 = 0;//la recepción para que sea a 8 bits.
RCSTAbits.SREN = 0;//modo asincronico
RCSTAbits.CREN = 1;//se habilita la recepcion
RCSTAbits.ADDEN = 0;//recepción será a 8 bit.
RCSTAbits.FERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha recibido un dato no válido.
RCSTAbits.OERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha producido un error por sobreescritura de algún dato recibido
RCSTAbits.RX9D = 0;////el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
SPBRG = 129;//para una velocidad de 9600baudios con un oscilador de 20Mhz*/
}
/*
Recepción de datos del módulo USART PIC modo asíncrono
*/
unsigned char Usart_read()
{
if(PIR1bits.RCIF == 1)//si el bit5 del registro PIR1 se ha puesto a 1
{
return RCREG;//devuelve el dato almacenado en el registro RCREG
}
else
{
return 0;
}
}
/*
Transmisión de datos del módulo USART PIC modo asíncrono
*/
void Usart_sent_character(unsigned char chtr)
{
while(TXSTAbits.TRMT == 0);// mientras el registro TSR esté lleno espera
TXREG = chtr;//cuando el el registro TSR está vacio se envia el caracter
}
/*
Transmisión de cadenas de caracteres con el módulo USART PIC modo asíncrono
*/
void Usart_sent_string(char* cadena)//cadena de caracteres de tipo char
{
while(*cadena != 0x00)//mientras el último valor de la cadena sea diferente de el caracter nulo
{
Usart_sent_character(*cadena);//transmite los caracteres de cadena
cadena++;//incrementa la ubicación de los caracteres en cadena para enviar el siguiente caracter de cadena
}
}
#endif /* USART_H */
RX
C:
// CONFIG1H
#pragma config OSC = HS // Oscillator Selection bits (XT oscillator)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)
// 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 = PORTC // 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 digital I/O on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 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
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP 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-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) 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.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#define _XTAL_FREQ 20000000
#include "Usart.h"
/*Definicion de pines a utilizar*/
#define servo1 LATBbits.LB0
#define servo1_tris TRISBbits.TRISB0
#define servo2 LATBbits.LB1
#define servo2_tris TRISBbits.TRISB1
#define servo3 LATBbits.LB2
#define servo3_tris TRISBbits.TRISB2
#define servo4 LATBbits.LB3
#define servo4_tris TRISBbits.TRISB3
#define pulsador1 PORTDbits.RD0
#define pulsador1_tris TRISDbits.TRISD0
#define pulsador2 PORTDbits.RD1
#define pulsador2_tris TRISDbits.TRISD1
#define pulsador3 PORTDbits.RD2
#define pulsador3_tris TRISDbits.TRISD2
#define pulsador4 PORTDbits.RD3
#define pulsador4_tris TRISDbits.TRISD3
#define pulsador5 PORTDbits.RD4
#define pulsador5_tris TRISDbits.TRISD4
#define pulsador6 PORTDbits.RD5
#define pulsador6_tris TRISDbits.TRISD5
#define pulsador7 PORTDbits.RD6
#define pulsador7_tris TRISDbits.TRISD6
#define pulsador8 PORTDbits.RD7
#define pulsador8_tris TRISDbits.TRISD7
/*Declaracion de variables a utilizar para el manejo de 4 servomotores de 0° a 180°*/
unsigned char dato;
unsigned char dato2;
int servo;
unsigned int pulsos_servo1 = 624;
unsigned int pulsos_servo2 = 624;
unsigned int pulsos_servo3 = 624;
unsigned int pulsos_servo4 = 624;
int i = 0;
void __interrupt(high_priority) usart(void)
{
if(PIR1bits.RCIF == 1)//si la bandera se pone a 1
{
PIR1bits.RCIF = 0;
dato = RCREG;//a dato se le asigna el caracter recibido
}
}
/*Funcion de interrpcion para el timer 1 */
void __interrupt(low_priority) servos(void)
{
if (PIR1bits.TMR1IF)
{
switch (servo)
{
case 0: servo1 = 1 - servo1;
if (servo1)
{
TMR1 = 65535 - pulsos_servo1;
}
else
{
TMR1 = 65535 - ((24999/4) - pulsos_servo1);
servo++;
}
break;
case 1: servo2 = 1 - servo2;
if(servo2)
{
TMR1 = 65535 - pulsos_servo2;
}
else
{
TMR1 = 65535 - ((24999/4) - pulsos_servo2);
servo++;
}
break;
case 2: servo3 = 1 - servo3;
if(servo3)
{
TMR1 = 65535 - pulsos_servo3;
}
else
{
TMR1 = 65535 - ((24999/4) - pulsos_servo3);
servo++;
}
break;
case 3: servo4 = 1 - servo4;
if(servo4)
{
TMR1 = 65535 - pulsos_servo4;
}
else
{
TMR1 = 65535 - ((24999/4) - pulsos_servo4);
servo = 0;
}
break;
}
}
PIR1bits.TMR1IF=0;
}
void main(void)
{
int i = 0;
servo1_tris = 0;
servo2_tris = 0;
servo3_tris = 0;
servo4_tris = 0;
/*Inicializacion de comunicacion USART*/
Usart_int();
//Configuracion interrupcion
INTCONbits.GIE = 1; // Interrupcion Global
INTCONbits.PEIE = 1; // Interrupcion por periferico
RCONbits.IPEN = 1; //habilito interrupcion de alta y baja prioridad
//Configurar timer
T1CONbits.T1CKPS = 0b10; // Divisor 4
T1CONbits.TMR1CS = 0b00;// Fosc /4
T1CONbits.T1OSCEN = 0; // Oscilador LP deshabilitao
T1CONbits.nT1SYNC = 1; // No sinconia
T1CONbits.TMR1ON = 1; // Encender el timer
PIE1bits.TMR1IE = 1; //Hanilita la interrupcion por timer
PIR1bits.TMR1IF = 0; //Limpia la bandera del timer 1
IPR1bits.TMR1IP = 0; //timer1 baja prioridad
/*Configuracion de las interrupciones para la recepcion de la Usart*/
PIE1bits.RCIE = 1;
IPR1bits.RCIP = 1;
do
{
/* if(servo == 0)
{
if(pulsador1 == 1)
{
__delay_ms(20);
if(pulsos_servo1 < 2499)
{
pulsos_servo1 = pulsos_servo1 + 25;
}
}
if(pulsador2 == 1)
{
__delay_ms(20);
if(pulsos_servo1 > 624)
{
pulsos_servo1 = pulsos_servo1 - 25;
}
}
}
if(servo == 1)
{
if(pulsador3 == 1)
{
__delay_ms(20);
if(pulsos_servo2 < 2499)
{
pulsos_servo2 = pulsos_servo2 + 25;
}
}
if(pulsador4 == 1)
{
__delay_ms(20);
if(pulsos_servo2 > 624)
{
pulsos_servo2 = pulsos_servo2 - 25;
}
}
}
if(servo == 2)
{
if(pulsador5 == 1)
{
__delay_ms(20);
if(pulsos_servo3 < 2499)
{
pulsos_servo3 = pulsos_servo3 + 25;
}
}
if(pulsador6 == 1)
{
__delay_ms(20);
if(pulsos_servo3 > 624)
{
pulsos_servo3 = pulsos_servo3 - 25;
}
}
}
if(servo == 3)
{
if(pulsador7 == 1)
{
__delay_ms(20);
if(pulsos_servo4 < 2499)
{
pulsos_servo4 = pulsos_servo4 + 25;
}
}
if(pulsador8 == 1)
{
__delay_ms(20);
if(pulsos_servo4 > 624)
{
pulsos_servo4 = pulsos_servo4 - 25;
}
}
}
*/
if(servo == 0)
{
if(dato == 'A')
{
if(pulsos_servo1 < 2499)
{
pulsos_servo1 = pulsos_servo1 + 25;
}
}
if(dato == 'B')
{
if(pulsos_servo1 > 624)
{
pulsos_servo1 = pulsos_servo1 - 25;
}
}
}
if(servo == 1)
{
if(dato == 'E')
{
if(pulsos_servo2 < 2499)
{
pulsos_servo2 = pulsos_servo2 + 25;
}
}
if(dato == 'F')
{
if(pulsos_servo2 > 624)
{
pulsos_servo2 = pulsos_servo2 - 25;
}
}
}
if(servo == 2)
{
if(dato == 'C')
{
if(pulsos_servo3 < 2499)
{
pulsos_servo3 = pulsos_servo3 + 25;
}
}
if(dato == 'D')
{
if(pulsos_servo3 > 624)
{
pulsos_servo3 = pulsos_servo3 - 25;
}
}
}
if(servo == 3)
{
if(dato == 'G')
{
if(pulsos_servo4 < 2499)
{
pulsos_servo4 = pulsos_servo4 + 25;
}
}
if(dato == 'H')
{
if(pulsos_servo4 > 624)
{
pulsos_servo4 = pulsos_servo4 - 25;
}
}
}
}while(1);
}
#ifndef USART_H
#define USART_H
void Usart_int();//función para iniciar el USART PIC asíncron, 8 bits, 9600 baudios
unsigned char Usart_read();//función para la recepción de caracteres
void Usart_sent_character(unsigned char);//función para la transmisión de caracteres
void Usart_sent_string(char*);//función para la transmisión de cadenas de caracteres
/*
Inicialización del módulo USART PIC modo asíncrono
en una función, a 8bits,a 9600 baudios */
void Usart_int()
{
TRISCbits.TRISC7 = 1;//pin RX como una entrada digital
TRISCbits.TRISC6 = 0;//pin TX como una salida digital
TXSTAbits.CSRC = 0;//modo asincronico
TXSTAbits.TX9 = 0;//selecion modo de transimicion 8bit.
TXSTAbits.TXEN = 1;//se habilita la transimcion
TXSTAbits.SYNC = 0;//la transmicion sera asincronica
TXSTAbits.SENDB = 0;//no habilito el control por hardware
TXSTAbits.BRGH = 1;//modo de transmicion de alta velocidad en 1
TXSTAbits.TRMT = 1;//se lo pondrá a 1 porque se está iniciando y tendría que estar vacío.
TXSTAbits.TX9D = 0;//el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
RCSTAbits.SPEN = 1;//se pondrá a 1 para habilitar el uso del módulo USART PIC.
RCSTAbits.RX9 = 0;//la recepción para que sea a 8 bits.
RCSTAbits.SREN = 0;//modo asincronico
RCSTAbits.CREN = 1;//se habilita la recepcion
RCSTAbits.ADDEN = 0;//recepción será a 8 bit.
RCSTAbits.FERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha recibido un dato no válido.
RCSTAbits.OERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha producido un error por sobreescritura de algún dato recibido
RCSTAbits.RX9D = 0;////el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
SPBRG = 129;//para una velocidad de 9600baudios con un oscilador de 20Mhz*/
}
/*
Recepción de datos del módulo USART PIC modo asíncrono
unsigned char Usart_read()
{
if(PIR1bits.RCIF == 1)//si el bit5 del registro PIR1 se ha puesto a 1
{
return RCREG;//devuelve el dato almacenado en el registro RCREG
}
else
{
return 0;
}
}*/
/*
Transmisión de datos del módulo USART PIC modo asíncrono
*/
void Usart_sent_character(unsigned char chtr)
{
while(TXSTAbits.TRMT == 0);// mientras el registro TSR esté lleno espera
TXREG = chtr;//cuando el el registro TSR está vacio se envia el caracter
}
/*
Transmisión de cadenas de caracteres con el módulo USART PIC modo asíncrono
*/
void Usart_sent_string(char* cadena)//cadena de caracteres de tipo char
{
while(*cadena !=0x00)//mientras el último valor de la cadena sea diferente de el caracter nulo
{
Usart_sent_character(*cadena);//transmite los caracteres de cadena
cadena++;//incrementa la ubicación de los caracteres en cadena para enviar el siguiente caracter de cadena
}
}
#endif /* USART_H */