Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

PRACTICA Nº11

LABORATORIO DE MICROCONTROLADORES
2020-2

ESTUDIANTE:
JOSÉ AGUILAR MALAVÉ
CURSO:
ELECTRONICA 8/1
DOCENTE:
ING. SENDEY VERA GONZALEZ

UPSE FACSISTEL
OBJETIVOS

- Conocer el comportamiento al guardar datos en una memoria EEPROM por


comunicación I2C.
- Mostrar los datos y las direcciones mediante el protocolo de comunicación UART en un
terminal virtual del software de simulación PROTEUS.

DESARROLLO

Parte A Código

// Software I2C connections


sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
char seconds, minutes, hours, day, month, year; // Global date/time variables
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8583, see PCF8583 datasheet
Soft_I2C_Write(2); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xA1); // Address PCF8583 for reading R/W=1

seconds = Soft_I2C_Read(1); // Read seconds byte


minutes = Soft_I2C_Read(1); // Read minutes byte
hours = Soft_I2C_Read(1); // Read hours byte
day = Soft_I2C_Read(1); // Read year/day byte
month = Soft_I2C_Read(0); // Read weekday/month byte
Soft_I2C_Stop(); // Issue stop signal

}
//-------------------- Formats date and time
void Transform_Time() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); //Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); //Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); //Transform hours
year = (day & 0xC0) >> 6; //Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F); //Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); //Transform month
}
//-------------------- Output values to LCD
void Display_Time() {
Lcd_Chr(1, 6, (day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (month / 10) + 48);
Lcd_Chr(1,10, (month % 10) + 48);
Lcd_Chr(1,15, year + 48); // Print year variable (start from year 2010)
Lcd_Chr(2, 6, (hours / 10) + 48);
Lcd_Chr(2, 7, (hours % 10) + 48);
Lcd_Chr(2, 9, (minutes / 10) + 48);
Lcd_Chr(2,10, (minutes % 10) + 48);
Lcd_Chr(2,12, (seconds / 10) + 48);
Lcd_Chr(2,13, (seconds % 10) + 48);
}
//------------------ Performs project-wide init
void Init_Main() {
TRISB = 0;
PORTB = 0xFF;
TRISB = 0xff;
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;

Soft_I2C_Init(); // Initialize Soft I2C communication


Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Date:"); // Prepare and output static text on LCD
Lcd_Chr(1,8,'.');
Lcd_Chr(1,11,'.');
Lcd_Chr(1,16,'.');
Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');
Lcd_Out(1,12,"201"); // start from year 2010
}
//----------------- Main procedure
void main() {
Delay_ms(500);

Init_Main(); // Perform initialization


while (1) { // Endless loop
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
}
}

Codificación en Mikro C
Planteamiento del sistema electrónico

Funcionamiento
Diagrama de flujo
Parte B Código

dim num_temp as byte


dim i as word
dim text as string[6]
dim mem as byte

' Proceso que escribimos en la eeprom y necesita direccion alta y baja


' mas el dato a escribir
sub procedure escribe_eeprom(dim direccion_h, direccion_l, dato as byte)
I2C1_Start ' Empieza la comunicacion
I2C1_Wr($A0) ' Control de formato de byte
I2C1_Wr(direccion_h) ' Direccion en alta
I2C1_Wr(direccion_l) ' Direccion en baja
I2C1_Wr(dato) ' Dato a escribir en la eeprom
I2C1_Stop ' Detenemos la comunicacicon
Delay_ms(10)
end sub

' Proceso de leer en la eeprom y necesita direccion alta y baja


sub function leer_eeprom(dim direccion_h, direccion_l as byte) as byte
dim datov as byte
I2C1_Start ' Empieza la comunicacion
I2C1_Wr($A0) ' Control de formato de byte
I2C1_Wr(direccion_h) ' Direccion en alta
I2C1_Wr(direccion_l) ' Direccion en baja
I2C1_Repeated_Start ' Re-iniciamos la comunicaion
I2C1_Wr($A1) ' Control de formato de byte + bit = 1 de lectura
datov = I2C1_Rd(0) ' Obtenemos en dato de la direccion de memoria
I2C1_Stop ' Detenemos la comunicacion
Delay_ms(10)
result = datov
end sub

main:

' Registro Oscilador de control


OSCCON = 0X75 ' Oscilador interno de 8mhz

' Registro Puerta B


TRISB = 0X01 ' Portb.0 como entrada
PORTB = 0X00

' Registro Puerta E


TRISE = 0X04 ' Porte.2 como entrada
PORTE = 0X00

' Registro Puerta D


TRISD = 0X00 ' Portd como salida
PORTD = 0X00

' Seleccion de registro analogico. 1 analogico, 0 digitales


ANSEL = 0X00 ' AN<7:0>
ANSELH = 0X00 ' AN<13:8>

delay_ms(250)

I2C1_Init(100000) ' Inicializamos la comunicacion i2c a 100khz


UART1_Init(9600) ' Inicializamos la comunicacion uart a 9600bps

' Enviamos un primer texto


UART1_Write_Text("ideastechnology.com")
UART1_Write(0x0D) ' Ascci del enter

num_temp = 0 ' valor que vamos a guardar en la memoria

while true
i=0
while Button(PORTE, 2, 1, 1) and Button(PORTB, 0, 1, 1)
UART1_Write_Text("memoria ") ' Enviamos el texto memoria
WordToStr( i, text)
UART1_Write_Text(text) ' Enviamos la localidad de memoria
UART1_Write_Text(" valor ") ' Enviamos el texto valor
WordToStr( leer_eeprom(hi(i), lo(i)), text)
UART1_Write_Text(text) ' Enviamos el valor de la localidad de memoria
UART1_Write(0x0D) ' Ascci del enter
delay_ms(75)
inc(i)
' if i=0x8000 then ' Recorremos hasta que el valor de la direccion de 32767
if i=0x0100 then ' Recorremos hasta que el valor de la direccion de 255
i=0 ' de memoria no supere los 0x8000
end if
wend
'' ESCRIBIR EN LA EEPROM
if Button(PORTE, 2, 1, 0) then
' for i=0 to 0x7FFF ' Recorremos todas las direcciones de memoria 32767
num_temp = 0
for i=0 to 0x00FF ' Recorremos todas las direcciones de memoria 255
escribe_eeprom(hi(i), lo(i), num_temp)
inc(num_temp) ' Incrementamos el valor a guardar en la memoria
PORTD = 0XFF ' Encendemos los leds indicando el trabajo
next i
PORTD = 0X00 ' Apagamos los leds indicando el terminado
end if
'' BORRAR LA EEPROM
if Button(PORTB, 0, 1, 0) then
' for i=0 to 0x7FFF ' Recorremos todas las direcciones de memoria 32767
for i=0 to 0x00FF ' Recorremos todas las direcciones de memoria 255
' por defecto el valor de la eeprom es 0xFF
escribe_eeprom(hi(i), lo(i), 0xFF)
PORTD = 0XFF ' Encendemos los leds indicando el trabajo
next i
PORTD = 0X00 ' Apagamos los leds indicando el terminado
end if
wend
end.

Codificación en Mikro BASIC


Planteamiento del esquema electrónico
Funcionamiento
Diagrama de flujo

CONCLUSIONES

Como aplicaciones dentro del campo de la electrónica denotan la complejidad de los


protocolos usados y de los componentes que hacen que el sistema sea mas robusto como el
caso de mostrar la hora y la fecha o del encendido y apagado de componentes usados en el
esquema.
También dentro de la codificación se observo el uso de varias librerías que activan
funciones especificas al usar elementos para el sistema,

You might also like