Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OBJETIVOS:
Familiarizarse con el manejo del PWM.
Familiarizarse con el uso de las comunicaciones seriales USART e I2C
Ejemplo 1
/*
* Nombre del Proyecto:
P9a_pwm.c
* Nombre del Autor:
(c) Mikroelektronika, 2009.
* Description:
(Explicación del ejercicio)
* Test configuration:
MCU: PIC16F887
Oscillator: HS, 08.0000 MHz
SW: mikroC PRO for PIC
* NOTES:
*/
unsigned short current_duty, old_duty, current_duty1, old_duty1;
void InitMain() {
ANSEL = 0; // Configure AN pins as
digital
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
PORTA = 255;
TRISA = 255; // configure PORTA pins as
input
PORTB = 0; // set PORTB to 0
TRISB = 0; // designate PORTB pins as
output
PORTC = 0; // set PORTC to 0
TRISC = 0; // designate PORTC pins as
output
PWM1_Init(5000); // Initialize PWM1 module at
5KHz
PWM2_Init(5000); // Initialize PWM2 module at
5KHz
}
void main() {
InitMain();
current_duty = 16; // initial value for
current_duty
current_duty1 = 16; // initial value for
current_duty1
Observe el aislamiento eléctrico del motor con respecto al control, para poder
realizar la medición con el osciloscopio deberá colocar el canal A en “A+B”
y el canal B en “-B”, tal como se indica a continuación:
Ejemplo 2
/*
* Nombre del Proyecto:
P10a_usart.c
* Nombre del Autor:
(c) Mikroelektronika, 2009.
* Description:
(Explicación del ejercicio)
* Test configuration:
MCU: PIC16F887
Oscillator: HS, 08.0000 MHz
SW: mikroC PRO for PIC
* NOTES:
*//*Header******************************************************/
unsigned short i;
void main() {
UART1_Init(19200); // Initialize USART module
// (8 bit, 19200 baud rate, no
parity bit...)
TRISB=0;
PORTB=0;
while (1) {
if (UART1_Data_Ready()) { // If data has been received
i = UART1_Read(); // read it
UART1_Write(i); // and send it back
PORTB=i;
}
}
}
Ejercicio 3
PIC 2
PIC 1
PIC 1
void interrupt(){
if (RB3_bit==1) { //arriba
UART1_Write(1);}
else if (RB4_bit==1) { //abajo
UART1_Write(2);}
else if (RB5_bit==1) { //izq
UART1_Write(3);}
else if (RB6_bit==1){ //der
UART1_Write(4);}
RBIF_bit = 0;
}
void main() {
ANSEL=0;
ANSELH=0;
TRISB=0xFF;
PORTB=0;
IOCB=0XFF;
INTCON=0x88;
UART1_Init(19200); // Initialize USART module
while (1) {
}
}
PIC2
char lectura;
void main() {
ANSEL=0;
ANSELH=0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISB=0x00;
PORTB=0;
UART1_Init(19200); // Initialize USART module
Delay_ms(100);
while (1) {
if (UART1_Data_Ready()) { // If data has been received
lectura = UART1_Read(); // read it
if (lectura==1){
PORTB=0x0A;}
else if (lectura==2){
PORTB=0X05;}
else if (lectura==3){
PORTB=0X04;}
else if (lectura==4){
PORTB=0X02;}
}
}
}
Preguntas
Realice una comunicación SERIAL VIA USART con otro microcontrolador en el
mismo circuito. En este ejercicio cuando apriete un pulsador hará que el otro
microcontrolador parpadee los bits del puerto B utilizando PWM. Para conectar
los dos microcontroladores en este ejercicio conecte en forma cruzada Rx1 – Tx2
y Tx1 – Rx2.