INTRODUCTION TO ARM7 PROCESSOR
PIN DIAGRAM:
K Nikhil Praneeth 17311A19B7
BLOCK DIAGRAM:
FEATURES:
• 16/32-bit ARM7TDMI-S microcontroller in a tiny LQFP64 package.
• 8 to 40 kB of on-chip static RAM and 32 to 512 kB of on-chip flash program memory.
128 bit wide interface/accelerator enables high speed 60 MHz operation.
• USB 2.0 Full Speed compliant Device Controller with 2 kB of endpoint RAM.
The LPC2146/8 provide 8 kB of on-chip RAM accessible to USB by DMA.
• One or two (LPC2141/2 vs. LPC2144/6/8) 10-bit A/D converters provide a total of 6/14
analog inputs, with conversion times as low as 2.44 μs per channel.
• Single 10-bit D/A converter provides variable analog output.
• Two 32-bit timers/external event counters (with four capture and four compare
channels each), PWM unit (six outputs) and watchdog.
• Low power real-time clock with independent power and dedicated 32 kHz clock input.
• Multiple serial interfaces including two UARTs (16C550), two Fast I2C-bus
(400 kbit/s), SPI and SSP with buffering and variable data length capabilities.
• Vectored interrupt controller with configurable priorities and vector addresses.
• Up to 45 of 5 V tolerant fast general purpose I/O pins in a tiny LQFP64 package.
• Up to nine edge or level sensitive external interrupt pins available.
APPLICATIONS:
• Industrial control
• Medical systems
• Access control
• Point-of-sale
• Communication gateway
• Embedded soft modem
• General purpose applications
GPIO REGISTERS:
IOPIN:
K Nikhil Praneeth 17311A19B7
GPIO Port pin value register.The current state of GPIO configured port pins can always be read from
this register,regardless of pin direction.
IOSET:
GPIO Port Output Set register.This register controls the state of output pins in conjunction with IOCLR
register.writing ones produces highs at the corresponding port pins. Writing zeroes has no effect
IODIR:
GPIO Port Direction control register.This register individually controls the direction of each port pin.
IOCLR:
GPIO Port Output Clear register.This register controls the state of output pins. Writing ones produces
lows at the corresponding port pins and clears the corresponding bits in the IOSET register. Writing
zeroes has no effect
UART REGISTERS:
FEATURES:
• 16 byte Receive and Transmit FIFOs
• Register locations conform to ‘550 industry standard.
• Receiver FIFO trigger points at 1, 4, 8, and 14 bytes.
• Built-in fractional baud rate generator with autobauding capabilities.
• Mechanism that enables software and hardware flow control implementation.
UART0 Receiver Buffer Register:
The U0RBR is the top byte of the UART0 Rx FIFO. The top byte of the Rx FIFO contains the oldest
character received and can be read via the bus interface. The LSB (bit 0) represents the “oldest”
received data bit. If the character received is less than 8 bits, the unused MSBs are padded with
zeroes.
The Divisor Latch Access Bit (DLAB) in U0LCR must be zero in order to access the U0RBR . The
U0RBR is always Read Only.
UART0 Transmit Holding Register:
The U0THR is the top byte of the UART0 TX FIFO. The top byte is the newest character in the TX
FIFO and can be written via the bus interface. The LSB represents the first bit to transmit .
The Divisor Latch Access Bit (DLAB) in U0LCR must be zero in order to access the U0THR. The
U0THR is always Write Only.
UART0 Divisor Latch Registers:
The UART0 Divisor Latch is part of the UART0 Fractional Baud Rate Generator and holds the value
used to divide the clock supplied by the fractional prescaler in order to produce the baud rate clock,
which must be 16x the desired baud rate.
UART0 FIFO Control Register:
K Nikhil Praneeth 17311A19B7
UART0 LINE CONTROL REGISTER:
The UART0 LCR determines the format of the data character that is to be transmitted or received.
UART0 LINE STATUS REGISTER:
K Nikhil Praneeth 17311A19B7
PINSEL REGISTER DESCRIPTION:
PINSEL0:
The PINSEL0 register controls the functions of the pins as per the settings . The direction control bit in
the IO0DIR register is effective only when the GPIO function is selected for a pin. For other functions,
direction is controlled automatically.
PINSEL1:
The PINSEL1 register controls the functions of the pins as per the settings . The direction control bit in
the IO0DIR register is effective only when the GPIO function is selected for a pin . For other functions
direction is controlled automatically.
PINSEL2:
The PINSEL2 register controls the functions of the pins as per the settings . The direction control bit in
the IO1DIR register is effective only when the GPIO function is selected for a pin. For other functions
direction is controlled automatically.
K Nikhil Praneeth 17311A19B7
PULSE WIDTH MODULATION:
Rules for single edge controlled PWM outputs
● All single edge controlled PWM outputs go high at the beginning of a PWM cycle unless
their match value is equal to 0.
● Each PWM output will go low when its match value is reached. If no match occurs (i.e.
the match value is greater than the PWM rate), the PWM output remains continuously
high.
Rules for double edge controlled PWM:
Five rules are used to determine the next value of a PWM output when a new cycle is
about to begin:
● The match values for the next PWM cycle are used at the end of a PWM cycle (a time
point which is coincident with the beginning of the next PWM cycle), except as noted
in rule 3.
A match value equal to 0 or the current PWM rate (the same as the Match channel 0 value) have the
same effect, except as noted in rule 3. For example, a request for a falling edge at the beginning
of the PWM cycle has the same effect as a request for a falling edge at the end of a PWM cycle.
● When match values are changing, if one of the "old" match values is equal to the PWM rate, it
is used again once if the neither of the new match values are equal to 0 or the PWM rate, and
there was no old match value equal to 0.
● If both a set and a clear of a PWM output are requested at the same time, clear takes
precedence. This can occur when the set and clear match values are the same as in, or
when the set or clear value equals 0 and the other value equals the PWM rate.
● If a match value is out of range (i.e. greater than the PWM rate value), no match event
occurs and that match channel has no effect on the output. This means that the PWM
output will remain always in one state, allowing always low, always high, or
"no change" outputs.
REGISTERS:
PWM Timer Control Register (PWMTCR - 0xE001 4004)
The PWM Timer Control Register (PWMTCR) is used to control the operation of the PWM
Timer Counter.
K Nikhil Praneeth 17311A19B7
PWM Timer Counter (PWMTC - 0xE001 4008)
The 32-bit PWM Timer Counter is incremented when the Prescale Counter reaches its terminal count.
Unless it is reset before reaching its upper limit, the PWMTC will count up through the value 0xFFFF
FFFF and then wrap back to the value 0x0000 0000. This event does not cause an interrupt, but a
Match register can be used to detect an overflow if needed.
PWM Prescale Register (PWMPR - 0xE001 400C)
The 32-bit PWM Prescale Register specifies the maximum value for the PWM Prescale Counter.
PWM Prescale Counter register (PWMPC - 0xE001 4010)
The 32-bit PWM Prescale Counter controls division of PCLK by some constant value
before it is applied to the PWM Timer Counter. This allows control of the relationship of the resolution
of the timer versus the maximum time before the timer overflows. The PWM Prescale Counter is
incremented on every PCLK. When it reaches the value stored in the PWM Prescale Register, the
PWM Timer Counter is incremented and the PWM Prescale Counter is reset on the next PCLK. This
causes the PWM TC to increment on every PCLK when PWMPR = 0, every 2 PCLKs when PWMPR
= 1, etc.
PWM Match Registers (PWMMR0 - PWMMR6)
The 32-bit PWM Match register values are continuously compared to the PWM Timer Counter value.
When the two values are equal, actions can be triggered automatically. The action possibilities are to
generate an interrupt, reset the PWM Timer Counter, or stop the timer. Actions are controlled by the
settings in the PWMMCR register.
PWM Latch Enable Register (PWMLER - 0xE001 4050)
The PWM Latch Enable Register is used to control the update of the PWM Match registers when they
are used for PWM generation. When software writes to the location of a PWM Match register while
the Timer is in PWM mode, the value is held in a shadow register. When a PWM Match 0 event
occurs (normally also resetting the timer in PWM mode), the contents of shadow registers will be
transferred to the actual Match registers if the corresponding bit in the Latch Enable Register has
been set. At that point, the new values will take effect and determine the course of the next PWM
cycle. Once the transfer of new values has taken place, all bits of the LER are automatically cleared.
Until the corresponding bit in the PWMLER is set and a PWM Match 0 event occurs, any value written
to the PWM Match registers has no effect on PWM operation.
K Nikhil Praneeth 17311A19B7
1. LED BLINKING USING ARM7
Aim: To write a embedded C program for LED lights using ARM7.
Apparatus:
Software Requirements: Keil 4, Flash Magic
Hardware requirements: LPC2148, RS232, 9V Adapter
Program
#include<lpc214x.h>
void delay();
int main () {
PINSEL0=0x00000000;
IO0DIR=0x0000000f;
while (1) {
IO0SET=0x0000000f;
delay();
IO0CLR=0x0000000f;
delay();
}
}
void delay () {
int i,j;
for (i=0;i<1000;i++)
for (j=0;j<1000;j++);
}
Output:
K Nikhil Praneeth 17311A19B7
2. SERIAL DATA TRANSMISSION USING ARM7
Aim: To write a embedded C program for Serial Communication using ARM7.
Apparatus:
Software Requirements: Keil 4, Flash Magic
Hardware requirements: LPC2148, RS232, 9V Adapter
Program (TRANSRECIEVER)
#include <LPC214x.h>
#define DESIRED_BAUDRATE 9600
#define CRYSTAL_FREQUENCY_IN_HZ 12000000
#define MAX_PCLK (CRYSTAL_FREQUENCY_IN_HZ*5)
#define PCLK (MAX_PCLK/4)
#define DIVISOR (PCLK/(16*DESIRED_BAUDRATE))
void InitUart0(void);
unsigned char Rx_char(void);
void Tx_char(char );
int Tx_string(char *);
int main() {
PINSEL0=0x05;
InitUart0();
Tx_string(“This program will echo characters received\n”);
while(1)
Tx_char(Rx_char());
}
void InitUart0(void) {
U0LCR=0x83;
U0DLL=DIVISOR&0xFF;
U0DLM=DIVISOR>>8;
U0LCR=0x03;
U0FCR=0x05;
}
void Tx_char(char ch) {
if(ch==’\n’)
{
while(!(U0LSR&0x20));
U0THR=’\r’;
}
while(!(U0LSR&0x20));
U0THR=ch;
}
int Tx_string(char *s) {
int i=0;
while(s[i]!=’\0’) {
Tx_char(s[i]);
i++;
}
return(i);
}
unsigned char Rx_char(void) {
char ch;
while(!(U0LSR&0x01));
ch=U0RBR;
return ch;
}
K Nikhil Praneeth 17311A19B7
Output:
Program (TRANSMITTER)
#include <LPC214x.h>
int main()
{
char a[3]="ECM";
unsigned int z;
PINSEL0 =0x00000005;
U0LCR=0x83;
U0DLM=0x00;
U0DLL=0x61;
U0LCR=0x03;
for(z=0;z<3;z++)
{
while(!(U0LSR&0x20));
U0THR=a[z];
}
}
Output:
K Nikhil Praneeth 17311A19B7
Program (Reciever)
#include <LPC214x.h>
int main() {
char a[10];
unsigned int z;
PINSEL0 =0x00000005;
U0LCR=0x83;
U0DLM=0x00;
U0DLL=0x61;
U0LCR=0x03;
while(1)
for(z=0;z<10;z++) {
while(!(U0LSR&0x01));
a[z]=U0RBR;
while(!(U0LSR&0x20));
U0THR=a[z];
}
}
Output:
K Nikhil Praneeth 17311A19B7
3. LCD INTERFACING USING ARM7
Aim: To write a embedded C program for LCD interfacing using ARM7.
Apparatus:
Software Requirements: Keil 4, Flash Magic
Hardware requirements: LPC2148, RS232, 9V Adapter
Program
#include <LPC214x.H>
#define MAX_CHAR_IN_ONE_LINE 16
enum ROW_NUMBERS {
LINE1,
LINE2
};
#define LCD_DATA_DIR IO0DIR
#define LCD_DATA_SET IO0SET
#define LCD_DATA_CLR IO0CLR
#define LCD_CTRL_DIR IO1DIR
#define LCD_CTRL_SET IO1SET
#define LCD_CTRL_CLR IO1CLR
#define LCDRS (1 << 24)
#define LCDRW (1 << 23)
#define LCDEN (1 << 22)
#define LCD_D4 (1 << 10)
#define LCD_D5 (1 << 11)
#define LCD_D6 (1 << 12)
#define LCD_D7 (1 << 13)
#define LCD_DATA_MASK (LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7)
#define LCD_BUSY_FLAG LCD_D7
void delay(int count) {
int j=0,i=0;
for(j=0;j<count;j++)
for(i=0;i<35;i++);
}
void wait_lcd( void ) {
LCD_CTRL_CLR |= LCDRS;
LCD_CTRL_SET |= LCDRW |LCDEN;
while(IO1PIN & LCD_BUSY_FLAG);
LCD_CTRL_CLR |= LCDEN | LCDRW;
LCD_DATA_DIR |= LCD_DATA_MASK;
delay(100);
}
void lcd_command_write( unsigned char command ) {
unsigned char temp=0;
unsigned int temp1=0;
temp=command;
temp=(temp>>4) & 0x0F;
temp1=(temp<<10) & LCD_DATA_MASK;
LCD_CTRL_CLR = LCDRS;
LCD_CTRL_SET = LCDEN;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
delay(10000);
LCD_CTRL_CLR = LCDEN;
temp=command;
temp&=0x0F;
temp1=(temp<<10) & LCD_DATA_MASK;
K Nikhil Praneeth 17311A19B7
delay(100*2);
LCD_CTRL_CLR |= LCDRS;
LCD_CTRL_SET |= LCDEN;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
delay(10000);
LCD_CTRL_CLR |= LCDEN;
wait_lcd();
}
void set_lcd_port_output( void ) {
LCD_CTRL_DIR |= ( LCDEN | LCDRS | LCDRW );
LCD_CTRL_CLR |= ( LCDEN | LCDRS | LCDRW );
LCD_DATA_DIR |= LCD_DATA_MASK;
}
void lcd_clear( void) {
lcd_command_write( 0x01 );
}
int lcd_gotoxy( unsigned int x, unsigned int y) {
int retval = 0;
if( (x > 1) && (y > 15) )
retval = -1;
else
if( x == 0 )
lcd_command_write( 0x80 + y );
else if( x==1 ){
lcd_command_write( 0xC0 + y );
return retval;
}
void lcd_data_write( unsigned char data )//similar to command {
unsigned char temp=0;
unsigned int temp1=0;
temp=data;
temp=(temp>>4)&0x0F;
temp1=(temp<<10)&LCD_DATA_MASK;
LCD_CTRL_SET |= LCDEN|LCDRS;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
LCD_CTRL_CLR |= LCDEN;
temp=data;
temp&=0x0F;
temp1=(temp<<10)&LCD_DATA_MASK;
LCD_CTRL_SET |= LCDEN|LCDRS;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
LCD_CTRL_CLR |= LCDEN;
wait_lcd();
}
void lcd_putchar( char ch ) {
lcd_data_write( ch );
}
void lcd_putstring( unsigned char line, char *string ) {
unsigned char len = MAX_CHAR_IN_ONE_LINE;
lcd_gotoxy( line, 0);
while(*string != '\0' && len--)
{
lcd_putchar( *string );
string++;
}
K Nikhil Praneeth 17311A19B7
}
int main( void ) {
set_lcd_port_output();
delay(100*100);
lcd_command_write(0x28);
lcd_clear() ;
lcd_command_write(0x02);
lcd_command_write(0x06);
lcd_command_write(0x0C) ;
lcd_gotoxy(0, 0);
lcd_clear();
lcd_putstring(0," I am sreenidhian ");
lcd_putstring(1," my name is Sankeerth");
}
Output:
K Nikhil Praneeth 17311A19B7
K Nikhil Praneeth 17311A19B7
4. ADC INTERFACING USING ARM7
Aim: To write a embedded C program for ADC interfacing using ARM7.
Apparatus:
Software Requirements: Keil 4, Flash Magic
Hardware requirements: LPC2148, RS232, 9V Adapter
Program
#include <stdio.h>
#include <LPC214x.H>
#include "lcd.h"
#define ADC0 0
#define CHANNEL_3 3
void init_adc0( void );
unsigned short adc_read(unsigned char adc_num, unsigned char ch);
void wait(int count) {
int j=0,i=0;
for(j=0;j<count;j++)
for(i=0;i<35;i++);
}
void process_adc(void) {
float vin=0;
unsigned short adc_value = 0;
unsigned char buf[16] = {0};
adc_value = adc_read(ADC0, CHANNEL_3);
sprintf((char *)buf, "ADC:%d ", adc_value);
lcd_putstring(LINE1, (char *)buf);
vin=adc_value*3.3/1023;
sprintf((char *)buf, "vin:%f", vin);
lcd_putstring(LINE2, (char *)buf);
}
int main (void) {
init_adc0();
init_lcd();
wait(100000);
lcd_clear();
while(1) {
process_adc();
wait(30000);
}
}
void init_adc0(void) {
PINSEL1 = (PINSEL1 & ~(3 << 28)) | (1 << 28);
}
unsigned short adc_read(unsigned char adc_num, unsigned char ch) {
unsigned int i=0;
AD0CR = 0x00200D00 | (1<<ch);
AD0CR |= 0x01000000;
do {
i = AD0GDR;
}while ((i & 0x80000000) == 0);
return (i >> 6) & 0x03FF;
}
K Nikhil Praneeth 17311A19B7
Output:
K Nikhil Praneeth 17311A19B7
5. DAC INTERFACING USING ARM7
Aim: To write a embedded C program for DAC interfacing using ARM7.
Apparatus:
Software Requirements: Keil 4, Flash Magic
Hardware requirements: LPC2148, RS232, 9V Adapter
Port connections:
9th pin of lpc2148 to cro positive
18th pin of lpc2148 to cro negative
Program (Square Wave)
#include <LPC214x.h>
#define DAC_BIAS 0
void DacInit( void );
void DacOut( unsigned int val );
void Delay45Us(void);
int main (void) {
unsigned int i = 0;
DacInit();
IODIR1|=0xFFFF0000;
IOPIN1&=~(0xFFFF0000);
while (1){
i=1023;
DacOut(i);
Delay45Us();
i=0;
DacOut(i);
Delay45Us();
}
}
void Delay45Us(void) {
volatile unsigned int k;
for(k=0;k<409;k++){
__asm
{
nop;
}
}
}
void DacInit( void ){
PINSEL1 |= 0x00080000;
}
void DacOut( unsigned int val ){
DACR = (val << 6) | (DAC_BIAS<<16);
}
Output:
K Nikhil Praneeth 17311A19B7
Program (Triangular Wave)
#include <LPC214x.h>
#define DAC_BIAS 0
void DacInit( void );
void DacOut( unsigned int val );
void Delay45Us(void);
int main (void){
unsigned int i = 0;
DacInit();
IODIR1|=0xFFFF0000;
IOPIN1&=~(0xFFFF0000);
while (1) {
for(i=0;i<1024;i++)
DacOut(i);
for(i=1022;i>0;i--)
DacOut(i);
}
}
void DacInit( void ) {
PINSEL1 |= 0x00080000;
}
void DacOut( unsigned int val ) {
DACR = (val << 6) | (DAC_BIAS<<16);
}
Output:
K Nikhil Praneeth 17311A19B7
Program (Ramp Wave)
#include <LPC214x.h>
#define DAC_BIAS 0
void DacInit( void );
void DacOut( unsigned int val );
void Delay45Us(void);
int main (void) {
unsigned int i = 0;
DacInit();
IODIR1|=0xFFFF0000;
IOPIN1&=~(0xFFFF0000);
while (1) {
for(i=0;i<1024;i++)
DacOut(i);
}
}
void DacInit( void ) {
PINSEL1 |= 0x00080000;
}
void DacOut( unsigned int val ) {
DACR = (val << 6) | (DAC_BIAS<<16);
}
Output:
K Nikhil Praneeth 17311A19B7
6. DC MOTOR SPEED CONTROL USING PWM USING ARM7
Write a embedded C program for DC Motor speed control using PWM using ARM7
Aim:
To write a embedded C program for DC Motor speed control using PWM using ARM7.
Apparatus:
Software Requirements:
i. Keil 4
ii. Flash Magic
Hardware requirements:
i. LPC2148
ii. RS232
iii. 9V Adapter
Port connections:
18th pin of IC to cro negative.
19th pin of IC to cro positive.
Program:
#include <lpc214x.h>
#define PLOCK 0x00000400
#define PWMPRESCALE 60
void initPWM(void);
void initClocks(void);
void setupPLL0(void);
void feedSeq(void);
void connectPLL0(void);
int main(void)
{
initClocks();
initPWM();
while(1)
{
PWMMR1 = 2500;
PWMLER = (1<<1);
}
}
void initPWM(void)
{
PINSEL0 = (1<<1);
PWMPCR = 0x0;
PWMPR = PWMPRESCALE-1;
PWMMR0 = 10000;
PWMMCR = (1<<1);
PWMLER = (1<<1) | (1<<0);
PWMPCR = (1<<9);
PWMTCR = (1<<1);
PWMTCR = (1<<0) | (1<<3);
}
void initClocks(void){
setupPLL0();
feedSeq();
connectPLL0();
feedSeq();
VPBDIV = 0x01;
}
K Nikhil Praneeth 17311A19B7
void setupPLL0(void)
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
}
void feedSeq(void)
{
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
}
void connectPLL0(void)
{
while( !( PLL0STAT & PLOCK ));
PLL0CON = 0x03;
}
K Nikhil Praneeth 17311A19B7
Output:
When PWMMR1=2500
When PWMMR1=5000
When PWMMR1=7500
K Nikhil Praneeth 17311A19B7
7. I2C INTERFACE SERIAL EEPROM USING ARM7
Write an embedded C program for I 2 C interface serial EEPROM using ARM7
Aim:To write a embedded C program for I 2 C interface serial EEPROM using ARM7.
Apparatus:
Software Requirements:
iii. Keil 4
iv. Flash Magic
Hardware requirements:
iv. LPC2148
v. RS232
vi. 9V Adapter
Diagram:
Program:
#include <LPC214X.H>
#include <Stdio.h>
#include "Type.h"
#include "lcd.h"
#include "I2C.h"
#include "TIMER.h"
K Nikhil Praneeth 17311A19B7
int main(void)
{
uint8 write_buffer[16] = {'E', 'E', 'P', 'R', 'O', 'M', 'T', 'E', 'S', 'T', 'P', 'A', 'S', 'S',0};
uint8 read_buffer[16];
init_lcd();
I2C_Init();
TIMER_Init();
lcd_putstring(0," ES LAB ");
lcd_putstring(1,"I2C EEPROM Demo ");
delay(3000*500);
while(1)
{
if (!I2C_WriteToEEPROM(0, write_buffer, 16)) // write into EEPROM
lcd_putstring(1,"Memory WR error ");
if (!I2C_ReadFromEEPROM(0, read_buffer, 16)) // read from EEPROM
lcd_putstring(1,"Memory RD error ");
lcd_clear();
lcd_putstring(0," Read Data is: ");
lcd_putstring(1,(char *)read_buffer);
delay(5000*5000);
}
}
I2C.C
#include <LPC214X.H>
#include <Stdio.h>
#include "Type.h"
#include "I2C.h"
#include "TIMER.h"
void I2C_Init (void)
{
// Power on I2C0 peripheral
PCONP |= 0x00000080;
// Define port pin as SDA and SCL
PINSEL0 |= 0x00000050 ;
I2C0CONCLR = 0x6C; // clear all I2C config bits
I2C0CONSET = 0x40; // set I2EN
// I2C Clock Duty Cycle (high and low)
I2C0SCLH =
PERIFERAL_OPERATING_FREQUENCY_IN_HZ/(2*EEPROM_OPERATING_FREQUENCY_IN_HZ
);
I2C0SCLL =
PERIFERAL_OPERATING_FREQUENCY_IN_HZ/(2*EEPROM_OPERATING_FREQUENCY_IN_HZ
);
}
/*Waits until given status occured. Return:True on status occured and False on time out */
K Nikhil Praneeth 17311A19B7
BOOL I2C_WaitStatus (uint8 u8status)
{
TIMER0_RESET();
TIMER0_ENABLE();
while (T0TC < EEPROM_WAIT_TIME_OUT)
{
if (I2C0CONSET & 8) // poll SI bit
{
if (I2C0STAT == u8status)
{
TIMER0_DISABLE();
return TRUE;
}
}
}
TIMER0_DISABLE();
return FALSE;
}
/*Reads data from EEPROM.
Return: True on valid data and False on time out or any error with device */
BOOL I2C_ReadFromEEPROM (uint32 u32startAddr, uint8 *u8ptr2arr, uint32 u32len)
{
uint32 u32i;
// Check for upper limit
if (u32startAddr + u32len > EEPROM_SIZE)
return FALSE;
for (u32i=0;u32i<u32len;u32i++)
{
I2C0CONSET = 0x20; // Start set
if (!I2C_WaitStatus(0x08)) // 0x08: ready for device address
return FALSE;
I2C0DAT = EEPROM_DEVADDR;// addr[0]=0 means I2C write
I2C0CONCLR= 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x18)) // 0x18: ready for data byte
return FALSE;
// Transmit start address - Dummy byte write
I2C0DAT = (u32startAddr & 0x000000FF) ;
#ifndef EEPROM_24C02
I2C0CONCLR = 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x28)) // 0x28: ACK has been received
return FALSE;
I2C0CONCLR = 0x08; // clear SI flag
I2C0DAT = ((u32startAddr & 0x0000FF00)>>8) &0xFF;
#endif
I2C0CONCLR = 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x28)) // 0x28: ACK has been received
return FALSE;
I2C0CONCLR = 0x08; // clear SI flag
I2C0CONSET = 0x10; // generate stop condition
// Read data - Sequential mode.
I2C0CONSET = 0x20; // Start set
if (!I2C_WaitStatus(0x08)) // 0x08: ready for device address
return FALSE;
I2C0DAT= EEPROM_DEVADDR|0x01; // addr[0]=1 means I2C read
I2C0CONCLR = 0x28; // clear all except I2EN and AA
K Nikhil Praneeth 17311A19B7
if (!I2C_WaitStatus(0x40)) // 0x40: ready for data byte
return FALSE;
I2C0CONCLR = 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x58)) // 0x58: data byte received return ACK
return FALSE;
u8ptr2arr[u32i] = (uint8)I2C0DAT ;
u32startAddr++;
I2C0CONSET = 0x10; // generate stop condition
I2C0CONCLR = 0x2C;
}
return TRUE;
}
/*Writes data to EEPROM.
Return: True on successful write and False on time out or any error with device */
BOOL I2C_WriteToEEPROM (uint32 u32startAddr, uint8 *u8ptr2arr, uint32 u32len){
uint32 u32i,u32j;
// Check for upper limit
if (u32startAddr + u32len > EEPROM_SIZE)
return FALSE;
// write data byte wise
for (u32i = 0; u32i < u32len; u32i++){
I2C0CONSET = 0x20; // Start set
if (!I2C_WaitStatus(0x08)) // 0x08: ready for device address
return FALSE;
I2C0DAT = EEPROM_DEVADDR;// addr[0]=0 means I2C write
I2C0CONCLR = 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x18)) // 0x18: ready for data byte
return FALSE;
// Transmit start address
I2C0DAT = (u32startAddr & 0x000000FF);
#ifndef EEPROM_24C02
I2C0CONCLR = 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x28)) // 0x28: ACK has been received
return FALSE;
//I2C0CONCLR = 0x08; // clear SI flag
I2C0DAT = ((u32startAddr & 0x0000FF00)>>8) &0xFF;
#endif
I2C0CONCLR = 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x28)) // 0x28: ACK has been received
return FALSE;
I2C0DAT = (u8ptr2arr[u32i])&0x000000FF;
u32startAddr++;
I2C0CONCLR = 0x2C; // clear all except I2EN
if (!I2C_WaitStatus(0x28)) // 0x28: ACK has been received
return FALSE;
I2C0CONSET = 0x10; // generate stop condition
I2C0CONCLR = 0x2C;
// Poll for write done
for (u32j=20;u32j>0;u32j--){
I2C0CONSET = 0x20; // Start set
if (!I2C_WaitStatus(0x08)) // 0x08: ready for device address
return FALSE;
I2C0DAT = EEPROM_DEVADDR;// addr[0]=0 means I2C write
K Nikhil Praneeth 17311A19B7
I2C0CONCLR = 0x2C; // clear all except I2EN
if (I2C_WaitStatus(0x18)) // 0x18: ready for data byte
break;
else{
I2C0CONCLR = 0x2C;
I2C0CONSET = 0x10; // generate stop condition
I2C0CONCLR = 0x2C;
}
}
I2C0CONSET = 0x10; // generate stop condition
I2C0CONCLR = 0x2C;
}
return TRUE;
}
OUTPUT:
K Nikhil Praneeth 17311A19B7
8. SPI INTERFACE FOR SD-MMC CARD
Write a program to implement SPI interface for SD-MMC card interface
Aim:
To write a program to implement SPI interface for SD-MMC card interface
Software requirements:
1)flash magic
2)keil-4
Hardware reqirements:
1)LPC2148
2)Adapter
3)Rs 232
4)SDCARD
Block diagram:
Program:
#include "lcd.h"
#include "type.h"
#include "types.h"
U8 init_sdcard(void)
{
if(if_initInterface(0)==0)
return 1;
else
return 0;
}
void test_sd_card(void)
{
if(init_sdcard())
{
lcd_clear();
lcd_putstring(LINE1,"SD card - OK");
}
else
{
lcd_clear();
lcd_putstring(LINE1,"SD card - not OK");
}
}
K Nikhil Praneeth 17311A19B7
int main (void)
{
euint8 write_buffer[16] = {'S', 'D', 'C', 'A', 'R', 'D', 'T', 'E', 'S', 'T', 'P', 'A', 'S', 'S',0};
euint8 read_buffer[16];
init_lcd();
test_sd_card();
delay(3000*200);
while (1)
{
if (!sd_writeSector(0, write_buffer)) // write into SD CARD
lcd_putstring(1,"Memory WR error ");
if (!sd_readSector(0, read_buffer,16)) // read from SD CARD
lcd_putstring(1,"Memory RD error ");
lcd_clear();
lcd_putstring(0," Read Data is: ");
lcd_putstring(1,(char *)read_buffer);
delay(5000*5000);
}
}
Output:
K Nikhil Praneeth 17311A19B7
9. PORTING OF RTOS
AIM: To write a program for porting of RTOS on MBED boards
SOFTWARE REQUIREMENTS
-ONLINE COMPILER (developer.mbed.org)
HARDWARE REQUIREDMENTS
-USB Cable
-MBED Board – FRDM-KL25Z
PROGRAM
#include "mbed.h"
#include "rtos.h"
DigitalOut LEDs[4] = {
DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
};
void blink(void const *n) {
LEDs[(int)n] = !LEDs[(int)n];
}
int main(void) {
RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
led_1_timer.start(2000);
led_2_timer.start(1000);
led_3_timer.start(500);
led_4_timer.start(250);
Thread::wait(osWaitForever);
}
OUTPUT:
The threads have been executed and LEDS glow randomly.
K Nikhil Praneeth 17311A19B7
10. PRIORITIZING TASKS USING RTOS
AIM: To write a program for sending a message to PC through serial port by 2
different tasks on priority bases.
SOFTWARE REQUIREMENTS
-ONLINE COMPILER (developer.mbed.org)
HARDWARE REQUIREDMENTS
-USB Cable
-MBED Board – FRDM-KL25Z
PROGRAM
#include "mbed.h"
#include "rtos.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Serial pc(USBTX, USBRX); // tx, rx
Thread *(test);
void led2_thread(void const *args) {
while (true) {
led2 = !led2;
Thread::wait(1000);
}
}
int main() {
Thread thread(led2_thread);
test = &thread;
test->set_priority(osPriorityHigh);
pc.printf("%i\n\r",test->get_priority());
while (true) {
led1 = !led1;
Thread::wait(500);
}
}
OUTPUT:
The program sends a 2 to the serial program on my computer confirming that the priority is set high.
K Nikhil Praneeth 17311A19B7