-
Notifications
You must be signed in to change notification settings - Fork 0
/
Usart.c
86 lines (77 loc) · 2.86 KB
/
Usart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <xc.h>
#include "Config.h"
#include <stdint.h>
#include "Usart.h"
#define _XTAL_FREQ 20000000UL
void Confi_Blue(int SPBRG_DATO){
Tris_Rx=1; //Configurar el pin Rx como entrada
Tris_Tx=0; //Configurar el pin Tx como salida
SPBRG=SPBRG_DATO; //Baudios
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
RCSTAbits.SPEN = 1; //Habilitar Tx y Rx
TXSTAbits.SYNC = 0; //Asincrono
TXSTAbits.BRGH = 0; //Low Speed
/*64 (8bits) ---> 000
*8 (8bits) ---> 001
*8 (16bits)---> 010
*4 (16bits)---> 011*/
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//Transmisión
TXSTAbits.TX9 = 0; //8 bits
TXSTAbits.TXEN = 1; //Activamos transmisión
//Recepción
RCSTAbits.RC9 = 0; //8 bits
RCSTAbits.CREN = 1; //Activamos recepción
}
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
void Tx_str(char data){
while(TXSTAbits.TRMT == 0);
TXREG = data;
} //Esta función envia datos cadena
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
char Rx(void){
return RCREG;
} //Esta función recibe datos
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
void Tx_num(uint8_t dita){
TXREG = dita;
} //Esta función envia datos cadena
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
void UART0_Tx_String(char *str){
/* Mientras no se alcance el final de la cadena */
while(*str != '\0'){
/* Transmitir el caracter correspondiente */
Tx_str(*str);
/* Incrementar el valor del puntero (apuntar al siguiente caracter
en el arreglo) */
str++;
}
}
void UART0_Tx_Integer(int32_t num){
char signo_y_digitos[12];
uint8_t signo = 0;
int32_t digito;
int8_t indice = 11;
/* Determinar el signo del número */
if(num < 0){
signo = 1;
num = -num;
}
/* Indicar el fin de la cadena de caracteres */
signo_y_digitos[indice--] = '\0';
/* Extraer los dígitos uno a uno, empezando por las unidades */
do{
digito = (num % 10) + '0';
signo_y_digitos[indice--] = (char)digito;
num /= 10;
}while(num);
/* Agregar el signo (de ser necesario) */
if(signo){
signo_y_digitos[indice] = '-';
}else{
indice++;
}
__delay_ms(100);
/* Transmitir el número */
UART0_Tx_String(&signo_y_digitos[indice]);
}