Esdi Printout

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

20EC512– EMBEDDED SYSTEM DESIGN AND IOT

5 INTERFACING REAL TIME CLOCK AND SERIAL PORT

AIM:
To develop and verify the interfacing of real time clock and serial port with ARMDEVELOPMENT
KIT microcontroller using embedded C program.

APPARATUS REQUIRED:

HARDWARE:

LPC2148 Kit - 1 No

PC

SOFTWARE:

Keil compiler & Flash magic

PROCEDURE:

 Connect RTC & RS323 (Serial) to the LPC2148 Development Board as shown incircuit.
 Connect COM-0/ISP to PC using the Serial Port Cable.
 Connect Adaptor with LPC2148 Kit & Switch ON.
 To Edit / Compile / generate Hex file: Refer General procedure given in Page MDK-ARM Keil
µVision
 To down load and run this program: Refer General procedure given in Page FlashMagic

PROGRAM:

RTC:

#include <LPC213x.h>
#include <stdio.h> #include
<ctype.h> #include
<stdlib.h> #include
"UART.h" #define BUZZ 7
//Buzzer Connected to P0.7

void UART1_ISR(void) irq;void


RTC_ISR (void) irq; unsigned
char Flag=0;
char Key;
K.L.N.C.E/B.E/EEE/VI/20EC512– EMBEDDED SYSTEM DESIGN AND IOT
void DelayMs(long ms) // delay 1 ms per count @ CCLK 60 MHz
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
{
long i,j;
for (i = 0; i < ms; i++ ) for (j =
0; j < 6659; j++ );
}
void Initialize(void)
{
PLLCFG = 0x24; // M : 4 | P = 2 -> Fosc = 12MHz &CCLK =
60MHz
PLLFEED = 0xAA; // Feed ProcessPLLFEED =
0x55;
PLLCON = 0x01;
PLLFEED = 0xAA; // Feed ProcessPLLFEED =
0x55;
while (!(PLLSTAT & 0x400)); //Wait until PLL is Locked!
PLLCON = 0x03; //Connect PLL as the Clock Source for MicrocontrollerPLLFEED = 0xAA; //
Feed Process

PLLFEED = 0x55;
MAMCR = 0x02; //Memory Acceleration Module Fully Enabled MAMTIM =
0x04; //MAM fetch cycles are 4 CCLKs in duration VPBDIV = 0x02; //Divide
Clock for PCLK = 30MHz
}
void RTC_Setup(char *Buff)
{
unsigned char TimE;char
i=0; for(i=0;i<2;i++)
{
while(!isdigit(Key)); //Waittill
Key = 0 to 9
if (i==0)
{
TimE = 10 * atoi( &Key );
}
if (i==1)
{
TimE += atoi( &Key );
}
putchar(Key);
Key = 0;
}
*Buff = TimE; /Load Setup New Value
}
void Delay()
{
unsigned int i,j;
for(i=0;i<50;i++)
Kf.oLr.N
(j.=C0.E;j/<B7.E
00/E
;jE
+E
+/);VI/20EC512– EMBEDDED SYSTEM DESIGN AND IOT
}
void Wait()
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
{
Delay();Delay();Delay();
Delay();Delay();Delay();
Delay();Delay(); Delay();
}
void Alarm(void)
{
IOSET0 = 1 << BUZZ;
Wait();Wait();
IOCLR0 = 1 << BUZZ;
Wait();
}
//void Clean(void)
//{
// unsigned char i;

// //for(i=0;i<250;i++)
// printf("[2M");
//}
void main(void)
{
Initialize(); UART1_Init(9600/*Baud
Rate*/);
U1IER = 3;//Enable UART1 Recieve Interrupt
//PINSEL0 |= (1 << 18); //Select Pin as UART1 IO0DIR |=
(1<<7); //Configure P0.7 as O/p (Buzzer)VICVectAddr0 =
(unsigned)UART1_ISR; VICVectCntl0 = 0x20 | 7;
VICIntEnable |= (1 << 7); VICVectAddr2 =
(unsigned)RTC_ISR;VICVectCntl2 = 0x20 |
13; VICIntEnable |= (1 << 13);
AMR = 0xFF; //Mask all valued except hh:mm:ss foralarm
comparision
PREINT = 0x00000392; // Set RTC Prescaler for PCLK 30 MHzPREFRAC =
0x00004380;// printf("[2J\0");// Clear Screen
CCR = 0x01;
//CIIR = 0x01;
UART1_PutS("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
UART1_PutS(" ARM LPC2138 RTC Demo\n\r ----------------------------------- \n\n\n");
UART1_PutS("> Press * to Set Time\n"); UART1_PutS("> Press ! to
Set Alarm\n"); UART1_PutS("> Press $ to Snooze Alarm 5
Minutes\n");UART1_PutS("> Press . to Stop Alarm\n");
UART1_PutS("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");while(1)
{
printf("CTC : %d\t",CTC);
//
Display time format hh:mm:ss
DelayMs(100); // Delay for display
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
if (Key == '*')
{
Key = 0;
UART1_PutS(">> Set Time: ");
RTC_Setup(&HOUR);
UART1_PutC(':');
RTC_Setup (&MIN);
UART1_PutC (':');
RTC_Setup (&SEC);
//printf("\r\tTIME: %02d:%02d:%02d \r",HOUR,MIN,SEC);
// Display time format hh:mm:ss
//printf("^[2J");
U1THR = 0x1B; //Escape UART1_PutS("[2J\0"); // Clear
Screen
if (Key == '!')
{
AMR = 0xF8;
Key = 0;
UART1_PutS(">>\tSet Alarm: ");
RTC_Setup(&ALHOUR);
UART1_PutC(':');
RTC_Setup(&ALMIN);
UART1_PutC(':');
RTC_Setup(&ALSEC)
}
if (Key == '$' && Flag == 1)
{
if (MIN >= 55)
{
ALHOUR = HOUR + 1; ALMIN = 5 -
(60 - MIN);
}
else
{
ALMIN = MIN + 5;
}
Key = 0;
Flag = 0;
}
if (Key == '.')
{
Key = 0;
Flag = 0;
}
if (Flag == 1)
{
Alarm (); Wait (); Alarm ();
}
}
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
void UART1_ISR(void) irq
{
char Msg;
if(((Msg = U1IIR) & 0x01) == 0)//Check Flag Status ofRecieve
Interrupt
{
switch(Msg & 0x0E) //Filter Msg
{
case 0x04: while (!(U1LSR & 0x20));
//Recieve DataKey
= U1RBR;
case 0x02: break; // Interruptdefault :
break;
}
}

VICVectAddr = 0; //Acknowledge Interrupt


}
void RTC_ISR (void) irq
{
if ((ILR & 0x02) == 0x02)
{
Flag = 1;
UART1_PutS("\nALARM\n");
ILR = 0x02; //Clear Current Interrupt
}
VICVectAddr = 0; //Ack Interrupt
}

SERIAL PORT:

#define CR 0x0D #include


<LPC21xx.H>

void init_serial (void);int


putchar (int ch); int
getchar (void); unsigned
char test;
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code Begins Here
int main(void)
{
char *Ptr = "*** UART0 Demo ***\n\n\rType Characters to be echoed!!\n\n\r";
VPBDIV = 0x02; //Divide Pclk by twoinit_serial()
while(1)
{
while (*Ptr)
{
}
putchar(getchar()); //Echo terminal
} 20EC512– EMBEDDED SYSTEM DESIGN AND IOT
void init_serial (void) /* Initialize Serial Interface */
{
PINSEL0 = 0x00000005; /* Enable RxD0 and TxD0
*/U0LCR = 0x00000083; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 0x000000C3; /*9600 Baud Rate @ 30MHz VPB Clock */ U0LCR =0x00000003;
/* DLAB = 0 */
int putchar (int ch) /* Write character to Serial Port */
{
if (ch == '\n')
{
while (!(U0LSR & 0x20)); U0THR = CR; /*
output CR */
}
while (!(U0LSR & 0x20)); return(U0THR = ch);
}
int getchar (void) /* Read character from Serial Port */
{
while (!(U0LSR & 0x01)); return(U0RBR);
}

OUTPUT:

RESULT:
Thus the real time clock and serial port interfacing with ARM DEVELOPMENT KIT processorhas been executed
successfully.
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
TO INTERFACE WIFI MODULE WITH RASPBERRY PI

Interfacing of ESP8266 WiFi Module with Raspberry Pi Pico

The ESP-01 ESP8266 is a serial WIFI Wireless Transceiver Module developed based on ESP8266 SoC withintegrated TCP/IP
protocol stack that can give any microcontroller access to your WiFi network. This Wi-Fi Module is capable of of either hosting an
application or offloading all Wi-Fi networking functions from another application processor.

The ESP-01 ESP8266 module has a powerful onboard processing and storage capabilities that allow it to be integrated with the
sensors and other application specific devices through its GPIOs .

specifications for this ESP8266 Wi-Fi Module.

Please note The ESP8266 Module is not capable of 5-3V logic shifting and will require an external LogicLevel Converter.
Please do not power it directly from your 5V dev board.

Table of Contents

 Specifications and Features:-


 Raspberry Pi Pico Pinout
 Components Required
 Circuit Diagram :
 MicroPython Script for Interfacing ESP8266 Wi-Fi Module
 Result:

Specifications and Features:-

 802.11 b/g/n
 Wi-Fi Direct (P2P), soft-AP
 Integrated TCP/IP protocol stack
 Integrated TR switch, balun, LNA, power amplifier and matching network
 Integrated PLLs, regulators, DCXO and power management units
 +19.5dBm output power in 802.11b mode
 Power down leakage current of <10uA
 1MB Flash Memory
 Dimensions: 25 x 15 x 11 mm (LxWxH)
 PCB Thickness: 1mm
 Weight: 2 gm
 Integrated low power 32-bit CPU could be used as application processor
 SDIO 1.1 / 2.0, SPI, UART
 STBC, 1×1 MIMO, 2×1 MIMO
 A-MPDU & A-MSDU aggregation & 0.4ms guard interval
 Wake up and transmit packets in < 2ms
 Standby power consumption of < 1.0mW (DTIM3)

We have seen previously the interfacing guides for this Wi-Fi Module with Arduino and PIC Microcontroller. In this Tutorial we
will Interface this ESP8266 Wi-Fi Module with Raspberry Pi Pico development board to set up Wi-Fi wireless connectivity. This
Wireless Wi-Fi Connectivity with Pico development board help to facilitate all the IOT prospective features so we can host the data
and Post the data
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
to the server and any API connected over clouds like we can send Sensors reading to the Webserver, SendingData over Email.

Before proceeding further you may look at our previous tutorials for Interfacing ESP-01 ESP8266 Wi-Fimodule.

Recommended Tutorials:

 [PIC16F877A] : How to Send E-mail using PIC Microcontroller and ESP8266


 Sending an Email using Arduino and ESP8266 WiFi Module

To proceed further with concept of Interfacing the ESP-01 Wi-Fi Module with Raspberry Pi Pico Board weneed to

 Download and install the latest version of Python3 on Windows PC


 Download and install the latest version of either uPyCraft IDE or Thonny IDE.
 Setting up an MicroPython within Raspberry Pi Pico.

Refer below tutorials to follow above steps to make our Pico board ready for programming

 Getting Started with Raspberry Pi Pico using Thonny IDE


 Getting Started-Raspberry Pi Pico-uPyCraft and MicroPython

As the ESP-01 Wi-Fi module communicating over serial bus , the interfacing or connection with Raspberry Pi Pico Development
board will be very easy as we need to used only two UART0 GPIO pins of Pico board. Wewill connect Wi-Fi module at TX(0) pin/
GPIO0 and RX(0) pin/ GPIO1. Refer below Pinout and circuit diagram for your better interfacing.

Raspberry Pi Pico Pinout

The following image is the pinout diagram of Raspberry Pi Pico Board , it is 40 pin development board that includes 26-GPIO,
GND, Debug and +5VCC pins. Raspberry Pi Pico GPIOs are the multifunction pins which can be configured or used for the one the
function at a time. Like Digital or Analog configuration.
20EC512– EMBEDDED SYSTEM DESIGN AND IOT

Components Required

To do this project we need very few components as listed below

 Raspberry Pi Pico Board


 ESP-01 – ESP8266 Wi-Fi Module
 Breadboard
 Jumper Wire

Circuit Diagram :

Below is the Circuit diagram for interfacing ESP-01 Wi-Fi Module Module with Pico Board usingMicroPython
programming.
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
MicroPython Script for Interfacing ESP8266 Wi-Fi Module

To know how to program Raspberry Pi Pico board using MicroPython tools please refer below tutorials step by steps:

 Getting Started with Raspberry Pi Pico using Thonny IDE


 Getting Started-Raspberry Pi Pico-uPyCraft and MicroPython

To establish a communication between ESP8266 module and Raspberry Pi Pico we need to sends AT commands to the ESP8266
module to configure ESP8266 as a TCP web server. In the below MicroPython script we have passed AT commands serially one
by one enable Wi-Fi module so it can communicate with Pico Board and setup a Webserver. To program Raspberry Pi Pico board
we will use uPyCraft IDE , copy thebelow code and save it as a PIco-ESP8266.py.

PIco-ESP8266.py:

import uos
import machine
import utime

recv_buf="" # receive buffer global variable

print()
print("Machine: \t" + uos.uname()[4])
print("MicroPython: \t" + uos.uname()[3])

uart0 = machine.UART(0, baudrate=115200)


print(uart0)

def Rx_ESP_Data():
recv=bytes()
while uart0.any()>0:
recv+=uart0.read(1)
res=recv.decode('utf-8')
return res

def Connect_WiFi(cmd, uart=uart0, timeout=3000):


print("CMD: " + cmd)
uart.write(cmd)
utime.sleep(7.0)
Wait_ESP_Rsp(uart, timeout)
print()

def Send_AT_Cmd(cmd, uart=uart0, timeout=3000):


print("CMD: " + cmd)
uart.write(cmd)
Wait_ESP_Rsp(uart, timeout)
print()

def Wait_ESP_Rsp(uart=uart0, timeout=3000):


prvMills = utime.ticks_ms()
resp = b""
while (utime.ticks_ms()-prvMills)<timeout:
if uart.any():
resp = b"".join([resp, uart.read(1)])
print("resp:")
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
try:
print(resp.decode())
except UnicodeError:
print(resp)

Send_AT_Cmd('AT\r\n') #Test AT startup


Send_AT_Cmd('AT+GMR\r\n') #Check version information
Send_AT_Cmd('AT+CIPSERVER=0\r\n') #Check version information
Send_AT_Cmd('AT+RST\r\n') #Check version information
Send_AT_Cmd('AT+RESTORE\r\n') #Restore Factory Default Settings
Send_AT_Cmd('AT+CWMODE?\r\n') #Query the WiFi mode
Send_AT_Cmd('AT+CWMODE=1\r\n') #Set the WiFi mode = Station mode
Send_AT_Cmd('AT+CWMODE?\r\n') #Query the WiFi mode again
#Send_AT_Cmd('AT+CWLAP\r\n', timeout=10000) #List available APs
Connect_WiFi('AT+CWJAP="SSID","Password"\r\n', timeout=5000) #Connect to AP using SSID &
Password
Send_AT_Cmd('AT+CIFSR\r\n') #Obtain the Local IP Address
utime.sleep(3.0)
Send_AT_Cmd('AT+CIPMUX=1\r\n') #Obtain the Local IP Address
utime.sleep(1.0)
Send_AT_Cmd('AT+CIPSERVER=1,80\r\n') #Obtain the Local IP Address
utime.sleep(1.0)
print ('Starting connection to ESP8266...')
while True:
res =""
res=Rx_ESP_Data()
utime.sleep(2.0)
if '+IPD' in res: # if the buffer contains IPD(a connection), then respond with HTML
handshake
id_index = res.find('+IPD')
print("resp:")
print(res)
connection_id = res[id_index+5]
print("connectionId:" + connection_id)
print ('! Incoming connection - sending webpage')
uart0.write('AT+CIPSEND='+connection_id+',200'+'\r\n') #Send a HTTP response
then a webpage as bytes the 108 is the amount of bytes you are sending, change this if
you change the data sent below
utime.sleep(1.0)
uart0.write('HTTP/1.1 200 OK'+'\r\n')
uart0.write('Content-Type: text/html'+'\r\n')
uart0.write('Connection: close'+'\r\n')
uart0.write(''+'\r\n')
uart0.write('<!DOCTYPE HTML>'+'\r\n')
uart0.write('<html>'+'\r\n')
uart0.write('<body><center><h1>Raspberry Pi Pico-ESP-01-ESP8266- Web
Server</h1></center>'+'\r\n')
uart0.write('<center><h2>Interfacing of ESP8266 WiFi Module with Raspberry Pi
Pico</h2></center>'+'\r\n')
uart0.write('</body></html>'+'\r\n')
utime.sleep(4.0)
Send_AT_Cmd('AT+CIPCLOSE='+ connection_id+'\r\n') # once file sent, close
connection
utime.sleep(2.0)
recv_buf="" #reset buffer
print ('Waiting For connection...')
20EC512– EMBEDDED SYSTEM DESIGN AND IOT

After Downloading the PIco-ESP8266.py to the Pico board we need to AT commands starts executing sequentially toconfigure
the ESP-01 board as webserver. The response each AT commands will get printed on the Python shell window.

Ready to download this file,please wait!


..........................
download ok
exec(open('PIco-ESP8266.py').read(),globals())
Machine: Raspberry Pi Pico with RP2040
MicroPython: v1.16 on 2021-06-18 (GNU 10.2.0 MinSizeRel)
UART(0, baudrate=115200, bits=8, parity=None, stop=1, tx=0, rx=1, txbuf=256, rxbuf=256,
timeout=0, timeout_char=1, invert=None)
CMD: AT
resp:
AT
OK
CMD: AT+GMR
resp:
AT+GMR
resp:
AT+CIPSERVER=0

OK
1,CLOSED
CMD: AT+RST
resp:
b'AT+RST\r\r\n\r\nOum 0x2a\r\nload 0x3ffe8000, len 2124, room 8 \r\ntail 4\r\nchksum
0x07\r\nload 0x3ffe8850, len 9276, room 4 \r\ntail
8\r\nch8\x80\x0c\x8es\x88\x03\x8c\xe3No\x83d\x8e\x98l\x130\xec\x0c\x88\x80l\x8es\x88\x03
s\x88\x03\x87\t\x907\xff\r\nready\r\nWIFI CONNECTED\r\n'
CMD: AT+RESTORE
resp:
b'AT+RESTORE\r\r\n\rhksum 0x2a\r\nload 0x3ffe8000, len 2124, room 8 \r\ntail 4\r\nchksum
0x07\r\nload 0x3ffe8850, len 9276, room 420EC512 – EMBEDDED
\r\ntail SYSTEM
8\r\nchksum DESIGN AND IOT
0xba\r\ncsum
0xba\r\n\x8c\xe3No\x83l\x8e\x84l\x130\xec\x0c\x88\x80\x0c\x8es\x88\x03\x8c\xe3No\x83l\x8
e\x8fl\x1b0\xec\x0c\x88\x80\x04\x8es\x88\x03\x84\xe3Ng\x83l\x8e\x90l\x130\xec\x04\x88\x8
0l\x8es\x88\x03s\x88\x03\x8f\t\x907\x03F\xe7\x93N\xe3c\x0c\x88\x83\x84cs\xf1cv\x03c\xc1\
x13N\xe0Io\xe0Io\xc1cNl\x01cx\x7f\x93o\xf0\x13o\xce\x93N\x8c\x03F\x00\x03F\x8f\to\xfc\x0
1cpy\x0c\x88\x8c\x04\x8e\x83\x8c\x8f\x80l\x8e\xe3\x8c\x8f\x8c\xec\x8e\xecl\x8f\xc8d`\t\x
03O\x98\x1b\x03F\xe7\x93N\xe3c\x04\x88\xe3\xec\x8f\xc6\x0cc@\x03O\xe0\x03F\xef\x93N\xe3c
\x0c\x88\x83\x0c\x8e|l\x8es\x88\x03s\x88\x03\r\nready\r\n'
CMD: AT+CWMODE?
resp:
AT+CWMODE?
+CWMODE:2
OK
CMD: AT+CWMODE=1
resp:
AT+CWMODE=1
OK
CMD: AT+CWMODE?
resp:
AT+CWMODE?
+CWMODE:1
OK
CMD: AT+CWJAP="JioFiberhome3G","Micro@123"
resp:
AT+CWJAP="JioFiberhome3G","Micro@123"
WIFI CONNECTED
WIFI GOT IP
OK
CMD: AT+CIFSR
resp:
AT+CIFSR
+CIFSR:STAIP,"192.168.29.83"

+CIFSR:STAMAC,"8c:aa:b5:c4:e6:da"
OK
CMD: AT+CIPMUX=1
resp:
AT+CIPMUX=1
OK
CMD: AT+CIPSERVER=1,80
resp:
AT+CIPSERVER=1,80
OK
Starting connection to ESP8266...
resp:
0,CONNECT
1,CONNECT
+IPD,0,454:GET / HTTP/1.1
Host: 192.168.29.83
Connection: keep-Kit/537.36 (KHTML, like Gecko) Chrome/102.
connectionId:0
20EC512– EMBEDDED SYSTEM DESIGN AND IOT
! Incoming connection - sending webpage
CMD: AT+CIPCLOSE=0
resp:
0,CLOSED10.0; Win64; *,*/*;q=0.8

Referer: http://192.168.29.83/

Accept-Encoding: gzip, deflate

Accept-Language: en-US,en;q=0.9
Waiting For connection...

Result:

In the result of AT command response in Python shell window we can find the IP address configured andconnected with ESP8266
module. Enter the same IP address in your web browser to get the Webserver response from Pico board using ESP-01 module. It
indicated that our Interfacing of Wi-Fi module on successfully completed and it is ready to send any data like sensor reading over
webserver.

You might also like